Skip to content

Commit

Permalink
Merge pull request hashicorp#37866 from hashicorp/td-return-diags-s-etc
Browse files Browse the repository at this point in the history
Enable and fix semgrep `diags` checks for all services
  • Loading branch information
ewbankkit authored Jun 7, 2024
2 parents 4b423ee + 2968ded commit 240891a
Show file tree
Hide file tree
Showing 164 changed files with 2,193 additions and 1,732 deletions.
23 changes: 1 addition & 22 deletions .ci/semgrep/pluginsdk/diags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,41 @@ rules:
- id: avoid-return-diag_FromErr
languages: [go]
message: Prefer `sdkdiag.AppendFromErr` to `diag.FromErr`
paths:
exclude:
- internal/service/[o-s]*
pattern: return diag.FromErr($ERR)
fix: return sdkdiag.AppendFromErr(diags, $ERR)
severity: WARNING

- id: avoid-append-diag_FromErr
languages: [go]
message: Prefer `sdkdiag.AppendFromErr` to `diag.FromErr`
paths:
exclude:
- internal/service/[o-s]*
pattern: append(diags, diag.FromErr($ERR)...)
fix: sdkdiag.AppendFromErr(diags, $ERR)
severity: WARNING

- id: avoid-diag_Errorf
languages: [go]
message: Prefer `sdkdiag.AppendErrorf` to `diag.Errorf`
paths:
exclude:
- internal/service/[o-s]*
pattern: diag.Errorf($...ARGS)
fix: sdkdiag.AppendErrorf(diags, $...ARGS)
severity: WARNING

- id: avoid-return-create_DiagError
languages: [go]
message: Prefer `create.AppendDiagError` to `create.DiagError`
paths:
exclude:
- internal/service/[o-s]*
pattern: return create.DiagError($...ARGS)
fix: return create.AppendDiagError(diags, $...ARGS)
severity: WARNING

- id: avoid-append-create_DiagError
languages: [go]
message: Prefer `create.AppendDiagError` to `create.DiagError`
paths:
exclude:
- internal/service/[o-s]*
pattern: append(diags, create.DiagError($...ARGS)...)
fix: create.AppendDiagError(diags, $...ARGS)
severity: WARNING

- id: append-Read-to-diags
languages: [go]
message: Append results of $READFN to diags instead of returning directly
paths:
exclude:
- internal/service/[o-s]*
patterns:
- pattern: return $READFN($...ARGS)
- metavariable-regex:
Expand All @@ -70,9 +52,6 @@ rules:
- id: append-Update-to-diags
languages: [go]
message: Append results of $UPDATEFN to diags instead of returning directly
paths:
exclude:
- internal/service/[o-s]*
patterns:
- pattern: return $UPDATEFN($...ARGS)
- metavariable-regex:
Expand All @@ -93,7 +72,7 @@ rules:
include:
- internal/service
exclude:
- internal/service/[o-s]*
- internal/service/o*
patterns:
- pattern: return nil
- pattern-not-inside: |
Expand Down
18 changes: 11 additions & 7 deletions internal/service/oam/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const (
)

func resourceLinkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

in := &oam.CreateLinkInput{
Expand All @@ -105,19 +106,20 @@ func resourceLinkCreate(ctx context.Context, d *schema.ResourceData, meta interf

out, err := conn.CreateLink(ctx, in)
if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionCreating, ResNameLink, d.Get("sink_identifier").(string), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionCreating, ResNameLink, d.Get("sink_identifier").(string), err)
}

if out == nil || out.Id == nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionCreating, ResNameLink, d.Get("sink_identifier").(string), errors.New("empty output"))
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionCreating, ResNameLink, d.Get("sink_identifier").(string), errors.New("empty output"))
}

d.SetId(aws.ToString(out.Arn))

return resourceLinkRead(ctx, d, meta)
return append(diags, resourceLinkRead(ctx, d, meta)...)
}

func resourceLinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

out, err := findLinkByID(ctx, conn, d.Id())
Expand All @@ -129,7 +131,7 @@ func resourceLinkRead(ctx context.Context, d *schema.ResourceData, meta interfac
}

