|
| 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