Skip to content

Commit

Permalink
Merge pull request #39456 from hashicorp/td-errs-must
Browse files Browse the repository at this point in the history
[TechDebt]: Remove use of `errs.Must` within service packages
  • Loading branch information
jar-b authored Sep 27, 2024
2 parents 651ccce + 52df9b9 commit 9d0ae12
Show file tree
Hide file tree
Showing 45 changed files with 476 additions and 141 deletions.
11 changes: 11 additions & 0 deletions .ci/.semgrep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,14 @@ rules:
$DIAGS = sdkdiag.AppendErrorf($DIAGS, ...)
}
severity: ERROR

- id: avoid-errs-Must
languages: [go]
message: Avoid use of `errs.Must()` in service packages, handle errors explicitly instead.
paths:
include:
- internal/service
patterns:
- pattern-either:
- pattern: errs.Must(...)
severity: WARNING
7 changes: 5 additions & 2 deletions internal/service/account/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -74,11 +73,15 @@ func resourceRegionUpdate(ctx context.Context, d *schema.ResourceData, meta inte
conn := meta.(*conns.AWSClient).AccountClient(ctx)

var id string
var err error
region := d.Get("region_name").(string)
accountID := ""
if v, ok := d.GetOk(names.AttrAccountID); ok {
accountID = v.(string)
id = errs.Must(flex.FlattenResourceId([]string{accountID, region}, regionResourceIDPartCount, false))
id, err = flex.FlattenResourceId([]string{accountID, region}, regionResourceIDPartCount, false)
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
} else {
id = region
}
Expand Down
10 changes: 6 additions & 4 deletions internal/service/acmpca/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func resourcePermissionCreate(ctx context.Context, d *schema.ResourceData, meta
caARN := d.Get("certificate_authority_arn").(string)
principal := d.Get(names.AttrPrincipal).(string)
sourceAccount := d.Get("source_account").(string)
id := errs.Must(flex.FlattenResourceId([]string{caARN, principal, sourceAccount}, permissionResourceIDPartCount, true))
id, err := flex.FlattenResourceId([]string{caARN, principal, sourceAccount}, permissionResourceIDPartCount, true)
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}

input := &acmpca.CreatePermissionInput{
Actions: expandPermissionActions(d.Get(names.AttrActions).(*schema.Set)),
CertificateAuthorityArn: aws.String(caARN),
Expand All @@ -92,9 +96,7 @@ func resourcePermissionCreate(ctx context.Context, d *schema.ResourceData, meta
input.SourceAccount = aws.String(sourceAccount)
}

_, err := conn.CreatePermission(ctx, input)

if err != nil {
if _, err := conn.CreatePermission(ctx, input); err != nil {
return sdkdiag.AppendErrorf(diags, "creating ACM PCA Permission (%s): %s", id, err)
}

Expand Down
24 changes: 20 additions & 4 deletions internal/service/appfabric/app_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,13 @@ func (r *appAuthorizationResource) Create(ctx context.Context, request resource.
return
}

uuid, err := uuid.GenerateUUID()
if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("creating AppFabric App (%s) Authorization", data.App.ValueString()), err.Error())
}

input.AppBundleIdentifier = data.AppBundleARN.ValueStringPointer()
input.ClientToken = aws.String(errs.Must(uuid.GenerateUUID()))
input.ClientToken = aws.String(uuid)
input.Tags = getTagsIn(ctx)

output, err := conn.CreateAppAuthorization(ctx, input)
Expand All @@ -229,9 +234,15 @@ func (r *appAuthorizationResource) Create(ctx context.Context, request resource.
return
}

id, err := data.setID()
if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("flattening resource ID AppFabric App (%s) Authorization", data.App.ValueString()), err.Error())
return
}
data.ID = types.StringValue(id)

// Set values for unknowns.
data.AppAuthorizationARN = fwflex.StringToFramework(ctx, output.AppAuthorization.AppAuthorizationArn)
data.setID()

appAuthorization, err := waitAppAuthorizationCreated(ctx, conn, data.AppAuthorizationARN.ValueString(), data.AppBundleARN.ValueString(), r.CreateTimeout(ctx, data.Timeouts))

Expand Down Expand Up @@ -525,8 +536,13 @@ func (m *appAuthorizationResourceModel) InitFromID() error {
return nil
}

