-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathworkspace_vars.py
167 lines (132 loc) · 7.46 KB
/
workspace_vars.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
"""
Module for Terraform Enterprise/Cloud Migration Worker: Workspace vars.
"""
from .base_worker import TFCMigratorBaseWorker
class WorkspaceVarsWorker(TFCMigratorBaseWorker):
"""
A class to represent the worker that will migrate all workspace vars from
one TFC/E org to another TFC/E org.
"""
_api_module_used = "workspace_vars"
_required_entitlements = []
def migrate(self, workspaces_map, return_sensitive_variable_data=True):
"""
Function to migrate all workspace variables from one TFC/E org to another TFC/E org.
"""
self._logger.info("Migrating workspace variables...")
sensitive_variable_data = []
for source_workspace_id in workspaces_map:
target_workspace_id = workspaces_map[source_workspace_id]
target_workspace_name = \
self._api_target.workspaces.show(\
workspace_id=target_workspace_id)["data"]["attributes"]["name"]
# Pull variables from the old workspace
source_workspace_vars = \
self._api_source.workspace_vars.list(source_workspace_id)["data"]
# Get the variables that may already exist in the target workspace from a previous run
existing_target_workspace_vars = \
self._api_target.workspace_vars.list(target_workspace_id)["data"]
target_workspace_var_data = {}
for target_workspace_var in existing_target_workspace_vars:
target_workspace_var_data[target_workspace_var["attributes"]["key"]] = \
target_workspace_var["id"]
# NOTE: this is reversed to maintain the order present in the source
for source_variable in reversed(source_workspace_vars):
target_variable_key = source_variable["attributes"]["key"]
target_variable_value = source_variable["attributes"]["value"]
target_variable_category = source_variable["attributes"]["category"]
target_variable_hcl = source_variable["attributes"]["hcl"]
target_variable_description = source_variable["attributes"]["description"]
target_variable_sensitive = source_variable["attributes"]["sensitive"]
if target_variable_sensitive:
sensitive_variable_map = {
"workspace_name": target_workspace_name,
"workspace_id": target_workspace_id,
"variable_key": target_variable_key,
"variable_value": target_variable_value,
"variable_description": target_variable_description,
"variable_category": target_variable_category,
"variable_hcl": target_variable_hcl
}
# Make sure we haven't already created this variable in a past run
if target_variable_key in target_workspace_var_data:
self._logger.info("Workspace variable: %s, for workspace %s exists. Skipped.", \
target_variable_key, target_workspace_name)
if target_variable_sensitive and return_sensitive_variable_data:
sensitive_variable_map["variable_id"] = \
target_workspace_var_data[target_variable_key]
# Build the sensitive variable map
sensitive_variable_data.append(sensitive_variable_map)
continue
# Build the new variable payload
new_variable_payload = {
"data": {
"type": "vars",
"attributes": {
"key": target_variable_key,
"value": target_variable_value,
"description": target_variable_description,
"category": target_variable_category,
"hcl": target_variable_hcl,
"sensitive": target_variable_sensitive
}
}
}
# Migrate variables to the target Workspace
target_variable = self._api_target.workspace_vars.create(
target_workspace_id, new_variable_payload)["data"]
target_variable_id = target_variable["id"]
if target_variable_sensitive and return_sensitive_variable_data:
sensitive_variable_map["variable_id"] = target_variable_id
# Build the sensitive variable map
sensitive_variable_data.append(sensitive_variable_map)
self._logger.info("Workspace variables migrated.")
return sensitive_variable_data
def migrate_sensitive(self):
"""
NOTE: The sensitive_variable_data_map map must be created ahead of time.
The easiest way to do this is to update the value for each variable in
the list returned by the migrate_workspace_variables method
"""
if "sensitive_variable_data_map" in self._sensitive_data_map:
sensitive_variable_data_map = self._sensitive_data_map["sensitive_variable_data_map"]
for sensitive_variable in sensitive_variable_data_map:
# Build the new variable payload
update_variable_payload = {
"data": {
"id": sensitive_variable["variable_id"],
"attributes": {
"key": sensitive_variable["variable_key"],
"value": sensitive_variable["variable_value"],
"description": sensitive_variable["variable_description"],
"category": sensitive_variable["variable_category"],
"hcl": sensitive_variable["variable_hcl"],
"sensitive": "true"
},
"type": "vars"
}
}
# Update the sensitive variable value in the target workspace
self._api_target.workspace_vars.update(
sensitive_variable["workspace_id"], \
sensitive_variable["variable_id"], update_variable_payload)
self._logger.info("Sensitive workspace variables migrated.")
def delete_all_from_target(self):
"""
Function to delete all workspace vars from the target TFC/E org.
"""
self._logger.info("Deleting workspace variables...")
target_workspaces = self._api_target.workspaces.list()["data"]
if target_workspaces:
for target_workspace in target_workspaces:
target_workspace_id = target_workspace["id"]
target_workspace_variables = \
self._api_target.workspace_vars.list(target_workspace_id)["data"]
if target_workspace_variables:
for target_workspace_variable in target_workspace_variables:
self._api_target.workspace_vars.destroy(\
target_workspace_id, target_workspace_variable["id"])
self._logger.info("Workspace variable %s, from workspace %s, deleted.", \
target_workspace_variable["attributes"]["key"], \
target_workspace["attributes"]["name"])
self._logger.info("Workspace variables deleted.")