-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_models.py
509 lines (464 loc) · 15.4 KB
/
tf_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
from datetime import datetime
def remove_none(d):
"""
Delete keys with the value ``None`` in a dictionary, recursively.
This alters the input so you may wish to ``copy`` the dict first.
:return: dict
"""
for key, value in d.items():
if value is None:
del d[key]
elif isinstance(value, dict):
remove_none(value)
return d
def time_now():
"""
:return: str of time now like -20160906-1224
"""
return datetime.strftime(datetime.now(), '-%Y%m%d-%H%M')
class Terraform:
@staticmethod
def set_provider(region):
"""
:param region: str
"""
return 'aws.' + region
@staticmethod
def set_id(resource, name):
"""
:param resource: str
:param name: str
"""
return '${' + resource + '.' + name + '.id}'
@classmethod
def aws_instance(
cls,
name=None,
region=None,
ami=None,
instance_type='m3.medium',
subnet_id=None,
key_name=None, # the keypair name
tags=None,
vpc_security_group_ids=None,
associate_public_ip_address=None,
source_dest_check=None):
"""
:param name: str
:param region: str
:param ami: str
:param instance_type: str
:param subnet_id: str
:param key_name: str
:param tags: dict
:param vpc_security_group_ids: list
:param associate_public_ip_address: bool
:param source_dest_check: bool
:return: Terraform
"""
tags.update({'Name': name}) if tags else tags
resource = 'aws_instance'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, ami=ami, subnet_id=subnet_id,
key_name=key_name, instance_type=instance_type,
vpc_security_group_ids=vpc_security_group_ids, tags=tags,
associate_public_ip_address=associate_public_ip_address,
source_dest_check=source_dest_check)}))
@classmethod
def aws_ami_from_instance(
cls,
name,
region,
source_instance_id,
tags=None):
"""
:param name: str
:param region: str
:param source_instance_id: str
:param tags: dict
:return: Terraform
"""
name += time_now()
tags.update({'Name': name}) if tags else None
resource = 'aws_ami_from_instance'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, name=name,
source_instance_id=source_instance_id, tags=tags)}))
@classmethod
def aws_elb(
cls,
name,
region,
listeners,
subnets,
health_check=None,
internal=None,
security_groups=None,
instances=None):
"""
:param name: str
:param region: str
:param listeners: list
:param subnets: list
:param health_check: dict
:param internal: bool
:param security_groups: list
:param instances: list
:return: Terraform
"""
resource = 'aws_elb'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, name=name, listener=listeners,
subnets=subnets, health_check=health_check,
internal=internal, security_groups=security_groups,
instances=instances)}))
@classmethod
def aws_elasticache_cluster(
cls,
name,
region,
node_type,
num_cache_nodes,
parameter_group_name,
port,
engine,
subnet_group_name=None,
security_group_ids=None):
"""
:param name: str
:param region: str
:param node_type: str
:param num_cache_nodes: str
:param parameter_group_name: str
:param port: int
:param engine: str
:param subnet_group_name: str
:param security_group_ids: list
:return: Terraform
"""
resource = 'aws_elasticache_cluster'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, cluster_id=name, node_type=node_type,
num_cache_nodes=num_cache_nodes,
parameter_group_name=parameter_group_name, port=port,
subnet_group_name=subnet_group_name,
security_group_ids=security_group_ids, engine=engine)}))
@classmethod
def aws_rds(
cls,
name,
region,
instance_class,
snapshot_id,
allocated_storage=None,
engine=None,
engine_version=None,
db_subnet_group_name=None,
vpc_security_group_ids=None,
parameter_group_name=None,
publicly_accessible=False,
username=None,
password=''):
"""
:param name: str
:param region: str
:param instance_class: str
:param snapshot_id: str
:param allocated_storage: str
:param engine: str
:param engine_version: str
:param db_subnet_group_name: str
:param vpc_security_group_ids: list
:param parameter_group_name: str
:param publicly_accessible: bool
:param username:
:param password:
:return: Terraform
"""
resource = 'aws_db_instance'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, identifier=name,
snapshot_identifier=snapshot_id,
allocated_storage=allocated_storage, engine=engine,
engine_version=engine_version,
instance_class=instance_class,
db_subnet_group_name=db_subnet_group_name,
vpc_security_group_ids=vpc_security_group_ids,
parameter_group_name=parameter_group_name,
publicly_accessible=publicly_accessible,
username=username, password=password)}))
@classmethod
def aws_security_group(
cls,
name,
region,
name_prefix=None,
description=None,
ingress=None,
egress=None,
vpc_id=None,
tags=None):
"""
:param name:
:param region:
:param name_prefix:
:param description:
:param ingress:
:param egress:
:param vpc_id:
:param tags:
:return:
"""
resource = 'aws_security_group'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, name=name, name_prefix=name_prefix,
description=description, ingress=ingress, egress=egress,
tags=tags, vpc_id=vpc_id)}))
# *** VPC (Networking) ***
@classmethod
def aws_vpc(
cls,
name,
region,
cidr_block,
instance_tenancy=None,
enable_dns_support=None,
enable_dns_hostnames=None,
enable_classiclink=None,
tags=None):
"""
:param name: str
:param region: str
:param cidr_block: str
:param instance_tenancy: str
:param enable_dns_support: str
:param enable_dns_hostnames: str
:param enable_classiclink: str
:param tags: dict
:return: Terraform
"""
resource = 'aws_vpc'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, cidr_block=cidr_block,
instance_tenancy=instance_tenancy,
enable_dns_support=enable_dns_support,
enable_dns_hostnames=enable_dns_hostnames,
enable_classiclink=enable_classiclink, tags=tags)}))
@classmethod
def aws_internet_gateway(
cls,
name,
region,
vpc_id,
tags=None):
"""
:param name: str
:param region: str
:param vpc_id: str
:param tags: dict
:return: Terraform
"""
resource = 'aws_internet_gateway'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, vpc_id=vpc_id, tags=tags)}))
@classmethod
def aws_subnet(
cls,
name,
region,
vpc_id,
cidr_block,
map_public_ip_on_launch=None,
availability_zone=None,
tags=None):
"""
:param name: str
:param region: str
:param vpc_id: str
:param cidr_block: str
:param map_public_ip_on_launch: bool
:param availability_zone: str
:param tags: dict
:return: Terraform
"""
resource = 'aws_subnet'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, vpc_id=vpc_id, cidr_block=cidr_block,
map_public_ip_on_launch=map_public_ip_on_launch,
availability_zone=availability_zone, tags=tags)}))
@classmethod
def aws_eip(
cls,
name,
region,
vpc=None,
instance=None,
network_interface=None,
associate_with_private_ip=None):
"""
:param name: str
:param region: str
:param vpc: str
:param instance: str
:param network_interface: str
:param associate_with_private_ip: bool
:return: Terraform
"""
resource = 'aws_eip'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, vpc=vpc, instance=instance,
network_interface=network_interface,
associate_with_private_ip=associate_with_private_ip)}))
@classmethod
def aws_nat_gateway(
cls,
name,
region,
allocation_id,
subnet_id):
"""
:param name: str
:param region: str
:param allocation_id: str
:param subnet_id: str
:return: Terraform
"""
resource = 'aws_nat_gateway'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, allocation_id=allocation_id,
subnet_id=subnet_id)}))
@classmethod
def aws_route_table(
cls,
name,
region,
vpc_id,
route=None,
propagating_vgws=None,
tags=None):
"""
:param name: str
:param region: str
:param vpc_id: str
:param route: dict
:param propagating_vgws: bool
:param tags: dict
:return:
"""
resource = 'aws_route_table'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, vpc_id=vpc_id, route=route,
propagating_vgws=propagating_vgws, tags=tags)}))
@classmethod
def aws_route(
cls,
name,
region,
route_table_id,
destination_cidr_block,
vpc_peering_connection_id=None,
gateway_id=None,
nat_gateway_id=None,
instance_id=None,
network_interface_id=None):
"""
:param name: str
:param region: str
:param route_table_id: str
:param destination_cidr_block: str
:param vpc_peering_connection_id: str
:param gateway_id: str
:param nat_gateway_id: str
:param instance_id: str
:param network_interface_id: str
:return: Terraform
"""
resource = 'aws_route'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, route_table_id=route_table_id,
destination_cidr_block=destination_cidr_block,
vpc_peering_connection_id=vpc_peering_connection_id,
gateway_id=gateway_id, nat_gateway_id=nat_gateway_id,
instance_id=instance_id,
network_interface_id=network_interface_id)}))
@classmethod
def aws_route_table_association(
cls,
name,
region,
subnet_id,
route_table_id):
"""
:param name: str
:param region: str
:param subnet_id: str
:param route_table_id: str
:return: Terraform
"""
resource = 'aws_route_table_association'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, subnet_id=subnet_id,
route_table_id=route_table_id)}))
@classmethod
def aws_main_route_table_association(
cls,
name,
region,
vpc_id,
route_table_id):
"""
:param name: str
:param region: str
:param vpc_id: str
:param route_table_id: str
:return: Terraform
"""
resource = 'aws_main_route_table_association'
id = Terraform.set_id(resource, name)
provider = Terraform.set_provider(region)
return cls(id, name, provider, resource, remove_none({
name: dict(provider=provider, vpc_id=vpc_id,
route_table_id=route_table_id)}))
def __init__(self, id, name, provider, resource, terraform):
"""
:param id: str
:param name: str
:param provider: str
:param resource: str
:param terraform: dict
"""
self.id = id
self.terraform = terraform
self.name = name
self.resource = resource
self.provider = provider
def __str__(self):
return self.id