Skip to content

Commit

Permalink
Fix updating Security Group error
Browse files Browse the repository at this point in the history
Fix reading security group right after creation (hashicorp#21628)
  • Loading branch information
fomichevmi committed Jan 7, 2022
1 parent 7b58ccc commit 9cbd6c5
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions internal/service/ec2/security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,26 @@ func resourceSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

sg, err := FindSecurityGroupByID(conn, d.Id())
var sg *ec2.SecurityGroup
var err error
var nfe *resource.NotFoundError
if !d.IsNewResource() && errors.As(err, &nfe) {
log.Printf("[WARN] Security group (%s) not found, removing from state", d.Id())
d.SetId("")
err = resource.Retry(15 * time.Second, func() *resource.RetryError {
sg, err = FindSecurityGroupByID(conn, d.Id())
if !d.IsNewResource() && errors.As(err, &nfe) {
log.Printf("[WARN] Security group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if d.IsNewResource() && errors.As(err, &nfe) {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if tfresource.TimedOut(err) {
sg, err = FindSecurityGroupByID(conn, d.Id())
}
if err != nil {
return fmt.Errorf("error reading Security Group (%s): %w", d.Id(), err)
Expand Down Expand Up @@ -405,7 +419,22 @@ func resourceSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
func resourceSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EC2Conn

group, err := FindSecurityGroupByID(conn, d.Id())
var group *ec2.SecurityGroup
var err error
var nfe *resource.NotFoundError
err = resource.Retry(15 * time.Second, func() *resource.RetryError {
group, err = FindSecurityGroupByID(conn, d.Id())
if errors.As(err, &nfe) {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if tfresource.TimedOut(err) {
group, err = FindSecurityGroupByID(conn, d.Id())
}
if err != nil {
return fmt.Errorf("error updating Security Group (%s): %w", d.Id(), err)
}
Expand Down

0 comments on commit 9cbd6c5

Please sign in to comment.