Skip to content

Commit aefac59

Browse files
committed
feat: Add aws_ce_cost_allocation_tags data source
1 parent 03d14bb commit aefac59

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package ce
5+
6+
import (
7+
"context"
8+
9+
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
10+
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
11+
"github.com/hashicorp/terraform-plugin-framework/datasource"
12+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-provider-aws/internal/framework"
15+
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
16+
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
17+
"github.com/hashicorp/terraform-provider-aws/names"
18+
)
19+
20+
type costAllocationTagsDataSource struct {
21+
framework.DataSourceWithModel[costAllocationTagsDataSourceModel]
22+
}
23+
24+
// @FrameworkDataSource("aws_ce_cost_allocation_tags", name="Cost Allocation Tags")
25+
func newCostAllocationTagsDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
26+
return &costAllocationTagsDataSource{}, nil
27+
}
28+
29+
func (d *costAllocationTagsDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
30+
response.Schema = schema.Schema{
31+
Attributes: map[string]schema.Attribute{
32+
names.AttrStatus: schema.StringAttribute{
33+
Optional: true,
34+
Computed: true,
35+
},
36+
names.AttrType: schema.StringAttribute{
37+
Optional: true,
38+
Computed: true,
39+
},
40+
"tag_keys": schema.ListAttribute{
41+
CustomType: fwtypes.ListOfStringType,
42+
ElementType: types.StringType,
43+
Optional: true,
44+
},
45+
names.AttrTags: framework.DataSourceComputedListOfObjectAttribute[costAllocationTagModel](ctx),
46+
},
47+
}
48+
}
49+
50+
func (d *costAllocationTagsDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
51+
var data costAllocationTagsDataSourceModel
52+
53+
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
54+
55+
if response.Diagnostics.HasError() {
56+
return
57+
}
58+
59+
conn := d.Meta().CEClient(ctx)
60+
61+
input := &costexplorer.ListCostAllocationTagsInput{}
62+
response.Diagnostics.Append(flex.Expand(ctx, data, input)...)
63+
64+
output, err := conn.ListCostAllocationTags(ctx, input)
65+
if err != nil {
66+
response.Diagnostics.AddError("listing Cost Allocation Tags", err.Error())
67+
return
68+
}
69+
70+
response.Diagnostics.Append(flex.Flatten(ctx, output.CostAllocationTags, &data.Tags)...)
71+
if response.Diagnostics.HasError() {
72+
return
73+
}
74+
75+
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
76+
}
77+
78+
type costAllocationTagsDataSourceModel struct {
79+
Status types.String `tfsdk:"status"`
80+
Type types.String `tfsdk:"type"`
81+
TagKeys fwtypes.ListOfString `tfsdk:"tag_keys"`
82+
Tags fwtypes.ListNestedObjectValueOf[costAllocationTagModel] `tfsdk:"tags"`
83+
}
84+
85+
type costAllocationTagModel struct {
86+
TagKey types.String `tfsdk:"tag_key"`
87+
Status types.String `tfsdk:"status"`
88+
Type types.String `tfsdk:"type"`
89+
LastUpdatedDate timetypes.RFC3339 `tfsdk:"last_updated_date"`
90+
LastUsedDate timetypes.RFC3339 `tfsdk:"last_used_date"`
91+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package ce_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
11+
"github.com/hashicorp/terraform-provider-aws/names"
12+
)
13+
14+
func TestAccCECostAllocationTagsDataSource_basic(t *testing.T) {
15+
ctx := acctest.Context(t)
16+
dataSourceName := "data.aws_ce_cost_allocation_tags.test"
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
acctest.PreCheck(ctx, t)
21+
},
22+
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
23+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccCostAllocationTagsDataSourceConfig_basic(),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttrSet(dataSourceName, "tags.#"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func testAccCostAllocationTagsDataSourceConfig_basic() string {
36+
return `
37+
data "aws_ce_cost_allocation_tags" "test" {}
38+
`
39+
}

internal/service/ce/service_package_gen.go

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)