-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform-mutate.py
60 lines (45 loc) · 1.75 KB
/
terraform-mutate.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
import os
import yaml
import json
import subprocess
with open('mutate-config.yml') as f:
config = yaml.load(f)
state = json.loads(subprocess.check_output(['terraform', 'state', 'pull']))
def get_module(module_name):
module_path = ['root', module_name.split('.')[1]]
return next((m for m in state['modules'] if m['path'] == module_path), {})
def get_resource_from_module(resource_name, module):
return module['resources'].get(resource_name, {})
def extract_module_resource_names(resource_name):
tokens = resource_name.split('.')
if tokens[0] == 'module':
module_name = '.'.join(tokens[0:2])
resource_name = '.'.join(tokens[2:4])
else:
return
return module_name, resource_name
def get_resource(resource_name):
module_name, resource_name = extract_module_resource_names(resource_name)
module = get_module(module_name)
if not module:
return
resource = get_resource_from_module(resource_name, module)
if not resource:
return
resource['id'] = resource['primary']['id']
return resource
def import_by_config_mapping():
for source_name, target_name in config.items():
resource = get_resource(source_name)
if not resource:
print '\033[33msource "%s" not found, skipping...\033[0m' % source_name
continue
print 'Moving', source_name, 'to', target_name
if get_resource(target_name):
replace_it = raw_input('"%s" already exists, should we replace it? yes/no\n' % target_name)
if replace_it == 'yes':
os.system('terraform state rm %s' % target_name)
else:
continue
os.system('terraform state mv %s %s' % (source_name, target_name))
import_by_config_mapping()