if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, ResNameLink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, ResNameLink, d.Id(), err)
}

d.Set(names.AttrARN, out.Arn)
Expand All @@ -144,6 +146,7 @@ func resourceLinkRead(ctx context.Context, d *schema.ResourceData, meta interfac
}

func resourceLinkUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

update := false
Expand All @@ -161,14 +164,15 @@ func resourceLinkUpdate(ctx context.Context, d *schema.ResourceData, meta interf
log.Printf("[DEBUG] Updating ObservabilityAccessManager Link (%s): %#v", d.Id(), in)
_, err := conn.UpdateLink(ctx, in)
if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionUpdating, ResNameLink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionUpdating, ResNameLink, d.Id(), err)
}
}

return resourceLinkRead(ctx, d, meta)
return append(diags, resourceLinkRead(ctx, d, meta)...)
}

func resourceLinkDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

log.Printf("[INFO] Deleting ObservabilityAccessManager Link %s", d.Id())
Expand All @@ -183,7 +187,7 @@ func resourceLinkDelete(ctx context.Context, d *schema.ResourceData, meta interf
return nil
}

return create.DiagError(names.ObservabilityAccessManager, create.ErrActionDeleting, ResNameLink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionDeleting, ResNameLink, d.Id(), err)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions internal/service/oam/link_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ const (
)

func dataSourceLinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

linkIdentifier := d.Get("link_identifier").(string)

out, err := findLinkByID(ctx, conn, linkIdentifier)
if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, DSNameLink, linkIdentifier, err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, DSNameLink, linkIdentifier, err)
}

d.SetId(aws.ToString(out.Arn))
Expand All @@ -83,13 +84,13 @@ func dataSourceLinkRead(ctx context.Context, d *schema.ResourceData, meta interf

tags, err := listTags(ctx, conn, d.Id())
if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, DSNameLink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, DSNameLink, d.Id(), err)
}

ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

if err := d.Set(names.AttrTags, tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionSetting, DSNameLink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionSetting, DSNameLink, d.Id(), err)
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions internal/service/oam/links_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ const (
)

func dataSourceLinksRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)
listLinksInput := &oam.ListLinksInput{}

listLinksInput := &oam.ListLinksInput{}
paginator := oam.NewListLinksPaginator(conn, listLinksInput)
var arns []string

for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)

if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, DSNameLinks, "", err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, DSNameLinks, "", err)
}

for _, listLinksItem := range page.Items {
Expand Down
13 changes: 8 additions & 5 deletions internal/service/oam/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const (
)

func resourceSinkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

in := &oam.CreateSinkInput{
Expand All @@ -78,19 +79,20 @@ func resourceSinkCreate(ctx context.Context, d *schema.ResourceData, meta interf

out, err := conn.CreateSink(ctx, in)
if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionCreating, ResNameSink, d.Get(names.AttrName).(string), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionCreating, ResNameSink, d.Get(names.AttrName).(string), err)
}

if out == nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionCreating, ResNameSink, d.Get(names.AttrName).(string), errors.New("empty output"))
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionCreating, ResNameSink, d.Get(names.AttrName).(string), errors.New("empty output"))
}

d.SetId(aws.ToString(out.Arn))

return resourceSinkRead(ctx, d, meta)
return append(diags, resourceSinkRead(ctx, d, meta)...)
}

func resourceSinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

out, err := findSinkByID(ctx, conn, d.Id())
Expand All @@ -102,7 +104,7 @@ func resourceSinkRead(ctx context.Context, d *schema.ResourceData, meta interfac
}

if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, ResNameSink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, ResNameSink, d.Id(), err)
}

d.Set(names.AttrARN, out.Arn)
Expand All @@ -118,6 +120,7 @@ func resourceSinkUpdate(ctx context.Context, d *schema.ResourceData, meta interf
}

func resourceSinkDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

log.Printf("[INFO] Deleting ObservabilityAccessManager Sink %s", d.Id())
Expand All @@ -132,7 +135,7 @@ func resourceSinkDelete(ctx context.Context, d *schema.ResourceData, meta interf
return nil
}

