Skip to content

Commit 8c0021f

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

File tree

4 files changed

+196
-1
lines changed

4 files changed

+196
-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.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
subcategory: "CE (Cost Explorer)"
3+
layout: "aws"
4+
page_title: "AWS: ce_cost_allocation_tags"
5+
description: |-
6+
Provides the available cost allocation tags.
7+
---
8+
9+
# Data Source: ce_cost_allocation_tags
10+
11+
Provides the available cost allocation tags.
12+
13+
## Example Usage
14+
15+
### Basic Usage
16+
17+
```terraform
18+
data "aws_ce_cost_allocation_tags" "tags" {}
19+
```
20+
21+
### Filter by Status and Type
22+
23+
```terraform
24+
data "aws_ce_cost_allocation_tags" "inactive_tags" {
25+
status = "Inactive"
26+
type = "UserDefined"
27+
}
28+
```
29+
30+
### Filter by Tag Keys
31+
32+
```terraform
33+
data "aws_ce_cost_allocation_tags" "tags" {
34+
tag_keys = ["tag_a", "tag_b"]
35+
}
36+
```
37+
38+
## Argument Reference
39+
40+
This data source supports the following arguments:
41+
42+
* `status` - (Optional) The status of cost allocation tags that you want to return values for.
43+
* `type` - (Optional) The type of cost allocation tags that you want to return values for.
44+
* `tag_keys` - (Optional) Keys of the tag that you want to return values for.
45+
46+
## Attribute Reference
47+
48+
This data source exports the following attributes in addition to the arguments above:
49+
50+
* `tags` - Tags that match your request.
51+
52+
### `tags` Attribute Reference
53+
54+
* `last_updated_date` - The last date that the tag was either activated or deactivated.
55+
* `last_used_date` - The last month that the tag was used on an Amazon Web Services resource.
56+
* `status` - The status of the tag.
57+
* `tag_key` - Key of the tag.
58+
* `type` - The type of the tag.

0 commit comments

Comments
 (0)