示例#1
0
// Helper: retrieve a route
func findResourceRoute(conn *ec2.EC2, rtbid string, cidr string) (*ec2.Route, error) {
	routeTableID := rtbid

	findOpts := &ec2.DescribeRouteTablesInput{
		RouteTableIds: []*string{&routeTableID},
	}

	resp, err := conn.DescribeRouteTables(findOpts)
	if err != nil {
		return nil, err
	}

	if len(resp.RouteTables) < 1 || resp.RouteTables[0] == nil {
		return nil, fmt.Errorf("Route table %s is gone, so route does not exist.",
			routeTableID)
	}

	for _, route := range (*resp.RouteTables[0]).Routes {
		if *route.DestinationCidrBlock == cidr {
			return route, nil
		}
	}

	return nil, fmt.Errorf(`
error finding matching route for Route table (%s) and destination CIDR block (%s)`,
		rtbid, cidr)
}
示例#2
0
文件: saws.go 项目: jamesunger/saws
func getPrivateRouteTable(svc *ec2.EC2, subnetid *string, VpcId string) (*string, error) {
	keyname := "association.subnet-id"
	filters := make([]*ec2.Filter, 0)
	filter := ec2.Filter{
		Name: &keyname, Values: []*string{subnetid}}
	filters = append(filters, &filter)

	//fmt.Println("Filters ", filters)

	drti := &ec2.DescribeRouteTablesInput{Filters: filters}
	drto, err := svc.DescribeRouteTables(drti)
	if err != nil {
		panic(err)
	}

	for i := range drto.RouteTables {
		if *drto.RouteTables[i].VpcId == VpcId {
			//fmt.Println("Route table is", *drto.RouteTables[i].RouteTableId)
			return drto.RouteTables[i].RouteTableId, nil
		}
	}

	return nil, errors.New(fmt.Sprintf("No route table found for subnet", *subnetid))

}
示例#3
0
func resourceAwsVpcSetDefaultRouteTable(conn *ec2.EC2, d *schema.ResourceData) error {
	filter1 := &ec2.Filter{
		Name:   aws.String("association.main"),
		Values: []*string{aws.String("true")},
	}
	filter2 := &ec2.Filter{
		Name:   aws.String("vpc-id"),
		Values: []*string{aws.String(d.Id())},
	}

	findOpts := &ec2.DescribeRouteTablesInput{
		Filters: []*ec2.Filter{filter1, filter2},
	}

	resp, err := conn.DescribeRouteTables(findOpts)
	if err != nil {
		return err
	}

	if len(resp.RouteTables) < 1 || resp.RouteTables[0] == nil {
		return fmt.Errorf("Default Route table not found")
	}

	// There Can Be Only 1 ... Default Route Table
	d.Set("default_route_table_id", resp.RouteTables[0].RouteTableId)

	return nil
}
示例#4
0
文件: saws.go 项目: jamesunger/saws
func getMainRouteTableFromVPC(svc *ec2.EC2, VpcId *string) (*string, error) {

	keyname := "association.main"
	asbool := "true"
	filters := make([]*ec2.Filter, 0)
	filter := ec2.Filter{
		Name: &keyname, Values: []*string{&asbool}}
	filters = append(filters, &filter)

	//fmt.Println("Filters ", filters)

	drti := &ec2.DescribeRouteTablesInput{Filters: filters}
	drto, err := svc.DescribeRouteTables(drti)
	if err != nil {
		panic(err)
	}

	for i := range drto.RouteTables {
		if *drto.RouteTables[i].VpcId == *VpcId {
			//fmt.Println("Route table is", *drto.RouteTables[i].RouteTableId)
			return drto.RouteTables[i].RouteTableId, nil
		}
	}

	return nil, errors.New(fmt.Sprintf("No main route table found for vpc", *VpcId))

}
示例#5
0
文件: awsvpc.go 项目: luxas/flannel
func (be *AwsVpcBackend) checkMatchingRoutes(routeTableID, instanceID, subnet string, ec2c *ec2.EC2) (bool, error) {
	matchingRouteFound := false

	filter := newFilter()
	filter.Add("route.destination-cidr-block", subnet)
	filter.Add("route.state", "active")

	input := ec2.DescribeRouteTablesInput{Filters: filter, RouteTableIds: []*string{&routeTableID}}

	resp, err := ec2c.DescribeRouteTables(&input)
	if err != nil {
		return matchingRouteFound, err
	}

	for _, routeTable := range resp.RouteTables {
		for _, route := range routeTable.Routes {
			if subnet == *route.DestinationCidrBlock && *route.State == "active" {

				if *route.InstanceId == instanceID {
					matchingRouteFound = true
					break
				}

				log.Errorf("Deleting invalid *active* matching route: %s, %s \n", *route.DestinationCidrBlock, *route.InstanceId)
			}
		}
	}

	return matchingRouteFound, nil
}
示例#6
0
文件: awsvpc.go 项目: luxas/flannel
func (be *AwsVpcBackend) detectRouteTableID(instanceID string, ec2c *ec2.EC2) (string, error) {
	instancesInput := &ec2.DescribeInstancesInput{
		InstanceIds: []*string{&instanceID},
	}

	resp, err := ec2c.DescribeInstances(instancesInput)
	if err != nil {
		return "", fmt.Errorf("error getting instance info: %v", err)
	}

	if len(resp.Reservations) == 0 {
		return "", fmt.Errorf("no reservations found")
	}

	if len(resp.Reservations[0].Instances) == 0 {
		return "", fmt.Errorf("no matching instance found with id: %v", instanceID)
	}

	subnetID := resp.Reservations[0].Instances[0].SubnetId
	vpcID := resp.Reservations[0].Instances[0].VpcId

	log.Info("Subnet-ID: ", *subnetID)
	log.Info("VPC-ID: ", *vpcID)

	filter := newFilter()
	filter.Add("association.subnet-id", *subnetID)

	routeTablesInput := &ec2.DescribeRouteTablesInput{
		Filters: filter,
	}

	res, err := ec2c.DescribeRouteTables(routeTablesInput)
	if err != nil {
		return "", fmt.Errorf("error describing routeTables for subnetID %s: %v", *subnetID, err)
	}

	if len(res.RouteTables) != 0 {
		return *res.RouteTables[0].RouteTableId, nil
	}

	filter = newFilter()
	filter.Add("association.main", "true")
	filter.Add("vpc-id", *vpcID)

	routeTablesInput = &ec2.DescribeRouteTablesInput{
		Filters: filter,
	}

	res, err = ec2c.DescribeRouteTables(routeTablesInput)
	if err != nil {
		log.Info("error describing route tables: ", err)
	}

	if len(res.RouteTables) == 0 {
		return "", fmt.Errorf("main route table not found")
	}

	return *res.RouteTables[0].RouteTableId, nil
}
func findMainRouteTable(conn *ec2.EC2, vpcId string) (*ec2.RouteTable, error) {
	mainFilter := &ec2.Filter{
		Name:   aws.String("association.main"),
		Values: []*string{aws.String("true")},
	}
	vpcFilter := &ec2.Filter{
		Name:   aws.String("vpc-id"),
		Values: []*string{aws.String(vpcId)},
	}
	routeResp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
		Filters: []*ec2.Filter{mainFilter, vpcFilter},
	})
	if err != nil {
		return nil, err
	} else if len(routeResp.RouteTables) != 1 {
		return nil, nil
	}

	return routeResp.RouteTables[0], nil
}
示例#8
0
// Helper: retrieve a route
func findResourceRoute(conn *ec2.EC2, rtbid string, cidr string) (*ec2.Route, error) {
	routeTableID := rtbid

	findOpts := &ec2.DescribeRouteTablesInput{
		RouteTableIds: []*string{&routeTableID},
	}

	resp, err := conn.DescribeRouteTables(findOpts)
	if err != nil {
		return nil, err
	}

	for _, route := range (*resp.RouteTables[0]).Routes {
		if *route.DestinationCidrBlock == cidr {
			return route, nil
		}
	}

	return nil, nil
}
示例#9
0
// 指定したInstance IDが登録されているRouteTable IDを返す
// ToDo: MySQL VIP用RouteTableが複数返されたらどーすんべ対応
func instanceIDToRouteTableID(svc *ec2.EC2, InstanceID string) string {
	findOpts := &ec2.DescribeRouteTablesInput{
		Filters: []*ec2.Filter{
			{
				Name: aws.String("route.instance-id"),
				Values: []*string{
					aws.String(InstanceID),
				},
			},
		},
	}
	resp, err := svc.DescribeRouteTables(findOpts)
	var routetableID string
	for _, idx := range resp.RouteTables {
		for _, rtbs := range idx.Associations {
			routetableID = *rtbs.RouteTableId
		}
	}
	if err != nil {
		panic(err)
	}
	return routetableID
}
// resourceAwsRouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a RouteTable.
func resourceAwsRouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
	return func() (interface{}, string, error) {
		resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
			RouteTableIds: []*string{aws.String(id)},
		})
		if err != nil {
			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidRouteTableID.NotFound" {
				resp = nil
			} else {
				log.Printf("Error on RouteTableStateRefresh: %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
		}

		rt := resp.RouteTables[0]
		return rt, "ready", nil
	}
}
示例#11
0
func validateRouteTable(ec2Client *ec2.EC2, routeTable *providers.RouteTable, vpc *providers.Vpc, createIfNotFound bool) (success bool, warnings []string, err []error) {

	routeTableName := routeTable.RouteTableName
	log.WithFields(log.Fields{
		"VpcID":          vpc.VpcID,
		"RouteTableName": routeTableName,
	}).Info("Querying AWS for VPC Route Table")
	filters := []*ec2.Filter{}
	if routeTable.IsMain {
		filters = append(filters, newEc2Filter("association.main", "true"))
	} else {
		filters = append(filters, newEc2Filter("tag:"+nameTag, routeTableName))
	}
	filters = append(filters, newEc2Filter("vpc-id", vpc.VpcID))
	//Query route table
	log.Debugf("Filters are : %v", filters)
	params := &ec2.DescribeRouteTablesInput{Filters: filters}
	routeTableQueryOutput, rtErr := ec2Client.DescribeRouteTables(params)

	if rtErr != nil {
		success = false
		err = append(err, rtErr)
	} else {
		//RouteTable exists
		if len(routeTableQueryOutput.RouteTables) > 0 {
			if routeTable.IsMain {
				log.WithFields(log.Fields{
					"routeTable": *routeTableQueryOutput.RouteTables[0].RouteTableID,
				}).Info("Configuring main route table")
				//Lets check and see if the route table we found is the default route table, and use that one
				for tag := range routeTableQueryOutput.RouteTables[0].Tags {
					log.Infof("tag %v key is %s and value is %s", *routeTableQueryOutput.RouteTables[0].Tags[tag].Key, *routeTableQueryOutput.RouteTables[0].Tags[tag].Value)
				}
				if tagContains(routeTableQueryOutput.RouteTables[0].Tags, "Name", routeTable.RouteTableName) {
					success = true
					routeTable.RouteTableID = *routeTableQueryOutput.RouteTables[0].RouteTableID
				} else {
					tagCreated, tagErr := addDockerTagToResource(ec2Client, *routeTableQueryOutput.RouteTables[0].RouteTableID, vpc.Name, routeTableName)
					if tagErr != nil {
						log.WithFields(log.Fields{
							"AWS Error": err,
						}).Error("Error Tagging Route Table")
						//This will not stop the processing of the provider configuration, but we will return a warning so the user knows
						success = true
						warnings = append(warnings, "WARNING: Error creating tag for "+routeTableName+", processing will continue")
					} else {
						log.WithFields(log.Fields{
							"Created": tagCreated,
						}).Info("Tag Created")
						success = true
					}
				}
			}
			for r := range routeTable.Routes {
				vrSuccess, vrWarnings, vrErrors := validateRoute(ec2Client, &routeTable.Routes[r], routeTableQueryOutput, vpc.InternetGateway.InternetGatewayID)
				if vrSuccess {
					for vrw := range vrWarnings {
						warnings = append(warnings, vrWarnings[vrw])
					}
					success = true
				} else {
					for vre := range vrErrors {
						err = append(err, vrErrors[vre])
					}
					success = false
				}
			}
			if success && !routeTable.IsMain {
				log.Infof("Starting to validate Assocoations for %v", routeTableQueryOutput)
				vaSuccess, vaWarnings, vaErrors := validateAssociations(ec2Client, routeTable, routeTableQueryOutput.RouteTables[0].Associations, vpc.Subnets)
				if vaSuccess {
					for vaw := range vaWarnings {
						warnings = append(warnings, vaWarnings[vaw])
					}
					success = true
				} else {
					for vae := range vaErrors {
						err = append(err, vaErrors[vae])
					}
					success = false
				}
			}

		} else {
			//Does not exist
			callSuccess, rtCreateWarn, rtCreateErr := addRouteTable(ec2Client, routeTable, vpc)
			if callSuccess {
				//call was successful
				if len(rtCreateWarn) > 0 {
					outputWarnings("Adding Route Table", "addRouteTable", rtCreateWarn)
				}
				success = true
				//now lets retrieve the new routetable from aws
				rtFilters := []*ec2.Filter{}
				rtFilters = append(rtFilters, newEc2Filter("tag:"+nameTag, routeTableName))
				rtFilters = append(rtFilters, newEc2Filter("vpc-id", vpc.VpcID))
				rtParams := &ec2.DescribeRouteTablesInput{Filters: rtFilters}

				newRouteTableQueryOutput, rtErr := ec2Client.DescribeRouteTables(rtParams)
				if rtErr != nil {
					success = false
					err = append(err, rtErr)
				} else {
					//validate if routes are configured correctly
					for r := range routeTable.Routes {
						vrSuccess, vrWarnings, vrErrors := validateRoute(ec2Client, &routeTable.Routes[r], newRouteTableQueryOutput, vpc.InternetGateway.InternetGatewayID)
						if vrSuccess {
							for vrw := range vrWarnings {
								warnings = append(warnings, vrWarnings[vrw])
							}
							success = true
						} else {
							for vre := range vrErrors {
								err = append(err, vrErrors[vre])
							}
							success = false
						}
					}
				}
			} else {
				//outputErrors("Error creating route table", "addRouteTable", []error{rtCreateErr})
				for e := range rtCreateErr {
					err = append(err, rtCreateErr[e])
				}
				success = false
			}
			if success {
				rtFilters := []*ec2.Filter{}
				rtFilters = append(rtFilters, newEc2Filter("tag:"+nameTag, routeTableName))
				rtFilters = append(rtFilters, newEc2Filter("vpc-id", vpc.VpcID))
				rtParams := &ec2.DescribeRouteTablesInput{Filters: rtFilters}

				newRouteTableQueryOutput, rtErr := ec2Client.DescribeRouteTables(rtParams)
				if rtErr != nil {
					success = false
					err = append(err, rtErr)
				} else {
					vaSuccess, vaWarnings, vaErrors := validateAssociations(ec2Client, routeTable, newRouteTableQueryOutput.RouteTables[0].Associations, vpc.Subnets)
					if vaSuccess {
						for vaw := range vaWarnings {
							warnings = append(warnings, vaWarnings[vaw])
						}
						success = true
					} else {
						for vae := range vaErrors {
							err = append(err, vaErrors[vae])
						}
						success = false
					}
				}
			}
		}

	}
	return success, warnings, err
}