Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fields to azurerm_*_cost_management_export: partition_data and export_data_options.data_version #26219

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports"
Copy link

@flanakin flanakin Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably keep the old version as well. I believe there are some gaps between what's supported in each version.

@jojohpm would know more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have a provision to keep 2 versions, then we could keep both.
2021-10-01 = Exports v1. Legacy exports.
2023-07-01-preview = Exports v2. The new one with file partitioning, compression, file format, newer datasets etc... This is going to be the default experience going forward.

"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"
"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"
"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 @@ -39,6 +39,25 @@ func TestAccBillingAccountCostManagementExport_basic(t *testing.T) {
})
}

func TestAccBillingAccountCostManagementExport_complete(t *testing.T) {
if os.Getenv("ARM_BILLING_ACCOUNT") == "" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Subscriptions are supported as well

t.Skip("skipping tests - no billing account data provided")
}

data := acceptance.BuildTestData(t, "azurerm_billing_account_cost_management_export", "test")
r := BillingAccountCostManagementExport{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccBillingAccountCostManagementExport_update(t *testing.T) {
if os.Getenv("ARM_BILLING_ACCOUNT") == "" {
t.Skip("skipping tests - no billing account data provided")
Expand Down Expand Up @@ -146,7 +165,7 @@ resource "azurerm_billing_account_cost_management_export" "test" {

export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
root_folder_path = "root"
}

export_data_options {
Expand All @@ -157,6 +176,56 @@ resource "azurerm_billing_account_cost_management_export" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, billingAccount, start, end)
}

func (BillingAccountCostManagementExport) complete(data acceptance.TestData) string {
start := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
end := time.Now().AddDate(0, 0, 2).Format("2006-01-02")
billingAccount := os.Getenv("ARM_BILLING_ACCOUNT")
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-cm-%d"
location = "%s"
}

resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name

location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_container" "test" {
name = "acctestcontainer%s"
storage_account_name = azurerm_storage_account.test.name
}

resource "azurerm_billing_account_cost_management_export" "test" {
name = "accs%d"
billing_account_id = "%s"
recurrence_type = "Monthly"
recurrence_period_start_date = "%sT00:00:00Z"
recurrence_period_end_date = "%sT00:00:00Z"
partition_data_enabled = 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 = "2021-10-01"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, billingAccount, start, end)
}

func (BillingAccountCostManagementExport) update(data acceptance.TestData) string {
start := time.Now().AddDate(0, 3, 0).Format("2006-01-02")
end := time.Now().AddDate(0, 4, 0).Format("2006-01-02")
Expand Down Expand Up @@ -194,12 +263,12 @@ resource "azurerm_billing_account_cost_management_export" "test" {

export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root/updated"
root_folder_path = "root/updated"
}

export_data_options {
type = "Usage"
time_frame = "WeekToDate"
type = "Usage"
time_frame = "WeekToDate"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, billingAccount, start, end)
Expand All @@ -216,15 +285,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_enabled = 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"
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: 37 additions & 6 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,13 @@ func (br costManagementExportBaseResource) arguments(fields map[string]*pluginsd
Default: true,
},

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

"recurrence_type": {
Type: pluginsdk.TypeString,
Required: true,
Expand Down Expand Up @@ -104,6 +111,12 @@ func (br costManagementExportBaseResource) arguments(fields map[string]*pluginsd
"TheLast7Days",
}, false),
},

"data_version": {
joshk0 marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeString,
Optional: true,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be optional. I'm pretty sure it's required unless the API is defaulting.

ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
Expand Down Expand Up @@ -174,6 +187,8 @@ func (br costManagementExportBaseResource) readFunc(scopeFieldName string) sdk.R

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
metadata.ResourceData.Set("partition_data_enabled", pointer.From(props.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 +287,7 @@ func createOrUpdateCostManagementExport(ctx context.Context, client *exports.Exp
props := exports.Export{
ETag: etag,
Properties: &exports.ExportProperties{
PartitionData: pointer.To(metadata.ResourceData.Get("partition_data_enabled").(bool)),
Schedule: &exports.ExportSchedule{
Recurrence: &recurrenceType,
RecurrencePeriod: &exports.ExportRecurrencePeriod{
Expand Down Expand Up @@ -321,7 +337,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: pointer.To(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 +400,14 @@ func flattenExportDefinition(input *exports.ExportDefinition) []interface{} {
queryType = string(input.Type)
}

return []interface{}{
map[string]interface{}{
"time_frame": string(input.Timeframe),
"type": queryType,
},
result := map[string]interface{}{
"time_frame": string(input.Timeframe),
"type": queryType,
}

if input.DataSet != nil && input.DataSet.Configuration != nil && input.DataSet.Configuration.DataVersion != nil {
result["data_version"] = pointer.From(input.DataSet.Configuration.DataVersion)
}

return []interface{}{result}
}
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 All @@ -34,6 +34,21 @@ func TestAccResourceGroupCostManagementExport_basic(t *testing.T) {
})
}

func TestAccResourceGroupCostManagementExport_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_group_cost_management_export", "test")
r := ResourceGroupCostManagementExport{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccResourceGroupCostManagementExport_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_group_cost_management_export", "test")
r := ResourceGroupCostManagementExport{}
Expand Down Expand Up @@ -130,11 +145,57 @@ resource "azurerm_resource_group_cost_management_export" "test" {

export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
root_folder_path = "root"
}
export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, start, end)
}

func (ResourceGroupCostManagementExport) complete(data acceptance.TestData) string {
start := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
end := time.Now().AddDate(0, 0, 2).Format("2006-01-02")

return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-cm-%d"
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_container" "test" {
name = "acctestcontainer%s"
storage_account_name = azurerm_storage_account.test.name
}

resource "azurerm_resource_group_cost_management_export" "test" {
name = "accrg%d"
resource_group_id = azurerm_resource_group.test.id
recurrence_type = "Monthly"
recurrence_period_start_date = "%sT00:00:00Z"
recurrence_period_end_date = "%sT00:00:00Z"
partition_data_enabled = 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"
type = "Usage"
time_frame = "TheLastMonth"
data_version = "2021-10-01"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, start, end)
Expand Down Expand Up @@ -174,11 +235,11 @@ resource "azurerm_resource_group_cost_management_export" "test" {

export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root/updated"
root_folder_path = "root/updated"
}
export_data_options {
type = "Usage"
time_frame = "WeekToDate"
type = "Usage"
time_frame = "WeekToDate"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, start, end)
Expand All @@ -198,12 +259,12 @@ resource "azurerm_resource_group_cost_management_export" "import" {

export_data_storage_location {
container_id = azurerm_storage_container.test.resource_manager_id
root_folder_path = "/root"
root_folder_path = "root"
}

export_data_options {
type = "Usage"
time_frame = "TheLastMonth"
type = "Usage"
time_frame = "TheLastMonth"
}
}
`, template)
Expand Down
Loading
Loading