forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hashicorp#38188 from drewtul/f-sc-appregistry-attr…
…ibute-group-ds New DataSource for Service Catalog AppRegistry Attribute Group
- Loading branch information
Showing
11 changed files
with
562 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_servicecatalogappregistry_attribute_group | ||
``` |
122 changes: 122 additions & 0 deletions
122
internal/service/servicecatalogappregistry/attribute_group_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package servicecatalogappregistry | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" | ||
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-aws/internal/create" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex" | ||
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" | ||
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
// @FrameworkDataSource("aws_servicecatalogappregistry_attribute_group", name="Attribute Group") | ||
// @Tags(identifierAttribute="arn") | ||
func newDataSourceAttributeGroup(context.Context) (datasource.DataSourceWithConfigure, error) { | ||
return &dataSourceAttributeGroup{}, nil | ||
} | ||
|
||
const ( | ||
DSNameAttributeGroup = "Attribute Group Data Source" | ||
) | ||
|
||
type dataSourceAttributeGroup struct { | ||
framework.DataSourceWithConfigure | ||
} | ||
|
||
func (d *dataSourceAttributeGroup) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name | ||
resp.TypeName = "aws_servicecatalogappregistry_attribute_group" | ||
} | ||
|
||
func (d *dataSourceAttributeGroup) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
names.AttrARN: schema.StringAttribute{ | ||
CustomType: fwtypes.ARNType, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
names.AttrAttributes: schema.StringAttribute{ | ||
CustomType: jsontypes.NormalizedType{}, | ||
Computed: true, | ||
}, | ||
names.AttrDescription: schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
names.AttrID: schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
names.AttrName: schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
names.AttrTags: tftags.TagsAttributeComputedOnly(), | ||
}, | ||
} | ||
} | ||
|
||
func (d *dataSourceAttributeGroup) ConfigValidators(_ context.Context) []datasource.ConfigValidator { | ||
return []datasource.ConfigValidator{ | ||
datasourcevalidator.ExactlyOneOf( | ||
path.MatchRoot(names.AttrARN), | ||
path.MatchRoot(names.AttrID), | ||
path.MatchRoot(names.AttrName), | ||
), | ||
} | ||
} | ||
|
||
func (d *dataSourceAttributeGroup) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
conn := d.Meta().ServiceCatalogAppRegistryClient(ctx) | ||
|
||
var data dataSourceAttributeGroupData | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
var id string | ||
|
||
if !data.ID.IsNull() { | ||
id = data.ID.ValueString() | ||
} else if !data.Name.IsNull() { | ||
id = data.Name.ValueString() | ||
} else if !data.ARN.IsNull() { | ||
id = data.ARN.ValueString() | ||
} | ||
|
||
out, err := findAttributeGroupByID(ctx, conn, id) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
create.ProblemStandardMessage(names.ServiceCatalogAppRegistry, create.ErrActionReading, DSNameAttributeGroup, data.Name.String(), err), | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(flex.Flatten(ctx, out, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
type dataSourceAttributeGroupData struct { | ||
ARN fwtypes.ARN `tfsdk:"arn"` | ||
Attributes jsontypes.Normalized `tfsdk:"attributes"` | ||
Description types.String `tfsdk:"description"` | ||
ID types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Tags tftags.Map `tfsdk:"tags"` | ||
} |
194 changes: 194 additions & 0 deletions
194
internal/service/servicecatalogappregistry/attribute_group_data_source_tags_gen_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
internal/service/servicecatalogappregistry/attribute_group_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package servicecatalogappregistry_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/YakDriver/regexache" | ||
"github.com/aws/aws-sdk-go-v2/service/servicecatalogappregistry" | ||
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
func TestAccServiceCatalogAppRegistryAttributeGroupDataSource_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
|
||
var attributegroup servicecatalogappregistry.GetAttributeGroupOutput | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
description := "Simple Description" | ||
expectJsonV1 := `{"a":"1","b":"2"}` | ||
dataSourceName := "data.aws_servicecatalogappregistry_attribute_group.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acctest.PreCheck(ctx, t) | ||
acctest.PreCheckPartitionHasService(t, names.ServiceCatalogAppRegistryEndpointID) | ||
testAccPreCheck(ctx, t) | ||
}, | ||
ErrorCheck: acctest.ErrorCheck(t, names.ServiceCatalogAppRegistryServiceID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckAttributeGroupDestroy(ctx), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAttributeGroupDataSourceConfig_basic(rName, description), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAttributeGroupExists(ctx, dataSourceName, &attributegroup), | ||
resource.TestCheckResourceAttr(dataSourceName, names.AttrName, rName), | ||
resource.TestCheckResourceAttr(dataSourceName, names.AttrDescription, description), | ||
resource.TestCheckResourceAttr(dataSourceName, names.AttrAttributes, expectJsonV1), | ||
acctest.MatchResourceAttrRegionalARN(ctx, dataSourceName, names.AttrARN, "servicecatalog", regexache.MustCompile(`/attribute-groups/+.`)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAttributeGroupDataSourceConfig_basic(rName, description string) string { | ||
return fmt.Sprintf(` | ||
data "aws_servicecatalogappregistry_attribute_group" "test" { | ||
name = aws_servicecatalogappregistry_attribute_group.test.name | ||
} | ||
resource "aws_servicecatalogappregistry_attribute_group" "test" { | ||
name = %[1]q | ||
description = %[2]q | ||
attributes = jsonencode({ | ||
a = "1" | ||
b = "2" | ||
}) | ||
} | ||
`, rName, description) | ||
} |
Oops, something went wrong.