Skip to content

Commit

Permalink
Merge pull request #84 from aniketbhatnagar/issue#81-DBSubnetGroup-Su…
Browse files Browse the repository at this point in the history
…bnetIds-As-ParameterRef

Subnet list can now be passed as ParameterRef to DBSubnetGroup
  • Loading branch information
T.J. Corrigan committed Apr 8, 2016
2 parents d559c25 + 38e78b8 commit b611075
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ object `Fn::Base64` extends DefaultJsonProtocol {
// but you also want to be able to pass a literal ResourceRef[R]
sealed trait Token[R]
object Token extends DefaultJsonProtocol {
implicit def fromAnySeq[R: JsonFormat](r: Seq[R]): AnyToken[Seq[R]] = AnyToken(r)
implicit def fromAny[R: JsonFormat](r: R): AnyToken[R] = AnyToken(r)
implicit def fromOptionAny[R: JsonFormat](or: Option[R]): Option[AnyToken[R]] = or.map(r => Token.fromAny(r))
implicit def fromString(s: String): StringToken = StringToken(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object Parameter extends DefaultJsonProtocol {
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
case s: `AWS::EC2::Subnet_Parameter_List` => s.toJson
}

JsObject( raw.asJsObject.fields - "name" - "ConfigDefault" + ("Type" -> JsString(obj.Type)) )
Expand Down Expand Up @@ -206,6 +207,15 @@ object `AWS::RDS::DBSubnetGroup_Parameter` extends DefaultJsonProtocol {
implicit val format: JsonFormat[`AWS::RDS::DBSubnetGroup_Parameter`] = jsonFormat4(`AWS::RDS::DBSubnetGroup_Parameter`.apply)
}

case class `AWS::EC2::Subnet_Parameter_List`(
name: String,
Description: Option[String],
ConfigDefault: Option[String] = None
) extends Parameter("List<AWS::EC2::Subnet::Id>"){type Rep = Seq[ResourceRef[`AWS::EC2::Subnet`]]}
object `AWS::EC2::Subnet_Parameter_List` extends DefaultJsonProtocol {
implicit val format: JsonFormat[`AWS::EC2::Subnet_Parameter_List`] = jsonFormat3(`AWS::EC2::Subnet_Parameter_List`.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 @@ -247,5 +257,6 @@ object InputParameter extends DefaultJsonProtocol {
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)
case `AWS::EC2::Subnet_Parameter_List`(n, _, _) => InputParameter(n, "".toJson)
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ object RDSDBSecurityGroupRule extends DefaultJsonProtocol {
case class `AWS::RDS::DBSubnetGroup`(
name: String,
DBSubnetGroupDescription: String,
SubnetIds: Seq[ResourceRef[`AWS::EC2::Subnet`]],
SubnetIds: Token[Seq[ResourceRef[`AWS::EC2::Subnet`]]],
Tags: Option[Seq[AmazonTag]] = None,
override val Condition: Option[ConditionRef] = None
) extends Resource[`AWS::RDS::DBSubnetGroup`]{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.monsanto.arch.cloudformation.model.resource

import com.monsanto.arch.cloudformation.model._
import org.scalatest.{FunSpec, Matchers}
import spray.json.{JsNumber, JsString, _}

class Subnet_Parameter_List_UT extends FunSpec with Matchers {
describe("AWS::EC2::Subnet_Parameter_List") {

it("should serialize into valid json") {
val subnetListParam = `AWS::EC2::Subnet_Parameter_List`("subnets", "Select subnets where the RDS instances should be created")
val expectedJson = JsObject(
"subnets" -> JsObject(
"Description" -> JsString("Select subnets where the RDS instances should be created"),
"Type" -> JsString("List<AWS::EC2::Subnet::Id>")
)
)
Seq[Parameter](subnetListParam).toJson should be (expectedJson)
}

it("should serialize into valid json as InputParameter") {
val subnetListParam = `AWS::EC2::Subnet_Parameter_List`("subnets", "Select subnets where the RDS instances should be created")
val expectedJson = JsObject(
"ParameterKey" -> JsString("subnets"),
"ParameterValue" -> JsString("")
)
val inputParam = InputParameter.templateParameterToInputParameter(Some(Seq(subnetListParam)))
inputParam.get(0).toJson should be (expectedJson)
}

it("can be passed as ParameterRef to AWS::RDS::DBSubnetGroup") {
val subnetListParam = `AWS::EC2::Subnet_Parameter_List`("subnets", "Select subnets where the RDS instances should be created")
val dbSubnetGroup = `AWS::RDS::DBSubnetGroup`(
name = "dbSubnetGroup",
DBSubnetGroupDescription = "DB subnet group",
SubnetIds = ParameterRef(subnetListParam)
)
val expected = JsObject(
"dbSubnetGroup" -> JsObject(
"Type" -> JsString("AWS::RDS::DBSubnetGroup"),
"Properties" -> JsObject(
"DBSubnetGroupDescription" -> JsString("DB subnet group"),
"SubnetIds" -> JsObject("Ref" -> JsString("subnets"))
)
)
)
Seq[Resource[_]](dbSubnetGroup).toJson should be (expected)
}
}
}

0 comments on commit b611075

Please sign in to comment.