From 34422e36e1b69ad0d8459f8dcf6753dddcdb1f0d Mon Sep 17 00:00:00 2001 From: Akira Kure Date: Wed, 22 Oct 2025 14:58:43 +0900 Subject: [PATCH 1/2] feat: Add aws_ce_cost_allocation_tags data source --- .../ce/cost_allocation_tags_data_source.go | 91 +++++++++++++++++++ .../cost_allocation_tags_data_source_test.go | 39 ++++++++ internal/service/ce/service_package_gen.go | 9 +- .../d/ce_cost_allocation_tags.html.markdown | 58 ++++++++++++ 4 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 internal/service/ce/cost_allocation_tags_data_source.go create mode 100644 internal/service/ce/cost_allocation_tags_data_source_test.go create mode 100644 website/docs/d/ce_cost_allocation_tags.html.markdown diff --git a/internal/service/ce/cost_allocation_tags_data_source.go b/internal/service/ce/cost_allocation_tags_data_source.go new file mode 100644 index 000000000000..ddea5de4b06d --- /dev/null +++ b/internal/service/ce/cost_allocation_tags_data_source.go @@ -0,0 +1,91 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ce + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/service/costexplorer" + "github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-provider-aws/internal/framework" + "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" + "github.com/hashicorp/terraform-provider-aws/names" +) + +type costAllocationTagsDataSource struct { + framework.DataSourceWithModel[costAllocationTagsDataSourceModel] +} + +// @FrameworkDataSource("aws_ce_cost_allocation_tags", name="Cost Allocation Tags") +func newCostAllocationTagsDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + return &costAllocationTagsDataSource{}, nil +} + +func (d *costAllocationTagsDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + names.AttrStatus: schema.StringAttribute{ + Optional: true, + Computed: true, + }, + names.AttrType: schema.StringAttribute{ + Optional: true, + Computed: true, + }, + "tag_keys": schema.ListAttribute{ + CustomType: fwtypes.ListOfStringType, + ElementType: types.StringType, + Optional: true, + }, + names.AttrTags: framework.DataSourceComputedListOfObjectAttribute[costAllocationTagModel](ctx), + }, + } +} + +func (d *costAllocationTagsDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data costAllocationTagsDataSourceModel + + response.Diagnostics.Append(request.Config.Get(ctx, &data)...) + + if response.Diagnostics.HasError() { + return + } + + conn := d.Meta().CEClient(ctx) + + input := &costexplorer.ListCostAllocationTagsInput{} + response.Diagnostics.Append(flex.Expand(ctx, data, input)...) + + output, err := conn.ListCostAllocationTags(ctx, input) + if err != nil { + response.Diagnostics.AddError("listing Cost Allocation Tags", err.Error()) + return + } + + response.Diagnostics.Append(flex.Flatten(ctx, output.CostAllocationTags, &data.Tags)...) + if response.Diagnostics.HasError() { + return + } + + response.Diagnostics.Append(response.State.Set(ctx, &data)...) +} + +type costAllocationTagsDataSourceModel struct { + Status types.String `tfsdk:"status"` + Type types.String `tfsdk:"type"` + TagKeys fwtypes.ListOfString `tfsdk:"tag_keys"` + Tags fwtypes.ListNestedObjectValueOf[costAllocationTagModel] `tfsdk:"tags"` +} + +type costAllocationTagModel struct { + TagKey types.String `tfsdk:"tag_key"` + Status types.String `tfsdk:"status"` + Type types.String `tfsdk:"type"` + LastUpdatedDate timetypes.RFC3339 `tfsdk:"last_updated_date"` + LastUsedDate timetypes.RFC3339 `tfsdk:"last_used_date"` +} diff --git a/internal/service/ce/cost_allocation_tags_data_source_test.go b/internal/service/ce/cost_allocation_tags_data_source_test.go new file mode 100644 index 000000000000..870e3e6e5f40 --- /dev/null +++ b/internal/service/ce/cost_allocation_tags_data_source_test.go @@ -0,0 +1,39 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ce_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func TestAccCECostAllocationTagsDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + dataSourceName := "data.aws_ce_cost_allocation_tags.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccCostAllocationTagsDataSourceConfig_basic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "tags.#"), + ), + }, + }, + }) +} + +func testAccCostAllocationTagsDataSourceConfig_basic() string { + return ` +data "aws_ce_cost_allocation_tags" "test" {} +` +} diff --git a/internal/service/ce/service_package_gen.go b/internal/service/ce/service_package_gen.go index 15d5c98dfa43..29519510616d 100644 --- a/internal/service/ce/service_package_gen.go +++ b/internal/service/ce/service_package_gen.go @@ -18,7 +18,14 @@ import ( type servicePackage struct{} func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*inttypes.ServicePackageFrameworkDataSource { - return []*inttypes.ServicePackageFrameworkDataSource{} + return []*inttypes.ServicePackageFrameworkDataSource{ + { + Factory: newCostAllocationTagsDataSource, + TypeName: "aws_ce_cost_allocation_tags", + Name: "Cost Allocation Tags", + Region: unique.Make(inttypes.ResourceRegionDisabled()), + }, + } } func (p *servicePackage) FrameworkResources(ctx context.Context) []*inttypes.ServicePackageFrameworkResource { diff --git a/website/docs/d/ce_cost_allocation_tags.html.markdown b/website/docs/d/ce_cost_allocation_tags.html.markdown new file mode 100644 index 000000000000..de626fd56c00 --- /dev/null +++ b/website/docs/d/ce_cost_allocation_tags.html.markdown @@ -0,0 +1,58 @@ +--- +subcategory: "CE (Cost Explorer)" +layout: "aws" +page_title: "AWS: ce_cost_allocation_tags" +description: |- + Provides the available cost allocation tags. +--- + +# Data Source: ce_cost_allocation_tags + +Provides the available cost allocation tags. + +## Example Usage + +### Basic Usage + +```terraform +data "aws_ce_cost_allocation_tags" "tags" {} +``` + +### Filter by Status and Type + +```terraform +data "aws_ce_cost_allocation_tags" "inactive_tags" { + status = "Inactive" + type = "UserDefined" +} +``` + +### Filter by Tag Keys + +```terraform +data "aws_ce_cost_allocation_tags" "tags" { + tag_keys = ["tag_a", "tag_b"] +} +``` + +## Argument Reference + +This data source supports the following arguments: + +* `status` - (Optional) The status of cost allocation tags that you want to return values for. +* `type` - (Optional) The type of cost allocation tags that you want to return values for. +* `tag_keys` - (Optional) Keys of the tag that you want to return values for. + +## Attribute Reference + +This data source exports the following attributes in addition to the arguments above: + +* `tags` - Tags that match your request. + +### `tags` Attribute Reference + +* `last_updated_date` - The last date that the tag was either activated or deactivated. +* `last_used_date` - The last month that the tag was used on an Amazon Web Services resource. +* `status` - The status of the tag. +* `tag_key` - Key of the tag. +* `type` - The type of the tag. From 8f612a5dd79bd2dfd29174f52dec3d51ff262c46 Mon Sep 17 00:00:00 2001 From: Akira Kure Date: Wed, 22 Oct 2025 20:16:14 +0900 Subject: [PATCH 2/2] add Changelog Entry --- .changelog/44763.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/44763.txt diff --git a/.changelog/44763.txt b/.changelog/44763.txt new file mode 100644 index 000000000000..db80bbf3e38c --- /dev/null +++ b/.changelog/44763.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_ce_cost_allocation_tags +```