Skip to content

Commit af4795c

Browse files
Adds data source for appconfig application
1 parent ad94254 commit af4795c

File tree

5 files changed

+231
-1
lines changed

5 files changed

+231
-1
lines changed

.changelog/44168.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
aws_appconfig_application
3+
```
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package appconfig_test
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/YakDriver/regexache"
11+
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
12+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
14+
"github.com/hashicorp/terraform-provider-aws/names"
15+
)
16+
17+
func TestAccAppConfigApplicationDataSource_basic(t *testing.T) {
18+
ctx := acctest.Context(t)
19+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
20+
dataSourceName := "data.aws_appconfig_application.test"
21+
resourceName := "aws_appconfig_application.test"
22+
23+
resource.ParallelTest(t, resource.TestCase{
24+
PreCheck: func() {
25+
acctest.PreCheck(ctx, t)
26+
acctest.PreCheckPartitionHasService(t, names.AppConfigEndpointID)
27+
},
28+
ErrorCheck: acctest.ErrorCheck(t, names.AppConfigServiceID),
29+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
30+
CheckDestroy: testAccCheckApplicationDestroy(ctx),
31+
Steps: []resource.TestStep{
32+
{
33+
Config: testAccApplicationDataSourceConfig_basic(rName),
34+
Check: resource.ComposeAggregateTestCheckFunc(
35+
testAccCheckApplicationExists(ctx, dataSourceName),
36+
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrDescription, resourceName, names.AttrDescription),
37+
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrID, resourceName, names.AttrID),
38+
resource.TestMatchResourceAttr(dataSourceName, names.AttrID, regexache.MustCompile(`[a-z\d]{4,7}`)),
39+
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrName, resourceName, names.AttrName),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func testAccApplicationDataSourceConfig_basic(rName string) string {
47+
return fmt.Sprintf(`
48+
resource "aws_appconfig_application" "test" {
49+
name = %[1]q
50+
description = "Example AppConfig Application"
51+
}
52+
53+
data "aws_appconfig_application" "test" {
54+
name = aws_appconfig_application.test.name
55+
}
56+
`, rName)
57+
}

internal/service/appconfig/service_package_gen.go

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory: "AppConfig"
3+
layout: "aws"
4+
page_title: "AWS: aws_appconfig_application"
5+
description: |-
6+
Retrieves an AWS AppConfig Application by name.
7+
---
8+
9+
# Data Source: aws_appconfig_application
10+
11+
Provides details about an AWS AppConfig Application.
12+
13+
## Example Usage
14+
15+
### Basic Usage
16+
17+
```terraform
18+
data "aws_appconfig_application" "example" {
19+
name = "my-appconfig-application
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are required:
26+
27+
* `name` - (Required) AWS AppConfig Application name.
28+
29+
## Attribute Reference
30+
31+
This data source exports the following attributes in addition to the arguments above:
32+
33+
* `description` - Description of the Application.
34+
* `id` - ID of the Application.
35+
* `name` - Name of the Application

0 commit comments

Comments
 (0)