Skip to content

Commit

Permalink
d/aws_securityhub_standards_control_associations: Tidy up.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Sep 27, 2024
1 parent 553717e commit f2a5704
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 68 deletions.
2 changes: 1 addition & 1 deletion internal/service/securityhub/service_package_gen.go

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

10 changes: 5 additions & 5 deletions internal/service/securityhub/standards_control_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
autoflex "github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -146,7 +146,7 @@ func (r *standardsControlAssociationResource) Read(ctx context.Context, request
return
}

response.Diagnostics.Append(flex.Flatten(ctx, output, &data)...)
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
if response.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -226,7 +226,7 @@ const (
)

func (m *standardsControlAssociationResourceModel) InitFromID(ctx context.Context) error {
parts, err := autoflex.ExpandResourceId(m.ID.ValueString(), standardsControlAssociationResourceIDPartCount, false)
parts, err := flex.ExpandResourceId(m.ID.ValueString(), standardsControlAssociationResourceIDPartCount, false)
if err != nil {
return err
}
Expand All @@ -243,7 +243,7 @@ func (m *standardsControlAssociationResourceModel) setID() {
}

func standardsControlAssociationCreateResourceID(securityControlID, standardsARN string) (string, error) {
return autoflex.FlattenResourceId([]string{securityControlID, standardsARN}, standardsControlAssociationResourceIDPartCount, false)
return flex.FlattenResourceId([]string{securityControlID, standardsARN}, standardsControlAssociationResourceIDPartCount, false)
}

func findStandardsControlAssociationByTwoPartKey(ctx context.Context, conn *securityhub.Client, securityControlID string, standardsARN string) (*awstypes.StandardsControlAssociationSummary, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,58 @@ package securityhub

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/securityhub"
awstypes "github.com/aws/aws-sdk-go-v2/service/securityhub/types"
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"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"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource(name=Standards Control Associations)
func newDataSourceStandardsControlAssociations(context.Context) (datasource.DataSourceWithConfigure, error) {
d := &dataSourceStandardsControlAssociations{}
// @FrameworkDataSource(name="Standards Control Associations")
func newStandardsControlAssociationsDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
d := &standardsControlAssociationsDataSource{}

return d, nil
}

const (
DSNameStandardsControlAssociations = "Standards Control Associations Data Source"
)

type dataSourceStandardsControlAssociations struct {
type standardsControlAssociationsDataSource struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceStandardsControlAssociations) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
func (*standardsControlAssociationsDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
response.TypeName = "aws_securityhub_standards_control_associations"
}

func (d *dataSourceStandardsControlAssociations) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
func (d *standardsControlAssociationsDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrID: framework.IDAttribute(),
"security_control_id": schema.StringAttribute{
Required: true,
},
},
Blocks: map[string]schema.Block{
"standards_control_associations": schema.ListNestedBlock{
"standards_control_associations": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[standardsControlAssociationData](ctx),
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"association_status": schema.StringAttribute{
CustomType: fwtypes.StringEnumType[awstypes.AssociationStatus](),
Computed: true,
},
"related_requirements": schema.ListAttribute{
CustomType: fwtypes.ListOfStringType,
ElementType: types.StringType,
Computed: true,
},
"security_control_arn": schema.StringAttribute{
Computed: true,
},
"security_control_id": schema.StringAttribute{
Computed: true,
},
"standards_arn": schema.StringAttribute{
Computed: true,
},
"standards_control_description": schema.StringAttribute{
Computed: true,
},
"standards_control_title": schema.StringAttribute{
Computed: true,
},
"updated_at": schema.StringAttribute{
CustomType: timetypes.RFC3339Type{},
Computed: true,
},
"updated_reason": schema.StringAttribute{
Computed: true,
},
},
Computed: true,
ElementType: types.ObjectType{
AttrTypes: fwtypes.AttributeTypesMust[standardsControlAssociationData](ctx),
},
},
},
}
}

func (d *dataSourceStandardsControlAssociations) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data dataSourceStandardsControlAssociationsData
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
func (d *standardsControlAssociationsDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
var data standardsControlAssociationsDataSourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

Expand All @@ -106,19 +69,17 @@ func (d *dataSourceStandardsControlAssociations) Read(ctx context.Context, req d
out, err := findStandardsControlAssociations(ctx, conn, input, tfslices.PredicateTrue[*awstypes.StandardsControlAssociationSummary]())

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.DevOpsGuru, create.ErrActionReading, DSNameStandardsControlAssociations, data.SecurityControlID.String(), err),
err.Error(),
)
response.Diagnostics.AddError(fmt.Sprintf("reading SecurityHub Standards Control Associations (%s)", data.SecurityControlID.ValueString()), err.Error())

return
}

data.ID = types.StringValue(d.Meta().Region)
resp.Diagnostics.Append(flex.Flatten(ctx, out, &data.StandardsControlAssociations)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
response.Diagnostics.Append(fwflex.Flatten(ctx, out, &data.StandardsControlAssociations)...)
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

type dataSourceStandardsControlAssociationsData struct {
type standardsControlAssociationsDataSourceModel struct {
ID types.String `tfsdk:"id"`
SecurityControlID types.String `tfsdk:"security_control_id"`
StandardsControlAssociations fwtypes.ListNestedObjectValueOf[standardsControlAssociationData] `tfsdk:"standards_control_associations"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func testAccStandardsControlAssociationsDataSource_basic(t *testing.T) {
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.SecurityHubServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccStandardsControlAssociationsDataSourceConfig_basic,
Expand Down

0 comments on commit f2a5704

Please sign in to comment.