Reputation: 153
I want to call Rest API in terraform. Below is the Rest API sample request that needs to be used to create a cost analysis view in Azure. We need to deploy this resource as code using terraform. In order to create the View/s we can use REST API: https://learn.microsoft.com/en-us/rest/api/cost-management/views/create-or-update?tabs=HTTP
we can use this API to Email subscribe by creating scheduled action: https://learn.microsoft.com/en-us/rest/api/cost-management/scheduled-actions/create-or-update?tabs=HTTP
For the second API for scheduled action for email subscription, we should use the payload body below as example:
{
"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": [
[email protected]
],
"subject": "Test",
"message": "Test"
},
"fileDestination": {
"fileFormats": [
"Csv"
]
},
"scope": "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}"
}
}
Upvotes: 1
Views: 842
Reputation: 21
You can do it taking the example of the answer proposed on this topic: Adding filter to azurerm_subscription_cost_management_view This is the code:
resource "azurerm_subscription_cost_management_view" "example" {
name = "TerraformCostView"
display_name = "Terraform - Cost View"
subscription_id = "/subscriptions/${var.subscription_id}"
chart_type = "Area"
accumulated = true
report_type = "Usage"
timeframe = "MonthToDate"
dataset {
granularity = "Monthly"
aggregation {
name = "totalCost"
column_name = "Cost"
}
#grouping {
# name = "department"
# type = "TagKey"
#}
}
pivot {
name = "ServiceName"
type = "Dimension"
}
pivot {
name = "ResourceType"
type = "Dimension"
}
pivot {
name = "ResourceGroupName"
type = "Dimension"
}
}
Upvotes: 0
Reputation: 10859
I tried to reproduce the same in my environment .
Tried with the properties mentioned in your json
Code:
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 = [
"[email protected]"
]
}
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"
})
}
Here install AzApi VSCode Extension to use the AzApi provider
Microsoft.CostManagement/views support following format as the document .
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" }) }
It does not support following parameters.
So received the error:
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 has these parameters . azapi resource of Microsoft.CostManagement/scheduledActionsParentId can be tried giving as parentId in view .
Upvotes: 0