Terraform -通过Terraform代码使用Rest API调用创建Azure成本管理视图

ddrv8njm  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(115)

我想在terraform中调用Rest API。下面是需要用于在Azure中创建成本分析视图的Rest API示例请求。我们需要使用terraform将此资源部署为代码。为了创建视图,我们可以使用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": [
                test@microsoft.com
            ],
            "subject": "Test",
            "message": "Test"
        },
        "fileDestination": {
            "fileFormats": [
                "Csv"
            ]
        },
        "scope": "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}"
    }
}
oknwwptz

oknwwptz1#

我试着在我的环境中重现同样的情景。

尝试使用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 = [
              "xx@xxx.com"
            ]
          }
          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"
      })
    }

此处安装AzApi VSCode扩展以使用AzApi提供程序
Microsoft. CostManagement/视图支持以下格式作为文档。

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`?

Microsoft. CostManagement/scheduledActions具有这些参数。可以尝试将Microsoft. CostManagement/scheduledActionsParentId的azapi资源作为视图中的parentId。

相关问题