// IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an internet gateway.
func IGStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
	return func() (interface{}, string, error) {
		resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
			InternetGatewayIds: []*string{aws.String(id)},
		})
		if err != nil {
			ec2err, ok := err.(awserr.Error)
			if ok && ec2err.Code() == "InvalidInternetGatewayID.NotFound" {
				resp = nil
			} else {
				log.Printf("[ERROR] Error on IGStateRefresh: %s", err)
				return nil, "", err
			}
		}

		if resp == nil {
			// Sometimes AWS just has consistency issues and doesn't see
			// our instance yet. Return an empty state.
			return nil, "", nil
		}

		ig := resp.InternetGateways[0]
		return ig, "available", nil
	}
}
Exemplo n.º 2
0
func validateInternetGateway(ec2Client *ec2.EC2, providerConfig *providers.JSONObject, createIfNotFound bool) (success bool, warnings []string, err []error) {
	filters := []*ec2.Filter{newEc2Filter("attachment.vpc-id", providerConfig.Provider.ProviderConfig.Aws.Vpc.VpcID)}
	params := &ec2.DescribeInternetGatewaysInput{Filters: filters}
	internetGatewayOutput, callErr := ec2Client.DescribeInternetGateways(params)
	if callErr != nil {
		err = append(err, callErr)
		success = false
	} else {
		if len(internetGatewayOutput.InternetGateways) > 0 {
			providerConfig.Provider.ProviderConfig.Aws.Vpc.InternetGateway.InternetGatewayID = *internetGatewayOutput.InternetGateways[0].InternetGatewayID
			success = true
		} else {
			//Does not exist
			createSuccess, createWarning, createError := addInternetGateway(ec2Client, providerConfig)
			if createSuccess {
				for w := range createWarning {
					warnings = append(warnings, createWarning[w])
				}
				attachSuccess, attachWarnings, attachErrors := attachGatewayToVpc(ec2Client, providerConfig.Provider.ProviderConfig.Aws.Vpc.InternetGateway.InternetGatewayID, providerConfig.Provider.ProviderConfig.Aws.Vpc.VpcID)
				if attachSuccess {
					for w := range attachWarnings {
						warnings = append(warnings, attachWarnings[w])
					}
					success = true
				} else {
					for e := range attachErrors {
						err = append(err, attachErrors[e])
					}
					success = false
				}

			} else {
				success = false
				for e := range createError {
					err = append(err, createError[e])
				}
			}
		}

	}
	return success, warnings, err
}
Exemplo n.º 3
0
func getGatewayIds(c *ec2.EC2, vpcid string) ([]string, error) {
	gatewayids := []string{}

	filters := make([]*ec2.Filter, 0)
	keyname := "attachment.vpc-id"
	filter := ec2.Filter{
		Name: &keyname, Values: []*string{&vpcid}}
	filters = append(filters, &filter)

	digi := &ec2.DescribeInternetGatewaysInput{Filters: filters}
	digo, err := c.DescribeInternetGateways(digi)
	if err != nil {
		return gatewayids, err
	}

	for i := range digo.InternetGateways {
		gatewayids = append(gatewayids, *digo.InternetGateways[i].InternetGatewayId)
	}
	return gatewayids, nil
}
// IGAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used
// watch the state of an internet gateway's attachment.
func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
	var start time.Time
	return func() (interface{}, string, error) {
		if start.IsZero() {
			start = time.Now()
		}

		resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
			InternetGatewayIds: []*string{aws.String(id)},
		})
		if err != nil {
			ec2err, ok := err.(awserr.Error)
			if ok && ec2err.Code() == "InvalidInternetGatewayID.NotFound" {
				resp = nil
			} else {
				log.Printf("[ERROR] Error on IGStateRefresh: %s", err)
				return nil, "", err
			}
		}

		if resp == nil {
			// Sometimes AWS just has consistency issues and doesn't see
			// our instance yet. Return an empty state.
			return nil, "", nil
		}

		ig := resp.InternetGateways[0]

		if time.Now().Sub(start) > 10*time.Second {
			return ig, expected, nil
		}

		if len(ig.Attachments) == 0 {
			// No attachments, we're detached
			return ig, "detached", nil
		}

		return ig, *ig.Attachments[0].State, nil
	}
}