Skip to content

Commit

Permalink
Merge pull request hashicorp#3892 from jammerful/sgIdFix
Browse files Browse the repository at this point in the history
resource/aws_security_group: Fix Missing Id Error
  • Loading branch information
bflad committed Mar 26, 2018
2 parents 680afc8 + 2e41ac6 commit 22fe492
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions aws/resource_aws_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,7 @@ func resourceAwsSecurityGroupCreate(d *schema.ResourceData, meta interface{}) er
log.Printf("[INFO] Security Group ID: %s", d.Id())

// Wait for the security group to truly exist
log.Printf(
"[DEBUG] Waiting for Security Group (%s) to exist",
d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{""},
Target: []string{"exists"},
Refresh: SGStateRefreshFunc(conn, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
}

resp, err := stateConf.WaitForState()
resp, err := waitForSgToExist(conn, d.Id(), d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf(
"Error waiting for Security Group (%s) to become available: %s",
Expand Down Expand Up @@ -342,11 +332,20 @@ func resourceAwsSecurityGroupCreate(d *schema.ResourceData, meta interface{}) er
func resourceAwsSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

sgRaw, _, err := SGStateRefreshFunc(conn, d.Id())()
var sgRaw interface{}
var err error
if d.IsNewResource() {
sgRaw, err = waitForSgToExist(conn, d.Id(), d.Timeout(schema.TimeoutRead))
} else {
sgRaw, _, err = SGStateRefreshFunc(conn, d.Id())()
}

if err != nil {
return err
}

if sgRaw == nil {
log.Printf("[WARN] Security group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand Down Expand Up @@ -393,11 +392,19 @@ func resourceAwsSecurityGroupRead(d *schema.ResourceData, meta interface{}) erro
func resourceAwsSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

sgRaw, _, err := SGStateRefreshFunc(conn, d.Id())()
var sgRaw interface{}
var err error
if d.IsNewResource() {
sgRaw, err = waitForSgToExist(conn, d.Id(), d.Timeout(schema.TimeoutRead))
} else {
sgRaw, _, err = SGStateRefreshFunc(conn, d.Id())()
}

if err != nil {
return err
}
if sgRaw == nil {
log.Printf("[WARN] Security group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand Down Expand Up @@ -840,6 +847,18 @@ func SGStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
}
}

func waitForSgToExist(conn *ec2.EC2, id string, timeout time.Duration) (interface{}, error) {
log.Printf("[DEBUG] Waiting for Security Group (%s) to exist", id)
stateConf := &resource.StateChangeConf{
Pending: []string{""},
Target: []string{"exists"},
Refresh: SGStateRefreshFunc(conn, id),
Timeout: timeout,
}

return stateConf.WaitForState()
}

// matchRules receives the group id, type of rules, and the local / remote maps
// of rules. We iterate through the local set of rules trying to find a matching
// remote rule, which may be structured differently because of how AWS
Expand Down

0 comments on commit 22fe492

Please sign in to comment.