|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package appconfig |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/YakDriver/regexache" |
| 11 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 12 | + "github.com/aws/aws-sdk-go-v2/service/appconfig" |
| 13 | + awstypes "github.com/aws/aws-sdk-go-v2/service/appconfig/types" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/internal/errs" |
| 20 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 21 | + "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" |
| 22 | + "github.com/hashicorp/terraform-provider-aws/internal/retry" |
| 23 | + "github.com/hashicorp/terraform-provider-aws/internal/smerr" |
| 24 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 25 | +) |
| 26 | + |
| 27 | +// Function annotations are used for datasource registration to the Provider. DO NOT EDIT. |
| 28 | +// @FrameworkDataSource("aws_appconfig_application", name="Application") |
| 29 | +func newDataSourceApplication(context.Context) (datasource.DataSourceWithConfigure, error) { |
| 30 | + return &dataSourceApplication{}, nil |
| 31 | +} |
| 32 | + |
| 33 | +const ( |
| 34 | + DSNameApplication = "Application Data Source" |
| 35 | +) |
| 36 | + |
| 37 | +type dataSourceApplication struct { |
| 38 | + framework.DataSourceWithModel[dataSourceApplicationModel] |
| 39 | +} |
| 40 | + |
| 41 | +func (d *dataSourceApplication) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 42 | + resp.Schema = schema.Schema{ |
| 43 | + Attributes: map[string]schema.Attribute{ |
| 44 | + names.AttrDescription: schema.StringAttribute{ |
| 45 | + Computed: true, |
| 46 | + Validators: []validator.String{ |
| 47 | + stringvalidator.LengthBetween(0, 1024), |
| 48 | + }, |
| 49 | + }, |
| 50 | + names.AttrID: schema.StringAttribute{ |
| 51 | + Computed: true, |
| 52 | + Validators: []validator.String{ |
| 53 | + stringvalidator.RegexMatches( |
| 54 | + regexache.MustCompile(`^[0-9a-z]{4,7}$`), |
| 55 | + "value must contain 4-7 lowercase letters or numbers", |
| 56 | + ), |
| 57 | + }, |
| 58 | + }, |
| 59 | + //names.AttrARN: framework.ARNAttributeComputedOnly(), |
| 60 | + names.AttrName: schema.StringAttribute{ |
| 61 | + Required: true, |
| 62 | + Validators: []validator.String{ |
| 63 | + stringvalidator.LengthBetween(1, 64), |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (d *dataSourceApplication) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 71 | + conn := d.Meta().AppConfigClient(ctx) |
| 72 | + |
| 73 | + var data dataSourceApplicationModel |
| 74 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, req.Config.Get(ctx, &data)) |
| 75 | + if resp.Diagnostics.HasError() { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + out, err := findApplicationByName(ctx, conn, data.Name.ValueString()) |
| 80 | + if err != nil { |
| 81 | + smerr.AddError(ctx, &resp.Diagnostics, err, smerr.ID, data.Name.String()) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, flex.Flatten(ctx, out, &data, flex.WithFieldNamePrefix("Application")), smerr.ID, data.Name.String()) |
| 86 | + if resp.Diagnostics.HasError() { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + smerr.EnrichAppend(ctx, &resp.Diagnostics, resp.State.Set(ctx, &data), smerr.ID, data.Name.String()) |
| 91 | +} |
| 92 | + |
| 93 | +type dataSourceApplicationModel struct { |
| 94 | + framework.WithRegionModel |
| 95 | + Description types.String `tfsdk:"description"` |
| 96 | + ID types.String `tfsdk:"id"` |
| 97 | + Name types.String `tfsdk:"name"` |
| 98 | +} |
| 99 | + |
| 100 | +func findApplicationByName(ctx context.Context, conn *appconfig.Client, name string) (*appconfig.GetApplicationOutput, error) { |
| 101 | + input := &appconfig.ListApplicationsInput{} |
| 102 | + |
| 103 | + pages := appconfig.NewListApplicationsPaginator(conn, input) |
| 104 | + for pages.HasMorePages() { |
| 105 | + page, err := pages.NextPage(ctx) |
| 106 | + |
| 107 | + if errs.IsA[*awstypes.ResourceNotFoundException](err) { |
| 108 | + return nil, &retry.NotFoundError{ |
| 109 | + LastError: err, |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + for _, app := range page.Items { |
| 118 | + if aws.ToString(app.Name) == name { |
| 119 | + // AppConfig does not support duplicate names, so we can return the first match |
| 120 | + return findApplicationByID(ctx, conn, aws.ToString(app.Id)) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil, &retry.NotFoundError{ |
| 126 | + LastError: fmt.Errorf("AppConfig Application (%s) not found", name), |
| 127 | + } |
| 128 | +} |
0 commit comments