Skip to content

Commit

Permalink
Add fields to azurerm_*_cost_management_export: partition_data an…
Browse files Browse the repository at this point in the history
…d `export_data_options.version`

`export_data_options.version` maps to
`export.definition.dataset.configuration.dataVersion` No default. Versions can
proliferate and even though we can collect a large list of possible versions
it's better to leave it free form.

`partition_data` maps to `export.partition_data`. Defaults to false.
  • Loading branch information
joshk0 committed Jun 4, 2024
1 parent 4508704 commit 6673a2e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 31 deletions.
2 changes: 1 addition & 1 deletion internal/services/costmanagement/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package client
import (
"fmt"

Check failure on line 7 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

could not import fmt (no metadata for fmt)

"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports"
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-06-01-preview/scheduledactions"

Check failure on line 9 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

could not import github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-06-01-preview/scheduledactions (no metadata for github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-06-01-preview/scheduledactions)
scheduledactions_v2022_10_01 "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/scheduledactions"

Check failure on line 10 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

could not import github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/scheduledactions (no metadata for github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/scheduledactions)
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views"

Check failure on line 11 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

could not import github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views (no metadata for github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views)
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports"

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / document-lint

cannot find module providing package github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports: import lookup disabled by -mod=vendor

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / detect

cannot find module providing package github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports: import lookup disabled by -mod=vendor

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

cannot find module providing package github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports: import lookup disabled by -mod=vendor

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

cannot find module providing package github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports: import lookup disabled by -mod=vendor

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

could not import github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports (no metadata for github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports)

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports" imported as client and not used

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / test

cannot find module providing package github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports: import lookup disabled by -mod=vendor

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / test

cannot find module providing package github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports: import lookup disabled by -mod=vendor

Check failure on line 12 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / test

cannot find module providing package github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports: import lookup disabled by -mod=vendor
"github.com/hashicorp/terraform-provider-azurerm/internal/common"

Check failure on line 13 in internal/services/costmanagement/client/client.go

View workflow job for this annotation

GitHub Actions / tflint

could not import github.com/hashicorp/terraform-provider-azurerm/internal/common (no metadata for github.com/hashicorp/terraform-provider-azurerm/internal/common)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports"
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -143,15 +143,17 @@ resource "azurerm_billing_account_cost_management_export" "test" {
recurrence_type = "Monthly"
recurrence_period_start_date = "%sT00:00:00Z"
recurrence_period_end_date = "%sT00:00:00Z"
partition_data = true
export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
}
export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
data_version = "2019-11-01"
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, billingAccount, start, end)
Expand Down Expand Up @@ -198,8 +200,9 @@ resource "azurerm_billing_account_cost_management_export" "test" {
}
export_data_options {
type = "Usage"
time_frame = "WeekToDate"
data_version = "2021-10-01"
type = "Usage"
time_frame = "WeekToDate"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, billingAccount, start, end)
Expand All @@ -216,15 +219,17 @@ resource "azurerm_billing_account_cost_management_export" "import" {
recurrence_type = azurerm_billing_account_cost_management_export.test.recurrence_type
recurrence_period_start_date = azurerm_billing_account_cost_management_export.test.recurrence_period_start_date
recurrence_period_end_date = azurerm_billing_account_cost_management_export.test.recurrence_period_start_date
partition_data = azurerm_billing_account_cost_management_export.test.partition_data
export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
}
export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
data_version = "2019-11-01"
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, template)
Expand Down
43 changes: 40 additions & 3 deletions internal/services/costmanagement/export_resource_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports"
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -29,6 +29,14 @@ func (br costManagementExportBaseResource) arguments(fields map[string]*pluginsd
Default: true,
},

"partition_data": {
Type: pluginsdk.TypeBool,
Optional: true,
Required: false,
Default: false,
ForceNew: true,
},

"recurrence_type": {
Type: pluginsdk.TypeString,
Required: true,
Expand Down Expand Up @@ -100,10 +108,17 @@ func (br costManagementExportBaseResource) arguments(fields map[string]*pluginsd
string(exports.TimeframeTypeTheLastMonth),
string(exports.TimeframeTypeWeekToDate),
string(exports.TimeframeTypeMonthToDate),
string(exports.TimeframeTypeMonthToDate),
// TODO Use value from SDK after https://github.com/Azure/azure-rest-api-specs/issues/23707 is fixed
"TheLast7Days",
}, false),
},

"data_version": {
Type: pluginsdk.TypeString,
Optional: true,
Required: false,
},
},
},
},
Expand Down Expand Up @@ -174,6 +189,10 @@ func (br costManagementExportBaseResource) readFunc(scopeFieldName string) sdk.R

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
if partitionData := props.PartitionData; partitionData != nil {
metadata.ResourceData.Set("partition_data", *partitionData)
}

if schedule := props.Schedule; schedule != nil {
if recurrencePeriod := schedule.RecurrencePeriod; recurrencePeriod != nil {
metadata.ResourceData.Set("recurrence_period_start_date", recurrencePeriod.From)
Expand Down Expand Up @@ -272,6 +291,7 @@ func createOrUpdateCostManagementExport(ctx context.Context, client *exports.Exp
props := exports.Export{
ETag: etag,
Properties: &exports.ExportProperties{
PartitionData: pointer.FromBool(metadata.ResourceData.Get("partition_data").(bool)),
Schedule: &exports.ExportSchedule{
Recurrence: &recurrenceType,
RecurrencePeriod: &exports.ExportRecurrencePeriod{
Expand Down Expand Up @@ -321,7 +341,18 @@ func expandExportDefinition(input []interface{}) *exports.ExportDefinition {
}

attrs := input[0].(map[string]interface{})
var dataset *exports.ExportDataset

if v, ok := attrs["data_version"]; ok {
dataset = &exports.ExportDataset{
Configuration: &exports.ExportDatasetConfiguration{
DataVersion: utils.String(v.(string)),
},
}
}

definitionInfo := &exports.ExportDefinition{
DataSet: dataset,
Type: exports.ExportType(attrs["type"].(string)),
Timeframe: exports.TimeframeType(attrs["time_frame"].(string)),
}
Expand Down Expand Up @@ -373,10 +404,16 @@ func flattenExportDefinition(input *exports.ExportDefinition) []interface{} {
queryType = string(input.Type)
}

version := ""
if input.DataSet != nil && input.DataSet.Configuration != nil && input.DataSet.Configuration.DataVersion != nil {
version = *input.DataSet.Configuration.DataVersion
}

return []interface{}{
map[string]interface{}{
"time_frame": string(input.Timeframe),
"type": queryType,
"data_version": version,
"time_frame": string(input.Timeframe),
"type": queryType,
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports"
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -127,14 +127,16 @@ resource "azurerm_resource_group_cost_management_export" "test" {
recurrence_type = "Monthly"
recurrence_period_start_date = "%sT00:00:00Z"
recurrence_period_end_date = "%sT00:00:00Z"
partition_data = true
export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
}
export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
data_version = "2019-11-01"
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, start, end)
Expand Down Expand Up @@ -171,14 +173,16 @@ resource "azurerm_resource_group_cost_management_export" "test" {
recurrence_type = "Monthly"
recurrence_period_start_date = "%sT00:00:00Z"
recurrence_period_end_date = "%sT00:00:00Z"
partition_data = true
export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root/updated"
}
export_data_options {
type = "Usage"
time_frame = "WeekToDate"
data_version = "2021-10-01"
type = "Usage"
time_frame = "WeekToDate"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, start, end)
Expand All @@ -195,15 +199,17 @@ resource "azurerm_resource_group_cost_management_export" "import" {
recurrence_type = azurerm_resource_group_cost_management_export.test.recurrence_type
recurrence_period_start_date = azurerm_resource_group_cost_management_export.test.recurrence_period_start_date
recurrence_period_end_date = azurerm_resource_group_cost_management_export.test.recurrence_period_start_date
partition_data = azurerm_resource_group_cost_management_export.test.partition_data
export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
}
export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
data_version = "2019-11-01"
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, template)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports"
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-07-01-preview/exports"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -132,15 +132,17 @@ resource "azurerm_subscription_cost_management_export" "test" {
recurrence_type = "Monthly"
recurrence_period_start_date = "%sT00:00:00Z"
recurrence_period_end_date = "%sT00:00:00Z"
partition_data = true
export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
}
export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
data_version = "2019-11-01"
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, start, end)
Expand Down Expand Up @@ -189,8 +191,9 @@ resource "azurerm_subscription_cost_management_export" "test" {
}
export_data_options {
type = "Usage"
time_frame = "WeekToDate"
data_version = "2021-10-01"
type = "Usage"
time_frame = "WeekToDate"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, start, end)
Expand All @@ -207,15 +210,17 @@ resource "azurerm_subscription_cost_management_export" "import" {
recurrence_type = azurerm_subscription_cost_management_export.test.recurrence_type
recurrence_period_start_date = azurerm_subscription_cost_management_export.test.recurrence_period_start_date
recurrence_period_end_date = azurerm_subscription_cost_management_export.test.recurrence_period_start_date
partition_data = azurerm_subscription_cost_management_export.test.partition_data
export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
}
export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
data_version = "2019-11-01"
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, template)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ resource "azurerm_storage_container" "example" {
resource "azurerm_billing_account_cost_management_export" "example" {
name = "example"
billing_account_id = "example"
partition_data = true
recurrence_type = "Monthly"
recurrence_period_start_date = "2020-08-18T00:00:00Z"
recurrence_period_end_date = "2020-09-18T00:00:00Z"
Expand All @@ -46,8 +47,9 @@ resource "azurerm_billing_account_cost_management_export" "example" {
}
export_data_options {
type = "Usage"
time_frame = "WeekToDate"
data_version = "2021-05-01"
type = "Usage"
time_frame = "WeekToDate"
}
}
```
Expand All @@ -60,6 +62,8 @@ The following arguments are supported:

* `billing_account_id` - (Required) The id of the billing account on which to create an export. Changing this forces a new resource to be created.

* `partition_data` - (Optional) Whether data will be partitioned into smaller files to facilitate consumption.

* `recurrence_type` - (Required) How often the requested information will be exported. Valid values include `Annually`, `Daily`, `Monthly`, `Weekly`.

* `recurrence_period_start_date` - (Required) The date the export will start capturing information.
Expand All @@ -86,6 +90,8 @@ A `export_data_storage_location` block supports the following:

A `export_data_options` block supports the following:

* `data_version` - (Optional) The dataset version to use for the export. The list of valid dataset versions can be viewed [here](https://learn.microsoft.com/en-us/azure/cost-management-billing/dataset-schema/schema-index).

* `type` - (Required) The type of the query. Possible values are `ActualCost`, `AmortizedCost` and `Usage`.

* `time_frame` - (Required) The time frame for pulling data for the query. If custom, then a specific time period must be provided. Possible values include: `WeekToDate`, `MonthToDate`, `BillingMonthToDate`, `TheLast7Days`, `TheLastMonth`, `TheLastBillingMonth`, `Custom`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ resource "azurerm_storage_container" "example" {
resource "azurerm_resource_group_cost_management_export" "example" {
name = "example"
resource_group_id = azurerm_resource_group.example.id
partition_data = true
recurrence_type = "Monthly"
recurrence_period_start_date = "2020-08-18T00:00:00Z"
recurrence_period_end_date = "2020-09-18T00:00:00Z"
Expand All @@ -45,8 +46,9 @@ resource "azurerm_resource_group_cost_management_export" "example" {
}
export_data_options {
type = "Usage"
time_frame = "WeekToDate"
data_version = "2021-10-01"
type = "Usage"
time_frame = "WeekToDate"
}
}
```
Expand All @@ -59,6 +61,8 @@ The following arguments are supported:

* `resource_group_id` - (Required) The id of the resource group on which to create an export. Changing this forces a new resource to be created.

* `partition_data` - (Optional) Whether data will be partitioned into smaller files to facilitate consumption.

* `recurrence_type` - (Required) How often the requested information will be exported. Valid values include `Annually`, `Daily`, `Monthly`, `Weekly`.

* `recurrence_period_start_date` - (Required) The date the export will start capturing information.
Expand All @@ -85,6 +89,8 @@ A `export_data_storage_location` block supports the following:

A `export_data_options` block supports the following:

* `data_version` - (Optional) The dataset version to use for the export. The list of valid dataset versions can be viewed [here](https://learn.microsoft.com/en-us/azure/cost-management-billing/dataset-schema/schema-index).

* `type` - (Required) The type of the query. Possible values are `ActualCost`, `AmortizedCost` and `Usage`.

* `time_frame` - (Required) The time frame for pulling data for the query. If custom, then a specific time period must be provided. Possible values include: `WeekToDate`, `MonthToDate`, `BillingMonthToDate`, `TheLast7Days`, `TheLastMonth`, `TheLastBillingMonth`, `Custom`.
Expand Down
Loading

0 comments on commit 6673a2e

Please sign in to comment.