Update vim-create and vim-update to accept config and credential files
[osm/osmclient.git] / osmclient / scripts / osm.py
index dd89b9d..30aa33f 100755 (executable)
@@ -94,6 +94,26 @@ def get_vim_name(vim_list, vim_id):
     return vim_name
 
 
+def create_config(config_file, json_string):
+    '''
+    Combines a YAML or JSON file with a JSON string into a Python3 structure
+    It loads the YAML or JSON file 'cfile' into a first dictionary.
+    It loads the JSON string into a second dictionary.
+    Then it updates the first dictionary with the info in the second dictionary.
+    If the field is present in both cfile and cdict, the field in cdict prevails.
+    If both cfile and cdict are None, it returns an empty dict (i.e. {})
+    '''
+    config = {}
+    if config_file:
+        with open(config_file, "r") as cf:
+            config = yaml.safe_load(cf.read())
+    if json_string:
+        cdict = yaml.safe_load(json_string)
+        for k, v in cdict.items():
+            config[k] = v
+    return config
+
+
 @click.group(
     context_settings=dict(help_option_names=["-h", "--help"], max_content_width=160)
 )
@@ -3095,18 +3115,13 @@ def pdu_delete(ctx, name, force):
 
 
 @cli_osm.command(name="vim-create", short_help="creates a new VIM account")
-@click.option("--name", prompt=True, help="Name to create datacenter")
-@click.option("--user", prompt=True, help="VIM username")
-@click.option(
-    "--password",
-    prompt=True,
-    hide_input=True,
-    confirmation_prompt=True,
-    help="VIM password",
-)
-@click.option("--auth_url", prompt=True, help="VIM url")
-@click.option("--tenant", prompt=True, help="VIM tenant name")
+@click.option("--name", required=True, help="Name to create datacenter")
+@click.option("--user", default=None, help="VIM username")
+@click.option("--password", default=None, help="VIM password")
+@click.option("--auth_url", default=None, help="VIM url")
+@click.option("--tenant", "--project", "tenant", default=None, help="VIM tenant/project name")
 @click.option("--config", default=None, help="VIM specific config parameters")
+@click.option("--config_file", default=None, help="VIM specific config parameters in YAML or JSON file")
 @click.option("--account_type", default="openstack", help="VIM type")
 @click.option("--description", default=None, help="human readable description")
 @click.option(
@@ -3128,6 +3143,7 @@ def pdu_delete(ctx, name, force):
     "until the operation is completed, or timeout",
 )
 @click.option("--vca", default=None, help="VCA to be used in this VIM account")
+@click.option("--creds", default=None, help="credentials file (only applycable for GCP VIM type)")
 @click.pass_context
 def vim_create(
     ctx,
@@ -3137,12 +3153,14 @@ def vim_create(
     auth_url,
     tenant,
     config,
+    config_file,
     account_type,
     description,
     sdn_controller,
     sdn_port_mapping,
     wait,
     vca,
+    creds,
 ):
     """creates a new VIM account"""
     logger.debug("")
@@ -3158,13 +3176,13 @@ def vim_create(
     vim["vim-tenant-name"] = tenant
     vim["vim-type"] = account_type
     vim["description"] = description
-    vim["config"] = config
     if vca:
         vim["vca"] = vca
-    if sdn_controller or sdn_port_mapping:
-        ctx.obj.vim.create(name, vim, sdn_controller, sdn_port_mapping, wait=wait)
-    else:
-        ctx.obj.vim.create(name, vim, wait=wait)
+    vim_config = create_config(config_file, config)
+    if creds:
+        with open(creds, "r") as cf:
+            vim_config["credentials"] = yaml.safe_load(cf.read())
+    ctx.obj.vim.create(name, vim, vim_config, sdn_controller, sdn_port_mapping, wait=wait)
     # except ClientException as e:
     #     print(str(e))
     #     exit(1)
@@ -3178,6 +3196,7 @@ def vim_create(
 @click.option("--auth_url", help="VIM url")
 @click.option("--tenant", help="VIM tenant name")
 @click.option("--config", help="VIM specific config parameters")
+@click.option("--config_file", default=None, help="VIM specific config parameters in YAML or JSON file")
 @click.option("--account_type", help="VIM type")
 @click.option("--description", help="human readable description")
 @click.option(
@@ -3199,6 +3218,7 @@ def vim_create(
     help="do not return the control immediately, but keep it "
     "until the operation is completed, or timeout",
 )
+@click.option("--creds", default=None, help="credentials file (only applycable for GCP VIM type)")
 @click.pass_context
 def vim_update(
     ctx,
@@ -3209,11 +3229,13 @@ def vim_update(
     auth_url,
     tenant,
     config,
+    config_file,
     account_type,
     description,
     sdn_controller,
     sdn_port_mapping,
     wait,
+    creds,
 ):
     """updates a VIM account
 
@@ -3237,9 +3259,14 @@ def vim_update(
         vim["vim_type"] = account_type
     if description:
         vim["description"] = description
-    if config:
-        vim["config"] = config
-    ctx.obj.vim.update(name, vim, sdn_controller, sdn_port_mapping, wait=wait)
+    vim_config = None
+    if config or config_file:
+        vim_config = create_config(config_file, config)
+    if creds:
+        with open(creds, "r") as cf:
+            vim_config["credentials"] = yaml.safe_load(cf.read())
+    logger.info(f"VIM: {vim}, VIM config: {vim_config}")
+    ctx.obj.vim.update(name, vim, vim_config, sdn_controller, sdn_port_mapping, wait=wait)
     # except ClientException as e:
     #     print(str(e))
     #     exit(1)
@@ -3316,6 +3343,8 @@ def vim_list(ctx, filter, long):
         if long:
             if "vim_password" in vim:
                 vim["vim_password"] = "********"
+            if "config" in vim and "credentials" in vim["config"]:
+                vim["config"]["credentials"] = "********"
             logger.debug("VIM details: {}".format(yaml.safe_dump(vim)))
             vim_state = vim["_admin"].get("operationalState", "-")
             error_details = "N/A"
@@ -3348,8 +3377,9 @@ def vim_list(ctx, filter, long):
     multiple=True,
     help="restricts the information to the fields in the filter",
 )
+@click.option("--literal", is_flag=True, help="print literally, no pretty table")
 @click.pass_context
-def vim_show(ctx, name, filter):
+def vim_show(ctx, name, filter, literal):
     """shows the details of a VIM account
 
     NAME: name or ID of the VIM account
@@ -3359,10 +3389,15 @@ def vim_show(ctx, name, filter):
     resp = ctx.obj.vim.get(name)
     if "vim_password" in resp:
         resp["vim_password"] = "********"
+    if "config" in resp and "credentials" in resp["config"]:
+        resp["config"]["credentials"] = "********"
     # except ClientException as e:
     #     print(str(e))
     #     exit(1)
 
+    if literal:
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
+        return
     table = PrettyTable(["key", "attribute"])
     for k, v in list(resp.items()):
         if not filter or k in filter: