Skip to content
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
3 changes: 3 additions & 0 deletions .changelog/44763.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_ce_cost_allocation_tags
```
91 changes: 91 additions & 0 deletions internal/service/ce/cost_allocation_tags_data_source.go
Original file line number Diff line number Diff line change
@@ -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"`
}
39 changes: 39 additions & 0 deletions internal/service/ce/cost_allocation_tags_data_source_test.go
Original file line number Diff line number Diff line change
@@ -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" {}
`
}
9 changes: 8 additions & 1 deletion internal/service/ce/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions website/docs/d/ce_cost_allocation_tags.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
Loading