簡體   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