Skip to content

Commit

Permalink
Merge pull request #80 from aniketbhatnagar/Parameterizing-VPC-And-Se…
Browse files Browse the repository at this point in the history
…curityGroups-For-RDS

Parameterized VPC and security group for RDS
  • Loading branch information
T.J. Corrigan committed Apr 6, 2016
2 parents 3606e93 + b39982a commit 25be4bb
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object Parameter extends DefaultJsonProtocol {
case k: `AWS::EC2::KeyPair::KeyName_Parameter` => k.toJson
case v: `AWS::EC2::VPC_Parameter` => v.toJson
case e: `AWS::RDS::DBInstance::Engine_Parameter` => e.toJson
case s: `AWS::RDS::DBSubnetGroup_Parameter` => s.toJson
}

JsObject( raw.asJsObject.fields - "name" - "ConfigDefault" + ("Type" -> JsString(obj.Type)) )
Expand Down Expand Up @@ -181,7 +182,7 @@ case class `AWS::EC2::VPC_Parameter`(
Description: Option[String],
Default: Option[Token[ResourceRef[`AWS::EC2::VPC`]]] = None,
ConfigDefault: Option[String] = None
) extends Parameter("String"){type Rep = ResourceRef[`AWS::EC2::VPC`]}
) extends Parameter("AWS::EC2::VPC::Id"){type Rep = ResourceRef[`AWS::EC2::VPC`]}
object `AWS::EC2::VPC_Parameter` extends DefaultJsonProtocol {
implicit val format: JsonFormat[`AWS::EC2::VPC_Parameter`] = jsonFormat4(`AWS::EC2::VPC_Parameter`.apply)
}
Expand All @@ -196,6 +197,15 @@ object `AWS::RDS::DBInstance::Engine_Parameter` extends DefaultJsonProtocol {
implicit val format: JsonFormat[`AWS::RDS::DBInstance::Engine_Parameter`] = jsonFormat4(`AWS::RDS::DBInstance::Engine_Parameter`.apply)
}

case class `AWS::RDS::DBSubnetGroup_Parameter`(name: String,
Description: Option[String],
Default: Option[String] = None,
ConfigDefault: Option[String] = None
) extends Parameter("String"){type Rep = ResourceRef[`AWS::RDS::DBSubnetGroup`]}
object `AWS::RDS::DBSubnetGroup_Parameter` extends DefaultJsonProtocol {
implicit val format: JsonFormat[`AWS::RDS::DBSubnetGroup_Parameter`] = jsonFormat4(`AWS::RDS::DBSubnetGroup_Parameter`.apply)
}

