繁体   English   中英

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

[英]Terraform - create azure cost management view using Rest API call via terraform code

我想在terraform中调用Rest API,下面是Rest API中创建成本分析视图需要用到的示例请求,我们需要在Azure中调用这个资源as9命令来创建视图we create8/81826可以使用 REST API: https://learn.microsoft.com/en-us/rest/api/cost-management/views/create-or-update?tabs=HTTP

我们可以通过创建计划操作来使用这个 API 到 Email 订阅: https://learn.microsoft.com/en-us/rest/api/cost-management/scheduled-actions/create-or-update?tabs=HTTP

对于 email 订阅的计划操作的第二个 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}"
    }
}

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

尝试使用您的 json 中提到的属性

代码:

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

在此处输入图像描述

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

参考: Microsoft.CostManagement/scheduledActions - Bicep,ARM 模板和 Terraform AzAPI 参考 | 微软学习

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM