+ """
+ 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('--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,
+ 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)
+ # 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('--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,
+ 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)
+ # 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',
+# 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):
+ """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=force)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='k8scluster-list')
+@click.option('--filter', default=None,
+ help='restricts the list to the K8s clusters matching the filter')
+@click.option('--literal', is_flag=True,
+ help='print literally, no pretty table')
+@click.pass_context
+def k8scluster_list(ctx, filter, literal):
+ """list all K8s clusters"""
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.k8scluster.list(filter)
+ if literal:
+ print(yaml.safe_dump(resp))
+ return
+ table = PrettyTable(['Name', 'Id', 'Version', 'VIM', 'K8s-nets', 'Operational State', 'Description'])
+ for cluster in resp:
+ table.add_row([cluster['name'], cluster['_id'], cluster['k8s_version'], cluster['vim_account'],
+ json.dumps(cluster['nets']), cluster["_admin"]["operationalState"],
+ trunc_text(cluster.get('description',''),40)])
+ 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))
+ 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)
+
+
+
+###########################
+# 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']),
+ prompt=True,
+ help='type of repo (helm-chart for Helm Charts, juju-bundle for Juju Bundles)')
+@click.option('--description',
+ default=None,
+ 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_add(ctx,
+ name,
+ uri,
+ type,
+ description):
+ """adds a repo to OSM
+
+ NAME: name of the repo
+ URI: URI of the repo
+ """
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ repo = {}
+ repo['name'] = name
+ repo['url'] = uri
+ repo['type'] = type
+ if description:
+ repo['description'] = description
+ ctx.obj.repo.create(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('--type', type=click.Choice(['helm-chart', 'juju-bundle']),
+ help='type of repo (helm-chart for Helm Charts, juju-bundle for Juju Bundles)')
+@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,
+ type,
+ 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 type: repo['type'] = type
+ if description: repo['description'] = description
+ ctx.obj.repo.update(name, repo)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@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
+ """
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.repo.delete(name, force=force)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='repo-list')
+@click.option('--filter', default=None,
+ 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:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.repo.list(filter)
+ if literal:
+ print(yaml.safe_dump(resp))
+ 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',''),40)])
+ table.align = 'l'
+ print(table)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='repo-show', short_help='shows the details of a repo')
+@click.argument('name')
+@click.option('--literal', is_flag=True,
+ help='print literally, no pretty table')
+@click.pass_context
+def repo_show(ctx, name, literal):
+ """shows the details of a repo
+
+ NAME: name or ID of the repo
+ """
+ # try:
+ resp = ctx.obj.repo.get(name)
+ if literal:
+ print(yaml.safe_dump(resp))
+ return
+ 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)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+
+####################
+# Project mgmt operations
+####################
+
+@cli_osm.command(name='project-create', short_help='creates a new project')
+@click.argument('name')
+#@click.option('--description',
+# default='no description',
+# help='human readable description')
+@click.option('--domain-name', 'domain_name',
+ default=None,
+ help='assign to a domain')
+@click.pass_context
+def project_create(ctx, name, domain_name):
+ """Creates a new project
+
+ NAME: name of the project
+ DOMAIN_NAME: optional domain name for the project when keystone authentication is used
+ """
+ logger.debug("")
+ project = {}
+ project['name'] = name
+ if domain_name:
+ project['domain_name'] = domain_name
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.project.create(name, project)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='project-delete', short_help='deletes a project')
+@click.argument('name')
+#@click.option('--force', is_flag=True, help='forces the deletion bypassing pre-conditions')
+@click.pass_context
+def project_delete(ctx, name):
+ """deletes a project
+
+ NAME: name or ID of the project to be deleted
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.project.delete(name)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='project-list', short_help='list all projects')
+@click.option('--filter', default=None,
+ help='restricts the list to the projects matching the filter')
+@click.pass_context
+def project_list(ctx, filter):
+ """list all projects"""
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.project.list(filter)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+ table = PrettyTable(['name', 'id'])
+ for proj in resp:
+ table.add_row([proj['name'], proj['_id']])
+ table.align = 'l'
+ print(table)
+
+
+@cli_osm.command(name='project-show', short_help='shows the details of a project')
+@click.argument('name')
+@click.pass_context
+def project_show(ctx, name):
+ """shows the details of a project
+
+ NAME: name or ID of the project
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.project.get(name)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+ table = PrettyTable(['key', 'attribute'])
+ for k, v in resp.items():
+ table.add_row([k, json.dumps(v, indent=2)])
+ table.align = 'l'
+ print(table)
+
+
+@cli_osm.command(name='project-update', short_help='updates a project (only the name can be updated)')
+@click.argument('project')
+@click.option('--name',
+ prompt=True,
+ help='new name for the project')
+
+@click.pass_context
+def project_update(ctx, project, name):
+ """
+ Update a project name
+
+ :param ctx:
+ :param project: id or name of the project to modify
+ :param name: new name for the project
+ :return:
+ """
+ logger.debug("")
+ project_changes = {}
+ project_changes['name'] = name
+
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.project.update(project, project_changes)
+ # except ClientException as e:
+ # print(str(e))
+
+
+####################
+# User mgmt operations
+####################
+
+@cli_osm.command(name='user-create', short_help='creates a new user')
+@click.argument('username')
+@click.option('--password',
+ prompt=True,
+ hide_input=True,
+ confirmation_prompt=True,
+ help='user password')
+@click.option('--projects',
+ # prompt="Comma separate list of projects",
+ multiple=True,
+ callback=lambda ctx, param, value: ''.join(value).split(',') if all(len(x)==1 for x in value) else value,
+ help='list of project ids that the user belongs to')
+@click.option('--project-role-mappings', 'project_role_mappings',
+ default=None, multiple=True,
+ help="assign role(s) in a project. Can be used several times: 'project,role1[,role2,...]'")
+@click.option('--domain-name', 'domain_name',
+ default=None,
+ help='assign to a domain')
+@click.pass_context
+def user_create(ctx, username, password, projects, project_role_mappings, domain_name):
+ """Creates a new user
+
+ \b
+ USERNAME: name of the user
+ PASSWORD: password of the user
+ PROJECTS: projects assigned to user (internal only)
+ PROJECT_ROLE_MAPPING: roles in projects assigned to user (keystone)
+ DOMAIN_NAME: optional domain name for the user when keystone authentication is used
+ """
+ logger.debug("")
+ user = {}
+ user['username'] = username
+ user['password'] = password
+ user['projects'] = projects
+ user['project_role_mappings'] = project_role_mappings
+ if domain_name:
+ user['domain_name'] = domain_name
+
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.user.create(username, user)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='user-update', short_help='updates user information')
+@click.argument('username')
+@click.option('--password',
+ # prompt=True,
+ # hide_input=True,
+ # confirmation_prompt=True,
+ help='user password')
+@click.option('--set-username', 'set_username',
+ default=None,
+ help='change username')
+@click.option('--set-project', 'set_project',
+ default=None, multiple=True,
+ help="create/replace the roles for this project: 'project,role1[,role2,...]'")
+@click.option('--remove-project', 'remove_project',
+ default=None, multiple=True,
+ help="removes project from user: 'project'")
+@click.option('--add-project-role', 'add_project_role',
+ default=None, multiple=True,
+ help="assign role(s) in a project. Can be used several times: 'project,role1[,role2,...]'")
+@click.option('--remove-project-role', 'remove_project_role',
+ default=None, multiple=True,
+ help="remove role(s) in a project. Can be used several times: 'project,role1[,role2,...]'")
+@click.pass_context
+def user_update(ctx, username, password, set_username, set_project, remove_project,
+ add_project_role, remove_project_role):
+ """Update a user information
+
+ \b
+ USERNAME: name of the user
+ PASSWORD: new password
+ SET_USERNAME: new username
+ SET_PROJECT: creating mappings for project/role(s)
+ REMOVE_PROJECT: deleting mappings for project/role(s)
+ ADD_PROJECT_ROLE: adding mappings for project/role(s)
+ REMOVE_PROJECT_ROLE: removing mappings for project/role(s)
+ """
+ logger.debug("")
+ user = {}
+ user['password'] = password
+ user['username'] = set_username
+ user['set-project'] = set_project
+ user['remove-project'] = remove_project
+ user['add-project-role'] = add_project_role
+ user['remove-project-role'] = remove_project_role
+
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.user.update(username, user)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='user-delete', short_help='deletes a user')
+@click.argument('name')
+#@click.option('--force', is_flag=True, help='forces the deletion bypassing pre-conditions')
+@click.pass_context
+def user_delete(ctx, name):
+ """deletes a user
+
+ \b
+ NAME: name or ID of the user to be deleted
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ ctx.obj.user.delete(name)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+
+
+@cli_osm.command(name='user-list', short_help='list all users')
+@click.option('--filter', default=None,
+ help='restricts the list to the users matching the filter')
+@click.pass_context
+def user_list(ctx, filter):
+ """list all users"""
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.user.list(filter)
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)
+ table = PrettyTable(['name', 'id'])
+ for user in resp:
+ table.add_row([user['username'], user['_id']])
+ table.align = 'l'
+ print(table)
+
+
+@cli_osm.command(name='user-show', short_help='shows the details of a user')
+@click.argument('name')
+@click.pass_context
+def user_show(ctx, name):
+ """shows the details of a user
+
+ NAME: name or ID of the user
+ """
+ logger.debug("")
+ # try:
+ check_client_version(ctx.obj, ctx.command.name)
+ resp = ctx.obj.user.get(name)
+ if 'password' in resp:
+ resp['password']='********'
+ # except ClientException as e:
+ # print(str(e))
+ # exit(1)