Skip to content

Commit

Permalink
Merge pull request hashicorp#38188 from drewtul/f-sc-appregistry-attr…
Browse files Browse the repository at this point in the history
…ibute-group-ds

New DataSource for Service Catalog AppRegistry Attribute Group
  • Loading branch information
nam054 authored Dec 6, 2024
2 parents 7548bff + c915cee commit 145fc0c
Show file tree
Hide file tree
Showing 11 changed files with 562 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/38188.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_servicecatalogappregistry_attribute_group
```
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"`
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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)
}
Loading

0 comments on commit 145fc0c

Please sign in to comment.