Skip to content

Commit

Permalink
feat(clouddriver/aws): Allow finding SG vpc IDs by name
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert committed Nov 13, 2017
1 parent 436b3c0 commit 5d03afe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ interface MortService {
String account

static VPC findForRegionAndAccount(Collection<MortService.VPC> allVPCs,
String sourceVpcId,
String sourceVpcIdOrName,
String region,
String account) {
def sourceVpc = allVPCs.find { it.id == sourceVpcId }
def sourceVpc = allVPCs.find { it.id == sourceVpcIdOrName || (it.name.equalsIgnoreCase(sourceVpcIdOrName) && it.region == region && it.account == account) }
def targetVpc = allVPCs.find { it.name == sourceVpc?.name && it.region == region && it.account == account }

if (!targetVpc) {
throw new IllegalStateException("No matching VPC found (vpcId: ${sourceVpcId}, region: ${region}, account: ${account}")
throw new IllegalStateException("No matching VPC found (vpcId: ${sourceVpcIdOrName}, region: ${region}, account: ${account}")
}

return targetVpc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

package com.netflix.spinnaker.orca.clouddriver

import com.netflix.spinnaker.orca.clouddriver.MortService
import spock.lang.Specification
import spock.lang.Unroll

import static com.netflix.spinnaker.orca.clouddriver.MortService.VPC.*
import static com.netflix.spinnaker.orca.clouddriver.MortService.SecurityGroup.*
import static com.netflix.spinnaker.orca.clouddriver.MortService.SecurityGroup.SecurityGroupIngress
import static com.netflix.spinnaker.orca.clouddriver.MortService.SecurityGroup.applyMappings
import static com.netflix.spinnaker.orca.clouddriver.MortService.SecurityGroup.filterForSecurityGroupIngress
import static com.netflix.spinnaker.orca.clouddriver.MortService.SecurityGroup.findById
import static com.netflix.spinnaker.orca.clouddriver.MortService.VPC.findForRegionAndAccount

class MortServiceSpec extends Specification {
void "should extract only the security group ingress rules from SecurityGroup"() {
Expand Down Expand Up @@ -98,6 +100,20 @@ class MortServiceSpec extends Specification {
findForRegionAndAccount(allVPCs, "vpc1-0", "us-east-1", "test") == allVPCs[1]
}

void "should find VPC for region and account given name"() {
given:
def allVPCs = [
new MortService.VPC(id: "vpc1-0", name: "vpc1", region: "us-west-1", account: "test"),
new MortService.VPC(id: "vpc1-1", name: "vpc1", region: "us-east-1", account: "test"),
]

expect:
findForRegionAndAccount(allVPCs, "vpc1", "us-west-1", "test") == allVPCs[0]
findForRegionAndAccount(allVPCs, "vpc1", "us-east-1", "test") == allVPCs[1]
findForRegionAndAccount(allVPCs, "VPC1", "us-east-1", "test") == allVPCs[1]
findForRegionAndAccount(allVPCs, "vpc2", "us-west-1", "test") == null
}

@Unroll
void "should throw exception if VPC cannot be found"() {
given:
Expand Down

0 comments on commit 5d03afe

Please sign in to comment.