Repository: zalando-stups/senza

Issue:
Support multiple VPCs
Senza's STUPS components currently assume a single VPC per region (Seven Seconds only configures a single VPC). There might be valid reasons to have multiple VPCs (e.g. a special VPC with VPN tunnel to some 3rd party location), so Senza needs to support that, too.

Discussion:

Patch:
diff --git a/senza/components/stups_auto_configuration.py b/senza/components/stups_auto_configuration.py
index 5969330..415e380 100644
--- a/senza/components/stups_auto_configuration.py
+++ b/senza/components/stups_auto_configuration.py
@@ -28,12 +28,13 @@ def find_taupage_image(region: str):
 def component_stups_auto_configuration(definition, configuration, args, info, force, account_info):
     ec2 = boto3.resource('ec2', args.region)
 
+    vpc_id = configuration.get('VpcId', account_info.VpcID)
     availability_zones = configuration.get('AvailabilityZones')
 
     server_subnets = []
     lb_subnets = []
     lb_internal_subnets = []
-    for subnet in ec2.subnets.filter(Filters=[{'Name': 'vpc-id', 'Values': [account_info.VpcID]}]):
+    for subnet in ec2.subnets.filter(Filters=[{'Name': 'vpc-id', 'Values': [vpc_id]}]):
         name = get_tag(subnet.tags, 'Name', '')
         if availability_zones and subnet.availability_zone not in availability_zones:
             # skip subnet as it's not in one of the given AZs


Test patch:
diff --git a/tests/test_components.py b/tests/test_components.py
index 1191bae..eceb9da 100644
--- a/tests/test_components.py
+++ b/tests/test_components.py
@@ -220,6 +220,42 @@ def test_component_stups_auto_configuration(monkeypatch):
     assert {'myregion': {'Subnets': ['sn-3']}} == result['Mappings']['ServerSubnets']
 
 
+def test_component_stups_auto_configuration_vpc_id(monkeypatch):
+    args = MagicMock()
+    args.region = 'myregion'
+
+    configuration = {
+        'Name': 'Config',
+        'VpcId': 'vpc-123'
+    }
+
+    sn1 = MagicMock()
+    sn1.id = 'sn-1'
+    sn1.tags = [{'Key': 'Name', 'Value': 'dmz-1'}]
+    sn1.availability_zone = 'az-1'
+    sn2 = MagicMock()
+    sn2.id = 'sn-2'
+    sn2.tags = [{'Key': 'Name', 'Value': 'dmz-2'}]
+    sn2.availability_zone = 'az-2'
+    sn3 = MagicMock()
+    sn3.id = 'sn-3'
+    sn3.tags = [{'Key': 'Name', 'Value': 'internal-3'}]
+    sn3.availability_zone = 'az-1'
+    ec2 = MagicMock()
+    def get_subnets(Filters):
+        assert Filters == [{'Name': 'vpc-id', 'Values': ['vpc-123']}]
+        return [sn1, sn2, sn3]
+    ec2.subnets.filter = get_subnets
+    image = MagicMock()
+    ec2.images.filter.return_value = [image]
+    monkeypatch.setattr('boto3.resource', lambda x, y: ec2)
+
+    result = component_stups_auto_configuration({}, configuration, args, MagicMock(), False, MagicMock())
+
+    assert {'myregion': {'Subnets': ['sn-1', 'sn-2']}} == result['Mappings']['LoadBalancerSubnets']
+    assert {'myregion': {'Subnets': ['sn-3']}} == result['Mappings']['ServerSubnets']
+
+
 def test_component_redis_node(monkeypatch):
     mock_string = "foo"