Skip to content

Commit

Permalink
Merge branch 'fix_resource_mapping_status_wait' of ssh://github.com/s…
Browse files Browse the repository at this point in the history
…huheiktgw/terraform-provider-aws into shuheiktgw-fix_resource_mapping_status_wait
  • Loading branch information
bflad committed Feb 11, 2021
2 parents ef17ab4 + 614c5a0 commit d2233f4
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions aws/resource_aws_lambda_event_source_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

const (
LambdaEventSourceMappingCreating = "Creating"
LambdaEventSourceMappingEnabling = "Enabling"
LambdaEventSourceMappingUpdating = "Updating"
LambdaEventSourceMappingDisabling = "Disabling"
LambdaEventSourceMappingEnabled = "Enabled"
LambdaEventSourceMappingDisabled = "Disabled"
)

func resourceAwsLambdaEventSourceMapping() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLambdaEventSourceMappingCreate,
Expand Down Expand Up @@ -256,6 +265,11 @@ func resourceAwsLambdaEventSourceMappingCreate(d *schema.ResourceData, meta inte
// No error
d.Set("uuid", eventSourceMappingConfiguration.UUID)
d.SetId(aws.StringValue(eventSourceMappingConfiguration.UUID))

if err := waitForLambdaEventSourceMapping(conn, *eventSourceMappingConfiguration.UUID); err != nil {
return err
}

return resourceAwsLambdaEventSourceMappingRead(d, meta)
}

Expand Down Expand Up @@ -302,12 +316,12 @@ func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interf
state := aws.StringValue(eventSourceMappingConfiguration.State)

switch state {
case "Enabled", "Enabling":
case LambdaEventSourceMappingEnabled:
d.Set("enabled", true)
case "Disabled", "Disabling":
case LambdaEventSourceMappingDisabled:
d.Set("enabled", false)
default:
log.Printf("[DEBUG] Lambda event source mapping is neither enabled nor disabled but %s", *eventSourceMappingConfiguration.State)
return fmt.Errorf("state is neither enabled nor disabled but %s", *eventSourceMappingConfiguration.State)
}

return nil
Expand Down Expand Up @@ -404,5 +418,35 @@ func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta inte
return fmt.Errorf("Error updating Lambda event source mapping: %s", err)
}

if err := waitForLambdaEventSourceMapping(conn, d.Id()); err != nil {
return err
}

return resourceAwsLambdaEventSourceMappingRead(d, meta)
}

func waitForLambdaEventSourceMapping(conn *lambda.Lambda, id string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{LambdaEventSourceMappingCreating, LambdaEventSourceMappingEnabling, LambdaEventSourceMappingUpdating, LambdaEventSourceMappingDisabling},
Target: []string{LambdaEventSourceMappingEnabled, LambdaEventSourceMappingDisabled},
Refresh: func() (interface{}, string, error) {
params := &lambda.GetEventSourceMappingInput{
UUID: aws.String(id),
}

res, err := conn.GetEventSourceMapping(params)
if err != nil {
return nil, "", err
}

return res, aws.StringValue(res.State), err
},
Timeout: 10 * time.Minute,
Delay: 5 * time.Second,
}

log.Printf("[DEBUG] Waiting for LambdaEventSourceMapping state update: %s", id)
_, err := stateConf.WaitForState()

return err
}

0 comments on commit d2233f4

Please sign in to comment.