|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package billing |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/billing" |
| 10 | + awstypes "github.com/aws/aws-sdk-go-v2/service/billing/types" |
| 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/internal/smerr" |
| 18 | +) |
| 19 | + |
| 20 | +// @FrameworkDataSource("aws_billing_views", name="Views") |
| 21 | +func newDataSourceViews(context.Context) (datasource.DataSourceWithConfigure, error) { |
| 22 | + return &dataSourceViews{}, nil |
| 23 | +} |
| 24 | + |
| 25 | +const ( |
| 26 | + DSNameViews = "Views Data Source" |
| 27 | +) |
| 28 | + |
| 29 | +type dataSourceViews struct { |
| 30 | + framework.DataSourceWithModel[dataSourceViewsModel] |
| 31 | +} |
| 32 | + |
| 33 | +func (d *dataSourceViews) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 34 | + resp.Schema = schema.Schema{ |
| 35 | + Attributes: map[string]schema.Attribute{ |
| 36 | + "billing_view_types": schema.ListAttribute{ |
| 37 | + CustomType: fwtypes.ListOfStringEnumType[awstypes.BillingViewType](), |
| 38 | + Optional: true, |
| 39 | + ElementType: types.StringType, |
| 40 | + }, |
| 41 | + "billing_view": framework.ResourceComputedListOfObjectsAttribute[dataSourceBillingViewModel](ctx, nil, nil), |
| 42 | + }, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (d *dataSourceViews) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 47 | + conn := d.Meta().BillingClient(ctx) |
| 48 | + |
| 49 | + var data dataSourceViewsModel |
| 50 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, req.Config.Get(ctx, &data)) |
| 51 | + if resp.Diagnostics.HasError() { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + var billingViewTypes []awstypes.BillingViewType |
| 56 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, data.BillingViewTypes.ElementsAs(ctx, &billingViewTypes, false)) |
| 57 | + if resp.Diagnostics.HasError() { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + out, err := findViewsByViewTypes(ctx, conn, billingViewTypes) |
| 62 | + if err != nil { |
| 63 | + smerr.AddError(ctx, &resp.Diagnostics, err, smerr.ID, data.BillingViewTypes.String()) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, flex.Flatten(ctx, out, &data.BillingView, flex.WithFieldNamePrefix("Views")), smerr.ID, data.BillingViewTypes.String()) |
| 68 | + if resp.Diagnostics.HasError() { |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, resp.State.Set(ctx, &data), smerr.ID, data.BillingViewTypes.String()) |
| 73 | +} |
| 74 | + |
| 75 | +func findViewsByViewTypes(ctx context.Context, conn *billing.Client, billingViewTypes []awstypes.BillingViewType) ([]awstypes.BillingViewListElement, error) { |
| 76 | + input := billing.ListBillingViewsInput{} |
| 77 | + if len(billingViewTypes) > 0 { |
| 78 | + input.BillingViewTypes = billingViewTypes |
| 79 | + } |
| 80 | + |
| 81 | + return findViews(ctx, conn, &input) |
| 82 | +} |
| 83 | + |
| 84 | +func findViews(ctx context.Context, conn *billing.Client, input *billing.ListBillingViewsInput) ([]awstypes.BillingViewListElement, error) { |
| 85 | + var results []awstypes.BillingViewListElement |
| 86 | + |
| 87 | + paginator := billing.NewListBillingViewsPaginator(conn, input) |
| 88 | + for paginator.HasMorePages() { |
| 89 | + page, err := paginator.NextPage(ctx) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + results = append(results, page.BillingViews...) |
| 94 | + } |
| 95 | + |
| 96 | + return results, nil |
| 97 | +} |
| 98 | + |
| 99 | +type dataSourceViewsModel struct { |
| 100 | + BillingViewTypes fwtypes.ListOfStringEnum[awstypes.BillingViewType] `tfsdk:"billing_view_types"` |
| 101 | + BillingView fwtypes.ListNestedObjectValueOf[dataSourceBillingViewModel] `tfsdk:"billing_view"` |
| 102 | +} |
| 103 | + |
| 104 | +type dataSourceBillingViewModel struct { |
| 105 | + ARN types.String `tfsdk:"arn"` |
| 106 | + BillingViewType types.String `tfsdk:"billing_view_type"` |
| 107 | + Description types.String `tfsdk:"description"` |
| 108 | + Name types.String `tfsdk:"name"` |
| 109 | + OwnerAccountId types.String `tfsdk:"owner_account_id"` |
| 110 | +} |
0 commit comments