Skip to content

Commit

Permalink
fix: pass through context to resource operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
parthpnx committed Nov 21, 2024
1 parent 71e94d0 commit 9347090
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
12 changes: 7 additions & 5 deletions internal/provider/log_group/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Create the log group.
func Create(d *schema.ResourceData, m interface{}) error {
func Create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
cfg := m.(aws.Config)
c := cloudwatchlogs.NewFromConfig(cfg)

var (
name = d.Get(Name).(string)
ctx = context.TODO()
exception *types.ResourceAlreadyExistsException
// Warning or errors can be collected in a slice type
diags diag.Diagnostics
)

_, err := c.CreateLogGroup(context.TODO(), &cloudwatchlogs.CreateLogGroupInput{
LogGroupName: aws.String(name),
})

if !errors.As(err, &exception) && err != nil {
return err
return diags
}

lg, err := findLogGroupByName(ctx, c, name)
if err != nil {
return err
return diags
}

d.Set(Name, name)
d.SetId(TrimLogGroupARNWildcardSuffix(aws.ToString(lg.Arn)))

return nil
return diags
}
11 changes: 7 additions & 4 deletions internal/provider/log_group/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,31 @@ import (
// "github.com/aws/aws-sdk-go-v2/aws/session"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Read the log group.
func Read(d *schema.ResourceData, m interface{}) error {
func Read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

cfg := m.(aws.Config)
c := cloudwatchlogs.NewFromConfig(cfg)

logGroupName := d.Get("name").(string)

lg, err := findLogGroupByName(context.TODO(), c, logGroupName)
lg, err := findLogGroupByName(ctx, c, logGroupName)

if !d.IsNewResource() && err != nil {
log.Printf("[WARN] CloudWatch Logs Log Group (%s) not found, removing from state", d.Id())
d.SetId("")
return err
return diag.FromErr(err)
}

d.SetId(TrimLogGroupARNWildcardSuffix(aws.ToString(lg.Arn)))
d.Set(Name, logGroupName)

return nil
return diags
}

func findLogGroupByName(ctx context.Context, conn *cloudwatchlogs.Client, name string) (*types.LogGroup, error) {
Expand Down
5 changes: 2 additions & 3 deletions internal/provider/log_group/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ const (
// Resource returns this packages resource.
func Resource() *schema.Resource {
return &schema.Resource{
Create: Create,
Read: Read,
Update: Update,
CreateContext: Create,
ReadContext: Read,

Schema: map[string]*schema.Schema{
Name: {
Expand Down
11 changes: 0 additions & 11 deletions internal/provider/log_group/update.go

This file was deleted.

0 comments on commit 9347090

Please sign in to comment.