azure - Terraform - 通过 terraform 代码使用 Rest API 调用创建 azure 成本管理 View

标签 azure terraform

我想在terraform中调用Rest API。以下是需要用于在 Azure 中创建成本分析 View 的 Rest API 示例请求。我们需要使用 terraform 将此资源部署为代码。 为了创建 View ,我们可以使用 REST API: https://learn.microsoft.com/en-us/rest/api/cost-management/views/create-or-update?tabs=HTTP

我们可以通过创建计划操作来使用此 API 进行电子邮件订阅: https://learn.microsoft.com/en-us/rest/api/cost-management/scheduled-actions/create-or-update?tabs=HTTP

对于用于电子邮件订阅的计划操作的第二个 API,我们应该使用下面的有效负载正文作为示例:

{
    "kind": "Email",
    "properties": {
        "displayName": "Test ",
        "status": "Enabled",
        "viewId": "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}/providers/Microsoft.CostManagement/views/test",
        "schedule": {
            "frequency": "Weekly",
            "startDate": "2023-01-11T02:30:00.000Z",
            "endDate": "2024-01-10T18:30:00.000Z",
            "daysOfWeek": [
                "Wednesday"
            ]
        },
        "notification": {
            "to": [
                <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691d0c1a1d2904000a1b061a060f1d470a0604" rel="noreferrer noopener nofollow">[email protected]</a>
            ],
            "subject": "Test",
            "message": "Test"
        },
        "fileDestination": {
            "fileFormats": [
                "Csv"
            ]
        },
        "scope": "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}"
    }
}

最佳答案

我尝试在我的环境中重现相同的内容。

尝试使用 json 中提到的属性

代码:

main.tf:

   resource "azapi_resource" "symbolicname" {
      name      = "kavyaexample"
      parent_id = data.azurerm_resource_group.example.id
      type      = "Microsoft.CostManagement/views@2019-11-01"
      // location = "eastus"
      body = jsonencode({
        properties = {
          displayName = "myfilefmt"
          fileDestination = {
            fileFormats = "Csv"
          }
          notification = {
            language       = "en"
            message        = "this is test notif"
            regionalFormat = "string"
            subject        = "Test"
            to = [
              "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aed6d6eed6d6d680cdc1c3" rel="noreferrer noopener nofollow">[email protected]</a>"
            ]
          }
          notificationEmail = "string"
          schedule = {
            dayOfMonth = 19
            daysOfWeek = [
              "Thursday"
            ]
            endDate   = "2024-01-10T18:30:00.000Z"
            frequency = "weekly"
            //hourOfDay = int
            startDate = "2023-01-19T11:30:00.000Z"
            weeksOfMonth = [
              "string"
            ]
          }
          "scope" : "/providers/Microsoft.Billing/billingAccounts/xxxxx"
          "status" = "Enabled"
          "viewId" : "/providers/Microsoft.Billing/billingAccounts/xxxxe/providers/Microsoft.CostManagement/views/test",
        }
        kind = "Email"
      })
    }

enter image description here

此处安装 AzApi VSCode Extension 以使用 AzApi 提供程序

Microsoft.CostManagement/views支持以下格式的文档。

resource "azapi_resource" "symbolicname" {
  type = "Microsoft.CostManagement/views@2019-11-01"
  name = "string"
  parent_id = "string"
  body = jsonencode({
    properties = {
      accumulated = "string"
      chart = "string"
      displayName = "string"
      kpis = [
        {
          enabled = bool
          id = "string"
          type = "string"
        }
      ]
      metric = "string"
      pivots = [
        {  
         …..
        }
      ]
      query = {
        dataSet = {
          aggregation = {}
          configuration = {
            columns = [
              "string"
            ]
          }
          filter = {
            and = [
              {
              dimensions = {
                name = "string"
                operator = "string"
                values = [
                  "string"
                ]
              }
              or = [
                {
                tagKey = {
                  name = "string"
                  operator = "string"
                  values = [
                    "string"
                  ]
                }
                tags = {
                  name = "string"
                  operator = "string"
                  values = [
                    "string"
                  ]
                }
                tagValue = {
                  name = "string"
                  operator = "string"
                  values = [
                    "string"
                  ]
                }
          }
          granularity = "string"
          grouping = [
            {
              name = "string"
              type = "string"
            }
          ]
          sorting = [
            {
              direction = "string"
              name = "string"
            }
          ]
        }
        timeframe = "string"
        timePeriod = {
          from = "string"
          to = "string"
        }
        type = "Usage"
      }
      scope = "string"
    }
    eTag = "string"
  })
}

不支持以下参数。

因此收到错误:

Error: the `body` is invalid: 
│ `properties.fileDestination` is not expected here. Do you mean `properties.metric`? 
│ `properties.notification` is not expected here. Do you mean `properties.modifiedOn`?

enter image description here

Microsoft.CostManagement/scheduledActions 具有这些参数。 Microsoft.CostManagement/scheduledActionsParentId 的 azapi 资源可以尝试在 View 中作为parentId给出。

引用: Microsoft.CostManagement/scheduledActions - Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn

关于azure - Terraform - 通过 terraform 代码使用 Rest API 调用创建 azure 成本管理 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75095277/

相关文章:

azure - App Insights 工作簿 - 是否可以更改堆叠列的顺序?

amazon-web-services - 在 Terraform 中解释 aws 的 secret

Terraform 可选嵌套对象变量

azure - 状态 = 403 代码 ="AuthorizationFailure"消息 ="This request is not authorized to perform this operation"

google-cloud-platform - Terraform:Cloud Run 服务上的云端点?

azure - Kubernetes pod 抛出 CredentialUnavailableException : EnvironmentCredential authentication unavailable

azure - 是否可以配置 NServiceBus 端点(在 Azure 传输上)以接受简单字符串作为输入?

azure - 从表存储中删除旧的 Windows Azure 诊断数据(性能计数器等)

Terraform AWS SES 凭证资源

azure - 使用 Pulumi 中的服务原理通过 Azure 进行身份验证