def show_template(vm_id): from management_api import ManagementApi from management_api import NotFoundException mgmt_api = ManagementApi(host) vm = {} try: vm = mgmt_api.show_registry(vm_id) except NotFoundException: template_id_from_list = mgmt_api.search_template_by_name(vm_id) if template_id_from_list: vm = mgmt_api.show_registry(template_id_from_list) else: click.echo('Template not found') exit(-1) versions = vm.pop('versions', []) vm_dict = OrderedDict([('id', vm.get('id')), ('name', vm.get('name'))]) formatted_versions = [] for version_dict in versions: number = version_dict.get('number') tag = version_dict.get('tag', '-') formatted_versions.append( OrderedDict([('number', number), ('tag', tag)])) vm_dict['versions'] = formatted_versions from formatter import Formatter formatter = Formatter() click.echo(formatter.format_dict(vm_dict))
def list_templates(): from management_api import ManagementApi mgmt_api = ManagementApi(host) result = mgmt_api.list_registry() from formatter import Formatter formatter = Formatter() click.echo(formatter.format_list_of_dicts(result))
def list_vms(): from management_api import ManagementApi mgmt_api = ManagementApi(host) list_of_vms = mgmt_api.list() click.echo("\nvms:\n") from formatter import Formatter formatter = Formatter() click.echo(formatter.format_list_of_dicts(list_of_vms)) click.echo("\n")
def terminate_vm(vm_id): from management_api import ManagementApi mgmt_api = ManagementApi(host) result = mgmt_api.terminate_vm(vm_id) if result: click.echo("\nterminating...\n") ctx = click.get_current_context() ctx.invoke(show_vm, vm_id=vm_id) else: click.echo("\nunable to terminate %s\n" % vm_id)
def show_node(node_id): from management_api import ManagementApi from management_api import NotFoundException mgmt_api = ManagementApi(host) try: result = mgmt_api.show_node(node_id) except NotFoundException: click.echo("no such node") exit(-1) from formatter import Formatter formatter = Formatter() click.echo(formatter.format_list_of_dicts(result))
def show_vm(vm_id): from management_api import ManagementApi from management_api import NotFoundException mgmt_api = ManagementApi(host) try: vm = mgmt_api.show_vm(vm_id) except NotFoundException: click.echo("vm not found") exit(-1) click.echo("\nshowing vm %s\n" % vm_id) from formatter import Formatter formatter = Formatter() click.echo(formatter.format_dict(vm))
def start_vm(template_id, tag, count, node): from management_api import ManagementApi from management_api import NotFoundException mgmt_api = ManagementApi(host) new_ids = None try: new_ids = mgmt_api.start_vm(template_id, count, tag=tag, node=node) except NotFoundException: template_id_from_list = mgmt_api.search_template_by_name(template_id) if template_id_from_list: new_ids = mgmt_api.start_vm(template_id_from_list, count, tag=tag, node=node) else: click.echo("Template not found") exit(-1) if new_ids: click.echo("\nvms created") vms_created = [] for new_id in new_ids: vm_info = mgmt_api.show_vm(new_id) info_to_show = { 'id': new_id, 'state': vm_info.get('instance_state') } vms_created.append(info_to_show) from formatter import Formatter formatter = Formatter() click.echo(formatter.format_list_of_dicts(vms_created)) else: click.echo("\ncreate failed")