return create.DiagError(names.ObservabilityAccessManager, create.ErrActionDeleting, ResNameSink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionDeleting, ResNameSink, d.Id(), err)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions internal/service/oam/sink_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ const (
)

func dataSourceSinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

sinkIdentifier := d.Get("sink_identifier").(string)

out, err := findSinkByID(ctx, conn, sinkIdentifier)
if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, DSNameSink, sinkIdentifier, err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, DSNameSink, sinkIdentifier, err)
}

d.SetId(aws.ToString(out.Arn))
Expand All @@ -64,13 +65,13 @@ func dataSourceSinkRead(ctx context.Context, d *schema.ResourceData, meta interf

tags, err := listTags(ctx, conn, d.Id())
if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, DSNameSink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, DSNameSink, d.Id(), err)
}

ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

if err := d.Set(names.AttrTags, tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionSetting, DSNameSink, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionSetting, DSNameSink, d.Id(), err)
}

return nil
Expand Down
13 changes: 8 additions & 5 deletions internal/service/oam/sink_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
Expand Down Expand Up @@ -76,13 +77,14 @@ const (
)

func resourceSinkPolicyPut(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

sinkIdentifier := d.Get("sink_identifier").(string)
policy, err := structure.NormalizeJsonString(d.Get(names.AttrPolicy).(string))

if err != nil {
return diag.Errorf("policy (%s) is invalid JSON: %s", d.Get(names.AttrPolicy).(string), err)
return sdkdiag.AppendErrorf(diags, "policy (%s) is invalid JSON: %s", d.Get(names.AttrPolicy).(string), err)
}

in := &oam.PutSinkPolicyInput{
Expand All @@ -92,17 +94,18 @@ func resourceSinkPolicyPut(ctx context.Context, d *schema.ResourceData, meta int

_, err = conn.PutSinkPolicy(ctx, in)
if err != nil {
return diag.Errorf("putting ObservabilityAccessManager Sink Policy (%s): %s", sinkIdentifier, err)
return sdkdiag.AppendErrorf(diags, "putting ObservabilityAccessManager Sink Policy (%s): %s", sinkIdentifier, err)
}

if d.IsNewResource() {
d.SetId(sinkIdentifier)
}

return resourceSinkPolicyRead(ctx, d, meta)
return append(diags, resourceSinkPolicyRead(ctx, d, meta)...)
}

func resourceSinkPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

out, err := findSinkPolicyByID(ctx, conn, d.Id())
Expand All @@ -114,7 +117,7 @@ func resourceSinkPolicyRead(ctx context.Context, d *schema.ResourceData, meta in
}

if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, ResNameSinkPolicy, d.Id(), err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, ResNameSinkPolicy, d.Id(), err)
}

d.Set(names.AttrARN, out.SinkArn)
Expand All @@ -123,7 +126,7 @@ func resourceSinkPolicyRead(ctx context.Context, d *schema.ResourceData, meta in

policyToSet, err := verify.PolicyToSet(d.Get(names.AttrPolicy).(string), aws.ToString(out.Policy))
if err != nil {
return diag.FromErr(err)
return sdkdiag.AppendFromErr(diags, err)
}

d.Set(names.AttrPolicy, policyToSet)
Expand Down
3 changes: 2 additions & 1 deletion internal/service/oam/sinks_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
)

func dataSourceSinksRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)
listSinksInput := &oam.ListSinksInput{}

Expand All @@ -45,7 +46,7 @@ func dataSourceSinksRead(ctx context.Context, d *schema.ResourceData, meta inter
page, err := paginator.NextPage(ctx)

if err != nil {
return create.DiagError(names.ObservabilityAccessManager, create.ErrActionReading, DSNameSinks, "", err)
return create.AppendDiagError(diags, names.ObservabilityAccessManager, create.ErrActionReading, DSNameSinks, "", err)
}

for _, listSinksItem := range page.Items {
Expand Down
Loading

0 comments on commit 240891a

Please sign in to comment.