Skip to content

Commit

Permalink
Merge pull request #30 from MonsantoCo/remove-dupes
Browse files Browse the repository at this point in the history
Remove dupes
  • Loading branch information
ddgenome committed Oct 16, 2015
2 parents 14a156c + 2563ba9 commit c855069
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ case class Template(
if(Resources.isEmpty) throw new RuntimeException("You cannot lookup in a None map")
val candidates = Resources.get.filter{r => r.name == name}

if(candidates.length == 0) throw new RuntimeException(s"Resource name $name not found in template: ${this.Description}")
if(candidates.isEmpty) throw new RuntimeException(s"Resource name $name not found in template: ${this.Description}")
if(candidates.length > 1) throw new RuntimeException(s"Name $name is not unique")

candidates.head.asInstanceOf[R]
Expand All @@ -65,7 +65,7 @@ case class Template(
if(Resources.isEmpty) throw new RuntimeException("You cannot lookup in a None map")
val candidates = Routables.get.filter{r => r.resource.name == name}

if(candidates.length == 0) throw new RuntimeException(s"Resource name $name not found in template: ${this.Description}")
if(candidates.isEmpty) throw new RuntimeException(s"Resource name $name not found in template: ${this.Description}")
if(candidates.length > 1) throw new RuntimeException(s"Name $name is not unique")

candidates.head.asInstanceOf[SecurityGroupRoutable[R]]
Expand All @@ -90,7 +90,11 @@ object Template extends DefaultJsonProtocol {

val EMPTY = Template("", None, None, None, None, None, None)

def collapse[R <: Resource[R]](rs: Seq[R]) = rs.foldLeft(Template.EMPTY)(_ ++ _)
def collapse[R <: Resource[R]](rs: Seq[R]) = {
val dupes = rs.groupBy(_.name).collect{case(y,xs) if xs.size>1 => y}
if (dupes.nonEmpty) throw new IllegalArgumentException(s"Multiple resources with the same name would clobber each other. Found duplicates of $dupes")
rs.foldLeft(Template.EMPTY)(_ ++ _)
}

// b/c we really dont need to implement READING yet, and its a bit trickier
implicit def optionWriter[T : JsonWriter]: JsonWriter[Option[T]] = new JsonWriter[Option[T]] {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.monsanto.arch.cloudformation.model

import com.monsanto.arch.cloudformation.model.resource.{CidrBlock, AmazonTag, `AWS::EC2::VPC`}
import org.scalatest.{Matchers, FlatSpec}

class Template_UT extends FlatSpec with Matchers{
it should "throw an exception if some resources have the same name" in {

val thrown = the[IllegalArgumentException] thrownBy Template.collapse(Seq(
`AWS::EC2::VPC`("repeat", CidrBlock(10, 10, 10, 10, 16), Seq(AmazonTag("a", "B"))),
`AWS::EC2::VPC`("repeat", CidrBlock(10, 10, 10, 12, 16), Seq(AmazonTag("c", "C")))
))
thrown.getMessage should include("repeat")
}
}

0 comments on commit c855069

Please sign in to comment.