-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_workspace.py
108 lines (89 loc) · 3.6 KB
/
import_workspace.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
import boto3
import translators.workspace_translator
import itglue_adapter
import itglue
from multiprocessing import Process
import argparse
class WorkspaceImportError(Exception):
pass
def get_workspaces(workspace_id=None):
workspace_client = boto3.client('workspaces')
workspaces = []
if workspace_id:
workspace = workspace_client.describe_workspaces(WorkspaceIds=[workspace_id])
return workspace.get('Workspaces')[0]
paginator = workspace_client.get_paginator('describe_workspaces')
response_iterator = paginator.paginate(
PaginationConfig={
'PageSize': 10,
'StartingToken': None
}
)
for page in response_iterator:
workspaces.extend(page['Workspaces'])
return workspaces
def import_workspaces(organization, workspace_id=None):
workspace_type = itglue.ConfigurationType.first_or_create(name='Workspace')
active_status, inactive_status = itglue_adapter.get_or_create_config_statuses()
processes = []
if workspace_id:
workspace = get_workspaces(workspace_id)
workspace_attributes = translate_workspaces(workspace, active_status, inactive_status)
update_configuration_and_interfaces(workspace_attributes, organization, workspace_type)
print("finished importing workspace: {}".format(workspace_id))
else:
workspaces = get_workspaces()
for workspace in workspaces:
workspace_attributes = translate_workspaces(workspace, active_status, inactive_status)
process = Process(target=update_configuration_and_interfaces, args=(workspace_attributes, organization, workspace_type))
processes.append(process)
itglue_adapter.batch_start_processes(processes)
print("finished importing workspaces")
def translate_workspaces(workspace, active_status, inactive_status):
workspace_attributes = translators.workspace_translator.WorkspaceTranslator(
workspace,
active_status_id=active_status.id,
inactive_status_id=inactive_status.id
)
return workspace_attributes.translated
def update_configuration_and_interfaces(workspace_attributes, organization, workspace_type):
configuration = itglue_adapter.update_or_create_configuration(
resource=workspace_attributes,
organization=organization,
conf_type=workspace_type
)
if workspace_attributes.get('ip_address'):
itglue_adapter.update_or_create_config_interface(workspace_attributes, configuration, primary=True, ip_address=workspace_attributes.get('ip_address'))
def main():
args = get_args()
organization = itglue_adapter.get_organization(args.organization)
id = args.workspace_id
if args.add_all and id:
id = None
import_workspaces(organization, workspace_id=id)
return True
def get_args():
parser = argparse.ArgumentParser(
description='Import Workspaces as Configurations into a specific IT Glue Organization')
parser.add_argument(
'organization',
metavar='ORG_ID_OR_NAME',
type=str,
help='Enter the name or ID of the IT Glue Organization'
)
parser.add_argument(
'-id', '--workspace-id',
type=str,
help="ID of the workspace to be created or updated"
)
parser.add_argument(
'--add-all',
action='store_true',
help='Add all the workspaces in AWS, will override workspace_id'
)
args = parser.parse_args()
if not args.add_all and not args.workspace_id:
parser.error('Must provide an Workspace ID or turn on --add-all flag')
return args
if __name__ == '__main__':
main()