Skip to content

Commit

Permalink
Merge pull request #58 from hibikir/master
Browse files Browse the repository at this point in the history
Refactor the way we create routes
  • Loading branch information
T.J. Corrigan committed Jan 22, 2016
2 parents 7f7f468 + a6ebecf commit 558f39a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ object `AWS::EC2::CustomerGateway` extends DefaultJsonProtocol {
}

@implicitNotFound("A Route can only have exactly ONE of GatewayId, InstanceId, NetworkInterfaceId or VpcPeeringConnectionId set")
class ValidRouteCombo[G, I, P] private ()
object ValidRouteCombo{
implicit object valid1T extends ValidRouteCombo[Some[Token[ResourceRef[`AWS::EC2::InternetGateway`]]], None.type, None.type]
implicit object valid1 extends ValidRouteCombo[Some[ResourceRef[`AWS::EC2::InternetGateway`]], None.type, None.type]
implicit object valid2T extends ValidRouteCombo[None.type , Some[Token[ResourceRef[`AWS::EC2::Instance`]]], None.type]
implicit object valid2 extends ValidRouteCombo[None.type , Some[ResourceRef[`AWS::EC2::Instance`]], None.type]
implicit object valid3T extends ValidRouteCombo[None.type , None.type, Some[Token[ResourceRef[`AWS::EC2::VPCPeeringConnection`]]]]
implicit object valid3 extends ValidRouteCombo[None.type , None.type, Some[ResourceRef[`AWS::EC2::VPCPeeringConnection`]]]
sealed trait ValidRouteComboOption
case class InternetGatewayRoute(v:Token[ResourceRef[`AWS::EC2::InternetGateway`]]) extends ValidRouteComboOption
case class EC2InstanceRoute(v:Token[ResourceRef[`AWS::EC2::Instance`]]) extends ValidRouteComboOption
case class VPCPeeringRoute(v:Token[ResourceRef[`AWS::EC2::VPCPeeringConnection`]]) extends ValidRouteComboOption

object ValidRouteComboOption {
implicit def toInternetGateway[T](v: T)(implicit t:T => Token[ResourceRef[`AWS::EC2::InternetGateway`]]) = InternetGatewayRoute(v)
implicit def toEC2InstanceRoute[T](v:T)(implicit t :T => Token[ResourceRef[`AWS::EC2::Instance`]]) = EC2InstanceRoute(v)
implicit def toVPCPeeringRoute[T](v:T)(implicit t :T => Token[ResourceRef[`AWS::EC2::VPCPeeringConnection`]]) = VPCPeeringRoute(v)
}

class `AWS::EC2::Route` private (
Expand Down Expand Up @@ -167,22 +168,22 @@ object `AWS::EC2::Route` extends DefaultJsonProtocol {
def read(json: JsValue) = ???
}

def apply[
G <: Option[Token[ResourceRef[`AWS::EC2::InternetGateway`]]],
I <: Option[Token[ResourceRef[`AWS::EC2::Instance`]]],
P <: Option[Token[ResourceRef[`AWS::EC2::VPCPeeringConnection`]]]
](
name: String,
RouteTableId: Token[ResourceRef[`AWS::EC2::RouteTable`]],
DestinationCidrBlock: Token[CidrBlock],
GatewayId: G = None,
InstanceId: I = None,
VpcPeeringConnectionId: P = None,
Condition: Option[ConditionRef] = None,
DependsOn: Option[Seq[String]] = None
)(implicit ev1: ValidRouteCombo[G, I, P]) =
new `AWS::EC2::Route`(name, RouteTableId, DestinationCidrBlock, GatewayId, InstanceId, VpcPeeringConnectionId,
Condition, DependsOn)
def apply(
name: String,
RouteTableId: Token[ResourceRef[`AWS::EC2::RouteTable`]],
DestinationCidrBlock: Token[CidrBlock],
connectionBobber: ValidRouteComboOption,
Condition: Option[ConditionRef] = None,
DependsOn: Option[Seq[String]] = None
) =
connectionBobber match {
case InternetGatewayRoute(v) => new `AWS::EC2::Route`(name, RouteTableId, DestinationCidrBlock,
Some(v), None,None, Condition, DependsOn)
case EC2InstanceRoute(v) => new `AWS::EC2::Route`(name, RouteTableId, DestinationCidrBlock,
None, Some(v), None, Condition, DependsOn)
case VPCPeeringRoute(v) => new `AWS::EC2::Route`(name, RouteTableId, DestinationCidrBlock,
None, None ,Some(v), Condition, DependsOn)
}
}

case class `AWS::EC2::RouteTable`(name: String, VpcId: Token[ResourceRef[`AWS::EC2::VPC`]], Tags: Seq[AmazonTag],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,36 @@ trait Outputs {

trait Route {
implicit class RichRouteTable(rt: `AWS::EC2::RouteTable`) {
def withRouteT[G <: Option[Token[ResourceRef[`AWS::EC2::InternetGateway`]]], I <: Option[Token[ResourceRef[`AWS::EC2::Instance`]]],
P <: Option[Token[ResourceRef[`AWS::EC2::VPCPeeringConnection`]]]](
def withRouteT(
visibility: String,
routeTableOrdinal: Int,
routeOrdinal: Int,
gateway: G = None,
instance: I = None,
vpcPeeringConn: P = None,
connectionBobber: ValidRouteComboOption,
cidr: CidrBlock = CidrBlock(0,0,0,0,0),
dependsOn: Option[Seq[String]] = None
)(implicit ev1: ValidRouteCombo[G,I,P]) =
) =
`AWS::EC2::Route`(
visibility + "RouteTable" + routeTableOrdinal + "Route" + routeOrdinal,
RouteTableId = ResourceRef(rt),
DestinationCidrBlock = cidr,
GatewayId = gateway,
InstanceId = instance,
VpcPeeringConnectionId = vpcPeeringConn,
connectionBobber: ValidRouteComboOption,
DependsOn = dependsOn
)

def withRoute[G <: Option[Token[ResourceRef[`AWS::EC2::InternetGateway`]]], I <: Option[Token[ResourceRef[`AWS::EC2::Instance`]]],
P <: Option[Token[ResourceRef[`AWS::EC2::VPCPeeringConnection`]]],
GwoT <% G, IwoT <% I, PwoT <% P](

def withRoute(
visibility: String,
routeTableOrdinal: Int,
routeOrdinal: Int,
gateway: GwoT = None,
instance: IwoT = None,
vpcPeeringConn: PwoT = None,
connectionBobber: ValidRouteComboOption,
cidr: CidrBlock = CidrBlock(0,0,0,0,0),
dependsOn: Option[Seq[String]] = None
)(implicit ev1: ValidRouteCombo[G,I,P]) =
`AWS::EC2::Route`[G, I, P](
) =
`AWS::EC2::Route`(
visibility + "RouteTable" + routeTableOrdinal + "Route" + routeOrdinal,
RouteTableId = ResourceRef(rt),
DestinationCidrBlock = cidr,
GatewayId = gateway,
InstanceId = instance,
VpcPeeringConnectionId = vpcPeeringConn,
connectionBobber = connectionBobber,
DependsOn = dependsOn
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1013,11 +1013,11 @@ object StaxTemplate {
internetGatewayResource,
gatewayAttachmentResource,
publicRouteTableResource,
publicRouteTableResource.withRoute("Public", 1, 1, gateway = Some(ResourceRef(internetGatewayResource))),
publicRouteTableResource.withRoute("Public", 1, 1, internetGatewayResource),
privateRouteTable1Resource,
privateRouteTable1Resource.withRoute("Private", 1, 1, instance = Some(ResourceRef(nat1InstanceResource))),
privateRouteTable1Resource.withRoute("Private", 1, 1, nat1InstanceResource),
privateRouteTable2Resource,
privateRouteTable2Resource.withRoute("Private", 2, 1, instance = Some(ResourceRef(nat2InstanceResource))),
privateRouteTable2Resource.withRoute("Private", 2, 1, nat2InstanceResource),
withRouteTableAssoc("Public", 1, ResourceRef(publicRouteTableResource))(pubSubnet1),
withRouteTableAssoc("Public", 2, ResourceRef(publicRouteTableResource))(pubSubnet2),
withRouteTableAssoc("Private", 1, ResourceRef(privateRouteTable1Resource))(priSubnet1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TemplateDoc_AT extends FunSpec with Matchers {
visibility = "Public",
routeTableOrdinal = 1,
routeOrdinal = 1,
gateway = Some(ResourceRef(internetGatewayResource))
internetGatewayResource
)
val gatewayStuff = Template.fromResource(internetGatewayResource) ++
gatewayToInternetResource ++
Expand Down

0 comments on commit 558f39a

Please sign in to comment.