|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package appconfig |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "iter" |
| 10 | + |
| 11 | + "github.com/YakDriver/regexache" |
| 12 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/appconfig" |
| 14 | + awstypes "github.com/aws/aws-sdk-go-v2/service/appconfig/types" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 20 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 21 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 22 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 23 | + "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" |
| 24 | + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" |
| 25 | + "github.com/hashicorp/terraform-provider-aws/internal/smerr" |
| 26 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 27 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 28 | +) |
| 29 | + |
| 30 | +// @FrameworkDataSource("aws_appconfig_application", name="Application") |
| 31 | +func newDataSourceApplication(context.Context) (datasource.DataSourceWithConfigure, error) { |
| 32 | + return &dataSourceApplication{}, nil |
| 33 | +} |
| 34 | + |
| 35 | +type dataSourceApplication struct { |
| 36 | + framework.DataSourceWithModel[dataSourceApplicationModel] |
| 37 | +} |
| 38 | + |
| 39 | +func (d *dataSourceApplication) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 40 | + resp.Schema = schema.Schema{ |
| 41 | + Attributes: map[string]schema.Attribute{ |
| 42 | + names.AttrARN: framework.ARNAttributeComputedOnly(), |
| 43 | + names.AttrDescription: schema.StringAttribute{ |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + names.AttrID: schema.StringAttribute{ |
| 47 | + Optional: true, |
| 48 | + Computed: true, |
| 49 | + Validators: []validator.String{ |
| 50 | + stringvalidator.RegexMatches( |
| 51 | + regexache.MustCompile(`^[0-9a-z]{4,7}$`), |
| 52 | + "value must contain 4-7 lowercase letters or numbers", |
| 53 | + ), |
| 54 | + }, |
| 55 | + }, |
| 56 | + names.AttrName: schema.StringAttribute{ |
| 57 | + Optional: true, |
| 58 | + Computed: true, |
| 59 | + Validators: []validator.String{ |
| 60 | + stringvalidator.LengthBetween(1, 64), |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func (d *dataSourceApplication) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 68 | + conn := d.Meta().AppConfigClient(ctx) |
| 69 | + |
| 70 | + var data dataSourceApplicationModel |
| 71 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, req.Config.Get(ctx, &data)) |
| 72 | + if resp.Diagnostics.HasError() { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + var out *awstypes.Application |
| 77 | + var err error |
| 78 | + var input appconfig.ListApplicationsInput |
| 79 | + if !data.ID.IsNull() { |
| 80 | + out, err = findApplicationWithFilter(ctx, conn, &input, func(v *awstypes.Application) bool { |
| 81 | + return aws.ToString(v.Id) == data.ID.ValueString() |
| 82 | + }, tfslices.WithReturnFirstMatch) |
| 83 | + } |
| 84 | + |
| 85 | + if !data.Name.IsNull() { |
| 86 | + out, err = findApplicationWithFilter(ctx, conn, &input, func(v *awstypes.Application) bool { |
| 87 | + return aws.ToString(v.Name) == data.Name.ValueString() |
| 88 | + }, tfslices.WithReturnFirstMatch) |
| 89 | + } |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + smerr.AddError(ctx, &resp.Diagnostics, err, smerr.ID, data.Name.String()) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, flex.Flatten(ctx, out, &data), smerr.ID, data.Name.String()) |
| 97 | + if resp.Diagnostics.HasError() { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + data.ARN = flex.StringValueToFramework(ctx, d.Meta().RegionalARN(ctx, "appconfig", "application/"+data.ID.ValueString())) |
| 102 | + |
| 103 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, resp.State.Set(ctx, &data), smerr.ID, data.Name.String()) |
| 104 | +} |
| 105 | + |
| 106 | +func (d *dataSourceApplication) ConfigValidators(_ context.Context) []datasource.ConfigValidator { |
| 107 | + return []datasource.ConfigValidator{ |
| 108 | + datasourcevalidator.ExactlyOneOf( |
| 109 | + path.MatchRoot(names.AttrID), |
| 110 | + path.MatchRoot(names.AttrName), |
| 111 | + ), |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +type dataSourceApplicationModel struct { |
| 116 | + framework.WithRegionModel |
| 117 | + ARN types.String `tfsdk:"arn"` |
| 118 | + Description types.String `tfsdk:"description"` |
| 119 | + ID types.String `tfsdk:"id"` |
| 120 | + Name types.String `tfsdk:"name"` |
| 121 | +} |
| 122 | + |
| 123 | +func findApplicationWithFilter(ctx context.Context, conn *appconfig.Client, input *appconfig.ListApplicationsInput, filter tfslices.Predicate[*awstypes.Application], optFns ...tfslices.FinderOptionsFunc) (*awstypes.Application, error) { |
| 124 | + opts := tfslices.NewFinderOptions(optFns) |
| 125 | + var output []awstypes.Application |
| 126 | + for value, err := range listApplications(ctx, conn, input, filter) { |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + output = append(output, value) |
| 132 | + if opts.ReturnFirstMatch() { |
| 133 | + break |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return tfresource.AssertSingleValueResult(output) |
| 138 | +} |
| 139 | + |
| 140 | +func listApplications(ctx context.Context, conn *appconfig.Client, input *appconfig.ListApplicationsInput, filter tfslices.Predicate[*awstypes.Application]) iter.Seq2[awstypes.Application, error] { |
| 141 | + return func(yield func(awstypes.Application, error) bool) { |
| 142 | + pages := appconfig.NewListApplicationsPaginator(conn, input) |
| 143 | + for pages.HasMorePages() { |
| 144 | + page, err := pages.NextPage(ctx) |
| 145 | + if err != nil { |
| 146 | + yield(awstypes.Application{}, fmt.Errorf("listing AppConfig Applications: %w", err)) |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + for _, v := range page.Items { |
| 151 | + if filter(&v) { |
| 152 | + if !yield(v, nil) { |
| 153 | + return |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments