Allow usage of predefined VPC

Some organisations already have predefined VPC. Allow
to use them instead of creating new one.

Feature: Issue 13142
Change-Id: I965f0967a2e2769d4f1f20dbe6c96d05b9c851e1
diff --git a/Configuration.md b/Configuration.md
index 4b12526..9003d14 100644
--- a/Configuration.md
+++ b/Configuration.md
@@ -41,6 +41,8 @@
   for more details. `12g` by default.
 * `INTERNET_GATEWAY_ID`: Optional. Id of the existing Internet Gateway.
   If not set, create a new Internet Gateway
+* `VPC_ID`: Optional. Id of the existing VPC.
+  If not set, create a new VPC.
 
 #### LDAP
 
diff --git a/common-templates/cf-gerrit-network-stack.yml b/common-templates/cf-gerrit-network-stack.yml
index 2812ca1..f856091 100644
--- a/common-templates/cf-gerrit-network-stack.yml
+++ b/common-templates/cf-gerrit-network-stack.yml
@@ -6,7 +6,10 @@
     Type: String
     Default: ""
     Description: Internet Gateway id. If empty Internet Gateway will be created
-
+  VPCIdProp:
+    Type: String
+    Default: ""
+    Description: VPC id. If empty VPC will be created
 Mappings:
   # Hard values for the subnet masks. These masks define
   # the range of internal IP addresses that can be assigned.
@@ -22,8 +25,10 @@
 
 Conditions:
   CreateInternetGateway: !Equals [!Ref InternetGatewayIdProp, ""]
+  CreateVPC: !Equals [!Ref VPCIdProp, ""]
 Resources:
   VPC:
+    Condition: CreateVPC
     Type: AWS::EC2::VPC
     Properties:
       EnableDnsSupport: true
@@ -38,7 +43,7 @@
          Fn::Select:
          - 0
          - Fn::GetAZs: {Ref: 'AWS::Region'}
-      VpcId: !Ref 'VPC'
+      VpcId: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ]
       CidrBlock: !FindInMap ['SubnetConfig', 'PublicOne', 'CIDR']
       MapPublicIpOnLaunch: true
 
@@ -51,12 +56,12 @@
   GatewayAttachement:
     Type: AWS::EC2::VPCGatewayAttachment
     Properties:
-      VpcId: !Ref 'VPC'
+      VpcId: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ]
       InternetGatewayId: !If [CreateInternetGateway, !Ref 'InternetGateway', !Ref 'InternetGatewayIdProp' ]
   PublicRouteTable:
     Type: AWS::EC2::RouteTable
     Properties:
-      VpcId: !Ref 'VPC'
+      VpcId: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ]
   PublicRoute:
     Type: AWS::EC2::Route
     DependsOn: GatewayAttachement
@@ -71,7 +76,7 @@
       RouteTableId: !Ref PublicRouteTable
 Outputs:
   VPCRef:
-    Value: !Ref VPC
+    Value: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ]
   PublicSubnetOneRef:
     Value: !Ref PublicSubnetOne
   PublicOneCIDR:
diff --git a/dual-master/Makefile b/dual-master/Makefile
index 2657b13..007158d 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -39,6 +39,7 @@
 		ParameterKey=DesiredCapacity,ParameterValue=$(CLUSTER_DESIRED_CAPACITY) \
 		ParameterKey=ECSKeyName,ParameterValue=$(CLUSTER_KEYS) \
 		ParameterKey=InternetGatewayIdProp,ParameterValue=$(INTERNET_GATEWAY_ID) \
+		ParameterKey=VPCIdProp,ParameterValue=$(VPC_ID) \
 		$(OPTIONAL_PARAMS)
 
 service-master-1:
diff --git a/dual-master/cf-cluster.yml b/dual-master/cf-cluster.yml
index 7de16c1..ccc78ee 100644
--- a/dual-master/cf-cluster.yml
+++ b/dual-master/cf-cluster.yml
@@ -29,6 +29,10 @@
     Type: String
     Default: ""
     Description: Internet Gateway id. If empty Internet Gateway will be created
+  VPCIdProp:
+    Type: String
+    Default: ""
+    Description: VPC id. If empty VPC will be created
   ECSKeyName:
     Type: String
     Default: gerrit-cluster-keys
@@ -256,6 +260,7 @@
       TimeoutInMinutes: '25'
       Parameters:
         InternetGatewayIdProp: !Ref 'InternetGatewayIdProp'
+        VPCIdProp: !Ref 'VPCIdProp'
 
 Outputs:
   ClusterName:
diff --git a/master-slave/Makefile b/master-slave/Makefile
index 956282a..9b6b70c 100644
--- a/master-slave/Makefile
+++ b/master-slave/Makefile
@@ -36,6 +36,7 @@
 		ParameterKey=DesiredCapacity,ParameterValue=$(CLUSTER_DESIRED_CAPACITY) \
 		ParameterKey=ECSKeyName,ParameterValue=$(CLUSTER_KEYS) \
 		ParameterKey=InternetGatewayIdProp,ParameterValue=$(INTERNET_GATEWAY_ID) \
+		ParameterKey=VPCIdProp,ParameterValue=$(VPC_ID) \
 		$(OPTIONAL_PARAMS)
 
 service-master:
diff --git a/master-slave/cf-cluster.yml b/master-slave/cf-cluster.yml
index 1b23226..d3a24fd 100644
--- a/master-slave/cf-cluster.yml
+++ b/master-slave/cf-cluster.yml
@@ -29,6 +29,10 @@
     Type: String
     Default: ""
     Description: Internet Gateway id. If empty Internet Gateway will be created
+  VPCIdProp:
+    Type: String
+    Default: ""
+    Description: VPC id. If empty VPC will be created
   ECSKeyName:
     Type: String
     Default: gerrit-cluster-keys
@@ -200,6 +204,7 @@
       TimeoutInMinutes: '25'
       Parameters:
         InternetGatewayIdProp: !Ref 'InternetGatewayIdProp'
+        VPCIdProp: !Ref 'VPCIdProp'
 
 Outputs:
   ClusterName:
diff --git a/single-master/Makefile b/single-master/Makefile
index ef36e8c..95a1f98 100644
--- a/single-master/Makefile
+++ b/single-master/Makefile
@@ -32,6 +32,7 @@
 		--parameters \
 		ParameterKey=ECSKeyName,ParameterValue=$(CLUSTER_KEYS) \
 		ParameterKey=InternetGatewayIdProp,ParameterValue=$(INTERNET_GATEWAY_ID) \
+		ParameterKey=VPCIdProp,ParameterValue=$(VPC_ID) \
 		$(OPTIONAL_PARAMS)
 
 service:
diff --git a/single-master/cf-cluster.yml b/single-master/cf-cluster.yml
index 203346b..4072f04 100644
--- a/single-master/cf-cluster.yml
+++ b/single-master/cf-cluster.yml
@@ -29,6 +29,10 @@
     Type: String
     Default: ""
     Description: Internet Gateway id. If empty Internet Gateway will be created
+  VPCIdProp:
+    Type: String
+    Default: ""
+    Description: VPC id. If empty VPC will be created
   ECSKeyName:
     Type: String
     Default: gerrit-cluster-keys
@@ -176,6 +180,7 @@
       TimeoutInMinutes: '25'
       Parameters:
         InternetGatewayIdProp: !Ref 'InternetGatewayIdProp'
+        VPCIdProp: !Ref 'VPCIdProp'
 
 Outputs:
   ClusterName: