mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
provider/aws: Fix dependency violation when deleting Internet Gateways
This commit is contained in:
parent
da7f307e56
commit
043a4848ee
@ -199,39 +199,14 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{})
|
|||||||
d.Id(),
|
d.Id(),
|
||||||
vpcID.(string))
|
vpcID.(string))
|
||||||
|
|
||||||
wait := true
|
|
||||||
err := ec2conn.DetachInternetGateway(&ec2.DetachInternetGatewayRequest{
|
|
||||||
InternetGatewayID: aws.String(d.Id()),
|
|
||||||
VPCID: aws.String(vpcID.(string)),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
ec2err, ok := err.(aws.APIError)
|
|
||||||
if ok {
|
|
||||||
if ec2err.Code == "InvalidInternetGatewayID.NotFound" {
|
|
||||||
err = nil
|
|
||||||
wait = false
|
|
||||||
} else if ec2err.Code == "Gateway.NotAttached" {
|
|
||||||
err = nil
|
|
||||||
wait = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !wait {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for it to be fully detached before continuing
|
// Wait for it to be fully detached before continuing
|
||||||
log.Printf("[DEBUG] Waiting for internet gateway (%s) to detach", d.Id())
|
log.Printf("[DEBUG] Waiting for internet gateway (%s) to detach", d.Id())
|
||||||
stateConf := &resource.StateChangeConf{
|
stateConf := &resource.StateChangeConf{
|
||||||
Pending: []string{"attached", "detaching", "available"},
|
Pending: []string{"detaching"},
|
||||||
Target: "detached",
|
Target: "detached",
|
||||||
Refresh: IGAttachStateRefreshFunc(ec2conn, d.Id(), "detached"),
|
Refresh: detachIGStateRefreshFunc(ec2conn, d.Id(), vpcID.(string)),
|
||||||
Timeout: 1 * time.Minute,
|
Timeout: 2 * time.Minute,
|
||||||
|
Delay: 10 * time.Second,
|
||||||
}
|
}
|
||||||
if _, err := stateConf.WaitForState(); err != nil {
|
if _, err := stateConf.WaitForState(); err != nil {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
@ -242,6 +217,32 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{})
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InstanceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
||||||
|
// an EC2 instance.
|
||||||
|
func detachIGStateRefreshFunc(conn *ec2.EC2, instanceID, vpcID string) resource.StateRefreshFunc {
|
||||||
|
return func() (interface{}, string, error) {
|
||||||
|
err := conn.DetachInternetGateway(&ec2.DetachInternetGatewayRequest{
|
||||||
|
InternetGatewayID: aws.String(instanceID),
|
||||||
|
VPCID: aws.String(vpcID),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
ec2err, ok := err.(aws.APIError)
|
||||||
|
if ok {
|
||||||
|
if ec2err.Code == "InvalidInternetGatewayID.NotFound" {
|
||||||
|
return nil, "Not Found", err
|
||||||
|
} else if ec2err.Code == "Gateway.NotAttached" {
|
||||||
|
return "detached", "detached", nil
|
||||||
|
} else if ec2err.Code == "DependencyViolation" {
|
||||||
|
return nil, "detaching", nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// DetachInternetGateway only returns an error, so if it's nil, assume we're
|
||||||
|
// detached
|
||||||
|
return "detached", "detached", nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
// IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
||||||
// an internet gateway.
|
// an internet gateway.
|
||||||
func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
||||||
@ -300,10 +301,6 @@ func IGAttachStateRefreshFunc(ec2conn *ec2.EC2, id string, expected string) reso
|
|||||||
|
|
||||||
ig := &resp.InternetGateways[0]
|
ig := &resp.InternetGateways[0]
|
||||||
|
|
||||||
if time.Now().Sub(start) > 10*time.Second {
|
|
||||||
return ig, expected, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ig.Attachments) == 0 {
|
if len(ig.Attachments) == 0 {
|
||||||
// No attachments, we're detached
|
// No attachments, we're detached
|
||||||
return ig, "detached", nil
|
return ig, "detached", nil
|
||||||
|
Loading…
Reference in New Issue
Block a user