+ 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")
+@click.pass_context
+def repo_list(ctx, filter, literal):
+ """list all repos"""
+ # try:
+ # K8s Repositories
+ check_client_version(ctx.obj, ctx.command.name)
+ if filter:
+ filter = "&".join(filter)
+ resp = ctx.obj.repo.list(filter)
+ resp += ctx.obj.osmrepo.list(filter)
+ if literal:
+ print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
+ return
+ table = PrettyTable(["Name", "Id", "Type", "URI", "Description"])
+ for repo in resp:
+ # cluster['k8s-nets'] = json.dumps(yaml.safe_load(cluster['k8s-nets']))
+ table.add_row(
+ [
+ repo["name"],
+ repo["_id"],
+ repo["type"],
+ repo["url"],
+ trunc_text(repo.get("description") or "", 40),
+ ]
+ )
+ table.align = "l"