case class InputParameter(ParameterKey: String, ParameterValue: JsValue = "<changeMe>".toJson)
object InputParameter extends DefaultJsonProtocol {
implicit val format: JsonFormat[InputParameter] = jsonFormat2(InputParameter.apply)
Expand Down Expand Up @@ -234,5 +244,8 @@ object InputParameter extends DefaultJsonProtocol {
case `AWS::RDS::DBInstance::Engine_Parameter`(n, _, _, Some(d)) => InputParameter(n, d.toJson)
case `AWS::RDS::DBInstance::Engine_Parameter`(n, _, Some(d), None) => InputParameter(n, d.toJson)
case `AWS::RDS::DBInstance::Engine_Parameter`(n, _, None, None) => InputParameter(n)
case `AWS::RDS::DBSubnetGroup_Parameter`(n, None, _, _) => InputParameter(n)
case `AWS::RDS::DBSubnetGroup_Parameter`(n, Some(d), None, _) => InputParameter(n, d.toJson)
case `AWS::RDS::DBSubnetGroup_Parameter`(n, _, Some(d), _) => InputParameter(n, d.toJson)
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ case class `AWS::RDS::DBInstance` private[resource] (
DBParameterGroupName: Option[ResourceRef[`AWS::RDS::DBParameterGroup`]],
DBSecurityGroups: Option[Seq[ResourceRef[`AWS::RDS::DBSecurityGroup`]]],
DBSnapshotIdentifier: Option[String],
DBSubnetGroupName: Option[ResourceRef[`AWS::RDS::DBSubnetGroup`]],
DBSubnetGroupName: Option[Token[ResourceRef[`AWS::RDS::DBSubnetGroup`]]],
Engine: Option[Token[`AWS::RDS::DBInstance::Engine`]],
EngineVersion: Option[String],
Iops: Option[Either[Int, Token[Int]]],
Expand Down Expand Up @@ -141,7 +141,7 @@ sealed trait RdsLocation {
* @param vpcSecurityGroups AWS::RDS::DBInstance(VPCSecurityGroups)
*/
case class RdsVpc(
dbSubnetGroupName: ResourceRef[`AWS::RDS::DBSubnetGroup`],
dbSubnetGroupName: Token[ResourceRef[`AWS::RDS::DBSubnetGroup`]],
vpcSecurityGroups: Option[Seq[ResourceRef[`AWS::EC2::SecurityGroup`]]] = None
) extends RdsLocation {
private[resource] def location(rdsInstance: `AWS::RDS::DBInstance`): `AWS::RDS::DBInstance` =
Expand All @@ -150,6 +150,7 @@ case class RdsVpc(
DBSubnetGroupName = Some(dbSubnetGroupName),
VPCSecurityGroups = vpcSecurityGroups
)

}

/** RDS instance not in a VPC, a "classic".
Expand Down Expand Up @@ -557,7 +558,7 @@ case class `AWS::RDS::DBSecurityGroup`(
name: String,
DBSecurityGroupIngress: Seq[RDSDBSecurityGroupRule],
GroupDescription: String,
EC2VpcId: Option[ResourceRef[`AWS::EC2::VPC`]] = None,
EC2VpcId: Option[Token[ResourceRef[`AWS::EC2::VPC`]]] = None,
Tags: Option[Seq[AmazonTag]] = None,
override val Condition: Option[ConditionRef] = None
) extends Resource[`AWS::RDS::DBSecurityGroup`]{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,60 @@ class RDS_UT extends FunSpec with Matchers {
Seq[Resource[_]](rdsInstance).toJson should be (expected)
}

it("should create a valid new RDS database instance when DBSubnetGroupName is passed in as a parameter") {
val dbSubnetGroupParamName = "dbSubnetGroup"
val dbSubnetGroupParam = `AWS::RDS::DBSubnetGroup_Parameter`(dbSubnetGroupParamName , "Subnet group where RDS instances are created.")
val rdsInstanceWithSubnetParam = rdsInstance.copy(DBSubnetGroupName = Some(ParameterRef(dbSubnetGroupParam)))
val expectedInstanceJson = JsObject(
"TestRds" -> JsObject(
"Type" -> JsString("AWS::RDS::DBInstance"),
"Properties" -> JsObject(
"AllocatedStorage" -> JsNumber(storage),
"DBInstanceClass" -> JsString(instanceClass),
"Engine" -> JsString("postgres"),
"MasterUsername" -> JsString(username),
"MasterUserPassword" -> JsString(pw),
"MultiAZ" -> JsBoolean(true),
"StorageEncrypted" -> JsBoolean(true),
"StorageType" -> JsString("standard"),
"DBSubnetGroupName" -> JsObject("Ref" -> JsString(dbSubnetGroupParamName))
)
)
)
Seq[Resource[_]](rdsInstanceWithSubnetParam).toJson should be (expectedInstanceJson)
}

it("should create a valid new RDS database instance when EC2VpcId is passed in as a parameter") {
val vpcParamName = "vpc"
val vpcParam = `AWS::EC2::VPC_Parameter`(vpcParamName, "VPC to create security groups")
val secGroupName = "SecurityGroup"
val secGroup = `AWS::RDS::DBSecurityGroup`(
name = secGroupName,
DBSecurityGroupIngress = Seq(),
GroupDescription = "Not a real group",
EC2VpcId = Some(ParameterRef(vpcParam))
)
val rdsInstanceWithVpcParam = rdsInstance.copy(DBSecurityGroups = Seq(ResourceRef(secGroup)))
val expectedInstanceJson = JsObject(
"TestRds" -> JsObject(
"Type" -> JsString("AWS::RDS::DBInstance"),
"Properties" -> JsObject(
"AllocatedStorage" -> JsNumber(storage),
"DBInstanceClass" -> JsString(instanceClass),
"Engine" -> JsString("postgres"),
"MasterUsername" -> JsString(username),
"MasterUserPassword" -> JsString(pw),
"MultiAZ" -> JsBoolean(true),
"DBSecurityGroups"-> JsArray(JsObject("Ref" -> JsString(secGroupName))),
"StorageEncrypted" -> JsBoolean(true),
"StorageType" -> JsString("standard"),
"DBSubnetGroupName" -> JsObject("Ref" -> JsString(subnetGroupName))
)
)
)
Seq[Resource[_]](rdsInstanceWithVpcParam).toJson should be (expectedInstanceJson)
}

it("should create a valid pIOPS RDS database instance from snapshot") {
val secGroupName = "SecurityGroup"
val secGroup = `AWS::RDS::DBSecurityGroup`(
Expand Down Expand Up @@ -249,4 +303,76 @@ class RDS_UT extends FunSpec with Matchers {
Seq[Resource[_]](rdsInstance).toJson should be (expected)
}
}

describe("AWS::RDS::DBSubnetGroup_Parameter") {
it("should serialize into valid json") {
val dbSubnetGroupParam = `AWS::RDS::DBSubnetGroup_Parameter`("dbSubnetGroup" , "Subnet group where RDS instances are created.", "defaultSubnetGroupId")
val expectedJson = JsObject(
"dbSubnetGroup" -> JsObject(
"Description" -> JsString("Subnet group where RDS instances are created."),
"Type" -> JsString("String"),
"Default" -> JsString("defaultSubnetGroupId")
)
)
Seq[Parameter](dbSubnetGroupParam).toJson should be (expectedJson)
}

it("should serialize into valid json as InputParameter") {
val dbSubnetGroupParam = `AWS::RDS::DBSubnetGroup_Parameter`("dbSubnetGroup" , "Subnet group where RDS instances are created.", "defaultSubnetGroupId")
val expectedJson = JsObject(
"ParameterKey" -> JsString("dbSubnetGroup"),
"ParameterValue" -> JsString("defaultSubnetGroupId")
)
val inputParam = InputParameter.templateParameterToInputParameter(Some(Seq(dbSubnetGroupParam)))
inputParam.get(0).toJson should be (expectedJson)
}
}

describe("AWS::RDS::DBSecurityGroup") {
it("should serialize into valid json") {
val vpcParamName = "vpc"
val vpcParam = `AWS::EC2::VPC_Parameter`(vpcParamName, "VPC to create security groups")
val secGroupName = "SecurityGroup"
val secGroup = `AWS::RDS::DBSecurityGroup`(
name = secGroupName,
DBSecurityGroupIngress = Seq(),
GroupDescription = "Not a real group",
EC2VpcId = Some(ParameterRef(vpcParam))
)
val expectedJson = JsObject(
"SecurityGroup" -> JsObject(
"Properties" -> JsObject(
"DBSecurityGroupIngress" -> JsArray(),
"GroupDescription" -> JsString("Not a real group"),
"EC2VpcId" -> JsObject("Ref" -> JsString(vpcParamName))
),
"Type" -> JsString("AWS::RDS::DBSecurityGroup")
)
)
Seq[Resource[_]](secGroup).toJson should be (expectedJson)
}
}

describe("AWS::EC2::VPC_Parameter") {
it("should serialize into valid json") {
val vpcParam = `AWS::EC2::VPC_Parameter`("vpc", "VPC to create security groups", None)
val expectedJson = JsObject(
"vpc" -> JsObject(
"Description" -> JsString("VPC to create security groups"),
"Type" -> JsString("AWS::EC2::VPC::Id")
)
)
Seq[Parameter](vpcParam).toJson should be (expectedJson)
}

it("should serialize into valid json as InputParameter") {
val vpcParam = `AWS::EC2::VPC_Parameter`("vpc", "VPC to create security groups", None, "defaultVpcId")
val expectedJson = JsObject(
"ParameterKey" -> JsString("vpc"),
"ParameterValue" -> JsString("defaultVpcId")
)
val inputParam = InputParameter.templateParameterToInputParameter(Some(Seq(vpcParam)))
inputParam.get(0).toJson should be (expectedJson)
}
}
}

0 comments on commit 25be4bb

Please sign in to comment.