Skip to content

Commit 9cca1d7

Browse files
authored
Infra (#16)
DataPak script changed * Script format
1 parent 4ce567e commit 9cca1d7

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

.infra/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
configs

.infra/.terraform/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#.terraform folder
2+
.terraform/
3+
.terraform.lock.hcl
4+
output.json
5+
terraform.tfstate
6+
terraform.tfstate.backup
7+
infra_state.json

.infra/.terraform/init_infra.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#### AUTH && PLUGIN
2+
3+
terraform {
4+
required_providers {
5+
ibm = {
6+
source = "IBM-Cloud/ibm"
7+
version = "~> 1.12.0"
8+
}
9+
}
10+
}
11+
12+
13+
provider "ibm" {}
14+
15+
#### RESOURCE GROUP
16+
17+
data "ibm_resource_group" "group" {
18+
name = "fpe_insper"
19+
}
20+
21+
#### Machine learning service
22+
resource "ibm_resource_instance" "wml" {
23+
name = "TESTE_TERRAFORM"
24+
service = "pm-20"
25+
plan = "lite"
26+
location = "us-south"
27+
resource_group_id = data.ibm_resource_group.group.id
28+
tags = ["TESTE", "TERRAFORM"]
29+
30+
}
31+
32+
#### Object storage
33+
34+
resource "ibm_resource_instance" "cos" {
35+
name = "TESTE_COS"
36+
service = "cloud-object-storage"
37+
plan = "standard"
38+
location = "global"
39+
resource_group_id = data.ibm_resource_group.group.id
40+
tags = ["TERRAFORM", "TEST"]
41+
42+
}

.infra/.terraform/output.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "cos_crn" {
2+
value = ibm_resource_instance.cos.crn
3+
}
4+
5+
output "wml_name" {
6+
value = ibm_resource_instance.wml.name
7+
}
8+
9+
output "wml_crn" {
10+
value = ibm_resource_instance.wml
11+
}

.infra/datapak_manage.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
"""
2+
DataPak deployment space manage script
3+
4+
"""
5+
6+
import os
7+
import sys
8+
from pprint import pprint
9+
import json
10+
from ibm_watson_machine_learning import APIClient
11+
12+
TERRAFORM_OUTPUT = ".terraform/terraform.tfstate"
13+
14+
15+
def authentication():
16+
17+
if os.getenv("IBMCLOUD_API_KEY"):
18+
19+
wml_credentials = {
20+
"url": "https://us-south.ml.cloud.ibm.com",
21+
"apikey": os.environ.get("IBMCLOUD_API_KEY"),
22+
}
23+
client = APIClient(wml_credentials) # Connect to IBM cloud
24+
25+
return client
26+
27+
raise Exception("API_KEY environment variable not defined")
28+
29+
30+
def terraform_output(terraform_path=TERRAFORM_OUTPUT):
31+
32+
output = dict(json.load(open(terraform_path)))["outputs"]
33+
34+
cos_crn = output["cos_crn"]["value"]
35+
wml_crn = output["wml_crn"]["value"]["crn"]
36+
wml_name = output["wml_crn"]["value"]["resource_name"]
37+
38+
state = {"cos_crn": cos_crn, "wml_name": wml_name, "wml_crn": wml_crn}
39+
return state
40+
41+
42+
def create_deployment_space(
43+
cos_crn, wml_name, wml_crn, space_name="default", description=""
44+
):
45+
46+
metadata = {
47+
client.spaces.ConfigurationMetaNames.NAME: space_name, ## Project info
48+
client.spaces.ConfigurationMetaNames.DESCRIPTION: description,
49+
client.spaces.ConfigurationMetaNames.STORAGE: {
50+
"type": "bmcos_object_storage",
51+
"resource_crn": cos_crn,
52+
},
53+
client.spaces.ConfigurationMetaNames.COMPUTE: { ## Project compute instance (WML)
54+
"name": wml_name,
55+
"crn": wml_crn,
56+
},
57+
}
58+
59+
space_details = client.spaces.store(meta_props=metadata) # Create a space
60+
return space_details
61+
62+
63+
def update_deployment_space(new_name, space_id):
64+
65+
metadata = {client.spaces.ConfigurationMetaNames.NAME: new_name}
66+
67+
space_details = client.spaces.update(space_id, changes=metadata)
68+
return space_details
69+
70+
71+
def delete_deployment_space(space_id):
72+
73+
client.spaces.delete(space_id)
74+
75+
76+
def list_deployment_space():
77+
spaces = client.spaces.list()
78+
print(spaces)
79+
80+
81+
def describe_deployment_space(space_id):
82+
info = client.spaces.get_details(space_id)
83+
pprint(info)
84+
85+
86+
def help():
87+
88+
print(
89+
"""
90+
datapak_config.py [options]
91+
92+
create
93+
update
94+
delete
95+
list
96+
describe
97+
"""
98+
)
99+
100+
101+
if __name__ == "__main__":
102+
103+
client = authentication()
104+
105+
args = sys.argv[1:]
106+
107+
if len(args) >= 1:
108+
action = args[0]
109+
110+
if action == "create":
111+
112+
infos = terraform_output()
113+
if len(args) == 2:
114+
space_name = args[1]
115+
space = create_deployment_space(
116+
infos["cos_crn"], infos["wml_name"], infos["wml_crn"], space_name
117+
)
118+
119+
elif len(args) > 2:
120+
space_name = args[1]
121+
description = args[2]
122+
space = create_deployment_space(
123+
infos["cos_crn"],
124+
infos["wml_name"],
125+
infos["wml_crn"],
126+
space_name,
127+
description,
128+
)
129+
130+
pprint(space)
131+
132+
elif action == "update":
133+
134+
try:
135+
new_name = args[1]
136+
space_id = args[2]
137+
except:
138+
raise Exception("Missing arguments")
139+
140+
space = update_deployment_space(new_name, space_id)
141+
pprint(space)
142+
143+
elif action == "delete":
144+
try:
145+
space_id = args[1]
146+
except:
147+
raise Exception("Missing space_id")
148+
149+
delete_deployment_space(space_id)
150+
151+
elif action == "list":
152+
list_deployment_space()
153+
154+
elif action == "describe":
155+
156+
try:
157+
space_id = args[1]
158+
except:
159+
raise Exception("Missing space_id")
160+
161+
describe_deployment_space(space_id)
162+
163+
else:
164+
help()
165+
166+
else:
167+
help()

0 commit comments

Comments
 (0)