upup: detach internet gateway if vpc is to be deleted

This commit is contained in:
Justin Santa Barbara 2016-06-17 09:36:27 -04:00
parent 3ede6c1f4a
commit 1e7159a923
1 changed files with 54 additions and 0 deletions

View File

@ -183,6 +183,32 @@ func (c *DeleteCluster) ListResources() (map[string]DeletableResource, error) {
}
}
{
glog.V(2).Infof("Listing all Internet Gateways")
request := &ec2.DescribeInternetGatewaysInput{}
response, err := cloud.EC2.DescribeInternetGateways(request)
if err != nil {
return nil, fmt.Errorf("error listing InternetGateways: %v", err)
}
for _, igw := range response.InternetGateways {
for _, attachment := range igw.Attachments {
vpcID := aws.StringValue(attachment.VpcId)
if vpcID == "" {
continue
}
if resources["vpc:"+vpcID] != nil {
r := &DetachableInternetGateway{
ID: aws.StringValue(igw.InternetGatewayId),
VPCID: vpcID,
}
resources["detach-igw:"+aws.StringValue(igw.InternetGatewayId)] = r
}
}
}
}
return resources, nil
}
@ -747,6 +773,34 @@ func (r *DeletableInternetGateway) String() string {
return "InternetGateway:" + r.ID
}
type DetachableInternetGateway struct {
ID string
VPCID string
}
func (r *DetachableInternetGateway) Delete(cloud fi.Cloud) error {
c := cloud.(*awsup.AWSCloud)
glog.V(2).Infof("Detaching EC2 InternetGateway %q", r.ID)
request := &ec2.DetachInternetGatewayInput{
InternetGatewayId: &r.ID,
VpcId: &r.VPCID,
}
_, err := c.EC2.DetachInternetGateway(request)
if err != nil {
if IsDependencyViolation(err) {
return err
}
return fmt.Errorf("error detaching InternetGateway %q: %v", r.ID, err)
}
return nil
}
func (r *DetachableInternetGateway) String() string {
return "DetachInternetGateway:" + r.ID
}
type DeletableVPC struct {
ID string
}