Skip to content

Commit

Permalink
Merge pull request #9757 from terraform-providers/rfd-retry-guardduty
Browse files Browse the repository at this point in the history
Final guard duty retry
  • Loading branch information
ryndaniels authored Aug 14, 2019
2 parents 8512a70 + 72075c7 commit b8fcd50
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions aws/resource_aws_guardduty_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,31 +235,61 @@ func inviteGuardDutyMemberWaiter(accountID, detectorID string, timeout time.Dura
}

// wait until e-mail verification finishes
return resource.Retry(timeout, func() *resource.RetryError {
var out *guardduty.GetMembersOutput
err := resource.Retry(timeout, func() *resource.RetryError {
log.Printf("[DEBUG] Reading GuardDuty Member: %s", input)
gmo, err := conn.GetMembers(&input)
var err error
out, err = conn.GetMembers(&input)

if err != nil {
return resource.NonRetryableError(fmt.Errorf("error reading GuardDuty Member %q: %s", accountID, err))
}

if gmo == nil || len(gmo.Members) == 0 {
return resource.RetryableError(fmt.Errorf("error reading GuardDuty Member %q: member missing from response", accountID))
retryable, err := guardDutyMemberInvited(out, accountID)
if err != nil {
if retryable {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}

member := gmo.Members[0]
status := aws.StringValue(member.RelationshipStatus)
return nil
})
if isResourceTimeoutError(err) {
out, err = conn.GetMembers(&input)

if status == "Disabled" || status == "Enabled" || status == "Invited" {
return nil
if err != nil {
return fmt.Errorf("Error reading GuardDuty member: %s", err)
}

if status == "Created" || status == "EmailVerificationInProgress" {
return resource.RetryableError(fmt.Errorf("Expected member to be invited but was in state: %s", status))
_, err = guardDutyMemberInvited(out, accountID)
if err != nil {
return err // Doesn't need fmt because that happens in the function
}
return nil
}
if err != nil {
return fmt.Errorf("Error waiting for GuardDuty email verification: %s", err)
}
return nil
}

return resource.NonRetryableError(fmt.Errorf("error inviting GuardDuty Member %q: invalid status: %s", accountID, status))
})
func guardDutyMemberInvited(out *guardduty.GetMembersOutput, accountID string) (bool, error) {
if out == nil || len(out.Members) == 0 {
return true, fmt.Errorf("error reading GuardDuty Member %q: member missing from response", accountID)
}

member := out.Members[0]
status := aws.StringValue(member.RelationshipStatus)

if status == "Disabled" || status == "Enabled" || status == "Invited" {
return false, nil
}

if status == "Created" || status == "EmailVerificationInProgress" {
return true, fmt.Errorf("Expected member to be invited but was in state: %s", status)
}

return false, fmt.Errorf("error inviting GuardDuty Member %q: invalid status: %s", accountID, status)
}

func decodeGuardDutyMemberID(id string) (accountID, detectorID string, err error) {
Expand Down

0 comments on commit b8fcd50

Please sign in to comment.