func (m *appAuthorizationResourceModel) setID() {
m.ID = types.StringValue(errs.Must(flex.FlattenResourceId([]string{m.AppAuthorizationARN.ValueString(), m.AppBundleARN.ValueString()}, appAuthorizationResourceIDPartCount, false)))
func (m *appAuthorizationResourceModel) setID() (string, error) {
parts := []string{
m.AppAuthorizationARN.ValueString(),
m.AppBundleARN.ValueString(),
}

return flex.FlattenResourceId(parts, appAuthorizationResourceIDPartCount, false)
}

var (
Expand Down
21 changes: 15 additions & 6 deletions internal/service/appfabric/app_authorization_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ func (r *appAuthorizationConnectionResource) Create(ctx context.Context, request
return
}

// Set values for unknowns.
data.setID()
id, err := data.setID()
if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("flattening resource ID AppFabric App Authorization (%s) Connection", data.AppAuthorizationARN.ValueString()), err.Error())
return
}
data.ID = types.StringValue(id)

appAuthorization, err := waitConnectAppAuthorizationCreated(ctx, conn, data.AppAuthorizationARN.ValueString(), data.AppBundleARN.ValueString(), r.CreateTimeout(ctx, data.Timeouts))

Expand Down Expand Up @@ -167,7 +171,7 @@ func (r *appAuthorizationConnectionResource) Read(ctx context.Context, request r
return
}

