|
| 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 | +} |
0 commit comments