+@cli_osm.command(name="ns-delete", short_help="deletes a NS instance")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.option(
+ "--config",
+ default=None,
+ help="specific yaml configuration for the termination, e.g. '{autoremove: False, timeout_ns_terminate: "
+ "600, skip_terminate_primitives: True}'",
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "until the operation is completed, or timeout",
+)
+@click.pass_context
+def ns_delete(ctx, name, force, config, wait):
+ """deletes a NS instance
+
+ NAME: name or ID of the NS instance to be deleted
+ """
+ logger.debug("")
+ # try:
+ if not force:
+ ctx.obj.ns.delete(name, config=config, wait=wait)
+ else:
+ check_client_version(ctx.obj, "--force")
+ ctx.obj.ns.delete(name, force, config=config, wait=wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+def nst_delete(ctx, name, force):
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.nst.delete(name, force)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="nst-delete", short_help="deletes a Network Slice Template (NST)")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.pass_context
+def nst_delete1(ctx, name, force):
+ """deletes a Network Slice Template (NST)
+
+ NAME: name or ID of the NST/NSTpkg to be deleted
+ """
+ logger.debug("")
+ nst_delete(ctx, name, force)
+
+
+@cli_osm.command(
+ name="netslice-template-delete", short_help="deletes a Network Slice Template (NST)"
+)
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.pass_context
+def nst_delete2(ctx, name, force):
+ """deletes a Network Slice Template (NST)
+
+ NAME: name or ID of the NST/NSTpkg to be deleted
+ """
+ logger.debug("")
+ nst_delete(ctx, name, force)
+
+
+def nsi_delete(ctx, name, force, wait):
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.nsi.delete(name, force, wait=wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="nsi-delete", short_help="deletes a Network Slice Instance (NSI)")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "until the operation is completed, or timeout",
+)
+@click.pass_context
+def nsi_delete1(ctx, name, force, wait):
+ """deletes a Network Slice Instance (NSI)
+
+ NAME: name or ID of the Network Slice instance to be deleted
+ """
+ logger.debug("")
+ nsi_delete(ctx, name, force, wait=wait)
+
+
+@cli_osm.command(
+ name="netslice-instance-delete", short_help="deletes a Network Slice Instance (NSI)"
+)
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.pass_context
+def nsi_delete2(ctx, name, force, wait):
+ """deletes a Network Slice Instance (NSI)
+
+ NAME: name or ID of the Network Slice instance to be deleted
+ """
+ logger.debug("")
+ nsi_delete(ctx, name, force, wait=wait)
+
+
+@cli_osm.command(
+ name="pdu-delete", short_help="deletes a Physical Deployment Unit (PDU)"
+)
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.pass_context
+def pdu_delete(ctx, name, force):
+ """deletes a Physical Deployment Unit (PDU)
+
+ NAME: name or ID of the PDU to be deleted
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.pdu.delete(name, force)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+#################
+# VIM operations
+#################
+
+
+@cli_osm.command(name="vim-create", short_help="creates a new VIM account")
+@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(
+ "--sdn_controller",
+ default=None,
+ help="Name or id of the SDN controller associated to this VIM account",
+)
+@click.option(
+ "--sdn_port_mapping",
+ default=None,
+ help="File describing the port mapping between compute nodes' ports and switch ports",
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "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.option("--prometheus_config_file", default=None, help="Prometheus configuration to get VIM data")
+@click.pass_context
+def vim_create(
+ ctx,
+ name,
+ user,
+ password,
+ auth_url,
+ tenant,
+ config,
+ config_file,
+ account_type,
+ description,
+ sdn_controller,
+ sdn_port_mapping,
+ wait,
+ vca,
+ creds,
+ prometheus_config_file
+):
+ """creates a new VIM account"""
+ logger.debug("")
+ # try:
+ if sdn_controller:
+ check_client_version(ctx.obj, "--sdn_controller")
+ if sdn_port_mapping:
+ check_client_version(ctx.obj, "--sdn_port_mapping")
+ vim = {}
+ if prometheus_config_file:
+ with open(prometheus_config_file) as prometheus_file:
+ prometheus_config_dict = json.load(prometheus_file)
+ vim["prometheus-config"] = prometheus_config_dict
+
+ vim["vim-username"] = user
+ vim["vim-password"] = password
+ vim["vim-url"] = auth_url
+ vim["vim-tenant-name"] = tenant
+ vim["vim-type"] = account_type
+ vim["description"] = description
+ if vca:
+ vim["vca"] = vca
+ 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)
+
+
+@cli_osm.command(name="vim-update", short_help="updates a VIM account")
+@click.argument("name")
+@click.option("--newname", help="New name for the VIM account")
+@click.option("--user", help="VIM username")
+@click.option("--password", help="VIM password")
+@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(
+ "--sdn_controller",
+ default=None,
+ help="Name or id of the SDN controller to be associated with this VIM"
+ "account. Use empty string to disassociate",
+)
+@click.option(
+ "--sdn_port_mapping",
+ default=None,
+ help="File describing the port mapping between compute nodes' ports and switch ports",
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ 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.option("--prometheus_config_file", default=None, help="Prometheus configuration to get VIM data")
+@click.pass_context
+def vim_update(
+ ctx,
+ name,
+ newname,
+ user,
+ password,
+ auth_url,
+ tenant,
+ config,
+ config_file,
+ account_type,
+ description,
+ sdn_controller,
+ sdn_port_mapping,
+ wait,
+ creds,
+ prometheus_config_file
+):
+ """updates a VIM account
+
+ NAME: name or ID of the VIM account
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ vim = {}
+ if newname:
+ vim["name"] = newname
+ if user:
+ vim["vim_user"] = user
+ if password:
+ vim["vim_password"] = password
+ if auth_url:
+ vim["vim_url"] = auth_url
+ if tenant:
+ vim["vim-tenant-name"] = tenant
+ if account_type:
+ vim["vim_type"] = account_type
+ if description:
+ vim["description"] = description
+ 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())
+ if prometheus_config_file:
+ with open(prometheus_config_file) as prometheus_file:
+ prometheus_config_dict = json.load(prometheus_file)
+ vim["prometheus-config"] = prometheus_config_dict
+ 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)
+
+
+@cli_osm.command(name="vim-delete", short_help="deletes a VIM account")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "until the operation is completed, or timeout",
+)
+@click.pass_context
+def vim_delete(ctx, name, force, wait):
+ """deletes a VIM account
+
+ NAME: name or ID of the VIM account to be deleted
+ """
+ logger.debug("")
+ # try:
+ if not force:
+ ctx.obj.vim.delete(name, wait=wait)
+ else:
+ check_client_version(ctx.obj, "--force")
+ ctx.obj.vim.delete(name, force, wait=wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="vim-list", short_help="list all VIM accounts")
+# @click.option('--ro_update/--no_ro_update',
+# default=False,
+# help='update list from RO')
+@click.option(
+ "--filter",
+ default=None,
+ multiple=True,
+ help="restricts the list to the VIM accounts matching the filter",
+)
+@click.option(
+ "--long",
+ is_flag=True,
+ help="get more details of the NS (project, vim, deployment status, configuration status.",
+)
+@click.pass_context
+def vim_list(ctx, filter, long):
+ """list all VIM accounts"""
+ logger.debug("")
+ if filter:
+ filter = "&".join(filter)
+ check_client_version(ctx.obj, "--filter")
+ # if ro_update:
+ # check_client_version(ctx.obj, '--ro_update', 'v1')
+ fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
+ if fullclassname == "osmclient.sol005.client.Client":
+ resp = ctx.obj.vim.list(filter)
+ # else:
+ # resp = ctx.obj.vim.list(ro_update)
+ if long:
+ table = PrettyTable(
+ ["vim name", "uuid", "project", "operational state", "error details"]
+ )
+ project_list = ctx.obj.project.list()
+ else:
+ table = PrettyTable(["vim name", "uuid", "operational state"])
+ for vim in resp:
+ 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"
+ if vim_state == "ERROR":
+ error_details = vim["_admin"].get("detailed-status", "Not found")
+ project_id, project_name = get_project(project_list, vim)
+ # project_info = '{} ({})'.format(project_name, project_id)
+ project_info = project_name
+ table.add_row(
+ [
+ vim["name"],
+ vim["uuid"],
+ project_info,
+ vim_state,
+ wrap_text(text=error_details, width=80),
+ ]
+ )
+ else:
+ table.add_row(
+ [vim["name"], vim["uuid"], vim["_admin"].get("operationalState", "-")]
+ )
+ table.align = "l"
+ print(table)
+
+
+@cli_osm.command(name="vim-show", short_help="shows the details of a VIM account")
+@click.argument("name")
+@click.option(
+ "--filter",
+ 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, literal):
+ """shows the details of a VIM account
+
+ NAME: name or ID of the VIM account
+ """
+ logger.debug("")
+ # try:
+ 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:
+ table.add_row([k, wrap_text(text=json.dumps(v, indent=2), width=100)])
+ table.align = "l"
+ print(table)
+
+
+####################
+# WIM operations
+####################
+
+
+@cli_osm.command(name="wim-create", short_help="creates a new WIM account")
+@click.option("--name", prompt=True, help="Name for the WIM account")
+@click.option("--user", help="WIM username")
+@click.option("--password", help="WIM password")
+@click.option("--url", prompt=True, help="WIM url")
+# @click.option('--tenant',
+# help='wIM tenant name')
+@click.option("--config", default=None, help="WIM specific config parameters")
+@click.option("--wim_type", help="WIM type")
+@click.option("--description", default=None, help="human readable description")
+@click.option(
+ "--wim_port_mapping",
+ default=None,
+ help="File describing the port mapping between DC edge (datacenters, switches, ports) and WAN edge "
+ "(WAN service endpoint id and info)",
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "until the operation is completed, or timeout",
+)
+@click.pass_context
+def wim_create(
+ ctx,
+ name,
+ user,
+ password,
+ url,
+ # tenant,
+ config,
+ wim_type,
+ description,
+ wim_port_mapping,
+ wait,
+):
+ """creates a new WIM account"""
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ # if sdn_controller:
+ # check_client_version(ctx.obj, '--sdn_controller')
+ # if sdn_port_mapping:
+ # check_client_version(ctx.obj, '--sdn_port_mapping')
+ wim = {}
+ if user:
+ wim["user"] = user
+ if password:
+ wim["password"] = password
+ if url:
+ wim["wim_url"] = url
+ # if tenant: wim['tenant'] = tenant
+ wim["wim_type"] = wim_type
+ if description:
+ wim["description"] = description
+ if config:
+ wim["config"] = config
+ ctx.obj.wim.create(name, wim, wim_port_mapping, wait=wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="wim-update", short_help="updates a WIM account")
+@click.argument("name")
+@click.option("--newname", help="New name for the WIM account")
+@click.option("--user", help="WIM username")
+@click.option("--password", help="WIM password")
+@click.option("--url", help="WIM url")
+@click.option("--config", help="WIM specific config parameters")
+@click.option("--wim_type", help="WIM type")
+@click.option("--description", help="human readable description")
+@click.option(
+ "--wim_port_mapping",
+ default=None,
+ help="File describing the port mapping between DC edge (datacenters, switches, ports) and WAN edge "
+ "(WAN service endpoint id and info)",
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def wim_update(
+ ctx,
+ name,
+ newname,
+ user,
+ password,
+ url,
+ config,
+ wim_type,
+ description,
+ wim_port_mapping,
+ wait,
+):
+ """updates a WIM account
+
+ NAME: name or ID of the WIM account
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ wim = {}
+ if newname:
+ wim["name"] = newname
+ if user:
+ wim["user"] = user
+ if password:
+ wim["password"] = password
+ if url:
+ wim["url"] = url
+ # if tenant: wim['tenant'] = tenant
+ if wim_type:
+ wim["wim_type"] = wim_type
+ if description:
+ wim["description"] = description
+ if config:
+ wim["config"] = config
+ ctx.obj.wim.update(name, wim, wim_port_mapping, wait=wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="wim-delete", short_help="deletes a WIM account")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def wim_delete(ctx, name, force, wait):
+ """deletes a WIM account
+
+ NAME: name or ID of the WIM account to be deleted
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.wim.delete(name, force, wait=wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="wim-list", short_help="list all WIM accounts")
+@click.option(
+ "--filter",
+ default=None,
+ multiple=True,
+ help="restricts the list to the WIM accounts matching the filter",
+)
+@click.pass_context
+def wim_list(ctx, filter):
+ """list all WIM accounts"""
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ if filter:
+ filter = "&".join(filter)
+ resp = ctx.obj.wim.list(filter)
+ table = PrettyTable(["wim name", "uuid"])
+ for wim in resp:
+ table.add_row([wim["name"], wim["uuid"]])
+ table.align = "l"
+ print(table)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="wim-show", short_help="shows the details of a WIM account")
+@click.argument("name")
+@click.pass_context
+def wim_show(ctx, name):
+ """shows the details of a WIM account
+
+ NAME: name or ID of the WIM account
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.wim.get(name)
+ if "password" in resp:
+ resp["wim_password"] = "********"
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+ table = PrettyTable(["key", "attribute"])
+ for k, v in list(resp.items()):
+ table.add_row([k, json.dumps(v, indent=2)])
+ table.align = "l"
+ print(table)
+
+
+####################
+# SDN controller operations
+####################
+
+
+@cli_osm.command(name="sdnc-create", short_help="creates a new SDN controller")
+@click.option("--name", prompt=True, help="Name to create sdn controller")
+@click.option("--type", prompt=True, help="SDN controller type")
+@click.option(
+ "--sdn_controller_version", # hidden=True,
+ help="Deprecated. Use --config {version: sdn_controller_version}",
+)
+@click.option("--url", help="URL in format http[s]://HOST:IP/")
+@click.option("--ip_address", help="Deprecated. Use --url") # hidden=True,
+@click.option("--port", help="Deprecated. Use --url") # hidden=True,
+@click.option(
+ "--switch_dpid", help="Deprecated. Use --config {switch_id: DPID}" # hidden=True,
+)
+@click.option(
+ "--config",
+ help="Extra information for SDN in yaml format, as {switch_id: identity used for the plugin (e.g. DPID: "
+ "Openflow Datapath ID), version: version}",
+)
+@click.option("--user", help="SDN controller username")
+@click.option(
+ "--password",
+ hide_input=True,
+ confirmation_prompt=True,
+ help="SDN controller password",
+)
+@click.option("--description", default=None, help="human readable description")
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def sdnc_create(ctx, **kwargs):
+ """creates a new SDN controller"""
+ logger.debug("")
+ sdncontroller = {
+ x: kwargs[x]
+ for x in kwargs
+ if kwargs[x] and x not in ("wait", "ip_address", "port", "switch_dpid")
+ }
+ if kwargs.get("port"):
+ print("option '--port' is deprecated, use '--url' instead")
+ sdncontroller["port"] = int(kwargs["port"])
+ if kwargs.get("ip_address"):
+ print("option '--ip_address' is deprecated, use '--url' instead")
+ sdncontroller["ip"] = kwargs["ip_address"]
+ if kwargs.get("switch_dpid"):
+ print(
+ "option '--switch_dpid' is deprecated, use '--config={switch_id: id|DPID}' instead"
+ )
+ sdncontroller["dpid"] = kwargs["switch_dpid"]
+ if kwargs.get("sdn_controller_version"):
+ print(
+ "option '--sdn_controller_version' is deprecated, use '--config={version: SDN_CONTROLLER_VERSION}'"
+ " instead"
+ )
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.sdnc.create(kwargs["name"], sdncontroller, wait=kwargs["wait"])
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="sdnc-update", short_help="updates an SDN controller")
+@click.argument("name")
+@click.option("--newname", help="New name for the SDN controller")
+@click.option("--description", default=None, help="human readable description")
+@click.option("--type", help="SDN controller type")
+@click.option("--url", help="URL in format http[s]://HOST:IP/")
+@click.option(
+ "--config",
+ help="Extra information for SDN in yaml format, as "
+ "{switch_id: identity used for the plugin (e.g. DPID: "
+ "Openflow Datapath ID), version: version}",
+)
+@click.option("--user", help="SDN controller username")
+@click.option("--password", help="SDN controller password")
+@click.option("--ip_address", help="Deprecated. Use --url") # hidden=True
+@click.option("--port", help="Deprecated. Use --url") # hidden=True
+@click.option(
+ "--switch_dpid", help="Deprecated. Use --config {switch_dpid: DPID}"
+) # hidden=True
+@click.option(
+ "--sdn_controller_version", help="Deprecated. Use --config {version: VERSION}"
+) # hidden=True
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def sdnc_update(ctx, **kwargs):
+ """updates an SDN controller
+
+ NAME: name or ID of the SDN controller
+ """
+ logger.debug("")
+ sdncontroller = {
+ x: kwargs[x]
+ for x in kwargs
+ if kwargs[x]
+ and x not in ("wait", "ip_address", "port", "switch_dpid", "new_name")
+ }
+ if kwargs.get("newname"):
+ sdncontroller["name"] = kwargs["newname"]
+ if kwargs.get("port"):
+ print("option '--port' is deprecated, use '--url' instead")
+ sdncontroller["port"] = int(kwargs["port"])
+ if kwargs.get("ip_address"):
+ print("option '--ip_address' is deprecated, use '--url' instead")
+ sdncontroller["ip"] = kwargs["ip_address"]
+ if kwargs.get("switch_dpid"):
+ print(
+ "option '--switch_dpid' is deprecated, use '--config={switch_id: id|DPID}' instead"
+ )
+ sdncontroller["dpid"] = kwargs["switch_dpid"]
+ if kwargs.get("sdn_controller_version"):
+ print(
+ "option '--sdn_controller_version' is deprecated, use '---config={version: SDN_CONTROLLER_VERSION}'"
+ " instead"
+ )
+
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.sdnc.update(kwargs["name"], sdncontroller, wait=kwargs["wait"])
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="sdnc-delete", short_help="deletes an SDN controller")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def sdnc_delete(ctx, name, force, wait):
+ """deletes an SDN controller
+
+ NAME: name or ID of the SDN controller to be deleted
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.sdnc.delete(name, force, wait=wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="sdnc-list", short_help="list all SDN controllers")
+@click.option(
+ "--filter",
+ default=None,
+ multiple=True,
+ help="restricts the list to the SDN controllers matching the filter with format: 'k[.k..]=v[&k[.k]=v2]'",
+)
+@click.pass_context
+def sdnc_list(ctx, filter):
+ """list all SDN controllers"""
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ if filter:
+ filter = "&".join(filter)
+ resp = ctx.obj.sdnc.list(filter)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+ table = PrettyTable(["sdnc name", "id"])
+ for sdnc in resp:
+ table.add_row([sdnc["name"], sdnc["_id"]])
+ table.align = "l"
+ print(table)
+
+
+@cli_osm.command(name="sdnc-show", short_help="shows the details of an SDN controller")
+@click.argument("name")
+@click.pass_context
+def sdnc_show(ctx, name):
+ """shows the details of an SDN controller
+
+ NAME: name or ID of the SDN controller
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.sdnc.get(name)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+ table = PrettyTable(["key", "attribute"])
+ for k, v in list(resp.items()):
+ table.add_row([k, json.dumps(v, indent=2)])
+ table.align = "l"
+ print(table)
+
+
+###########################
+# K8s cluster operations
+###########################
+
+
+@cli_osm.command(name="k8scluster-add", short_help="adds a K8s cluster to OSM")
+@click.argument("name")
+@click.option(
+ "--creds", prompt=True, help="credentials file, i.e. a valid `.kube/config` file"
+)
+@click.option("--version", prompt=True, help="Kubernetes version")
+@click.option(
+ "--vim", prompt=True, help="VIM target, the VIM where the cluster resides"
+)
+@click.option(
+ "--k8s-nets",
+ prompt=True,
+ help='''list of VIM networks, in JSON inline format, where the cluster is
+ accessible via L3 routing, e.g. "{(k8s_net1:vim_network1) [,(k8s_net2:vim_network2) ...]}"''',
+)
+@click.option("--description", default=None, help="human readable description")
+@click.option(
+ "--namespace",
+ default="kube-system",
+ help="namespace to be used for its operation, defaults to `kube-system`",
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "until the operation is completed, or timeout",
+)
+@click.option(
+ "--cni",
+ default=None,
+ help="list of CNI plugins, in JSON inline format, used in the cluster",
+)
+# @click.option('--skip-init',
+# is_flag=True,
+# help='If set, K8s cluster is assumed to be ready for its use with OSM')
+# @click.option('--wait',
+# is_flag=True,
+# help='do not return the control immediately, but keep it until the operation is completed, or timeout')
+@click.pass_context
+def k8scluster_add(
+ ctx, name, creds, version, vim, k8s_nets, description, namespace, wait, cni
+):
+ """adds a K8s cluster to OSM
+
+ NAME: name of the K8s cluster
+ """
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ cluster = {}
+ cluster["name"] = name
+ with open(creds, "r") as cf:
+ cluster["credentials"] = yaml.safe_load(cf.read())
+ cluster["k8s_version"] = version
+ cluster["vim_account"] = vim
+ cluster["nets"] = yaml.safe_load(k8s_nets)
+ if description:
+ cluster["description"] = description
+ if namespace:
+ cluster["namespace"] = namespace
+ if cni:
+ cluster["cni"] = yaml.safe_load(cni)
+ ctx.obj.k8scluster.create(name, cluster, wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="k8scluster-update", short_help="updates a K8s cluster")
+@click.argument("name")
+@click.option("--newname", help="New name for the K8s cluster")
+@click.option("--creds", help="credentials file, i.e. a valid `.kube/config` file")
+@click.option("--version", help="Kubernetes version")
+@click.option("--vim", help="VIM target, the VIM where the cluster resides")
+@click.option(
+ "--k8s-nets",
+ help='''list of VIM networks, in JSON inline format, where the cluster is accessible
+ via L3 routing, e.g. "{(k8s_net1:vim_network1) [,(k8s_net2:vim_network2) ...]}"''',
+)
+@click.option("--description", help="human readable description")
+@click.option(
+ "--namespace",
+ help="namespace to be used for its operation, defaults to `kube-system`",
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "until the operation is completed, or timeout",
+)
+@click.option(
+ "--cni", help="list of CNI plugins, in JSON inline format, used in the cluster"
+)
+@click.pass_context
+def k8scluster_update(
+ ctx, name, newname, creds, version, vim, k8s_nets, description, namespace, wait, cni
+):
+ """updates a K8s cluster
+
+ NAME: name or ID of the K8s cluster
+ """
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ cluster = {}
+ if newname:
+ cluster["name"] = newname
+ if creds:
+ with open(creds, "r") as cf:
+ cluster["credentials"] = yaml.safe_load(cf.read())
+ if version:
+ cluster["k8s_version"] = version
+ if vim:
+ cluster["vim_account"] = vim
+ if k8s_nets:
+ cluster["nets"] = yaml.safe_load(k8s_nets)
+ if description:
+ cluster["description"] = description
+ if namespace:
+ cluster["namespace"] = namespace
+ if cni:
+ cluster["cni"] = yaml.safe_load(cni)
+ ctx.obj.k8scluster.update(name, cluster, wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="k8scluster-delete", short_help="deletes a K8s cluster")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion from the DB (not recommended)"
+)
+@click.option(
+ "--wait",
+ required=False,
+ default=False,
+ is_flag=True,
+ help="do not return the control immediately, but keep it "
+ "until the operation is completed, or timeout",
+)
+@click.pass_context
+def k8scluster_delete(ctx, name, force, wait):
+ """deletes a K8s cluster
+
+ NAME: name or ID of the K8s cluster to be deleted
+ """
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.k8scluster.delete(name, force, wait)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="k8scluster-list")
+@click.option(
+ "--filter",
+ default=None,
+ multiple=True,
+ help="restricts the list to the K8s clusters matching the filter",
+)
+@click.option("--literal", is_flag=True, help="print literally, no pretty table")
+@click.option("--long", is_flag=True, help="get more details")
+@click.pass_context
+def k8scluster_list(ctx, filter, literal, long):
+ """list all K8s clusters"""
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ if filter:
+ filter = "&".join(filter)
+ resp = ctx.obj.k8scluster.list(filter)
+ if literal:
+ print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
+ return
+ if long:
+ table = PrettyTable(
+ [
+ "Name",
+ "Id",
+ "Project",
+ "Version",
+ "VIM",
+ "K8s-nets",
+ "Operational State",
+ "Op. state (details)",
+ "Description",
+ "Detailed status",
+ ]
+ )
+ project_list = ctx.obj.project.list()
+ else:
+ table = PrettyTable(
+ ["Name", "Id", "VIM", "Operational State", "Op. state details"]
+ )
+ try:
+ vim_list = ctx.obj.vim.list()
+ except Exception:
+ vim_list = []
+ for cluster in resp:
+ logger.debug("Cluster details: {}".format(yaml.safe_dump(cluster)))
+ vim_name = get_vim_name(vim_list, cluster["vim_account"])
+ # vim_info = '{} ({})'.format(vim_name,cluster['vim_account'])
+ vim_info = vim_name
+ op_state_details = "Helm: {}\nJuju: {}".format(
+ cluster["_admin"].get("helm-chart", {}).get("operationalState", "-"),
+ cluster["_admin"].get("juju-bundle", {}).get("operationalState", "-"),
+ )
+ if long:
+ project_id, project_name = get_project(project_list, cluster)
+ # project_info = '{} ({})'.format(project_name, project_id)
+ project_info = project_name
+ detailed_status = cluster["_admin"].get("detailed-status", "-")
+ table.add_row(
+ [
+ cluster["name"],
+ cluster["_id"],
+ project_info,
+ cluster["k8s_version"],
+ vim_info,
+ json.dumps(cluster["nets"]),
+ cluster["_admin"]["operationalState"],
+ op_state_details,
+ trunc_text(cluster.get("description") or "", 40),
+ wrap_text(text=detailed_status, width=40),
+ ]
+ )
+ else:
+ table.add_row(
+ [
+ cluster["name"],
+ cluster["_id"],
+ vim_info,
+ cluster["_admin"]["operationalState"],
+ op_state_details,
+ ]
+ )
+ table.align = "l"
+ print(table)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(
+ name="k8scluster-show", short_help="shows the details of a K8s cluster"
+)
+@click.argument("name")
+@click.option("--literal", is_flag=True, help="print literally, no pretty table")
+@click.pass_context
+def k8scluster_show(ctx, name, literal):
+ """shows the details of a K8s cluster
+
+ NAME: name or ID of the K8s cluster
+ """
+ # try:
+ resp = ctx.obj.k8scluster.get(name)
+ 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()):
+ table.add_row([k, wrap_text(text=json.dumps(v, indent=2), width=100)])
+ table.align = "l"
+ print(table)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+###########################
+# VCA operations
+###########################
+
+
+@cli_osm.command(name="vca-add", short_help="adds a VCA (Juju controller) to OSM")
+@click.argument("name")
+@click.option(
+ "--endpoints",
+ prompt=True,
+ help="Comma-separated list of IP or hostnames of the Juju controller",
+)
+@click.option("--user", prompt=True, help="Username with admin priviledges")
+@click.option("--secret", prompt=True, help="Password of the specified username")
+@click.option("--cacert", prompt=True, help="CA certificate")
+@click.option(
+ "--lxd-cloud",
+ prompt=True,
+ help="Name of the cloud that will be used for LXD containers (LXD proxy charms)",
+)
+@click.option(
+ "--lxd-credentials",
+ prompt=True,
+ help="Name of the cloud credentialsto be used for the LXD cloud",
+)
+@click.option(
+ "--k8s-cloud",
+ prompt=True,
+ help="Name of the cloud that will be used for K8s containers (K8s proxy charms)",
+)
+@click.option(
+ "--k8s-credentials",
+ prompt=True,
+ help="Name of the cloud credentialsto be used for the K8s cloud",
+)
+@click.option(
+ "--model-config",
+ default={},
+ help="Configuration options for the models",
+)
+@click.option("--description", default=None, help="human readable description")
+@click.pass_context
+def vca_add(
+ ctx,
+ name,
+ endpoints,
+ user,
+ secret,
+ cacert,
+ lxd_cloud,
+ lxd_credentials,
+ k8s_cloud,
+ k8s_credentials,
+ model_config,
+ description,
+):
+ """adds a VCA to OSM
+
+ NAME: name of the VCA
+ """
+ check_client_version(ctx.obj, ctx.command.name)
+ vca = {}
+ vca["name"] = name
+ vca["endpoints"] = endpoints.split(",")
+ vca["user"] = user
+ vca["secret"] = secret
+ vca["cacert"] = cacert
+ vca["lxd-cloud"] = lxd_cloud
+ vca["lxd-credentials"] = lxd_credentials
+ vca["k8s-cloud"] = k8s_cloud
+ vca["k8s-credentials"] = k8s_credentials
+ if description:
+ vca["description"] = description
+ if model_config:
+ model_config = load(model_config)
+ vca["model-config"] = model_config
+ ctx.obj.vca.create(name, vca)
+
+
+def load(data: Any):
+ if os.path.isfile(data):
+ return load_file(data)
+ else:
+ try:
+ return json.loads(data)
+ except ValueError as e:
+ raise ClientException(e)
+
+
+def load_file(file_path: str) -> Dict:
+ content = None
+ with open(file_path, "r") as f:
+ content = f.read()
+ try:
+ return yaml.safe_load(content)
+ except yaml.scanner.ScannerError:
+ pass
+ try:
+ return json.loads(content)
+ except ValueError:
+ pass
+ raise ClientException(f"{file_path} must be a valid yaml or json file")
+
+
+@cli_osm.command(name="vca-update", short_help="updates a K8s cluster")
+@click.argument("name")
+@click.option(
+ "--endpoints", help="Comma-separated list of IP or hostnames of the Juju controller"
+)
+@click.option("--user", help="Username with admin priviledges")
+@click.option("--secret", help="Password of the specified username")
+@click.option("--cacert", help="CA certificate")
+@click.option(
+ "--lxd-cloud",
+ help="Name of the cloud that will be used for LXD containers (LXD proxy charms)",
+)
+@click.option(
+ "--lxd-credentials",
+ help="Name of the cloud credentialsto be used for the LXD cloud",
+)
+@click.option(
+ "--k8s-cloud",
+ help="Name of the cloud that will be used for K8s containers (K8s proxy charms)",
+)
+@click.option(
+ "--k8s-credentials",
+ help="Name of the cloud credentialsto be used for the K8s cloud",
+)
+@click.option(
+ "--model-config",
+ help="Configuration options for the models",
+)
+@click.option("--description", default=None, help="human readable description")
+@click.pass_context
+def vca_update(
+ ctx,
+ name,
+ endpoints,
+ user,
+ secret,
+ cacert,
+ lxd_cloud,
+ lxd_credentials,
+ k8s_cloud,
+ k8s_credentials,
+ model_config,
+ description,
+):
+ """updates a K8s cluster
+
+ NAME: name or ID of the K8s cluster
+ """
+ check_client_version(ctx.obj, ctx.command.name)
+ vca = {}
+ vca["name"] = name
+ if endpoints:
+ vca["endpoints"] = endpoints.split(",")
+ if user:
+ vca["user"] = user
+ if secret:
+ vca["secret"] = secret
+ if cacert:
+ vca["cacert"] = cacert
+ if lxd_cloud:
+ vca["lxd-cloud"] = lxd_cloud
+ if lxd_credentials:
+ vca["lxd-credentials"] = lxd_credentials
+ if k8s_cloud:
+ vca["k8s-cloud"] = k8s_cloud
+ if k8s_credentials:
+ vca["k8s-credentials"] = k8s_credentials
+ if description:
+ vca["description"] = description
+ if model_config:
+ model_config = load(model_config)
+ vca["model-config"] = model_config
+ ctx.obj.vca.update(name, vca)
+
+
+@cli_osm.command(name="vca-delete", short_help="deletes a K8s cluster")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion from the DB (not recommended)"
+)
+@click.pass_context
+def vca_delete(ctx, name, force):
+ """deletes a K8s cluster
+
+ NAME: name or ID of the K8s cluster to be deleted
+ """
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.vca.delete(name, force=force)
+
+
+@cli_osm.command(name="vca-list")
+@click.option(
+ "--filter",
+ default=None,
+ multiple=True,
+ help="restricts the list to the VCAs matching the filter",
+)
+@click.option("--literal", is_flag=True, help="print literally, no pretty table")
+@click.option("--long", is_flag=True, help="get more details")
+@click.pass_context
+def vca_list(ctx, filter, literal, long):
+ """list VCAs"""
+ check_client_version(ctx.obj, ctx.command.name)
+ if filter:
+ filter = "&".join(filter)
+ resp = ctx.obj.vca.list(filter)
+ if literal:
+ print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
+ return
+ if long:
+ table = PrettyTable(
+ ["Name", "Id", "Project", "Operational State", "Detailed Status"]
+ )
+ project_list = ctx.obj.project.list()
+ else:
+ table = PrettyTable(["Name", "Id", "Operational State"])
+ for vca in resp:
+ logger.debug("VCA details: {}".format(yaml.safe_dump(vca)))
+ if long:
+ project_id, project_name = get_project(project_list, vca)
+ detailed_status = vca.get("_admin", {}).get("detailed-status", "-")
+ table.add_row(
+ [
+ vca["name"],
+ vca["_id"],
+ project_name,
+ vca.get("_admin", {}).get("operationalState", "-"),
+ wrap_text(text=detailed_status, width=40),
+ ]
+ )
+ else:
+ table.add_row(
+ [
+ vca["name"],
+ vca["_id"],
+ vca.get("_admin", {}).get("operationalState", "-"),
+ ]
+ )
+ table.align = "l"
+ print(table)
+
+
+@cli_osm.command(name="vca-show", short_help="shows the details of a K8s cluster")
+@click.argument("name")
+@click.option("--literal", is_flag=True, help="print literally, no pretty table")
+@click.pass_context
+def vca_show(ctx, name, literal):
+ """shows the details of a K8s cluster
+
+ NAME: name or ID of the K8s cluster
+ """
+ # try:
+ resp = ctx.obj.vca.get(name)
+ 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()):
+ table.add_row([k, wrap_text(text=json.dumps(v, indent=2), width=100)])
+ table.align = "l"
+ print(table)
+
+
+###########################
+# Repo operations
+###########################
+
+
+@cli_osm.command(name="repo-add", short_help="adds a repo to OSM")
+@click.argument("name")
+@click.argument("uri")
+@click.option(
+ "--type",
+ type=click.Choice(["helm-chart", "juju-bundle", "osm"]),
+ default="osm",
+ help="type of repo (helm-chart for Helm Charts, juju-bundle for Juju Bundles, osm for OSM Repositories)",
+)
+@click.option("--description", default=None, help="human readable description")
+@click.option(
+ "--user", default=None, help="OSM repository: The username of the OSM repository"
+)
+@click.option(
+ "--password",
+ default=None,
+ help="OSM repository: The password of the OSM repository",
+)
+# @click.option('--wait',
+# is_flag=True,
+# help='do not return the control immediately, but keep it until the operation is completed, or timeout')
+@click.pass_context
+def repo_add(ctx, **kwargs):
+ """adds a repo to OSM
+
+ NAME: name of the repo
+ URI: URI of the repo
+ """
+ # try:
+ kwargs = {k: v for k, v in kwargs.items() if v is not None}
+ repo = kwargs
+ repo["url"] = repo.pop("uri")
+ if repo["type"] in ["helm-chart", "juju-bundle"]:
+ ctx.obj.repo.create(repo["name"], repo)
+ else:
+ ctx.obj.osmrepo.create(repo["name"], repo)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="repo-update", short_help="updates a repo in OSM")
+@click.argument("name")
+@click.option("--newname", help="New name for the repo")
+@click.option("--uri", help="URI of the repo")
+@click.option("--description", help="human readable description")
+# @click.option('--wait',
+# is_flag=True,
+# help='do not return the control immediately, but keep it until the operation is completed, or timeout')
+@click.pass_context
+def repo_update(ctx, name, newname, uri, description):
+ """updates a repo in OSM
+
+ NAME: name of the repo
+ """
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ repo = {}
+ if newname:
+ repo["name"] = newname
+ if uri:
+ repo["uri"] = uri
+ if description:
+ repo["description"] = description
+ try:
+ ctx.obj.repo.update(name, repo)
+ except NotFound:
+ ctx.obj.osmrepo.update(name, repo)
+
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(
+ name="repo-index", short_help="Index a repository from a folder with artifacts"
+)
+@click.option(
+ "--origin", default=".", help="origin path where the artifacts are located"
+)
+@click.option(
+ "--destination", default=".", help="destination path where the index is deployed"
+)
+@click.pass_context
+def repo_index(ctx, origin, destination):
+ """Index a repository
+
+ NAME: name or ID of the repo to be deleted
+ """
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.osmrepo.repo_index(origin, destination)
+
+
+@cli_osm.command(name="repo-delete", short_help="deletes a repo")
+@click.argument("name")
+@click.option(
+ "--force", is_flag=True, help="forces the deletion from the DB (not recommended)"
+)
+# @click.option('--wait',
+# is_flag=True,
+# help='do not return the control immediately, but keep it until the operation is completed, or timeout')
+@click.pass_context
+def repo_delete(ctx, name, force):
+ """deletes a repo
+
+ NAME: name or ID of the repo to be deleted
+ """
+ logger.debug("")
+ try:
+ ctx.obj.repo.delete(name, force=force)
+ except NotFound:
+ ctx.obj.osmrepo.delete(name, force=force)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name="repo-list")
+@click.option(
+ "--filter",
+ default=None,
+ multiple=True,
+ help="restricts the list to the repos matching the filter",
+)
+@click.option("--literal", is_flag=True, help="print literally, no pretty table")