Skip to content

Commit

Permalink
Merge f018457 into e1a6c70
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Zoschke committed Feb 17, 2016
2 parents e1a6c70 + f018457 commit 20ce1e7
Show file tree
Hide file tree
Showing 13 changed files with 514 additions and 64 deletions.
88 changes: 88 additions & 0 deletions api/cmd/formation/handler/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/convox/rack/Godeps/_workspace/src/github.com/aws/aws-sdk-go/aws"
"github.com/convox/rack/Godeps/_workspace/src/github.com/aws/aws-sdk-go/aws/awserr"
"github.com/convox/rack/Godeps/_workspace/src/github.com/aws/aws-sdk-go/service/ec2"
)

Expand Down Expand Up @@ -52,6 +53,27 @@ func HandleEC2NatGateway(req Request) (string, map[string]string, error) {
return "", nil, fmt.Errorf("unknown RequestType: %s", req.RequestType)
}

func HandleEC2Route(req Request) (string, map[string]string, error) {
defer recoverFailure(req)

switch req.RequestType {
case "Create":
fmt.Println("CREATING ROUTE")
fmt.Printf("req %+v\n", req)
return EC2RouteCreate(req)
case "Update":
fmt.Println("UPDATING ROUTE")
fmt.Printf("req %+v\n", req)
return EC2RouteUpdate(req)
case "Delete":
fmt.Println("DELETING ROUTE")
fmt.Printf("req %+v\n", req)
return EC2RouteDelete(req)
}

return "", nil, fmt.Errorf("unknown RequestType: %s", req.RequestType)
}

var regexMatchAvailabilityZones = regexp.MustCompile(`following availability zones: ([^.]+)`)

func EC2AvailabilityZonesCreate(req Request) (string, map[string]string, error) {
Expand Down Expand Up @@ -111,3 +133,69 @@ func EC2NatGatewayDelete(req Request) (string, map[string]string, error) {

return req.PhysicalResourceId, nil, err
}

func EC2RouteCreate(req Request) (string, map[string]string, error) {
destinationCidrBlock := req.ResourceProperties["DestinationCidrBlock"].(string)
routeTableId := req.ResourceProperties["RouteTableId"].(string)

_, err := EC2(req).CreateRoute(&ec2.CreateRouteInput{
DestinationCidrBlock: aws.String(destinationCidrBlock),
NatGatewayId: aws.String(req.ResourceProperties["NatGatewayId"].(string)),
RouteTableId: aws.String(routeTableId),
})

if err != nil {
return "invalid", nil, err
}

return routeTableId + "/" + destinationCidrBlock, nil, nil
}

func EC2RouteUpdate(req Request) (string, map[string]string, error) {
parts := strings.SplitN(req.PhysicalResourceId, "/", 2)

destinationCidrBlock := req.ResourceProperties["DestinationCidrBlock"].(string)
routeTableId := req.ResourceProperties["RouteTableId"].(string)

if parts[0] == routeTableId && parts[1] == destinationCidrBlock {
return req.PhysicalResourceId, nil, nil
}

_, err := EC2(req).DeleteRoute(&ec2.DeleteRouteInput{
DestinationCidrBlock: aws.String(parts[1]),
RouteTableId: aws.String(parts[0]),
})

if err != nil {
return req.PhysicalResourceId, nil, err
}

_, err = EC2(req).CreateRoute(&ec2.CreateRouteInput{
DestinationCidrBlock: aws.String(destinationCidrBlock),
NatGatewayId: aws.String(req.ResourceProperties["NatGatewayId"].(string)),
RouteTableId: aws.String(routeTableId),
})

if err != nil {
return req.PhysicalResourceId, nil, err
}

return routeTableId + "/" + destinationCidrBlock, nil, nil
}

func EC2RouteDelete(req Request) (string, map[string]string, error) {
parts := strings.SplitN(req.PhysicalResourceId, "/", 2)

_, err := EC2(req).DeleteRoute(&ec2.DeleteRouteInput{
DestinationCidrBlock: aws.String(parts[1]),
RouteTableId: aws.String(parts[0]),
})

if ae, ok := err.(awserr.Error); ok {
if ae.Code() == "InvalidRoute.NotFound" {
return req.PhysicalResourceId, nil, nil
}
}

return req.PhysicalResourceId, nil, err
}
2 changes: 2 additions & 0 deletions api/cmd/formation/handler/formation.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ func HandleRequest(freq Request) error {
physical, outputs, err = HandleEC2AvailabilityZones(freq)
case "Custom::EC2NatGateway":
physical, outputs, err = HandleEC2NatGateway(freq)
case "Custom::EC2Route":
physical, outputs, err = HandleEC2Route(freq)
case "Custom::ECRRepository":
physical, outputs, err = HandleECRRepository(freq)
case "Custom::ECSCluster":
Expand Down

0 comments on commit 20ce1e7

Please sign in to comment.