if err := data.InitFromID(); err != nil {
if err := data.initFromID(); err != nil {
response.Diagnostics.AddError("parsing resource ID", err.Error())

return
Expand Down Expand Up @@ -272,7 +276,7 @@ const (
appAuthorizationConnectionResourceIDPartCount = 2
)

func (m *appAuthorizationConnectionResourceModel) InitFromID() error {
func (m *appAuthorizationConnectionResourceModel) initFromID() error {
parts, err := flex.ExpandResourceId(m.ID.ValueString(), appAuthorizationConnectionResourceIDPartCount, false)
if err != nil {
return err
Expand All @@ -284,8 +288,13 @@ func (m *appAuthorizationConnectionResourceModel) InitFromID() error {
return nil
}

func (m *appAuthorizationConnectionResourceModel) setID() {
m.ID = types.StringValue(errs.Must(flex.FlattenResourceId([]string{m.AppAuthorizationARN.ValueString(), m.AppBundleARN.ValueString()}, appAuthorizationConnectionResourceIDPartCount, false)))
func (m *appAuthorizationConnectionResourceModel) setID() (string, error) {
parts := []string{
m.AppAuthorizationARN.ValueString(),
m.AppBundleARN.ValueString(),
}

return flex.FlattenResourceId(parts, appAuthorizationConnectionResourceIDPartCount, false)
}

type authRequestModel struct {
Expand Down
7 changes: 6 additions & 1 deletion internal/service/appfabric/app_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ func (r *appBundleResource) Create(ctx context.Context, request resource.CreateR

conn := r.Meta().AppFabricClient(ctx)

uuid, err := uuid.GenerateUUID()
if err != nil {
response.Diagnostics.AddError("creating AppFabric App Bundle", err.Error())
}

input := &appfabric.CreateAppBundleInput{
ClientToken: aws.String(errs.Must(uuid.GenerateUUID())),
ClientToken: aws.String(uuid),
CustomerManagedKeyIdentifier: fwflex.StringFromFramework(ctx, data.CustomerManagedKeyARN),
Tags: getTagsIn(ctx),
}
Expand Down
23 changes: 19 additions & 4 deletions internal/service/appfabric/ingestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,14 @@ func (r *ingestionResource) Create(ctx context.Context, request resource.CreateR
return
}

uuid, err := uuid.GenerateUUID()
if err != nil {
response.Diagnostics.AddError("creating AppFabric Ingestion", err.Error())
}

// Additional fields.
input.AppBundleIdentifier = fwflex.StringFromFramework(ctx, data.AppBundleARN)
input.ClientToken = aws.String(errs.Must(uuid.GenerateUUID()))
input.ClientToken = aws.String(uuid)
input.Tags = getTagsIn(ctx)

output, err := conn.CreateIngestion(ctx, input)
Expand All @@ -130,7 +135,12 @@ func (r *ingestionResource) Create(ctx context.Context, request resource.CreateR

// Set values for unknowns.
data.ARN = fwflex.StringToFramework(ctx, output.Ingestion.Arn)
data.setID()
id, err := data.setID()
if err != nil {
response.Diagnostics.AddError("flattening resource ID AppFabric Ingestion", err.Error())
return
}
data.ID = types.StringValue(id)

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}
Expand Down Expand Up @@ -255,6 +265,11 @@ func (m *ingestionResourceModel) InitFromID() error {
return nil
}

func (m *ingestionResourceModel) setID() {
m.ID = types.StringValue(errs.Must(flex.FlattenResourceId([]string{m.AppBundleARN.ValueString(), m.ARN.ValueString()}, ingestionResourceIDPartCount, false)))
func (m *ingestionResourceModel) setID() (string, error) {
parts := []string{
m.AppBundleARN.ValueString(),
m.ARN.ValueString(),
}

return flex.FlattenResourceId(parts, ingestionResourceIDPartCount, false)
}
24 changes: 20 additions & 4 deletions internal/service/appfabric/ingestion_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,14 @@ func (r *ingestionDestinationResource) Create(ctx context.Context, request resou
input.ProcessingConfiguration = processingConfiguration
}

uuid, err := uuid.GenerateUUID()
if err != nil {
response.Diagnostics.AddError("creating AppFabric Ingestion Destination", err.Error())
}

// Additional fields.
input.AppBundleIdentifier = data.AppBundleARN.ValueStringPointer()
input.ClientToken = aws.String(errs.Must(uuid.GenerateUUID()))
input.ClientToken = aws.String(uuid)
input.IngestionIdentifier = data.IngestionARN.ValueStringPointer()
input.Tags = getTagsIn(ctx)

Expand All @@ -274,7 +279,12 @@ func (r *ingestionDestinationResource) Create(ctx context.Context, request resou

// Set values for unknowns.
data.ARN = fwflex.StringToFramework(ctx, output.IngestionDestination.Arn)
data.setID()
id, err := data.setID()
if err != nil {
response.Diagnostics.AddError("flattening resource ID AppFabric Ingestion Destination", err.Error())
return
}
data.ID = types.StringValue(id)

if _, err := waitIngestionDestinationActive(ctx, conn, data.AppBundleARN.ValueString(), data.IngestionARN.ValueString(), data.ARN.ValueString(), r.CreateTimeout(ctx, data.Timeouts)); err != nil {
response.State.SetAttribute(ctx, path.Root(names.AttrID), data.ID) // Set 'id' so as to taint the resource.
Expand Down Expand Up @@ -561,8 +571,14 @@ func (m *ingestionDestinationResourceModel) InitFromID() error {
return nil
}

func (m *ingestionDestinationResourceModel) setID() {
m.ID = types.StringValue(errs.Must(flex.FlattenResourceId([]string{m.AppBundleARN.ValueString(), m.IngestionARN.ValueString(), m.ARN.ValueString()}, ingestionDestinationResourceIDPartCount, false)))
func (m *ingestionDestinationResourceModel) setID() (string, error) {
parts := []string{
m.AppBundleARN.ValueString(),
m.IngestionARN.ValueString(),
m.ARN.ValueString(),
}

return flex.FlattenResourceId(parts, ingestionDestinationResourceIDPartCount, false)
}

type destinationConfigurationModel struct {
Expand Down
19 changes: 16 additions & 3 deletions internal/service/appsync/source_api_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,15 @@ func (r *sourceAPIAssociationResource) Create(ctx context.Context, request resou

plan.AssociationId = flex.StringToFramework(ctx, out.SourceApiAssociation.AssociationId)
plan.MergedAPIId = flex.StringToFramework(ctx, out.SourceApiAssociation.MergedApiId)
plan.setID()
id, err := plan.setID()
if err != nil {
response.Diagnostics.AddError(
create.ProblemStandardMessage(names.AppSync, create.ErrActionFlatteningResourceId, resNameSourceAPIAssociation, plan.MergedAPIId.String(), err),
err.Error(),
)
return
}
plan.ID = types.StringValue(id)

createTimeout := r.CreateTimeout(ctx, plan.Timeouts)
_, err = waitSourceAPIAssociationCreated(ctx, conn, plan.AssociationId.ValueString(), aws.ToString(out.SourceApiAssociation.MergedApiArn), createTimeout)
Expand Down Expand Up @@ -485,6 +493,11 @@ func (m *sourceAPIAssociationResourceModel) InitFromID() error {
return nil
}

func (m *sourceAPIAssociationResourceModel) setID() {
m.ID = types.StringValue(errs.Must(autoflex.FlattenResourceId([]string{m.MergedAPIId.ValueString(), m.AssociationId.ValueString()}, sourceAPIAssociationIDPartCount, false)))
func (m *sourceAPIAssociationResourceModel) setID() (string, error) {
parts := []string{
m.MergedAPIId.ValueString(),
m.AssociationId.ValueString(),
}

return autoflex.FlattenResourceId(parts, sourceAPIAssociationIDPartCount, false)
}
17 changes: 14 additions & 3 deletions internal/service/bedrockagent/agent_action_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ func (r *agentActionGroupResource) Create(ctx context.Context, request resource.
// Set values for unknowns.
data.ActionGroupID = fwflex.StringToFramework(ctx, output.AgentActionGroup.ActionGroupId)
data.ActionGroupState = fwtypes.StringEnumValue(output.AgentActionGroup.ActionGroupState)
data.setID()
id, err := data.setID()
if err != nil {
response.Diagnostics.AddError("flattening resource ID Bedrock Agent Action Group", err.Error())
return
}
data.ID = types.StringValue(id)

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}
Expand Down Expand Up @@ -430,8 +435,14 @@ func (m *agentActionGroupResourceModel) InitFromID() error {
return nil
}

func (m *agentActionGroupResourceModel) setID() {
m.ID = types.StringValue(errs.Must(flex.FlattenResourceId([]string{m.ActionGroupID.ValueString(), m.AgentID.ValueString(), m.AgentVersion.ValueString()}, agentActionGroupResourceIDPartCount, false)))
func (m *agentActionGroupResourceModel) setID() (string, error) {
parts := []string{
m.ActionGroupID.ValueString(),
m.AgentID.ValueString(),
m.AgentVersion.ValueString(),
}

return flex.FlattenResourceId(parts, agentActionGroupResourceIDPartCount, false)
}

type actionGroupExecutorModel struct {
Expand Down
16 changes: 13 additions & 3 deletions internal/service/bedrockagent/agent_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ func (r *agentAliasResource) Create(ctx context.Context, request resource.Create

// Set values for unknowns.
data.AgentAliasID = fwflex.StringToFramework(ctx, output.AgentAlias.AgentAliasId)
data.setID()
id, err := data.setID()
if err != nil {
response.Diagnostics.AddError("creating Bedrock Agent Alias", err.Error())
return
}
data.ID = types.StringValue(id)

alias, err := waitAgentAliasCreated(ctx, conn, data.AgentAliasID.ValueString(), data.AgentID.ValueString(), r.CreateTimeout(ctx, data.Timeouts))

Expand Down Expand Up @@ -376,8 +381,13 @@ func (m *agentAliasResourceModel) InitFromID() error {
return nil
}

func (m *agentAliasResourceModel) setID() {
m.ID = types.StringValue(errs.Must(flex.FlattenResourceId([]string{m.AgentAliasID.ValueString(), m.AgentID.ValueString()}, agentAliasResourceIDPartCount, false)))
func (m *agentAliasResourceModel) setID() (string, error) {
parts := []string{
m.AgentAliasID.ValueString(),
m.AgentID.ValueString(),
}

return flex.FlattenResourceId(parts, agentAliasResourceIDPartCount, false)
}

type agentAliasRoutingConfigurationListItemModel struct {
Expand Down
17 changes: 14 additions & 3 deletions internal/service/bedrockagent/agent_knowledge_base_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ func (r *agentKnowledgeBaseAssociationResource) Create(ctx context.Context, requ
}

// Set values for unknowns.
data.setID()
id, err := data.setID()
if err != nil {
response.Diagnostics.AddError("flattening resource ID Bedrock Agent Knowledge Base Association", err.Error())
return
}
data.ID = types.StringValue(id)

_, err = prepareAgent(ctx, conn, data.AgentID.ValueString(), r.CreateTimeout(ctx, data.Timeouts))
if err != nil {
Expand Down Expand Up @@ -288,6 +293,12 @@ func (m *agentKnowledgeBaseAssociationResourceModel) InitFromID() error {
return nil
}

func (m *agentKnowledgeBaseAssociationResourceModel) setID() {
m.ID = types.StringValue(errs.Must(flex.FlattenResourceId([]string{m.AgentID.ValueString(), m.AgentVersion.ValueString(), m.KnowledgeBaseID.ValueString()}, agentKnowledgeBaseAssociationResourceIDPartCount, false)))
func (m *agentKnowledgeBaseAssociationResourceModel) setID() (string, error) {
parts := []string{
m.AgentID.ValueString(),
m.AgentVersion.ValueString(),
m.KnowledgeBaseID.ValueString(),
}

return flex.FlattenResourceId(parts, agentKnowledgeBaseAssociationResourceIDPartCount, false)
}
Loading

0 comments on commit 9d0ae12

Please sign in to comment.