8
8
from models import *
9
9
10
10
class CloudFormer :
11
- def __init__ (self , access_key , secret_key , vpc_id , region_name = 'us-east-1' ):
11
+ def __init__ (self , access_key , secret_key , region_name = 'us-east-1' ):
12
12
self .access_key = access_key if access_key is not None else ''
13
13
self .secret_key = secret_key if secret_key is not None else ''
14
14
self .region = boto .ec2 .get_region (
15
15
region_name ,
16
16
aws_access_key_id = self .access_key ,
17
17
aws_secret_access_key = self .secret_key
18
18
)
19
- self .vpc_id = vpc_id
20
19
21
20
def form (self ):
22
21
self .vpcconn = boto .connect_vpc (
@@ -25,62 +24,63 @@ def form(self):
25
24
aws_secret_access_key = self .secret_key
26
25
)
27
26
context = {}
28
- self ._form_vpc (context )
29
- self ._form_internet_gateway (context )
30
- self ._form_gateway_attachments (context )
31
- self ._form_subnets (context )
32
- self ._form_instances (context )
33
- self ._form_route_tables (context )
34
- self ._form_route (context )
35
- self ._form_subnet_route_table_association (context )
27
+ vpcs = self ._form_vpc (context )
28
+ for vpc in vpcs :
29
+ internet_gateways = self ._form_internet_gateway (context , vpc )
30
+ subnets = self ._form_subnets (context , vpc )
31
+ instances = self ._form_instances (context , vpc , subnets )
32
+ route_tables = self ._form_route_tables (context , vpc )
33
+ self ._form_gateway_attachments (context , vpc , internet_gateways )
34
+ self ._form_route (context , route_tables , internet_gateways , instances )
35
+ self ._form_subnet_route_table_association (context , route_tables , subnets )
36
36
return context
37
37
38
38
def _form_vpc (self , context ):
39
- vpcs = self .vpcconn .get_all_vpcs (
40
- filters = [('vpc-id' , self .vpc_id )]
41
- )
42
- context ['vpc' ] = CfnVpc (vpcs [0 ])
39
+ vpcs = [CfnVpc (vpc ) for vpc in self .vpcconn .get_all_vpcs ()]
40
+ context ['vpcs' ] = vpcs
41
+ return vpcs
43
42
44
- def _form_internet_gateway (self , context ):
45
- context [ ' internet_gateways' ] = [
43
+ def _form_internet_gateway (self , context , vpc ):
44
+ internet_gateways = [
46
45
CfnInternetGateWay (igw )
47
46
for igw in self .vpcconn .get_all_internet_gateways (
48
- filters = [('attachment.vpc-id' ,self . vpc_id )]
47
+ filters = [('attachment.vpc-id' , vpc . id )]
49
48
)
50
49
]
50
+ context ['internet_gateways' ] = internet_gateways
51
+ return internet_gateways
51
52
52
- def _form_gateway_attachments (self , context ):
53
- vpc = context ['vpc' ]
54
- internet_gateways = context ['internet_gateways' ] if 'internet_gateways' in context else []
53
+ def _form_gateway_attachments (self , context , vpc , internet_gateways ):
55
54
attachments = []
56
55
for internet_gateway in internet_gateways :
57
56
attachments .extend ([
58
57
CfnVpcGatewayAttachment (att , cfn_vpc = vpc , cfn_gateway = internet_gateway )
59
58
for att in internet_gateway .attachments
60
59
])
61
60
context ['gateway_attachments' ] = attachments
61
+ return attachments
62
62
63
- def _form_subnets (self , context ):
64
- vpc = context ['vpc' ]
65
- context ['subnets' ] = [
63
+ def _form_subnets (self , context , vpc ):
64
+ subnets = [
66
65
CfnSubnet (s , vpc )
67
66
for s in self .vpcconn .get_all_subnets (
68
67
filters = [('vpc-id' , vpc .id )]
69
68
)
70
69
]
70
+ context ['subnets' ] = subnets
71
+ return subnets
71
72
72
- def _form_route_tables (self , context ):
73
- vpc = context ['vpc' ]
74
- context ['route_tables' ] = [
73
+ def _form_route_tables (self , context , vpc ):
74
+ route_tables = [
75
75
CfnRouteTable (rtb , cfn_vpc = vpc )
76
76
for rtb in self .vpcconn .get_all_route_tables (
77
77
filters = [('vpc-id' , vpc .id )]
78
78
)
79
79
]
80
+ context ['route_tables' ] = route_tables
81
+ return route_tables
80
82
81
- def _form_instances (self , context ):
82
- vpc = context ['vpc' ]
83
- subnets = context ['subnets' ] if 'subnets' in context else []
83
+ def _form_instances (self , context , vpc , subnets ):
84
84
instances = []
85
85
for reservation in self .vpcconn .get_all_instances (filters = {'vpc-id' : vpc .id }):
86
86
for instance in reservation .instances :
@@ -90,15 +90,12 @@ def _form_instances(self, context):
90
90
if subnet .id == instance .subnet_id
91
91
])
92
92
context ['instances' ] = instances
93
+ return instances
93
94
94
- def _form_route (self , context ):
95
- route_tables = context ['route_tables' ] if 'route_tables' in context else []
96
- gateways = context ['internet_gateways' ] if 'internet_gateways' in context else []
97
- instances = context ['instances' ] if 'instances' in context else []
98
- network_interfaces = context ['network_interfaces' ] if 'network_interfaces' in context else []
95
+ def _form_route (self , context , route_tables = [], internet_gateways = [], instances = [], network_interfaces = []):
99
96
routes = []
100
97
for route_table in route_tables :
101
- for gateway in gateways :
98
+ for gateway in internet_gateways :
102
99
routes .extend ([
103
100
CfnRoute (route , cfn_route_table = route_table , cfn_gateway = gateway )
104
101
for route in route_table .routes
@@ -117,10 +114,9 @@ def _form_route(self, context):
117
114
if network_interfaces .id == route .network_interface_id
118
115
])
119
116
context ['routes' ] = routes
117
+ return routes
120
118
121
- def _form_subnet_route_table_association (self , context ):
122
- route_tables = context ['route_tables' ]
123
- subnets = context ['subnets' ]
119
+ def _form_subnet_route_table_association (self , context , route_tables , subnets ):
124
120
associations = []
125
121
for subnet in subnets :
126
122
for route_table in route_tables :
@@ -130,4 +126,4 @@ def _form_subnet_route_table_association(self, context):
130
126
if assoc .subnet_id == subnet .id
131
127
])
132
128
context ['subnet_route_table_associations' ] = associations
133
-
129
+ return associations
0 commit comments