Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New DataSource for Service Catalog AppRegistry Attribute Group #38188

Merged
merged 18 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading