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

[TechDebt]: Remove use of errs.Must within service packages #39456

Merged
merged 45 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1c3a8cd
ci(semgrep): add avoid-errs-Must check
jar-b Sep 19, 2024
a39a441
account: remove use of `errs.Must`
jar-b Sep 23, 2024
46f5b0f
acmpca: remove use of `errs.Must`
jar-b Sep 23, 2024
de6e050
appfabric: remove use of `errs.Must`
jar-b Sep 19, 2024
489d731
appsync: remove use of `errs.Must`
jar-b Sep 23, 2024
44fd0bf
bedrockagent: remove use of `errs.Must`
jar-b Sep 23, 2024
bd3c194
cloudformation: remove use of `errs.Must`
jar-b Sep 23, 2024
8da6dc5
cloudfrontkeyvaluestore: remove use of `errs.Must`
jar-b Sep 23, 2024
9794f22
cognitoidp: remove use of `errs.Must`
jar-b Sep 23, 2024
6418761
computeoptimizer: remove use of `errs.Must`
jar-b Sep 23, 2024
7c12a39
connect: remove use of `errs.Must`
jar-b Sep 23, 2024
fc94c65
controltower: remove use of `errs.Must`
jar-b Sep 23, 2024
cbc3e98
dynamodb: remove use of `errs.Must`
jar-b Sep 23, 2024
ab1022c
ec2: remove use of `errs.Must`
jar-b Sep 23, 2024
14770dd
elasticache: remove use of `errs.Must`
jar-b Sep 23, 2024
6cbcd24
elbv2: remove use of `errs.Must`
jar-b Sep 23, 2024
7a2896d
fms: remove use of `errs.Must`
jar-b Sep 23, 2024
9b86753
grafana: remove use of `errs.Must`
jar-b Sep 23, 2024
d4a7d97
lambda: remove use of `errs.Must`
jar-b Sep 23, 2024
43d883e
lexv2models: remove use of `errs.Must`
jar-b Sep 23, 2024
1f6bba8
m2: remove use of `errs.Must`
jar-b Sep 23, 2024
a756de2
networkmonitor: remove use of `errs.Must`
jar-b Sep 23, 2024
b8c9660
ram: remove use of `errs.Must`
jar-b Sep 23, 2024
9aa74dc
redshiftserverless: remove use of `errs.Must`
jar-b Sep 23, 2024
a1b5fa7
route53: remove use of `errs.Must`
jar-b Sep 23, 2024
3ef58c7
route53domains: remove use of `errs.Must`
jar-b Sep 23, 2024
20c8985
s3control: remove use of `errs.Must`
jar-b Sep 23, 2024
8568616
securityhub: remove use of `errs.Must`
jar-b Sep 23, 2024
9e08d8d
securitylake: remove use of `errs.Must`
jar-b Sep 23, 2024
f02599e
ssm: remove use of `errs.Must`
jar-b Sep 23, 2024
f76139c
wafv2: remove use of `errs.Must`
jar-b Sep 23, 2024
27f6e6b
account: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
478750e
acmpca: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
f92a3c2
cognitoidp: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
a9b4cbd
connect: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
8500471
controltower: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
8872350
dynamodb: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
c1e82a6
elasticache: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
fcb8dac
lambda: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
597e5b6
ram: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
578ff16
route53: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
e5996f3
securityhub: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
4dcade9
ssm: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
2ead23c
wafv2: omit `id` from error if `FlattenResourceId` fails
jar-b Sep 25, 2024
52df9b9
Update internal/service/bedrockagent/agent_alias.go
jar-b Sep 27, 2024
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
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
6 changes: 4 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 @@ -78,7 +77,10 @@ func resourceRegionUpdate(ctx context.Context, d *schema.ResourceData, meta inte
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.AppendErrorf(diags, "enabling Account Region (%s): %s", id, err)
jar-b marked this conversation as resolved.
Show resolved Hide resolved
}
} 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.AppendErrorf(diags, "creating ACM PCA Permission (%s): %s", id, 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("flattning resource ID Bedrock Agent Alias", err.Error())
jar-b marked this conversation as resolved.
Show resolved Hide resolved
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
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
Loading