-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdatapak_manage.py
172 lines (119 loc) · 4.02 KB
/
datapak_manage.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
"""
DataPak deployment space manage script
"""
import os
import sys
from pprint import pprint
import json
from ibm_watson_machine_learning import APIClient
TERRAFORM_OUTPUT = ".terraform/terraform.tfstate"
def authentication():
if os.getenv("IBMCLOUD_API_KEY"):
wml_credentials = {
"url": "https://us-south.ml.cloud.ibm.com",
"apikey": os.environ.get("IBMCLOUD_API_KEY"),
}
client = APIClient(wml_credentials) # Connect to IBM cloud
return client
raise Exception("API_KEY environment variable not defined")
def terraform_output(terraform_path=TERRAFORM_OUTPUT):
output = dict(json.load(open(terraform_path)))["outputs"]
cos_crn = output["cos_crn"]["value"]
wml_crn = output["wml_crn"]["value"]["crn"]
wml_name = output["wml_crn"]["value"]["resource_name"]
state = {"cos_crn": cos_crn, "wml_name": wml_name, "wml_crn": wml_crn}
return state
def create_deployment_space(
client, cos_crn, wml_name, wml_crn, space_name="default", description=""
):
metadata = {
client.spaces.ConfigurationMetaNames.NAME: space_name, ## Project info
client.spaces.ConfigurationMetaNames.DESCRIPTION: description,
client.spaces.ConfigurationMetaNames.STORAGE: {
"type": "bmcos_object_storage",
"resource_crn": cos_crn,
},
client.spaces.ConfigurationMetaNames.COMPUTE: { ## Project compute instance (WML)
"name": wml_name,
"crn": wml_crn,
},
}
space_details = client.spaces.store(meta_props=metadata) # Create a space
return space_details
def update_deployment_space(client, new_name, space_id):
metadata = {client.spaces.ConfigurationMetaNames.NAME: new_name}
space_details = client.spaces.update(space_id, changes=metadata)
return space_details
def delete_deployment_space(client, space_id):
client.spaces.delete(space_id)
def list_deployment_space(client):
spaces = client.spaces.list()
print(spaces)
def describe_deployment_space(client, space_id):
info = client.spaces.get_details(space_id)
pprint(info)
def help():
print(
"""
datapak_config.py [options]
create
update
delete
list
describe
"""
)
if __name__ == "__main__":
client = authentication()
args = sys.argv[1:]
if len(args) >= 1:
action = args[0]
if action == "create":
infos = terraform_output()
if len(args) == 2:
space_name = args[1]
space = create_deployment_space(
client,
infos["cos_crn"],
infos["wml_name"],
infos["wml_crn"],
space_name,
)
elif len(args) > 2:
space_name = args[1]
description = args[2]
space = create_deployment_space(
client,
infos["cos_crn"],
infos["wml_name"],
infos["wml_crn"],
space_name,
description,
)
pprint(space)
elif action == "update":
try:
new_name = args[1]
space_id = args[2]
except:
raise Exception("Missing arguments")
space = update_deployment_space(client, new_name, space_id)
pprint(space)
elif action == "delete":
try:
space_id = args[1]
except:
raise Exception("Missing space_id")
delete_deployment_space(client, space_id)
elif action == "list":
list_deployment_space(client)
elif action == "describe":
try:
space_id = args[1]
except:
raise Exception("Missing space_id")
describe_deployment_space(client, space_id)
else:
help()
else:
help()