Esempio n. 1
0
func addRouteTable(ec2Client *ec2.EC2, routeTable *providers.RouteTable, vpc *providers.Vpc) (success bool, warnings []string, err []error) {
	routeTableName := routeTable.RouteTableName
	createRTOutput, callErr := ec2Client.CreateRouteTable(&ec2.CreateRouteTableInput{
		VPCID: &vpc.VpcID,
	})
	if callErr != nil {
		log.WithFields(log.Fields{
			"AWS Error":      callErr.Error(),
			"VpcID":          vpc.VpcID,
			"RouteTableName": routeTableName,
		}).Error("Error Creating Route Table")
		err = append(err, callErr)
		success = false
	} else {
		log.WithFields(log.Fields{
			"VpcID":        vpc.VpcID,
			"RouteTableID": routeTableName,
		}).Info("Route Table Created")
		routeTable.RouteTableID = *createRTOutput.RouteTable.RouteTableID

		tagCreated, tagErr := addDockerTagToResource(ec2Client, routeTable.RouteTableID, vpc.Name, routeTableName)
		if tagErr != nil {
			log.WithFields(log.Fields{
				"AWS Error": tagErr,
			}).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
		}
	}
	return success, warnings, err
}
Esempio n. 2
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
}