Skip to content

costmanagement: convert export resources to use typed models with Decode/Encode#32178

Open
katbyte wants to merge 2 commits intomainfrom
kt/bad-typed-cost-mgmt
Open

costmanagement: convert export resources to use typed models with Decode/Encode#32178
katbyte wants to merge 2 commits intomainfrom
kt/bad-typed-cost-mgmt

Conversation

@katbyte
Copy link
Copy Markdown
Collaborator

@katbyte katbyte commented Apr 16, 2026

Community Note

  • Please vote on this PR by adding a 👍 reaction to the original PR to help the community and maintainers prioritize for review
  • Please do not leave comments along the lines of "+1", "me too" or "any updates", they generate extra noise for PR followers and do not help prioritize for review

Description

Convert the three cost management export resources (azurerm_billing_account_cost_management_export, azurerm_resource_group_cost_management_export, azurerm_subscription_cost_management_export) from returning nil in ModelObject() with raw metadata.ResourceData.Get()/Set() calls to using proper typed model structs with metadata.Decode()/metadata.Encode().
This follows the same pattern applied to the consumption budget resources in #31929.

PR Checklist

  • I have followed the guidelines in our Contributing Documentation.
  • I have checked to ensure there aren't other open Pull Requests for the same update/change.
  • I have checked if my changes close any open issues. If so please include appropriate closing keywords below.
  • I have updated/added Documentation as required written in a helpful and kind way to assist users that may be unfamiliar with the resource / data source.
  • I have used a meaningful PR title to help maintainers and other users understand this change and help prevent duplicate work.

Changes to existing Resource / Data Source

  • I have added an explanation of what my changes do and why I'd like you to include them (This may be covered by linking to an issue above, but may benefit from additional explanation).
  • I have written new tests for my resource or datasource changes & updated any relevant documentation.
  • I have successfully run tests with my changes locally. If not, please provide details on testing challenges that prevented you running the tests.

Testing

  • go build ./internal/services/costmanagement/... — passes
  • go vet ./internal/services/costmanagement/... — passes
  • make static-analysis — passes
  • Test registration (go test -short) — all cost management export tests discover and register without ValidateModelObject panics
    No behavioral changes were made; existing acceptance tests cover the Create/Read/Update/Delete flows and will validate correctness when run with TF_ACC.

Change Log

  • azurerm_billing_account_cost_management_export - refactor to use typed model with Decode/Encode
  • azurerm_resource_group_cost_management_export - refactor to use typed model with Decode/Encode
  • azurerm_subscription_cost_management_export - refactor to use typed model with Decode/Encode
    This is a (please select all that apply):
  • Bug Fix
  • New Feature (ie adding a service, resource, or data source)
  • Enhancement
  • Breaking Change

Related Issue(s)

N/A — internal tech debt cleanup

AI Assistance Disclosure

  • AI Assisted - This contribution was made by, or with the assistance of, AI/LLMs
    AI was used for code generation of the typed model structs, expand/flatten helpers, and inlined CRUD methods. All changes were reviewed and validated manually.

Rollback Plan

If a change needs to be reverted, we will publish an updated version of the provider.

Changes to Security Controls

No changes to security controls.

Copy link
Copy Markdown
Collaborator

@sreallymatt sreallymatt left a comment

Choose a reason for hiding this comment

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

Thanks @katbyte - I've left some comments inline


format := exports.FormatType(config.FileFormat)
recurrenceType := exports.RecurrenceType(config.RecurrenceType)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These can be inlined into the exports.Export struct instantiation using pointer.ToEnum

Suggested change

props := exports.Export{
Properties: &exports.ExportProperties{
Schedule: &exports.ExportSchedule{
Recurrence: &recurrenceType,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Recurrence: &recurrenceType,
Recurrence: pointer.ToEnum[exports.RecurrenceType](config.RecurrenceType),

},
Status: &status,
},
DeliveryInfo: *deliveryInfo,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

expandExportDataStorageLocationFromModel returns nil without an error when the input length is 0, while the block is currently marked as Required and this should always be > 1, we may want to guard against nil pointer dereferences in case this changes to Optional in the future.

Status: &status,
},
DeliveryInfo: *deliveryInfo,
Format: &format,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Format: &format,
Format: pointer.ToEnum[exports.FormatType](config.FileFormat),

},
DeliveryInfo: *deliveryInfo,
Format: &format,
Definition: *expandExportDataOptionsFromModel(config.ExportDataOptions),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Similar concern as above with DeliveryInfo

var opts exports.GetOperationOptions
resp, err := client.Get(ctx, *id, opts)
if err != nil {
return fmt.Errorf("reading %s: %+v", *id, err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("reading %s: %+v", *id, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)

format := exports.FormatType(config.FileFormat)
recurrenceType := exports.RecurrenceType(config.RecurrenceType)

props := exports.Export{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we use d.HasChanges to conditionally update the returned model instead of rebuilding from scratch?

recurrenceType := exports.RecurrenceType(config.RecurrenceType)

props := exports.Export{
ETag: resp.Model.ETag,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Potential panic given the error on ln218 only happens when resp.Model is non-nil but eTag is nil

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

Build failure: GoLang Linting

This pull request contains a build failure in the GoLang Linting check which needs to be addressed here.

How to fix: Run the Go linter locally with golangci-lint run -v ./internal/... and fix any reported issues.

For more information, please refer to our Contributing Guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants