コード例 #1
0
ファイル: vca_cli_vca.py プロジェクト: digideskio/vca-cli
def role(cmd_proc, operation):
    """Operations with Roles"""
    if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA:
        utils.print_message('Operation not supported '
                            'in this service type')
        sys.exit(1)
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Role Name', 'Description', 'Permissions']
        table = []
        for r in cmd_proc.vca.get_roles():
            permissions = []
            if len(r.get('rights')) > 0:
                for p in r.get('rigths'):
                    permissions.append(str(p.get('name')))
            table.append([r.get('name'),
                          r.get('description'),
                          utils.beautified(permissions)])
        sorted_table = sorted(table, key=operator.itemgetter(0), reverse=False)
        utils.print_table("Available roles in instance '%s'"
                          ", profile '%s':" %
                          (cmd_proc.instance, cmd_proc.profile),
                          headers, sorted_table,
                          cmd_proc)
    cmd_proc.save_current_config()
コード例 #2
0
ファイル: vca_cli_vca.py プロジェクト: mlees19/vcd-cli
def role(cmd_proc, operation):
    """Operations with Roles"""
    if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA:
        utils.print_message('Operation not supported ' 'in this service type')
        sys.exit(1)
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Role Name', 'Description', 'Permissions']
        table = []
        for r in cmd_proc.vca.get_roles():
            permissions = []
            if len(r.get('rights')) > 0:
                for p in r.get('rigths'):
                    permissions.append(str(p.get('name')))
            table.append([
                r.get('name'),
                r.get('description'),
                utils.beautified(permissions)
            ])
        sorted_table = sorted(table, key=operator.itemgetter(0), reverse=False)
        utils.print_table(
            "Available roles in instance '%s'"
            ", profile '%s':" % (cmd_proc.instance, cmd_proc.profile), headers,
            sorted_table, cmd_proc)
    cmd_proc.save_current_config()
コード例 #3
0
ファイル: vca_cli_blueprint.py プロジェクト: namob/vca-cli
def _info_blueprint(cmd_proc, operation,
                    blueprint_id, blueprint_file,
                    include_plan, scoreclient):
    try:
        b = scoreclient.blueprints.get(blueprint_id)
        if blueprint_id is None or len(blueprint_id) == 0:
            utils.print_error('specify blueprint id')
            sys.exit(1)
        headers = ['Id', 'Created']
        table = cmd_proc.blueprints_to_table([b])
        if cmd_proc.json_output:
            json_object = {'blueprint':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Details of blueprint '%s', profile '%s':" %
                              (blueprint_id, cmd_proc.profile),
                              headers, table, cmd_proc)
        if include_plan:
            utils.print_json(b['plan'], "Blueprint plan", cmd_proc)
    except exceptions.ClientException as e:
                utils.print_error("Blueprint not found. Reason: {0}, {1}".
                                  format(str(e),
                                         scoreclient.response.content),
                                  cmd_proc)
コード例 #4
0
ファイル: vca_cli_vca.py プロジェクト: digideskio/vca-cli
def user(cmd_proc, operation, username, user_id, password, new_password,
         first_name, last_name, roles, token):
    """Operations with Users"""
    if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA:
        utils.print_error('Operation not supported '
                          'in this service type')
        sys.exit(1)
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    try:
        if 'list' == operation:
            headers = ['User Name', 'First', 'Last', 'Email', 'State', 'Id',
                       'Roles']
            table = []
            for u in cmd_proc.vca.get_users():
                roles = []
                for r in u.get('roles').get('roles'):
                    roles.append(str(r.get('name')))
                table.append([u.get('userName'), u.get('givenName'),
                              u.get('familyName'), u.get('email'),
                              u.get('state'),
                              u.get('id'),
                              utils.beautified(roles)])
            sorted_table = sorted(table, key=operator.itemgetter(0),
                                  reverse=False)
            utils.print_table("Available users in instance '%s'"
                              ", profile '%s':" %
                              (cmd_proc.instance, cmd_proc.profile),
                              headers, sorted_table,
                              cmd_proc)
        elif 'info' == operation:
            cmd_proc.error_message = 'not implemented'
            sys.exit(1)
        elif 'create' == operation:
            roles_array = roles.split(',')
            result = cmd_proc.vca.add_user(username, first_name,
                                           last_name, roles_array)
            utils.print_message("User '%s' successfully created" % username)
        elif 'delete' == operation:
            result = cmd_proc.vca.del_user(user_id)
            utils.print_message("Successfully deleted user '%s'" %
                                cmd_proc.vca.username)
        elif 'change-password' == operation:
            result = cmd_proc.vca.change_password(password, new_password)
            utils.print_message("Successfully changed password for user '%s'"
                                % cmd_proc.vca.username)
        elif 'validate' == operation:
            result = cmd_proc.vca.validate_user(username, password, token)
            print result
        elif 'reset-password' == operation:
            result = cmd_proc.vca.reset_password(user_id)
            utils.print_message("Successfully reset password for user id"
                                "'%s', check email to enter new password"
                                % user_id)
    except:
        utils.print_error('An error has ocurred', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #5
0
ファイル: vca_cli_compute.py プロジェクト: xcompass/vca-cli
def vm(cmd_proc, operation, vdc, vapp):
    """Operations with VMs"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['VM', "vApp", "Status", "IPs", "MACs", "Networks",
                   "vCPUs", "Memory (GB)", "CD/DVD", "OS", "Owner"]
        table = cmd_proc.vms_to_table(the_vdc, vapp)
        if cmd_proc.json_output:
            json_object = {'vms':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available VMs in '%s', profile '%s':" %
                              (vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #6
0
def vm(cmd_proc, operation, vdc, vapp):
    """Operations with VMs"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = [
            'VM', "vApp", "Status", "IPs", "Networks", "vCPUs", "Memory (GB)",
            "CD/DVD", "OS", "Owner"
        ]
        table = cmd_proc.vms_to_table(the_vdc, vapp)
        if cmd_proc.json_output:
            json_object = {'vms': utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table(
                "Available VMs in '%s', profile '%s':" %
                (vdc, cmd_proc.profile), headers, table, cmd_proc)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #7
0
ファイル: vca_cli_compute.py プロジェクト: xcompass/vca-cli
def disk(cmd_proc, operation, vdc, disk_name, disk_size, disk_id):
    """Operations with Independent Disks"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Disk', 'Size GB', 'Id', 'Owner']
        disks = cmd_proc.vca.get_disks(vdc)
        table = cmd_proc.disks_to_table(disks)
        if cmd_proc.json_output:
            json_object = {'disks':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available independent disks in '%s'"
                              ", profile '%s':" %
                              (vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'create' == operation:
        assert disk_name, "Disk name can't be empty"
        size = disk_size * cmd_proc.DISK_SIZE
        result = cmd_proc.vca.add_disk(vdc, disk_name, size)
        if result and len(result) > 0:
            if result[0]:
                utils.print_message('disk %s successfully created'
                                    % disk_name, cmd_proc)
            else:
                utils.print_error('disk %s could not be created'
                                  % disk_name, cmd_proc)
    elif 'delete' == operation:
        result = cmd_proc.vca.delete_disk(vdc, disk_name, disk_id)
        if result and len(result) > 0:
            if result[0]:
                utils.print_message('disk %s successfully deleted'
                                    % (disk_id if disk_id else disk_name),
                                    cmd_proc)
            else:
                utils.print_error('disk %s could not be deleted: %s'
                                  % (disk_name, result[1]),
                                  cmd_proc)
                sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #8
0
def disk(cmd_proc, operation, vdc, disk_name, disk_size, disk_id):
    """Operations with Independent Disks"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Disk', 'Size GB', 'Id', 'Owner']
        disks = cmd_proc.vca.get_disks(vdc)
        table = cmd_proc.disks_to_table(disks)
        if cmd_proc.json_output:
            json_object = {'disks': utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table(
                "Available independent disks in '%s'"
                ", profile '%s':" % (vdc, cmd_proc.profile), headers, table,
                cmd_proc)
    elif 'create' == operation:
        assert disk_name, "Disk name can't be empty"
        size = disk_size * cmd_proc.DISK_SIZE
        result = cmd_proc.vca.add_disk(vdc, disk_name, size)
        if result and len(result) > 0:
            if result[0]:
                utils.print_message('disk %s successfully created' % disk_name,
                                    cmd_proc)
            else:
                utils.print_error('disk %s could not be created' % disk_name,
                                  cmd_proc)
    elif 'delete' == operation:
        result = cmd_proc.vca.delete_disk(vdc, disk_name, disk_id)
        if result and len(result) > 0:
            if result[0]:
                utils.print_message(
                    'disk %s successfully deleted' %
                    (disk_id if disk_id else disk_name), cmd_proc)
            else:
                utils.print_error(
                    'disk %s could not be deleted: %s' %
                    (disk_name, result[1]), cmd_proc)
                sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #9
0
def _list_blueprints(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient):
    try:
        blueprints = scoreclient.blueprints.list()
        headers = ["Id", "Created"]
        table = cmd_proc.blueprints_to_table(blueprints)
        if cmd_proc.json_output:
            json_object = {"blueprints": utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available blueprints, profile '%s':" % cmd_proc.profile, headers, table, cmd_proc)
    except exceptions.ClientException as e:
        utils.print_message(
            "Unable to list blueprints. Reason {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc
        )
コード例 #10
0
ファイル: vca_cli_blueprint.py プロジェクト: amwelch/vca-cli
def _list_blueprints(cmd_proc, scoreclient):
    try:
        blueprints = scoreclient.blueprints.list()
        headers = ['Id', 'Created']
        table = cmd_proc.blueprints_to_table(blueprints)
        if cmd_proc.json_output:
            json_object = {'blueprints':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available blueprints, profile '%s':" %
                              cmd_proc.profile,
                              headers, table, cmd_proc)
    except exceptions.ClientException as e:
        utils.print_message('Unable to list blueprints. Reason %s.' %
                            str(e), cmd_proc)
コード例 #11
0
ファイル: vca_cli_network.py プロジェクト: amwelch/vca-cli
def firewall(cmd_proc, operation, vdc, gateway):
    """Operations with Edge Gateway Firewall Service"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = [
            'Source IP', 'Source Port', 'Destination IP', 'Destination Port',
            'Protocol', 'Enabled'
        ]
        table = cmd_proc.firewall_rules_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'fw-rules': utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table(
                "Firewall rules in gateway '%s', "
                "VDC '%s', profile '%s':" % (gateway, vdc, cmd_proc.profile),
                headers, table, cmd_proc)
    elif 'enable' == operation or 'disable' == operation:
        utils.print_message("%s firewall" % operation)
        the_gateway.enable_fw('enable' == operation)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(
                task, cmd_proc,
                cmd_proc.vca.vcloud_session.get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error(
                "can't '%s' the firewall: " % operation + error.get_message(),
                cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #12
0
ファイル: vca_cli_network.py プロジェクト: tsugliani/vca-cli
def firewall(cmd_proc, operation, vdc, gateway):
    """Operations with Edge Gateway Firewall Service"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Source IP', 'Source Port', 'Destination IP',
                   'Destination Port', 'Protocol', 'Enabled']
        table = cmd_proc.firewall_rules_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'fw-rules':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Firewall rules in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'enable' == operation or 'disable' == operation:
        utils.print_message("%s firewall" % operation)
        the_gateway.enable_fw('enable' == operation)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task,
                                   cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't '%s' the firewall: " % operation +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #13
0
ファイル: vca_cli_blueprint.py プロジェクト: namob/vca-cli
def _list_blueprints(cmd_proc, operation, blueprint_id,
                     blueprint_file, include_plan, scoreclient):
    try:
        blueprints = scoreclient.blueprints.list()
        headers = ['Id', 'Created']
        table = cmd_proc.blueprints_to_table(blueprints)
        if cmd_proc.json_output:
            json_object = {'blueprints':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available blueprints, profile '%s':" %
                              cmd_proc.profile,
                              headers, table, cmd_proc)
    except exceptions.ClientException as e:
        utils.print_message('Unable to list blueprints. Reason {0}, {1}'
                            .format(str(e), scoreclient.response.content),
                            cmd_proc)
コード例 #14
0
ファイル: vca_cli_blueprint.py プロジェクト: amwelch/vca-cli
def _info_blueprint(cmd_proc, scoreclient, include_plan=False):
    try:
        b = scoreclient.blueprints.get(blueprint)
        headers = ['Id', 'Created']
        table = cmd_proc.blueprints_to_table([b])
        if cmd_proc.json_output:
            json_object = {'blueprint':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Details of blueprint '%s', profile '%s':" %
                              (blueprint, cmd_proc.profile),
                              headers, table, cmd_proc)
        if include_plan:
            utils.print_json(b['plan'], "Blueprint plan", cmd_proc)
    except exceptions.ClientException as e:
                utils.print_error("Blueprint not found. Reason: %s." %
                                  str(e))
コード例 #15
0
def _info_blueprint(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient):
    try:
        b = scoreclient.blueprints.get(blueprint_id)
        if blueprint_id is None or len(blueprint_id) == 0:
            utils.print_error("specify blueprint id")
            sys.exit(1)
        headers = ["Id", "Created"]
        table = cmd_proc.blueprints_to_table([b])
        if cmd_proc.json_output:
            json_object = {"blueprint": utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table(
                "Details of blueprint '%s', profile '%s':" % (blueprint_id, cmd_proc.profile), headers, table, cmd_proc
            )
        if include_plan:
            utils.print_json(b["plan"], "Blueprint plan", cmd_proc)
    except exceptions.ClientException as e:
        utils.print_error(
            "Blueprint not found. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc
        )
コード例 #16
0
ファイル: vca_cli_compute.py プロジェクト: xcompass/vca-cli
def catalog(cmd_proc, operation, catalog_name, item_name, description,
            file_name):
    """Operations with Catalogs"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        catalogs = cmd_proc.vca.get_catalogs()
        headers = ['Catalog', 'Item']
        table = cmd_proc.catalogs_to_table(catalogs)
        if cmd_proc.json_output:
            json_object = {'catalogs':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available catalogs and items in org '%s', "
                              "profile '%s':" %
                              (cmd_proc.vca.org, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'create' == operation:
        task = cmd_proc.vca.create_catalog(catalog_name, description)
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't create the catalog", cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        result = cmd_proc.vca.delete_catalog(catalog_name)
        if result:
            utils.print_message('catalog deleted', cmd_proc)
        else:
            utils.print_error("can't delete the catalog", cmd_proc)
            sys.exit(1)
    elif 'delete-item' == operation:
        result = cmd_proc.vca.delete_catalog_item(catalog_name, item_name)
        if result:
            utils.print_message('catalog item deleted', cmd_proc)
        else:
            utils.print_error("can't delete the catalog item", cmd_proc)
            sys.exit(1)
    elif 'upload' == operation:
        if file_name.endswith('.iso'):
            result = cmd_proc.vca.upload_media(catalog_name, item_name,
                                               file_name, description, True,
                                               128*1024)
            if result:
                utils.print_message('file successfull uploaded', cmd_proc)
            else:
                utils.print_error("can't upload file", cmd_proc)
                sys.exit(1)
        else:
            utils.print_error('not implemented, ' +
                              'only .iso files are currently supported',
                              cmd_proc)
            sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #17
0
ファイル: vca_cli_network.py プロジェクト: digideskio/vca-cli
def gateway(cmd_proc, operation, vdc, gateway, ip):
    """Operations with Edge Gateway"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Name', 'External IPs', 'DHCP', 'Firewall', 'NAT',
                   'VPN', 'Routed Networks', 'Syslog', 'Uplinks', 'Selected']
        gateways = cmd_proc.vca.get_gateways(vdc)
        table = cmd_proc.gateways_to_table(gateways)
        if cmd_proc.json_output:
            json_object = {'gateways':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available gateways in '%s', profile '%s':" %
                              (vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif ('use' == operation or
          'info' == operation or
          'set-syslog' == operation or
          'add-ip' == operation or
          'del-ip' == operation):
        if 'set-syslog' == operation:
            task = the_gateway.set_syslog_conf(ip)
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't set syslog server: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        elif 'add-ip' == operation:
            utils.print_message("allocating public IP for gateway '%s' "
                                "in VDC '%s'" %
                                (gateway, vdc), cmd_proc)
            task = the_gateway.allocate_public_ip()
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't allocate public IP: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        elif 'del-ip' == operation:
            utils.print_message("deallocating public IP '%s' from gateway "
                                "'%s' in VDC '%s'" %
                                (ip, gateway, vdc), cmd_proc)
            task = the_gateway.deallocate_public_ip(ip)
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't deallocate public IP: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        elif 'use' == operation:
            cmd_proc.gateway = gateway
            utils.print_message("Selected gateway '%s'" % gateway, cmd_proc)
        elif 'info' == operation:
            headers = ['Property', 'Value']
            table = cmd_proc.gateway_to_table(the_gateway)
            if cmd_proc.json_output:
                json_object = {'gateway':
                               utils.table_to_json(headers, table)}
                utils.print_json(json_object, cmd_proc=cmd_proc)
            else:
                utils.print_table("Details of gateway '%s' in VDC '%s', "
                                  "profile '%s':" %
                                  (gateway, vdc, cmd_proc.profile),
                                  headers, table, cmd_proc)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #18
0
def vapp(cmd_proc, operation, vdc, vapp, catalog, template, network, mode,
         vm_name, cust_file, media, disk_name, count, cpu, ram, ip):
    """Operations with vApps"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['vApp', "VMs", "Status", "Deployed", "Description"]
        table = cmd_proc.vapps_to_table(the_vdc)
        if cmd_proc.json_output:
            json_object = {'vapps': utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table(
                "Available vApps in '%s', profile '%s':" %
                (vdc, cmd_proc.profile), headers, table, cmd_proc)
    elif 'create' == operation:
        for x in xrange(1, count + 1):
            vapp_name = vapp
            if count > 1:
                vapp_name += '-' + str(x)
            utils.print_message(
                "creating vApp '%s' in VDC '%s'"
                " from template '%s' in catalog '%s'" %
                (vapp_name, vdc, template, catalog), cmd_proc)
            task = None
            if ((vm_name is not None) and ((cmd_proc.vca.version == "1.0") or
                                           (cmd_proc.vca.version == "1.5") or
                                           (cmd_proc.vca.version == "5.1") or
                                           (cmd_proc.vca.version == "5.5"))):
                task = cmd_proc.vca.create_vapp(vdc, vapp_name, template,
                                                catalog)
            else:
                task = cmd_proc.vca.create_vapp(vdc,
                                                vapp_name,
                                                template,
                                                catalog,
                                                vm_name=vm_name)
            if task:
                utils.display_progress(
                    task, cmd_proc,
                    cmd_proc.vca.vcloud_session.get_vcloud_headers())
            else:
                utils.print_error("can't create the vApp", cmd_proc)
                sys.exit(1)
            the_vdc = cmd_proc.vca.get_vdc(vdc)
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if ((vm_name is not None) and ((cmd_proc.vca.version == "1.0") or
                                           (cmd_proc.vca.version == "1.5") or
                                           (cmd_proc.vca.version == "5.1") or
                                           (cmd_proc.vca.version == "5.5"))):
                utils.print_message("setting VM name to '%s'" % (vm_name),
                                    cmd_proc)
                task = the_vapp.modify_vm_name(1, vm_name)
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't set VM name", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if vm_name is not None:
                utils.print_message(
                    "setting computer name for VM '%s'" % (vm_name), cmd_proc)
                task = the_vapp.customize_guest_os(vm_name,
                                                   computer_name=vm_name)
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't set computer name", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if cpu is not None:
                utils.print_message(
                    "configuring '%s' vCPUs for VM '%s', vApp '%s'" %
                    (cpu, vm_name, vapp_name), cmd_proc)
                task = the_vapp.modify_vm_cpu(vm_name, cpu)
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't configure virtual CPUs", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if ram is not None:
                utils.print_message(
                    "configuring '%s' MB of memory"
                    " for VM '%s', vApp '%s'" % (ram, vm_name, vapp_name),
                    cmd_proc)
                task = the_vapp.modify_vm_memory(vm_name, ram)
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't configure RAM", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if '' != network:
                utils.print_message(
                    "disconnecting VM from networks"
                    " pre-defined in the template", cmd_proc)
                task = the_vapp.disconnect_vms()
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't disconnect VM from networks",
                                      cmd_proc)
                    sys.exit(1)
                utils.print_message(
                    "disconnecting vApp from networks"
                    " pre-defined in the template", cmd_proc)
                task = the_vapp.disconnect_from_networks()
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't disconnect vApp from networks",
                                      cmd_proc)
                    sys.exit(1)
                nets = filter(lambda n: n.name == network,
                              cmd_proc.vca.get_networks(vdc))
                if len(nets) == 1:
                    utils.print_message(
                        "connecting vApp to network"
                        " '%s' with mode '%s'" % (network, mode), cmd_proc)
                    task = the_vapp.connect_to_network(nets[0].name,
                                                       nets[0].href)
                    if task:
                        utils.display_progress(
                            task, cmd_proc,
                            cmd_proc.vca.vcloud_session.get_vcloud_headers())
                    else:
                        utils.print_error(
                            "can't connect the vApp "
                            "to the network", cmd_proc)
                        sys.exit(1)
                    utils.print_message(
                        "connecting VM to network '%s'"
                        " with mode '%s'" % (network, mode), cmd_proc)
                    task = the_vapp.connect_vms(
                        nets[0].name,
                        connection_index=0,
                        ip_allocation_mode=mode.upper(),
                        mac_address=None,
                        ip_address=ip)
                    if task:
                        utils.display_progress(
                            task, cmd_proc,
                            cmd_proc.vca.vcloud_session.get_vcloud_headers())
                    else:
                        utils.print_error(
                            "can't connect the VM "
                            "to the network", cmd_proc)
                        sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("deleting vApp '%s' from VDC '%s'" % (vapp, vdc),
                            cmd_proc)
        task = cmd_proc.vca.delete_vapp(vdc, vapp)
        if task:
            utils.display_progress(
                task, cmd_proc,
                cmd_proc.vca.vcloud_session.get_vcloud_headers())
        else:
            utils.print_error("can't delete the vApp", cmd_proc)
            sys.exit(1)
    elif 'deploy' == operation:
        utils.print_message("deploying vApp '%s' to VDC '%s'" % (vapp, vdc),
                            cmd_proc)
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
        task = the_vapp.deploy()
        if task:
            utils.display_progress(
                task, cmd_proc,
                cmd_proc.vca.vcloud_session.get_vcloud_headers())
        else:
            utils.print_error("can't deploy vApp", cmd_proc)
            sys.exit(1)
    elif 'undeploy' == operation:
        utils.print_message(
            "undeploying vApp '%s' from VDC '%s'" % (vapp, vdc), cmd_proc)
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
        task = the_vapp.undeploy()
        if task:
            utils.display_progress(
                task, cmd_proc,
                cmd_proc.vca.vcloud_session.get_vcloud_headers())
        else:
            utils.print_error("can't undeploy vApp", cmd_proc)
            sys.exit(1)
    elif ('info' == operation or 'power-off' == operation
          or 'power-on' == operation):
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
            if the_vapp and the_vapp.me:
                if 'info' == operation:
                    headers = ['Entity', 'Attribute', 'Value']
                    table = cmd_proc.vapp_details_to_table(the_vapp)
                    if cmd_proc.json_output:
                        json_object = {
                            'vapp': utils.table_to_json(headers, table)
                        }
                        utils.print_json(json_object, cmd_proc=cmd_proc)
                    else:
                        utils.print_table(
                            "Details of vApp '%s', "
                            "profile '%s':" % (vapp, cmd_proc.profile),
                            headers, table, cmd_proc)
                else:
                    task = None
                    if 'power-on' == operation:
                        task = the_vapp.poweron()
                    elif 'power-off' == operation:
                        task = the_vapp.poweroff()
                    elif 'delete' == operation:
                        task = the_vapp.delete()
                    if task:
                        utils.display_progress(
                            task, cmd_proc,
                            cmd_proc.vca.vcloud_session.get_vcloud_headers())
                    else:
                        utils.print_error("can't operate with the vApp",
                                          cmd_proc)
                        sys.exit(1)
            else:
                utils.print_error("vApp '%s' not found" % vapp, cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("VDC '%s' not found" % vdc, cmd_proc)
            sys.exit(1)
    elif 'customize' == operation:
        utils.print_message("customizing VM '%s'"
                            "in vApp '%s' in VDC '%s'" % (vm_name, vapp, vdc))
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
        if the_vdc and the_vapp and cust_file:
            utils.print_message("uploading customization script", cmd_proc)
            task = the_vapp.customize_guest_os(vm_name, cust_file.read())
            if task:
                utils.display_progress(
                    task, cmd_proc,
                    cmd_proc.vca.vcloud_session.get_vcloud_headers())
                utils.print_message("deploying and starting the vApp",
                                    cmd_proc)
                task = the_vapp.force_customization(vm_name)
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't customize vApp", cmd_proc)
                    sys.exit(1)
            else:
                utils.print_error("can't customize vApp", cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("can't find the resource", cmd_proc)
            sys.exit(1)
    elif 'insert' == operation or 'eject' == operation:
        utils.print_message("%s media '%s', VM '%s'"
                            " in vApp '%s' in VDC '%s'" %
                            (operation, media, vm_name, vapp, vdc))
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
            if the_vapp:
                the_media = cmd_proc.vca.get_media(catalog, media)
                task = the_vapp.vm_media(vm_name, the_media, operation)
                if task:
                    utils.display_progress(
                        task, cmd_proc,
                        cmd_proc.vca.vcloud_session.get_vcloud_headers())
                else:
                    utils.print_error("can't insert or eject media", cmd_proc)
                    sys.exit(1)
            else:
                utils.print_error("vApp '%s' not found" % vapp, cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("VDC '%s' not found" % vdc, cmd_proc)
            sys.exit(1)
    elif 'attach' == operation or 'detach' == operation:
        utils.print_message("%s disk '%s', VM '%s'"
                            " in vApp '%s' in VDC '%s'" %
                            (operation, disk_name, vm_name, vapp, vdc))
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
            if the_vapp:
                link = filter(lambda link: link.get_name() == disk_name,
                              cmd_proc.vca.get_diskRefs(the_vdc))
                if len(link) == 1:
                    if 'attach' == operation:
                        task = the_vapp.attach_disk_to_vm(vm_name, link[0])
                    else:
                        task = the_vapp.detach_disk_from_vm(vm_name, link[0])
                    if task:
                        utils.display_progress(
                            task, cmd_proc,
                            cmd_proc.vca.vcloud_session.get_vcloud_headers())
                    else:
                        utils.print_error("can't attach or detach disk",
                                          cmd_proc)
                        sys.exit(1)
                elif len(link) == 0:
                    utils.print_error("disk not found", cmd_proc)
                    sys.exit(1)
                elif len(link) > 1:
                    utils.print_error(
                        "more than one disk found with "
                        "the same name", cmd_proc)
                    sys.exit(1)
            else:
                utils.print_error("vApp '%s' not found" % vapp, cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("VDC '%s' not found" % vdc, cmd_proc)
            sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #19
0
def cluster(cmd_proc, operation, cluster_name, cluster_id, node_count, vdc,
            network_name):
    """Operations with Container Service Extension"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if cmd_proc.vca.vcloud_session and \
       cmd_proc.vca.vcloud_session.organization:
        cse = Cluster(session=cmd_proc.vca.vcloud_session,
                      verify=cmd_proc.verify,
                      log=cmd_proc.vca.log)
        if 'list' == operation:
            headers = ['Name', 'Id', 'Status', 'Leader IP', 'Masters', 'Nodes']
            table1 = []
            try:
                clusters = cse.get_clusters()
            except Exception:
                utils.print_error("can't get list of clusters", cmd_proc, cse)
                sys.exit(1)
            n = 1
            for cluster in clusters:
                cluster['name'] = 'k8s-cluster-%s' % n
                n += 1
                table1.append([
                    cluster['name'],
                    cluster['cluster_id'],
                    cluster['status'],
                    cluster['leader_endpoint'],
                    len(cluster['master_nodes']),
                    len(cluster['nodes']),
                ])
            table = sorted(table1, key=operator.itemgetter(0), reverse=False)
            utils.print_table(
                "Available clusters in VDC '%s', profile '%s':" %
                (vdc, cmd_proc.profile), headers, table, cmd_proc)
        elif 'create' == operation:
            utils.print_message("creating cluster '%s'" % (cluster_name))
            try:
                r = cse.create_cluster(vdc, network_name, cluster_name,
                                       node_count)
                t = Task(session=cmd_proc.vca.vcloud_session,
                         verify=cmd_proc.verify,
                         log=cmd_proc.vca.log)
                task = t.get_task(r['task_id'])
                utils.display_progress(
                    task, cmd_proc,
                    cmd_proc.vca.vcloud_session.get_vcloud_headers())
            except Exception:
                utils.print_error("can't create cluster", cmd_proc, cse)
                sys.exit(1)
        elif 'delete' == operation:
            utils.print_message("deleting cluster with id '%s'" % (cluster_id))
            try:
                r = cse.delete_cluster(cluster_id)
                t = Task(session=cmd_proc.vca.vcloud_session,
                         verify=cmd_proc.verify,
                         log=cmd_proc.vca.log)
                task = t.get_task(r['task_id'])
                utils.display_progress(
                    task, cmd_proc,
                    cmd_proc.vca.vcloud_session.get_vcloud_headers())
            except Exception:
                utils.print_error("can't delete cluster", cmd_proc, cse)
                sys.exit(1)
        else:
            utils.print_error('not implemented', cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #20
0
ファイル: vca_cli_network.py プロジェクト: digideskio/vca-cli
def firewall(cmd_proc, operation, vdc, gateway, is_enable, description, policy,
             protocol, dest_port, dest_ip, source_port, source_ip,
             enable_logging, fw_rules_file):
    """Operations with Edge Gateway Firewall Service"""

    def proto(name):
        return name[0].upper() + name[1:].lower()

    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Source IP', 'Source Port', 'Destination IP',
                   'Destination Port', 'Protocol', 'Enabled']
        table = cmd_proc.firewall_rules_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'fw-rules':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Firewall rules in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'enable' == operation or 'disable' == operation:
        utils.print_message("%s firewall" % operation)
        the_gateway.enable_fw('enable' == operation)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task,
                                   cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't '%s' the firewall: " % operation +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'add' == operation:
        utils.print_message("add firewall rule")
        if fw_rules_file:
            rules = yaml.load(fw_rules_file)
            if rules and rules[0]:
                fw_rules = rules[0].get('Firewall_rules')
                for rule in fw_rules:
                    # Take defaults for is_enable, policy, protocol and
                    # enable_logging from cmdline switches (or their defaults)
                    the_gateway.add_fw_rule(rule.get('is_enable', is_enable),
                                            rule.get('description', None),
                                            rule.get('policy', policy),
                                            proto(
                                              rule.get('protocol', protocol)
                                            ),
                                            rule.get('dest_port'),
                                            rule.get('dest_ip'),
                                            rule.get('source_port'),
                                            rule.get('source_ip'),
                                            rule.get('enable_logging',
                                                     enable_logging))
        else:
            the_gateway.add_fw_rule(is_enable, description, policy,
                                    proto(protocol),
                                    dest_port, dest_ip, source_port, source_ip,
                                    enable_logging)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't add firewall rule: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("delete firewall rule")
        if fw_rules_file:
            rules = yaml.load(fw_rules_file)
            if rules and rules[0]:
                fw_rules = rules[0].get('Firewall_rules')
                for rule in fw_rules:
                    # Take default for protocol cmdline switch (or its default)
                    the_gateway.delete_fw_rule(proto(
                                                 rule.get('protocol', protocol)
                                               ),
                                               str(rule.get('dest_port')),
                                               rule.get('dest_ip'),
                                               str(rule.get('source_port')),
                                               rule.get('source_ip'))
        else:
            the_gateway.delete_fw_rule(proto(protocol), dest_port, dest_ip,
                                       source_port, source_ip)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't delete firewall rule: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)

    cmd_proc.save_current_config()
コード例 #21
0
ファイル: vca_cli_network.py プロジェクト: mlees19/vcd-cli
def nat(cmd_proc, operation, vdc, gateway, rule_type, original_ip,
        original_port, translated_ip, translated_port, protocol,
        network_name, nat_rules_file, all_rules):
    """Operations with Edge Gateway NAT Rules"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ["Rule Id", "Enabled", "Type", "Original IP",
                   "Original Port", "Translated IP", "Translated Port",
                   "Protocol", "Applied On"]
        table = cmd_proc.nat_rules_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'nat-rules':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("NAT rules in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'add' == operation:
        utils.print_message("add NAT rule")
        if nat_rules_file:
            rules = yaml.load(nat_rules_file)
            if rules and rules[0]:
                nat_rules = rules[0].get('NAT_rules')
                for rule in nat_rules:
                    the_gateway.add_nat_rule(rule.get('type').upper(),
                                             rule.get('original_ip'),
                                             rule.get('original_port'),
                                             rule.get('translated_ip'),
                                             rule.get('translated_port'),
                                             rule.get('protocol'),
                                             rule.get('network_name'))
        else:
            the_gateway.add_nat_rule(rule_type.upper(),
                                     original_ip,
                                     original_port,
                                     translated_ip,
                                     translated_port,
                                     protocol,
                                     network_name)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't add NAT rule: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("delete NAT rule")
        found_rule = False
        if all_rules:
            the_gateway.del_all_nat_rules()
            found_rule = True
        else:
            found_rule = the_gateway.del_nat_rule(rule_type.upper(),
                                                  original_ip,
                                                  original_port,
                                                  translated_ip,
                                                  translated_port,
                                                  protocol,
                                                  network_name)
        if found_rule:
            task = the_gateway.save_services_configuration()
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't delete NAT rule: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("rule doesn't exist in edge gateway", cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #22
0
ファイル: vca_cli_network.py プロジェクト: mlees19/vcd-cli
def gateway(cmd_proc, operation, vdc, gateway, ip):
    """Operations with Edge Gateway"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Name', 'External IPs', 'DHCP', 'Firewall', 'NAT',
                   'VPN', 'Routed Networks', 'Syslog', 'Uplinks', 'Selected']
        gateways = cmd_proc.vca.get_gateways(vdc)
        table = cmd_proc.gateways_to_table(gateways)
        if cmd_proc.json_output:
            json_object = {'gateways':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available gateways in '%s', profile '%s':" %
                              (vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif ('use' == operation or
          'info' == operation or
          'set-syslog' == operation or
          'add-ip' == operation or
          'del-ip' == operation):
        if 'set-syslog' == operation:
            task = the_gateway.set_syslog_conf(ip)
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't set syslog server: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        elif 'add-ip' == operation:
            utils.print_message("allocating public IP for gateway '%s' "
                                "in VDC '%s'" %
                                (gateway, vdc), cmd_proc)
            task = the_gateway.allocate_public_ip()
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't allocate public IP: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        elif 'del-ip' == operation:
            utils.print_message("deallocating public IP '%s' from gateway "
                                "'%s' in VDC '%s'" %
                                (ip, gateway, vdc), cmd_proc)
            task = the_gateway.deallocate_public_ip(ip)
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't deallocate public IP: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        elif 'use' == operation:
            cmd_proc.gateway = gateway
            utils.print_message("Selected gateway '%s'" % gateway, cmd_proc)
        elif 'info' == operation:
            headers = ['Property', 'Value']
            table = cmd_proc.gateway_to_table(the_gateway)
            if cmd_proc.json_output:
                json_object = {'gateway':
                               utils.table_to_json(headers, table)}
                utils.print_json(json_object, cmd_proc=cmd_proc)
            else:
                utils.print_table("Details of gateway '%s' in VDC '%s', "
                                  "profile '%s':" %
                                  (gateway, vdc, cmd_proc.profile),
                                  headers, table, cmd_proc)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #23
0
ファイル: vca_cli_network.py プロジェクト: mlees19/vcd-cli
def firewall(cmd_proc, operation, vdc, gateway, is_enable, description, policy,
             protocol, dest_port, dest_ip, source_port, source_ip,
             enable_logging, fw_rules_file):
    """Operations with Edge Gateway Firewall Service"""

    def proto(name):
        return name[0].upper() + name[1:].lower()

    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Description', 'Source IP', 'Source Port', 'Destination IP',
                   'Destination Port', 'Protocol', 'Enabled']
        table = cmd_proc.firewall_rules_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'fw-rules':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Firewall rules in gateway '%s', "
                              "VDC '%s', profile '%s' (firewall is %s):" %
                              (gateway, vdc, cmd_proc.profile,
                               'On' if the_gateway.is_fw_enabled()
                               else 'Off'),
                              headers, table, cmd_proc)
    elif 'enable' == operation or 'disable' == operation:
        utils.print_message("%s firewall" % operation)
        the_gateway.enable_fw('enable' == operation)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task,
                                   cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't '%s' the firewall: " % operation +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'add' == operation:
        utils.print_message("add firewall rule")
        if fw_rules_file:
            rules = yaml.load(fw_rules_file)
            if rules and rules[0]:
                fw_rules = rules[0].get('Firewall_rules')
                for rule in fw_rules:
                    # Take defaults for is_enable, policy, protocol and
                    # enable_logging from cmdline switches (or their defaults)
                    the_gateway.add_fw_rule(rule.get('is_enable', is_enable),
                                            rule.get('description', None),
                                            rule.get('policy', policy),
                                            proto(
                                                rule.get('protocol', protocol)
                                                ),
                                            rule.get('dest_port'),
                                            rule.get('dest_ip'),
                                            rule.get('source_port'),
                                            rule.get('source_ip'),
                                            rule.get('enable_logging',
                                                     enable_logging))
        else:
            the_gateway.add_fw_rule(is_enable, description, policy,
                                    proto(protocol),
                                    dest_port, dest_ip, source_port, source_ip,
                                    enable_logging)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't add firewall rule: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("delete firewall rule")
        if fw_rules_file:
            rules = yaml.load(fw_rules_file)
            if rules and rules[0]:
                fw_rules = rules[0].get('Firewall_rules')
                for rule in fw_rules:
                    # Take default for protocol cmdline switch (or its default)
                    the_gateway.delete_fw_rule(proto(
                                               rule.get('protocol', protocol)
                                               ),
                                               str(rule.get('dest_port')),
                                               rule.get('dest_ip'),
                                               str(rule.get('source_port')),
                                               rule.get('source_ip'))
        else:
            the_gateway.delete_fw_rule(proto(protocol), dest_port, dest_ip,
                                       source_port, source_ip)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't delete firewall rule: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)

    cmd_proc.save_current_config()
コード例 #24
0
ファイル: vca_cli_network.py プロジェクト: mlees19/vcd-cli
def network(cmd_proc, operation, vdc, gateway, network_name, gateway_ip,
            netmask, dns1, dns2, dns_suffix, pool):
    """Operations with Networks"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Name', 'Mode', 'Gateway', 'Netmask', 'DNS 1', 'DNS 2',
                   'Pool IP Range']
        networks = cmd_proc.vca.get_networks(vdc)
        table = cmd_proc.networks_to_table(networks)
        if cmd_proc.json_output:
            json_object = {'networks':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available networks in '%s', profile '%s':" %
                              (vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'create' == operation:
        utils.print_message("Creating network '%s' "
                            "in VDC '%s'" %
                            (network_name, vdc), cmd_proc)
        start_address = pool.split('-')[0]
        end_address = pool.split('-')[1]
        result = cmd_proc.vca.create_vdc_network(vdc, network_name,
                                                 gateway,
                                                 start_address, end_address,
                                                 gateway_ip,
                                                 netmask, dns1, dns2,
                                                 dns_suffix)
        if result[0]:
            utils.display_progress(result[1], cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't create network", cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        result = cmd_proc.vca.delete_vdc_network(vdc, network_name)
        if result[0]:
            utils.display_progress(result[1], cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't delete network", cmd_proc)
            sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #25
0
ファイル: vca_cli_example.py プロジェクト: KohliDev/vca-cli
def example(ctx):
    """vCloud Air CLI Examples"""
    headers = ['Id', 'Example', 'Flavor', 'Command']
    example_id = 0
    table = []
    example_id += 1
    table.append([example_id, 'login to service', 'vCA',
                  'vca login [email protected] --password ****'])
    example_id += 1
    table.append([example_id, 'login to an instance', 'vCA',
                  'vca login [email protected] --password ****'
                  ' --instance c40ba6b4-c158-49fb-b164-5c66f90344fa'])
    example_id += 1
    table.append([example_id, 'login to a virtual data center', 'vCA',
                  'vca login [email protected] --password ****'
                  ' --instance c40ba6b4-c158-49fb-b164-5c66f90344fa'
                  ' --vdc VDC1'])
    example_id += 1
    table.append([example_id, 'login to service', 'vCHS',
                  'vca login [email protected] --password ****'
                  ' --host vchs.vmware.com --version 5.6'])
    example_id += 1
    table.append([example_id, 'login to an instance', 'vCHS',
                  'vca login [email protected] --password ****'
                  ' --host vchs.vmware.com --version 5.6'
                  ' --instance 55-234 --org MyOrg'])
    example_id += 1
    table.append([example_id, 'login to a virtual data center', 'vCHS',
                  'vca login [email protected] --password ****'
                  ' --host vchs.vmware.com --version 5.6'
                  ' --instance 55-234 --org MyOrg --vdc MyVDC'])
    example_id += 1
    table.append([example_id, 'login to vCloud Director', 'Standalone',
                  'vca login [email protected] --password ****'
                  ' --host myvcloud.company.com --version 5.5 --org MyOrg'])
    example_id += 1
    table.append([example_id, 'login to vCloud Director and VDC',
                  'Standalone',
                  'vca login [email protected] --password ****'
                  ' --host myvcloud.company.com --version 5.5 --org MyOrg'
                  ' --vdc MyVDC'])
    example_id += 1
    table.append([example_id, 'list available instances', 'vCA, vCHS',
                  'vca instance'])
    example_id += 1
    table.append([example_id, 'show details of instance', 'vCA, vCHS',
                  'vca instance info --instance '
                  'c40ba6b4-c158-49fb-b164-5c66f90344fa'])
    example_id += 1
    table.append([example_id, 'select an instance', 'vCA',
                  'vca instance use --instance '
                  'c40ba6b4-c158-49fb-b164-5c66f90344fa'])
    example_id += 1
    table.append([example_id, 'select an instance and VDC', 'vCA',
                  'vca instance use --instance '
                  'c40ba6b4-c158-49fb-b164-5c66f90344fa '
                  '--vdc MyVDC'])
    example_id += 1
    table.append([example_id, 'select an instance', 'vCHS',
                  'vca instance use --instance '
                  'M684216431-4470 --org M684216431-4470'])
    example_id += 1
    table.append([example_id, 'list organizations', 'All',
                  'vca org'])
    example_id += 1
    table.append([example_id, 'show organization details', 'All',
                  'vca org info'])
    example_id += 1
    table.append([example_id, 'select an organization', 'vCHS',
                  'vca org use --instance 55-234 --org MyOrg'])
    example_id += 1
    table.append([example_id, 'list VDC templates', 'All',
                  'vca org list-templates'])
    example_id += 1
    table.append([example_id, 'list VDC', 'All',
                  'vca vdc'])
    example_id += 1
    table.append([example_id, 'select VDC', 'All',
                  'vca vdc use --vdc vdc1'])
    example_id += 1
    table.append([example_id, 'create VDC', 'All',
                  'vca vdc create --vdc vdc2 --template d2p3v29-new-tp'])
    example_id += 1
    table.append([example_id, 'delete VDC (will prompt to confirm)', 'All',
                  'vca vdc delete --vdc vdc2'])
    example_id += 1
    table.append([example_id, 'delete VDC without prompt', 'All',
                  'vca vdc delete --vdc vdc2 --yes'])
    example_id += 1
    table.append([example_id, 'list catalogs and items', 'All',
                  'vca catalog'])
    example_id += 1
    table.append([example_id, 'create catalog', 'All',
                  'vca catalog create --catalog mycatalog'])
    example_id += 1
    table.append([example_id, 'delete catalog', 'All',
                  'vca catalog delete --catalog mycatalog'])
    example_id += 1
    table.append([example_id, 'delete catalog item', 'All',
                  'vca catalog delete-item --catalog mycatalog'
                  ' --item my_vapp_template'])
    example_id += 1
    table.append([example_id, 'upload media file (ISO) to catalog',
                  'All',
                  'vca catalog upload --catalog mycatalog'
                  ' --item esxi.iso --description ESXi-iso'
                  ' --file ~/VMware-VMvisor.iso'])
    example_id += 1
    table.append([example_id, 'list vApps',
                  'All',
                  'vca vapp'])
    example_id += 1
    table.append([example_id, 'create vApp',
                  'All',
                  'vca vapp create --vapp myvapp --vm myvm'
                  ' --catalog'
                  ' \'Public Catalog\' --template \'Ubuntu'
                  ' Server 12.04 LTS (amd64 20150127)\''])
    example_id += 1
    table.append([example_id, 'create vApp',
                  'All',
                  'vca vapp create -a myvapp -V myvm -c'
                  ' mycatalog -t mytemplate'
                  ' -n default-routed-network -m pool'])
    example_id += 1
    table.append([example_id, 'create vApp with manually assigned IP',
                  'All',
                  'vca vapp create -a myvapp -V myvm -c'
                  ' mycatalog -t mytemplate'
                  ' -n default-routed-network -mode manual'
                  ' --ip 192.168.109.25'])
    example_id += 1
    table.append([example_id, 'create multiple vApps',
                  'All',
                  'vca vapp create -a myvapp -V myvm -c'
                  ' mycatalog -t mytemplate'
                  ' -n default-routed-network -m pool'
                  ' --count 10'])
    example_id += 1
    table.append([example_id, 'create vApp and configure VM size',
                  'All',
                  'vca vapp create -a myvapp -V myvm -c'
                  ' mycatalog -t mytemplate'
                  ' -n default-routed-network -m pool'
                  ' --cpu 4 --ram 4096'])
    example_id += 1
    table.append([example_id, 'delete vApp',
                  'All',
                  'vca vapp delete --vapp myvapp'])
    example_id += 1
    table.append([example_id, 'show vApp details in JSON',
                  'All',
                  'vca -j vapp info --vapp myvapp'])
    example_id += 1
    table.append([example_id, 'deploy vApp',
                  'All',
                  'vca vapp deploy --vapp ubu'])
    example_id += 1
    table.append([example_id, 'undeploy vApp',
                  'All',
                  'vca vapp undeploy --vapp ubu'])
    example_id += 1
    table.append([example_id, 'power on vApp',
                  'All',
                  'vca vapp power-on --vapp ubu'])
    example_id += 1
    table.append([example_id, 'power off vApp',
                  'All',
                  'vca vapp power-off --vapp ubu'])
    example_id += 1
    table.append([example_id, 'customize vApp VM',
                  'All',
                  'vca vapp customize --vapp ubu --vm ubu'
                  ' --file add_public_ssh_key.sh'])
    example_id += 1
    table.append([example_id, 'insert ISO to vApp VM',
                  'All',
                  'vca vapp insert --vapp coreos1 --vm coreos1'
                  ' --catalog default-catalog'
                  ' --media coreos1-config.iso'])
    example_id += 1
    table.append([example_id, 'eject ISO from vApp VM',
                  'All',
                  'vca vapp eject --vapp coreos1 --vm coreos1'
                  ' --catalog default-catalog'
                  ' --media coreos1-config.iso'])
    example_id += 1
    table.append([example_id, 'attach disk to vApp VM',
                  'All',
                  'vca vapp attach --vapp myvapp'
                  ' --vm myvm --disk mydisk'])
    example_id += 1
    table.append([example_id, 'detach disk from vApp VM',
                  'All',
                  'vca vapp detach --vapp myvapp'
                  ' --vm myvm --disk mydisk'])
    example_id += 1
    table.append([example_id, 'list independent disks',
                  'All',
                  'vca vapp disk'])
    example_id += 1
    table.append([example_id, 'create independent disk of 100GB',
                  'All',
                  'vca disk create --disk mydisk'
                  ' --size 100'])
    example_id += 1
    table.append([example_id, 'delete independent disk by name',
                  'All',
                  'vca disk delete --disk mydisk'])
    example_id += 1
    table.append([example_id, 'delete independent disk by id',
                  'All',
                  'vca disk delete'
                  ' --id bce76ca7-29d0-4041-82d4-e4481804d5c4'])
    example_id += 1
    table.append([example_id, 'list VMs',
                  'All',
                  'vca vm'])
    example_id += 1
    table.append([example_id, 'list VMs in a vApp',
                  'All',
                  'vca vm -a ubu'])
    example_id += 1
    table.append([example_id, 'list VMs in JSON format',
                  'All',
                  'vca -j vm'])
    example_id += 1
    table.append([example_id, 'retrieve the IP of a VM',
                  'All',
                  "IP=`vca -j vm -a ubu | jq -r"
                  " '.vms[0].IPs[0]'` && echo $IP"])
    example_id += 1
    table.append([example_id, 'list networks',
                  'All',
                  'vca network'])
    example_id += 1
    table.append([example_id, 'create network',
                  'All',
                  'vca network create --network net-117'
                  ' --gateway-ip 192.168.117.1'
                  ' --netmask 255.255.255.0'
                  ' --dns1 8.8.8.8'
                  ' --pool 192.168.117.2-192.168.117.100'])
    example_id += 1
    table.append([example_id, 'delete network',
                  'All',
                  'vca network delete --network net-117'])
    example_id += 1
    table.append([example_id, 'list edge gateways',
                  'All',
                  'vca gateway'])
    example_id += 1
    table.append([example_id, 'get details of edge gateway',
                  'All',
                  'vca gateway info'])
    example_id += 1
    table.append([example_id, 'set syslog server on gateway',
                  'All',
                  'vca gateway set-syslog'
                  ' --ip 192.168.109.2'])
    example_id += 1
    table.append([example_id, 'unset syslog server on gateway',
                  'All',
                  'vca gateway set-syslog'])
    example_id += 1
    table.append([example_id, 'allocate external IP address',
                  'vCA',
                  'vca gateway add-ip'])
    example_id += 1
    table.append([example_id, 'release external IP address',
                  'vCA',
                  'vca gateway del-ip --ip 107.189.93.162'])
    example_id += 1
    table.append([example_id, 'list edge gateway firewall rules',
                  'All',
                  'vca firewall'])
    example_id += 1
    table.append([example_id, 'enable edge gateway firewall',
                  'All',
                  'vca firewall enable'])
    example_id += 1
    table.append([example_id, 'disable edge gateway firewall',
                  'All',
                  'vca firewall disable'])
    example_id += 1
    table.append([example_id, 'enable DHCP service',
                  'All',
                  'vca dhcp enable'])
    example_id += 1
    table.append([example_id, 'disable DHCP service',
                  'All',
                  'vca dhcp disable'])
    example_id += 1
    table.append([example_id, 'add DHCP service to a network',
                  'All',
                  'vca dhcp add --network net-117 '
                  '--pool 192.168.117.101-192.168.117.200'])
    example_id += 1
    table.append([example_id, 'remove DHCP service from a network',
                  'All',
                  'vca dhcp delete --network net-117'])
    example_id += 1
    table.append([example_id, 'list edge gateway NAT rules',
                  'All',
                  'vca nat'])
    example_id += 1
    table.append([example_id, 'add edge gateway DNAT rule',
                  'All',
                  "vca nat add --type dnat"
                  " --original-ip 107.189.93.162"
                  " --original-port 22"
                  " --translated-ip 192.168.109.2"
                  " --translated-port 22 --protocol tcp"])
    example_id += 1
    table.append([example_id, 'add edge gateway SNAT rule',
                  'All',
                  "vca nat add --type snat"
                  " --original-ip 192.168.109.0/24"
                  " --translated-ip 107.189.93.162"])
    example_id += 1
    table.append([example_id, 'add SNAT rule to network',
                  'All',
                  "vca nat add --type snat"
                  " --original-ip 192.168.109.0/24"
                  " --translated-ip 107.189.93.162"
                  " --network net-109"])
    example_id += 1
    table.append([example_id, 'add edge gateway rules from file',
                  'All',
                  "vca nat add --file natrules.yaml"])
    example_id += 1
    table.append([example_id, 'delete edge gateway NAT rule',
                  'All',
                  "vca nat delete --type dnat "
                  " --original-ip 107.189.93.162"
                  " --original-port 22"
                  " --translated-ip 192.168.109.4"
                  " --translated-port 22"
                  " --protocol tcp"])
    example_id += 1
    table.append([example_id, 'delete all edge gateway NAT rules',
                  'All',
                  "vca nat delete --all"])
    example_id += 1
    table.append([example_id, 'list edge gateway VPN config',
                  'All',
                  'vca vpn'])
    example_id += 1
    table.append([example_id, 'enable edge gateway VPN',
                  'All',
                  "vca vpn enable"])
    example_id += 1
    table.append([example_id, 'disable edge gateway VPN',
                  'All',
                  "vca vpn disable"])
    example_id += 1
    table.append([example_id, 'add VPN endpoint',
                  'All',
                  "vca vpn add-endpoint --network d1p10-ext"
                  " --public-ip 107.189.123.101"])
    example_id += 1
    table.append([example_id, 'delete VPN endpoint',
                  'All',
                  "vca vpn del-endpoint --network d1p10-ext"
                  " --public-ip 107.189.123.101"])
    example_id += 1
    table.append([example_id, 'add VPN tunnel',
                  'All',
                  "vca vpn add-tunnel --tunnel t1"
                  " --local-ip 107.189.123.101"
                  " --local-network net-116"
                  " --peer-ip 192.240.158.15"
                  " --peer-network 192.168.110.0/24 "
                  " --secret P8s3P...7v"])
    example_id += 1
    table.append([example_id, 'delete VPN tunnel',
                  'All',
                  "vca vpn del-tunnel --tunnel t1"])
    example_id += 1
    table.append([example_id, 'add local network to VPN tunnel',
                  'All',
                  "vca vpn add-network --tunnel t1"
                  " --local-network net-115"])
    example_id += 1
    table.append([example_id, 'add peer network to VPN tunnel',
                  'All',
                  "vca vpn add-network --tunnel t1"
                  " --peer-network 192.168.117.0/24"])
    example_id += 1
    table.append([example_id, 'delete local network from VPN tunnel',
                  'All',
                  "vca vpn del-network --tunnel t1"
                  " --local-network net-115"])
    example_id += 1
    table.append([example_id, 'delete peer network from VPN tunnel',
                  'All',
                  "vca vpn del-network --tunnel t1"
                  " --peer-network 192.168.117.0/24"])
    example_id += 1
    table.append([example_id, 'list user roles', 'vCA',
                  'vca role'])
    example_id += 1
    table.append([example_id, 'list users', 'vCA',
                  'vca user'])
    example_id += 1
    table.append([example_id, 'change current user password', 'vCA',
                  'vca user change-password --password current-pass'
                  ' --new-password new-pass'])
    example_id += 1
    table.append([example_id, 'create user', 'vCA',
                  'vca user create --user [email protected] --first Name'
                  " --last Name --roles 'Virtual Infrastructure Administrator,"
                  " Network Administrator'"])
    example_id += 1
    table.append([example_id, 'delete user', 'vCA',
                  'vca user delete --id 65737432-9159-418b-945d-e10264130ccb'])
    example_id += 1
    table.append([example_id, 'reset user password', 'vCA',
                  'vca user reset-password --id '
                  '65737432-9159-418b-945d-e10264130ccb'])
    example_id += 1
    table.append([example_id, 'list blueprints',
                  'vCA',
                  'vca blueprint'])
    example_id += 1
    table.append([example_id, 'validate blueprint',
                  'vCA',
                  'vca blueprint validate '
                  '--file helloworld/blueprint.yaml'])
    example_id += 1
    table.append([example_id, 'upload blueprint',
                  'vCA',
                  'vca blueprint upload --blueprint helloworld '
                  '--file helloworld/blueprint.yaml'])
    example_id += 1
    table.append([example_id, 'show details of a blueprint',
                  'vCA',
                  'vca blueprint info --blueprint helloworld'])
    example_id += 1
    table.append([example_id, 'delete blueprint',
                  'vCA',
                  'vca blueprint delete --blueprint helloworld'])
    example_id += 1
    table.append([example_id, 'create deployment',
                  'vCA',
                  'vca deployment create --blueprint helloworld '
                  '--deployment d1 --file inputs.yaml'])
    table.append([example_id, 'list deployments',
                  'vCA',
                  'vca deployment'])
    example_id += 1
    table.append([example_id, 'show details of a deployment',
                  'vCA',
                  'vca deployment info --deployment d1'])
    example_id += 1
    table.append([example_id, 'execute deployment workflow',
                  'vCA',
                  'vca deployment execute --deployment d1 '
                  '--workflow install'])
    example_id += 1
    table.append([example_id, 'delete deployment',
                  'vCA',
                  'vca deployment delete --deployment d1'])
    example_id += 1
    table.append([example_id, 'list workflow execution events',
                  'vCA',
                  'vca event list --id f98df6cf-08d8-47fa-947f-67c15337efae'])
    example_id += 1
    table.append([example_id, 'list workflow execution events',
                  'vCA',
                  'vca event list --id f98df6cf-08d8-47fa-947f-67c15337efae '
                  '--show-logs'])
    example_id += 1
    table.append([example_id, 'show status', 'All',
                  'vca status'])
    example_id += 1
    table.append([example_id, 'show status and password', 'All',
                  'vca status --show-password'])
    example_id += 1
    table.append([example_id, 'list profiles', 'All',
                  'vca profile'])
    example_id += 1
    table.append([example_id, 'switch to a profile', 'All',
                  'vca --profile p1 <command>'])
    example_id += 1
    table.append([example_id, 'send debug to $TMPDIR/pyvcloud.log', 'All',
                  'vca --debug vm'])
    example_id += 1
    table.append([example_id, 'show version', 'All',
                  'vca --version'])
    example_id += 1
    table.append([example_id, 'show general help', 'All',
                  'vca --help'])
    example_id += 1
    table.append([example_id, 'show command help', 'All',
                  'vca <command> --help'])
    utils.print_table('vca-cli usage examples:',
                      headers, table)
コード例 #26
0
ファイル: vca_cli_compute.py プロジェクト: xcompass/vca-cli
def vapp(cmd_proc, operation, vdc, vapp, catalog, template,
         network, mode, vm_name, cust_file,
         media, disk_name, count, cpu, ram, ip):
    """Operations with vApps"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['vApp', "VMs", "Status", "Deployed", "Description"]
        table = cmd_proc.vapps_to_table(the_vdc)
        if cmd_proc.json_output:
            json_object = {'vapps':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available vApps in '%s', profile '%s':" %
                              (vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'create' == operation:
        for x in xrange(1, count + 1):
            vapp_name = vapp
            if count > 1:
                vapp_name += '-' + str(x)
            utils.print_message("creating vApp '%s' in VDC '%s'"
                                " from template '%s' in catalog '%s'" %
                                (vapp_name, vdc, template, catalog),
                                cmd_proc)
            task = None
            if ((vm_name is not None) and
                ((cmd_proc.vca.version == "1.0") or
                 (cmd_proc.vca.version == "1.5") or
                 (cmd_proc.vca.version == "5.1") or
                 (cmd_proc.vca.version == "5.5"))):
                task = cmd_proc.vca.create_vapp(vdc, vapp_name,
                                                template, catalog)
            else:
                task = cmd_proc.vca.create_vapp(vdc, vapp_name,
                                                template, catalog,
                                                vm_name=vm_name)
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                utils.print_error("can't create the vApp", cmd_proc)
                sys.exit(1)
            the_vdc = cmd_proc.vca.get_vdc(vdc)
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if ((vm_name is not None) and
                ((cmd_proc.vca.version == "1.0") or
                 (cmd_proc.vca.version == "1.5") or
                 (cmd_proc.vca.version == "5.1") or
                 (cmd_proc.vca.version == "5.5"))):
                utils.print_message(
                    "setting VM name to '%s'"
                    % (vm_name), cmd_proc)
                task = the_vapp.modify_vm_name(1, vm_name)
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't set VM name", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if vm_name is not None:
                utils.print_message(
                    "setting computer name for VM '%s'"
                    % (vm_name), cmd_proc)
                task = the_vapp.customize_guest_os(vm_name,
                                                   computer_name=vm_name)
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't set computer name", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if cpu is not None:
                utils.print_message(
                    "configuring '%s' vCPUs for VM '%s', vApp '%s'"
                    % (cpu, vm_name, vapp_name), cmd_proc)
                task = the_vapp.modify_vm_cpu(vm_name, cpu)
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't configure virtual CPUs", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if ram is not None:
                utils.print_message("configuring '%s' MB of memory"
                                    " for VM '%s', vApp '%s'"
                                    % (ram, vm_name, vapp_name), cmd_proc)
                task = the_vapp.modify_vm_memory(vm_name, ram)
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't configure RAM", cmd_proc)
                    sys.exit(1)
                the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
            if '' != network:
                utils.print_message("disconnecting VM from networks"
                                    " pre-defined in the template", cmd_proc)
                task = the_vapp.disconnect_vms()
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't disconnect VM from networks",
                                      cmd_proc)
                    sys.exit(1)
                utils.print_message("disconnecting vApp from networks"
                                    " pre-defined in the template", cmd_proc)
                task = the_vapp.disconnect_from_networks()
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't disconnect vApp from networks",
                                      cmd_proc)
                    sys.exit(1)
                nets = filter(lambda n: n.name == network,
                              cmd_proc.vca.get_networks(vdc))
                if len(nets) == 1:
                    utils.print_message("connecting vApp to network"
                                        " '%s' with mode '%s'" %
                                        (network, mode), cmd_proc)
                    task = the_vapp.connect_to_network(
                        nets[0].name, nets[0].href)
                    if task:
                        utils.display_progress(task, cmd_proc,
                                               cmd_proc.vca.vcloud_session.
                                               get_vcloud_headers())
                    else:
                        utils.print_error("can't connect the vApp "
                                          "to the network",
                                          cmd_proc)
                        sys.exit(1)
                    utils.print_message("connecting VM to network '%s'"
                                        " with mode '%s'" % (network, mode),
                                        cmd_proc)
                    task = the_vapp.connect_vms(
                        nets[0].name,
                        connection_index=0,
                        ip_allocation_mode=mode.upper(),
                        mac_address=None, ip_address=ip)
                    if task:
                        utils.display_progress(task,
                                               cmd_proc,
                                               cmd_proc.vca.vcloud_session.
                                               get_vcloud_headers())
                    else:
                        utils.print_error("can't connect the VM "
                                          "to the network", cmd_proc)
                        sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("deleting vApp '%s' from VDC '%s'" % (vapp, vdc),
                            cmd_proc)
        task = cmd_proc.vca.delete_vapp(vdc, vapp)
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't delete the vApp", cmd_proc)
            sys.exit(1)
    elif 'deploy' == operation:
        utils.print_message("deploying vApp '%s' to VDC '%s'" % (vapp, vdc),
                            cmd_proc)
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
        task = the_vapp.deploy()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't deploy vApp", cmd_proc)
            sys.exit(1)
    elif 'undeploy' == operation:
        utils.print_message("undeploying vApp '%s' from VDC '%s'" %
                            (vapp, vdc), cmd_proc)
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
        task = the_vapp.undeploy()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't undeploy vApp", cmd_proc)
            sys.exit(1)
    elif ('info' == operation or 'power-off' == operation or
          'power-on' == operation):
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
            if the_vapp and the_vapp.me:
                if 'info' == operation:
                    headers = ['Entity', 'Attribute', 'Value']
                    table = cmd_proc.vapp_details_to_table(the_vapp)
                    if cmd_proc.json_output:
                        json_object = {'vapp':
                                       utils.table_to_json(headers, table)}
                        utils.print_json(json_object, cmd_proc=cmd_proc)
                    else:
                        utils.print_table("Details of vApp '%s', "
                                          "profile '%s':" %
                                          (vapp, cmd_proc.profile),
                                          headers, table, cmd_proc)
                else:
                    task = None
                    if 'power-on' == operation:
                        task = the_vapp.poweron()
                    elif 'power-off' == operation:
                        task = the_vapp.poweroff()
                    elif 'delete' == operation:
                        task = the_vapp.delete()
                    if task:
                        utils.display_progress(task,
                                               cmd_proc,
                                               cmd_proc.vca.vcloud_session.
                                               get_vcloud_headers())
                    else:
                        utils.print_error("can't operate with the vApp",
                                          cmd_proc)
                        sys.exit(1)
            else:
                utils.print_error("vApp '%s' not found" % vapp, cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("VDC '%s' not found" % vdc, cmd_proc)
            sys.exit(1)
    elif 'customize' == operation:
        utils.print_message("customizing VM '%s'"
                            "in vApp '%s' in VDC '%s'" %
                            (vm_name, vapp, vdc))
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
        if the_vdc and the_vapp and cust_file:
            utils.print_message("uploading customization script", cmd_proc)
            task = the_vapp.customize_guest_os(vm_name, cust_file.read())
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
                utils.print_message("deploying and starting the vApp",
                                    cmd_proc)
                task = the_vapp.force_customization(vm_name)
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't customize vApp", cmd_proc)
                    sys.exit(1)
            else:
                utils.print_error("can't customize vApp", cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("can't find the resource",
                              cmd_proc)
            sys.exit(1)
    elif 'insert' == operation or 'eject' == operation:
        utils.print_message("%s media '%s', VM '%s'"
                            " in vApp '%s' in VDC '%s'" %
                            (operation, media, vm_name, vapp, vdc))
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
            if the_vapp:
                the_media = cmd_proc.vca.get_media(catalog, media)
                task = the_vapp.vm_media(vm_name, the_media, operation)
                if task:
                    utils.display_progress(task, cmd_proc,
                                           cmd_proc.vca.vcloud_session.
                                           get_vcloud_headers())
                else:
                    utils.print_error("can't insert or eject media",
                                      cmd_proc)
                    sys.exit(1)
            else:
                utils.print_error("vApp '%s' not found" % vapp, cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("VDC '%s' not found" % vdc, cmd_proc)
            sys.exit(1)
    elif 'attach' == operation or 'detach' == operation:
        utils.print_message("%s disk '%s', VM '%s'"
                            " in vApp '%s' in VDC '%s'" %
                            (operation, disk_name, vm_name, vapp, vdc))
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp)
            if the_vapp:
                link = filter(lambda link:
                              link.get_name() == disk_name,
                              cmd_proc.vca.get_diskRefs(the_vdc))
                if len(link) == 1:
                    if 'attach' == operation:
                        task = the_vapp.attach_disk_to_vm(vm_name,
                                                          link[0])
                    else:
                        task = the_vapp.detach_disk_from_vm(vm_name,
                                                            link[0])
                    if task:
                        utils.display_progress(task, cmd_proc,
                                               cmd_proc.vca.vcloud_session.
                                               get_vcloud_headers())
                    else:
                        utils.print_error("can't attach or detach disk",
                                          cmd_proc)
                        sys.exit(1)
                elif len(link) == 0:
                    utils.print_error("disk not found", cmd_proc)
                    sys.exit(1)
                elif len(link) > 1:
                    utils.print_error("more than one disk found with "
                                      "the same name",
                                      cmd_proc)
                    sys.exit(1)
            else:
                utils.print_error("vApp '%s' not found" % vapp, cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("VDC '%s' not found" % vdc, cmd_proc)
            sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #27
0
ファイル: vca_cli_network.py プロジェクト: mlees19/vcd-cli
def vpn(cmd_proc, operation, vdc, gateway, network_name, public_ip, local_ip,
        local_network, peer_ip, peer_network, tunnel, secret):
    """Operations with Edge Gateway VPN"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers1 = ['EndPoint', 'Public IP']
        table1 = cmd_proc.vpn_endpoints_to_table(the_gateway)
        headers2 = ['Tunnel', 'Local IP', 'Local Networks', 'Peer IP',
                    'Peer Networks', 'Enabled', 'Operational']
        table2 = cmd_proc.vpn_tunnels_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'endpoints': table1, 'tunnels': table2}
            utils.print_json(json_object, 'VPN config',
                             cmd_proc)
        else:
            utils.print_table("VPN endpoints in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers1, table1, cmd_proc)
            utils.print_table("VPN tunnels in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers2, table2, cmd_proc)
    else:
        if 'enable' == operation or 'disable' == operation:
            utils.print_message("%s VPN service" % operation)
            the_gateway.enable_vpn('enable' == operation)
        elif 'add-endpoint' == operation:
            utils.print_message("add VPN endpoint")
            the_gateway.add_vpn_endpoint(network_name, public_ip)
        elif 'del-endpoint' == operation:
            utils.print_message("delete VPN endpoint")
            the_gateway.del_vpn_endpoint(network_name, public_ip)
        elif 'add-tunnel' == operation:
            utils.print_message("add VPN tunnel")
            the_gateway.add_vpn_tunnel(tunnel, local_ip, local_network,
                                       peer_ip, peer_network, secret)
        elif 'del-tunnel' == operation:
            utils.print_message("delete VPN tunnel")
            the_gateway.delete_vpn_tunnel(tunnel)
        elif 'add-network' == operation:
            utils.print_message("add network to VPN tunnel")
            the_gateway.add_network_to_vpn_tunnel(tunnel, local_network,
                                                  peer_network)
        elif 'del-network' == operation:
            utils.print_message("delete network from VPN tunnel")
            the_gateway.delete_network_from_vpn_tunnel(tunnel, local_network,
                                                       peer_network)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't update VPN configuration: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #28
0
ファイル: vca_cli_compute.py プロジェクト: xcompass/vca-cli
def vdc(cmd_proc, operation, vdc, template, yes):
    """Operations with Virtual Data Centers"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Virtual Data Center', "Selected"]
        table = ['', '']
        if cmd_proc.vca.vcloud_session and \
           cmd_proc.vca.vcloud_session.organization:
            links = (cmd_proc.vca.vcloud_session.organization.Link if
                     cmd_proc.vca.vcloud_session.organization else [])
            table1 = [[details.get_name(),
                      '*' if details.get_name() == cmd_proc.vdc_name else '']
                      for details in filter(lambda info: info.name and
                      (info.type_ == 'application/vnd.vmware.vcloud.vdc+xml'),
                      links)]
            table = sorted(table1, key=operator.itemgetter(0), reverse=False)
        utils.print_table(
            "Available Virtual Data Centers in org '%s', profile '%s':" %
            (cmd_proc.vca.org, cmd_proc.profile),
            headers, table, cmd_proc)
    elif 'use' == operation:
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc is not None:
            utils.print_message("Using vdc '%s', profile '%s'" %
                                (vdc, cmd_proc.profile), cmd_proc)
            cmd_proc.vdc_name = vdc
            cmd_proc.select_default_gateway()
        else:
            utils.print_error("Unable to select vdc '%s' in profile '%s'" %
                              (vdc, cmd_proc.profile), cmd_proc)
            sys.exit(1)
    elif 'info' == operation:
        if vdc is None:
            vdc = cmd_proc.vdc_name
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            gateways = cmd_proc.vca.get_gateways(vdc)
            headers1 = ['Type', 'Name']
            table1 = cmd_proc.vdc_to_table(the_vdc, gateways)
            headers2 = ['Resource', 'Allocated',
                        'Limit', 'Reserved', 'Used', 'Overhead']
            table2 = cmd_proc.vdc_resources_to_table(the_vdc)
            headers3 = ['Name', 'External IPs', 'DHCP', 'Firewall', 'NAT',
                        'VPN', 'Networks', 'Syslog', 'Uplinks', 'Selected']
            table3 = cmd_proc.gateways_to_table(gateways)
            if cmd_proc.json_output:
                json_object = {'vdc_entities':
                               utils.table_to_json(headers1, table1),
                               'vdc_resources':
                               utils.table_to_json(headers2, table2),
                               'gateways':
                               utils.table_to_json(headers3, table3)}
                utils.print_json(json_object, cmd_proc=cmd_proc)
            else:
                utils.print_table(
                    "Details of Virtual Data Center '%s', profile '%s':" %
                    (vdc, cmd_proc.profile),
                    headers1, table1, cmd_proc)
                utils.print_table("Compute capacity:",
                                  headers2, table2, cmd_proc)
                utils.print_table('Gateways:',
                                  headers3, table3, cmd_proc)
        else:
            utils.print_error("Unable to select VDC %s, profile '%s': "
                              "VDC not found" %
                              (vdc, cmd_proc.profile), cmd_proc)
            sys.exit(1)
    elif 'create' == operation:
        if vdc is None:
            utils.print_error('please enter new VDC name')
            sys.exit(1)
        task = cmd_proc.vca.create_vdc(vdc, template)
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't create the VDC", cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        if vdc is None:
            vdc = cmd_proc.vdc_name
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            if not yes:
                confirmed = click.confirm("This will delete the VDC, its "
                                          "edge gateway and all its virtual "
                                          "machines.\n"
                                          "The action can't be undone.\n"
                                          "Do you want to delete VDC '%s'?"
                                          % vdc)
                if not confirmed:
                    utils.print_message('Action cancelled, '
                                        'VDC will not be deleted')
                    sys.exit(0)
            result = cmd_proc.vca.delete_vdc(vdc)
            if result[0]:
                utils.display_progress(result[1], cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                utils.print_error("can't delete the VDC", cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("Unable to delete VDC '%s', profile '%s': "
                              "VDC not found" %
                              (vdc, cmd_proc.profile), cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #29
0
def example(ctx):
    """vCloud Air CLI Examples"""
    headers = ['Id', 'Example', 'Flavor', 'Command']
    example_id = 0
    table = []
    example_id += 1
    table.append([
        example_id, 'login to service', 'vCA',
        'vca login [email protected] --password ****'
    ])
    example_id += 1
    table.append([
        example_id, 'login to an instance', 'vCA',
        'vca login [email protected] --password ****'
        ' --instance c40ba6b4-c158-49fb-b164-5c66f90344fa'
    ])
    example_id += 1
    table.append([
        example_id, 'login to a virtual data center', 'vCA',
        'vca login [email protected] --password ****'
        ' --instance c40ba6b4-c158-49fb-b164-5c66f90344fa'
        ' --vdc VDC1'
    ])
    example_id += 1
    table.append([
        example_id, 'login to service', 'vCHS',
        'vca login [email protected] --password ****'
        ' --host vchs.vmware.com --version 5.6'
    ])
    example_id += 1
    table.append([
        example_id, 'login to an instance', 'vCHS',
        'vca login [email protected] --password ****'
        ' --host vchs.vmware.com --version 5.6'
        ' --instance 55-234 --org MyOrg'
    ])
    example_id += 1
    table.append([
        example_id, 'login to a virtual data center', 'vCHS',
        'vca login [email protected] --password ****'
        ' --host vchs.vmware.com --version 5.6'
        ' --instance 55-234 --org MyOrg --vdc MyVDC'
    ])
    example_id += 1
    table.append([
        example_id, 'login to vCloud Director', 'Standalone',
        'vca login [email protected] --password ****'
        ' --host myvcloud.company.com --version 5.5 --org MyOrg'
    ])
    example_id += 1
    table.append([
        example_id, 'login to vCloud Director and VDC', 'Standalone',
        'vca login [email protected] --password ****'
        ' --host myvcloud.company.com --version 5.5 --org MyOrg'
        ' --vdc MyVDC'
    ])
    example_id += 1
    table.append(
        [example_id, 'list available instances', 'vCA, vCHS', 'vca instance'])
    example_id += 1
    table.append([
        example_id, 'show details of instance', 'vCA, vCHS',
        'vca instance info --instance '
        'c40ba6b4-c158-49fb-b164-5c66f90344fa'
    ])
    example_id += 1
    table.append([
        example_id, 'select an instance', 'vCA', 'vca instance use --instance '
        'c40ba6b4-c158-49fb-b164-5c66f90344fa'
    ])
    example_id += 1
    table.append([
        example_id, 'select an instance and VDC', 'vCA',
        'vca instance use --instance '
        'c40ba6b4-c158-49fb-b164-5c66f90344fa '
        '--vdc MyVDC'
    ])
    example_id += 1
    table.append([
        example_id, 'select an instance', 'vCHS',
        'vca instance use --instance '
        'M684216431-4470 --org M684216431-4470'
    ])
    example_id += 1
    table.append([example_id, 'list organizations', 'All', 'vca org'])
    example_id += 1
    table.append(
        [example_id, 'show organization details', 'All', 'vca org info'])
    example_id += 1
    table.append([
        example_id, 'select an organization', 'vCHS',
        'vca org use --instance 55-234 --org MyOrg'
    ])
    example_id += 1
    table.append(
        [example_id, 'list VDC templates', 'All', 'vca org list-templates'])
    example_id += 1
    table.append([example_id, 'list VDC', 'All', 'vca vdc'])
    example_id += 1
    table.append([example_id, 'select VDC', 'All', 'vca vdc use --vdc vdc1'])
    example_id += 1
    table.append([
        example_id, 'create VDC', 'All',
        'vca vdc create --vdc vdc2 --template d2p3v29-new-tp'
    ])
    example_id += 1
    table.append([
        example_id, 'delete VDC (will prompt to confirm)', 'All',
        'vca vdc delete --vdc vdc2'
    ])
    example_id += 1
    table.append([
        example_id, 'delete VDC without prompt', 'All',
        'vca vdc delete --vdc vdc2 --yes'
    ])
    example_id += 1
    table.append([example_id, 'list catalogs and items', 'All', 'vca catalog'])
    example_id += 1
    table.append([
        example_id, 'create catalog', 'All',
        'vca catalog create --catalog mycatalog'
    ])
    example_id += 1
    table.append([
        example_id, 'delete catalog', 'All',
        'vca catalog delete --catalog mycatalog'
    ])
    example_id += 1
    table.append([
        example_id, 'delete catalog item', 'All',
        'vca catalog delete-item --catalog mycatalog'
        ' --item my_vapp_template'
    ])
    example_id += 1
    table.append([
        example_id, 'upload media file (ISO) to catalog', 'All',
        'vca catalog upload --catalog mycatalog'
        ' --item esxi.iso --description ESXi-iso'
        ' --file ~/VMware-VMvisor.iso'
    ])
    example_id += 1
    table.append([example_id, 'list vApps', 'All', 'vca vapp'])
    example_id += 1
    table.append([
        example_id, 'create vApp', 'All',
        'vca vapp create --vapp myvapp --vm myvm'
        ' --catalog'
        ' \'Public Catalog\' --template \'Ubuntu'
        ' Server 12.04 LTS (amd64 20150127)\''
    ])
    example_id += 1
    table.append([
        example_id, 'create vApp', 'All',
        'vca vapp create -a myvapp -V myvm -c'
        ' mycatalog -t mytemplate'
        ' -n default-routed-network -m pool'
    ])
    example_id += 1
    table.append([
        example_id, 'create vApp with manually assigned IP', 'All',
        'vca vapp create -a myvapp -V myvm -c'
        ' mycatalog -t mytemplate'
        ' -n default-routed-network -mode manual'
        ' --ip 192.168.109.25'
    ])
    example_id += 1
    table.append([
        example_id, 'create multiple vApps', 'All',
        'vca vapp create -a myvapp -V myvm -c'
        ' mycatalog -t mytemplate'
        ' -n default-routed-network -m pool'
        ' --count 10'
    ])
    example_id += 1
    table.append([
        example_id, 'create vApp and configure VM size', 'All',
        'vca vapp create -a myvapp -V myvm -c'
        ' mycatalog -t mytemplate'
        ' -n default-routed-network -m pool'
        ' --cpu 4 --ram 4096'
    ])
    example_id += 1
    table.append(
        [example_id, 'delete vApp', 'All', 'vca vapp delete --vapp myvapp'])
    example_id += 1
    table.append([
        example_id, 'show vApp details in JSON', 'All',
        'vca -j vapp info --vapp myvapp'
    ])
    example_id += 1
    table.append(
        [example_id, 'deploy vApp', 'All', 'vca vapp deploy --vapp ubu'])
    example_id += 1
    table.append(
        [example_id, 'undeploy vApp', 'All', 'vca vapp undeploy --vapp ubu'])
    example_id += 1
    table.append(
        [example_id, 'power on vApp', 'All', 'vca vapp power-on --vapp ubu'])
    example_id += 1
    table.append(
        [example_id, 'power off vApp', 'All', 'vca vapp power-off --vapp ubu'])
    example_id += 1
    table.append([
        example_id, 'customize vApp VM', 'All',
        'vca vapp customize --vapp ubu --vm ubu'
        ' --file add_public_ssh_key.sh'
    ])
    example_id += 1
    table.append([
        example_id, 'insert ISO to vApp VM', 'All',
        'vca vapp insert --vapp coreos1 --vm coreos1'
        ' --catalog default-catalog'
        ' --media coreos1-config.iso'
    ])
    example_id += 1
    table.append([
        example_id, 'eject ISO from vApp VM', 'All',
        'vca vapp eject --vapp coreos1 --vm coreos1'
        ' --catalog default-catalog'
        ' --media coreos1-config.iso'
    ])
    example_id += 1
    table.append([
        example_id, 'attach disk to vApp VM', 'All',
        'vca vapp attach --vapp myvapp'
        ' --vm myvm --disk mydisk'
    ])
    example_id += 1
    table.append([
        example_id, 'detach disk from vApp VM', 'All',
        'vca vapp detach --vapp myvapp'
        ' --vm myvm --disk mydisk'
    ])
    example_id += 1
    table.append(
        [example_id, 'list independent disks', 'All', 'vca vapp disk'])
    example_id += 1
    table.append([
        example_id, 'create independent disk of 100GB', 'All',
        'vca disk create --disk mydisk'
        ' --size 100'
    ])
    example_id += 1
    table.append([
        example_id, 'delete independent disk by name', 'All',
        'vca disk delete --disk mydisk'
    ])
    example_id += 1
    table.append([
        example_id, 'delete independent disk by id', 'All', 'vca disk delete'
        ' --id bce76ca7-29d0-4041-82d4-e4481804d5c4'
    ])
    example_id += 1
    table.append([example_id, 'list VMs', 'All', 'vca vm'])
    example_id += 1
    table.append([example_id, 'list VMs in a vApp', 'All', 'vca vm -a ubu'])
    example_id += 1
    table.append([example_id, 'list VMs in JSON format', 'All', 'vca -j vm'])
    example_id += 1
    table.append([
        example_id, 'retrieve the IP of a VM', 'All',
        "IP=`vca -j vm -a ubu | jq -r"
        " '.vms[0].IPs[0]'` && echo $IP"
    ])
    example_id += 1
    table.append([example_id, 'list networks', 'All', 'vca network'])
    example_id += 1
    table.append([
        example_id, 'create network', 'All',
        'vca network create --network net-117'
        ' --gateway-ip 192.168.117.1'
        ' --netmask 255.255.255.0'
        ' --dns1 8.8.8.8'
        ' --pool 192.168.117.2-192.168.117.100'
    ])
    example_id += 1
    table.append([
        example_id, 'delete network', 'All',
        'vca network delete --network net-117'
    ])
    example_id += 1
    table.append([example_id, 'list edge gateways', 'All', 'vca gateway'])
    example_id += 1
    table.append(
        [example_id, 'get details of edge gateway', 'All', 'vca gateway info'])
    example_id += 1
    table.append([
        example_id, 'set syslog server on gateway', 'All',
        'vca gateway set-syslog'
        ' --ip 192.168.109.2'
    ])
    example_id += 1
    table.append([
        example_id, 'unset syslog server on gateway', 'All',
        'vca gateway set-syslog'
    ])
    example_id += 1
    table.append([
        example_id, 'allocate external IP address', 'vCA', 'vca gateway add-ip'
    ])
    example_id += 1
    table.append([
        example_id, 'release external IP address', 'vCA',
        'vca gateway del-ip --ip 107.189.93.162'
    ])
    example_id += 1
    table.append([
        example_id, 'list edge gateway firewall rules', 'All', 'vca firewall'
    ])
    example_id += 1
    table.append([
        example_id, 'enable edge gateway firewall', 'All',
        'vca firewall enable'
    ])
    example_id += 1
    table.append([
        example_id, 'disable edge gateway firewall', 'All',
        'vca firewall disable'
    ])
    example_id += 1
    table.append([example_id, 'enable DHCP service', 'All', 'vca dhcp enable'])
    example_id += 1
    table.append(
        [example_id, 'disable DHCP service', 'All', 'vca dhcp disable'])
    example_id += 1
    table.append([
        example_id, 'add DHCP service to a network', 'All',
        'vca dhcp add --network net-117 '
        '--pool 192.168.117.101-192.168.117.200'
    ])
    example_id += 1
    table.append([
        example_id, 'remove DHCP service from a network', 'All',
        'vca dhcp delete --network net-117'
    ])
    example_id += 1
    table.append([example_id, 'list edge gateway NAT rules', 'All', 'vca nat'])
    example_id += 1
    table.append([
        example_id, 'add edge gateway DNAT rule', 'All',
        "vca nat add --type dnat"
        " --original-ip 107.189.93.162"
        " --original-port 22"
        " --translated-ip 192.168.109.2"
        " --translated-port 22 --protocol tcp"
    ])
    example_id += 1
    table.append([
        example_id, 'add edge gateway SNAT rule', 'All',
        "vca nat add --type snat"
        " --original-ip 192.168.109.0/24"
        " --translated-ip 107.189.93.162"
    ])
    example_id += 1
    table.append([
        example_id, 'add SNAT rule to network', 'All',
        "vca nat add --type snat"
        " --original-ip 192.168.109.0/24"
        " --translated-ip 107.189.93.162"
        " --network net-109"
    ])
    example_id += 1
    table.append([
        example_id, 'add edge gateway rules from file', 'All',
        "vca nat add --file natrules.yaml"
    ])
    example_id += 1
    table.append([
        example_id, 'delete edge gateway NAT rule', 'All',
        "vca nat delete --type dnat "
        " --original-ip 107.189.93.162"
        " --original-port 22"
        " --translated-ip 192.168.109.4"
        " --translated-port 22"
        " --protocol tcp"
    ])
    example_id += 1
    table.append([
        example_id, 'delete all edge gateway NAT rules', 'All',
        "vca nat delete --all"
    ])
    example_id += 1
    table.append(
        [example_id, 'list edge gateway VPN config', 'All', 'vca vpn'])
    example_id += 1
    table.append(
        [example_id, 'enable edge gateway VPN', 'All', "vca vpn enable"])
    example_id += 1
    table.append(
        [example_id, 'disable edge gateway VPN', 'All', "vca vpn disable"])
    example_id += 1
    table.append([
        example_id, 'add VPN endpoint', 'All',
        "vca vpn add-endpoint --network d1p10-ext"
        " --public-ip 107.189.123.101"
    ])
    example_id += 1
    table.append([
        example_id, 'delete VPN endpoint', 'All',
        "vca vpn del-endpoint --network d1p10-ext"
        " --public-ip 107.189.123.101"
    ])
    example_id += 1
    table.append([
        example_id, 'add VPN tunnel', 'All', "vca vpn add-tunnel --tunnel t1"
        " --local-ip 107.189.123.101"
        " --local-network net-116"
        " --peer-ip 192.240.158.15"
        " --peer-network 192.168.110.0/24 "
        " --secret P8s3P...7v"
    ])
    example_id += 1
    table.append([
        example_id, 'delete VPN tunnel', 'All',
        "vca vpn del-tunnel --tunnel t1"
    ])
    example_id += 1
    table.append([
        example_id, 'add local network to VPN tunnel', 'All',
        "vca vpn add-network --tunnel t1"
        " --local-network net-115"
    ])
    example_id += 1
    table.append([
        example_id, 'add peer network to VPN tunnel', 'All',
        "vca vpn add-network --tunnel t1"
        " --peer-network 192.168.117.0/24"
    ])
    example_id += 1
    table.append([
        example_id, 'delete local network from VPN tunnel', 'All',
        "vca vpn del-network --tunnel t1"
        " --local-network net-115"
    ])
    example_id += 1
    table.append([
        example_id, 'delete peer network from VPN tunnel', 'All',
        "vca vpn del-network --tunnel t1"
        " --peer-network 192.168.117.0/24"
    ])
    example_id += 1
    table.append([example_id, 'list user roles', 'vCA', 'vca role'])
    example_id += 1
    table.append([example_id, 'list users', 'vCA', 'vca user'])
    example_id += 1
    table.append([
        example_id, 'change current user password', 'vCA',
        'vca user change-password --password current-pass'
        ' --new-password new-pass'
    ])
    example_id += 1
    table.append([
        example_id, 'create user', 'vCA',
        'vca user create --user [email protected] --first Name'
        " --last Name --roles 'Virtual Infrastructure Administrator,"
        " Network Administrator'"
    ])
    example_id += 1
    table.append([
        example_id, 'delete user', 'vCA',
        'vca user delete --id 65737432-9159-418b-945d-e10264130ccb'
    ])
    example_id += 1
    table.append([
        example_id, 'reset user password', 'vCA',
        'vca user reset-password --id '
        '65737432-9159-418b-945d-e10264130ccb'
    ])
    example_id += 1
    table.append([example_id, 'list blueprints', 'vCA', 'vca blueprint'])
    example_id += 1
    table.append([
        example_id, 'validate blueprint', 'vCA', 'vca blueprint validate '
        '--file helloworld/blueprint.yaml'
    ])
    example_id += 1
    table.append([
        example_id, 'upload blueprint', 'vCA',
        'vca blueprint upload --blueprint helloworld '
        '--file helloworld/blueprint.yaml'
    ])
    example_id += 1
    table.append([
        example_id, 'show details of a blueprint', 'vCA',
        'vca blueprint info --blueprint helloworld'
    ])
    example_id += 1
    table.append([
        example_id, 'delete blueprint', 'vCA',
        'vca blueprint delete --blueprint helloworld'
    ])
    example_id += 1
    table.append([
        example_id, 'create deployment', 'vCA',
        'vca deployment create --blueprint helloworld '
        '--deployment d1 --file inputs.yaml'
    ])
    table.append([example_id, 'list deployments', 'vCA', 'vca deployment'])
    example_id += 1
    table.append([
        example_id, 'show details of a deployment', 'vCA',
        'vca deployment info --deployment d1'
    ])
    example_id += 1
    table.append([
        example_id, 'execute deployment workflow', 'vCA',
        'vca deployment execute --deployment d1 '
        '--workflow install'
    ])
    example_id += 1
    table.append([
        example_id, 'delete deployment', 'vCA',
        'vca deployment delete --deployment d1'
    ])
    example_id += 1
    table.append([
        example_id, 'list workflow execution events', 'vCA',
        'vca event list --id f98df6cf-08d8-47fa-947f-67c15337efae'
    ])
    example_id += 1
    table.append([
        example_id, 'list workflow execution events', 'vCA',
        'vca event list --id f98df6cf-08d8-47fa-947f-67c15337efae '
        '--show-logs'
    ])
    example_id += 1
    table.append([example_id, 'show status', 'All', 'vca status'])
    example_id += 1
    table.append([
        example_id, 'show status and password', 'All',
        'vca status --show-password'
    ])
    example_id += 1
    table.append([example_id, 'list profiles', 'All', 'vca profile'])
    example_id += 1
    table.append([
        example_id, 'switch to a profile', 'All', 'vca --profile p1 <command>'
    ])
    example_id += 1
    table.append([
        example_id, 'send debug to $TMPDIR/pyvcloud.log', 'All',
        'vca --debug vm'
    ])
    example_id += 1
    table.append([example_id, 'show version', 'All', 'vca --version'])
    example_id += 1
    table.append([example_id, 'show general help', 'All', 'vca --help'])
    example_id += 1
    table.append(
        [example_id, 'show command help', 'All', 'vca <command> --help'])
    utils.print_table('vca-cli usage examples:', headers, table)
コード例 #30
0
ファイル: vca_cli_network.py プロジェクト: digideskio/vca-cli
def network(cmd_proc, operation, vdc, gateway, network_name, gateway_ip,
            netmask, dns1, dns2, dns_suffix, pool):
    """Operations with Networks"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Name', 'Mode', 'Gateway', 'Netmask', 'DNS 1', 'DNS 2',
                   'Pool IP Range']
        networks = cmd_proc.vca.get_networks(vdc)
        table = cmd_proc.networks_to_table(networks)
        if cmd_proc.json_output:
            json_object = {'networks':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("Available networks in '%s', profile '%s':" %
                              (vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'create' == operation:
        utils.print_message("Creating network '%s' "
                            "in VDC '%s'" %
                            (network_name, vdc), cmd_proc)
        start_address = pool.split('-')[0]
        end_address = pool.split('-')[1]
        result = cmd_proc.vca.create_vdc_network(vdc, network_name,
                                                 gateway,
                                                 start_address, end_address,
                                                 gateway_ip,
                                                 netmask, dns1, dns2,
                                                 dns_suffix)
        if result[0]:
            utils.display_progress(result[1], cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't create network", cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        result = cmd_proc.vca.delete_vdc_network(vdc, network_name)
        if result[0]:
            utils.display_progress(result[1], cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't delete network", cmd_proc)
            sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #31
0
ファイル: vca_cli_network.py プロジェクト: digideskio/vca-cli
def nat(cmd_proc, operation, vdc, gateway, rule_type, original_ip,
        original_port, translated_ip, translated_port, protocol,
        network_name, nat_rules_file, all_rules):
    """Operations with Edge Gateway NAT Rules"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ["Rule Id", "Enabled", "Type", "Original IP",
                   "Original Port", "Translated IP", "Translated Port",
                   "Protocol", "Applied On"]
        table = cmd_proc.nat_rules_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'nat-rules':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("NAT rules in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'add' == operation:
        utils.print_message("add NAT rule")
        if nat_rules_file:
            rules = yaml.load(nat_rules_file)
            if rules and rules[0]:
                nat_rules = rules[0].get('NAT_rules')
                for rule in nat_rules:
                    the_gateway.add_nat_rule(rule.get('type').upper(),
                                             rule.get('original_ip'),
                                             rule.get('original_port'),
                                             rule.get('translated_ip'),
                                             rule.get('translated_port'),
                                             rule.get('protocol'),
                                             rule.get('network_name'))
        else:
            the_gateway.add_nat_rule(rule_type.upper(),
                                     original_ip,
                                     original_port,
                                     translated_ip,
                                     translated_port,
                                     protocol,
                                     network_name)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't add NAT rule: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("delete NAT rule")
        found_rule = False
        if all_rules:
            the_gateway.del_all_nat_rules()
            found_rule = True
        else:
            found_rule = the_gateway.del_nat_rule(rule_type.upper(),
                                                  original_ip,
                                                  original_port,
                                                  translated_ip,
                                                  translated_port,
                                                  protocol,
                                                  network_name)
        if found_rule:
            task = the_gateway.save_services_configuration()
            if task:
                utils.display_progress(task, cmd_proc,
                                       cmd_proc.vca.vcloud_session.
                                       get_vcloud_headers())
            else:
                error = parseString(the_gateway.response.content, True)
                utils.print_error("can't delete NAT rule: " +
                                  error.get_message(), cmd_proc)
                sys.exit(1)
        else:
            utils.print_error("rule doesn't exist in edge gateway", cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #32
0
def catalog(cmd_proc, operation, catalog_name, item_name, description,
            file_name):
    """Operations with Catalogs"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        catalogs = cmd_proc.vca.get_catalogs()
        headers = ['Catalog', 'Item']
        table = cmd_proc.catalogs_to_table(catalogs)
        if cmd_proc.json_output:
            json_object = {'catalogs': utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table(
                "Available catalogs and items in org '%s', "
                "profile '%s':" % (cmd_proc.vca.org, cmd_proc.profile),
                headers, table, cmd_proc)
    elif 'create' == operation:
        task = cmd_proc.vca.create_catalog(catalog_name, description)
        if task:
            utils.display_progress(
                task, cmd_proc,
                cmd_proc.vca.vcloud_session.get_vcloud_headers())
        else:
            utils.print_error("can't create the catalog", cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        result = cmd_proc.vca.delete_catalog(catalog_name)
        if result:
            utils.print_message('catalog deleted', cmd_proc)
        else:
            utils.print_error("can't delete the catalog", cmd_proc)
            sys.exit(1)
    elif 'delete-item' == operation:
        result = cmd_proc.vca.delete_catalog_item(catalog_name, item_name)
        if result:
            utils.print_message('catalog item deleted', cmd_proc)
        else:
            utils.print_error("can't delete the catalog item", cmd_proc)
            sys.exit(1)
    elif 'upload' == operation:
        if file_name.endswith('.iso'):
            result = cmd_proc.vca.upload_media(catalog_name, item_name,
                                               file_name, description, True,
                                               128 * 1024)
            if result:
                utils.print_message('file successfull uploaded', cmd_proc)
            else:
                utils.print_error("can't upload file", cmd_proc)
                sys.exit(1)
        else:
            utils.print_error(
                'not implemented, ' +
                'only .iso files are currently supported', cmd_proc)
            sys.exit(1)
    else:
        utils.print_error('not implemented', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #33
0
ファイル: vca_cli_vca.py プロジェクト: mlees19/vcd-cli
def user(cmd_proc, operation, username, user_id, password, new_password,
         first_name, last_name, roles, token):
    """Operations with Users"""
    if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA:
        utils.print_error('Operation not supported ' 'in this service type')
        sys.exit(1)
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    try:
        if 'list' == operation:
            headers = [
                'User Name', 'First', 'Last', 'Email', 'State', 'Id', 'Roles'
            ]
            table = []
            for u in cmd_proc.vca.get_users():
                roles = []
                for r in u.get('roles').get('roles'):
                    roles.append(str(r.get('name')))
                table.append([
                    u.get('userName'),
                    u.get('givenName'),
                    u.get('familyName'),
                    u.get('email'),
                    u.get('state'),
                    u.get('id'),
                    utils.beautified(roles)
                ])
            sorted_table = sorted(table,
                                  key=operator.itemgetter(0),
                                  reverse=False)
            utils.print_table(
                "Available users in instance '%s'"
                ", profile '%s':" % (cmd_proc.instance, cmd_proc.profile),
                headers, sorted_table, cmd_proc)
        elif 'info' == operation:
            cmd_proc.error_message = 'not implemented'
            sys.exit(1)
        elif 'create' == operation:
            roles_array = roles.split(',')
            result = cmd_proc.vca.add_user(username, first_name, last_name,
                                           roles_array)
            utils.print_message("User '%s' successfully created" % username)
        elif 'delete' == operation:
            result = cmd_proc.vca.del_user(user_id)
            utils.print_message("Successfully deleted user '%s'" %
                                cmd_proc.vca.username)
        elif 'change-password' == operation:
            result = cmd_proc.vca.change_password(password, new_password)
            utils.print_message("Successfully changed password for user '%s'" %
                                cmd_proc.vca.username)
        elif 'validate' == operation:
            result = cmd_proc.vca.validate_user(username, password, token)
            print(result)
        elif 'reset-password' == operation:
            result = cmd_proc.vca.reset_password(user_id)
            utils.print_message("Successfully reset password for user id"
                                "'%s', check email to enter new password" %
                                user_id)
    except Exception:
        utils.print_error('An error has ocurred', cmd_proc)
        sys.exit(1)
    cmd_proc.save_current_config()
コード例 #34
0
def vdc(cmd_proc, operation, vdc, template, yes):
    """Operations with Virtual Data Centers"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Virtual Data Center', "Selected"]
        table = ['', '']
        if cmd_proc.vca.vcloud_session and \
           cmd_proc.vca.vcloud_session.organization:
            links = (cmd_proc.vca.vcloud_session.organization.Link
                     if cmd_proc.vca.vcloud_session.organization else [])
            table1 = [[
                details.get_name(),
                '*' if details.get_name() == cmd_proc.vdc_name else ''
            ] for details in filter(
                lambda info: info.name and
                (info.type_ == 'application/vnd.vmware.vcloud.vdc+xml'), links)
                      ]
            table = sorted(table1, key=operator.itemgetter(0), reverse=False)
        utils.print_table(
            "Available Virtual Data Centers in org '%s', profile '%s':" %
            (cmd_proc.vca.org, cmd_proc.profile), headers, table, cmd_proc)
    elif 'use' == operation:
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc is not None:
            utils.print_message(
                "Using vdc '%s', profile '%s'" % (vdc, cmd_proc.profile),
                cmd_proc)
            cmd_proc.vdc_name = vdc
            cmd_proc.select_default_gateway()
        else:
            utils.print_error(
                "Unable to select vdc '%s' in profile '%s'" %
                (vdc, cmd_proc.profile), cmd_proc)
            sys.exit(1)
    elif 'info' == operation:
        if vdc is None:
            vdc = cmd_proc.vdc_name
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            gateways = cmd_proc.vca.get_gateways(vdc)
            headers1 = ['Type', 'Name']
            table1 = cmd_proc.vdc_to_table(the_vdc, gateways)
            headers2 = [
                'Resource', 'Allocated', 'Limit', 'Reserved', 'Used',
                'Overhead'
            ]
            table2 = cmd_proc.vdc_resources_to_table(the_vdc)
            headers3 = [
                'Name', 'External IPs', 'DHCP', 'Firewall', 'NAT', 'VPN',
                'Routed Networks', 'Syslog', 'Uplinks'
            ]
            table3 = cmd_proc.gateways_to_table(gateways)
            if cmd_proc.json_output:
                json_object = {
                    'vdc_entities': utils.table_to_json(headers1, table1),
                    'vdc_resources': utils.table_to_json(headers2, table2),
                    'gateways': utils.table_to_json(headers3, table3)
                }
                utils.print_json(json_object, cmd_proc=cmd_proc)
            else:
                utils.print_table(
                    "Details of Virtual Data Center '%s', profile '%s':" %
                    (vdc, cmd_proc.profile), headers1, table1, cmd_proc)
                utils.print_table("Compute capacity:", headers2, table2,
                                  cmd_proc)
                utils.print_table('Gateways:', headers3, table3, cmd_proc)
        else:
            utils.print_error(
                "Unable to select VDC %s, profile '%s': "
                "VDC not found" % (vdc, cmd_proc.profile), cmd_proc)
            sys.exit(1)
    elif 'create' == operation:
        if vdc is None:
            utils.print_error('please enter new VDC name')
            sys.exit(1)
        task = cmd_proc.vca.create_vdc(vdc, template)
        if task:
            utils.display_progress(
                task, cmd_proc,
                cmd_proc.vca.vcloud_session.get_vcloud_headers())
        else:
            utils.print_error("can't create the VDC", cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        if vdc is None:
            vdc = cmd_proc.vdc_name
        the_vdc = cmd_proc.vca.get_vdc(vdc)
        if the_vdc:
            if not yes:
                confirmed = click.confirm("This will delete the VDC, its "
                                          "edge gateway and all its virtual "
                                          "machines.\n"
                                          "The action can't be undone.\n"
                                          "Do you want to delete VDC '%s'?" %
                                          vdc)
                if not confirmed:
                    utils.print_message('Action cancelled, '
                                        'VDC will not be deleted')
                    sys.exit(0)
            result = cmd_proc.vca.delete_vdc(vdc)
            if result[0]:
                utils.display_progress(
                    result[1], cmd_proc,
                    cmd_proc.vca.vcloud_session.get_vcloud_headers())
            else:
                utils.print_error("can't delete the VDC", cmd_proc)
                sys.exit(1)
        else:
            utils.print_error(
                "Unable to delete VDC '%s', profile '%s': "
                "VDC not found" % (vdc, cmd_proc.profile), cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()
コード例 #35
0
ファイル: vca_cli_network.py プロジェクト: digideskio/vca-cli
def dhcp(cmd_proc, operation, vdc, gateway, network_name, pool):
    """Operations with Edge Gateway DHCP Service"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Network', 'IP Range From', 'To',
                   'Enabled', 'Default lease', 'Max Lease']
        table = cmd_proc.dhcp_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'dhcp-pools':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("DHCP pools in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'add' == operation:
        utils.print_message("adding DHCP pool to network '%s'" %
                            network_name,
                            cmd_proc)
        the_gateway.add_dhcp_pool(network_name, pool.split('-')[0],
                                  pool.split('-')[1], default_lease=None,
                                  max_lease=None)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't add DHCP pool: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("deleting all DHCP pools in network '%s'" %
                            network_name)
        the_gateway.delete_dhcp_pool(network_name)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't delete DHCP pool", cmd_proc)
            sys.exit(1)
    elif 'enable' == operation or 'disable' == operation:
        utils.print_message("%s DHCP service" % operation)
        the_gateway.enable_dhcp('enable' == operation)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't '%s' the DHCP service: " % operation +
                              error.get_message(), cmd_proc)
    cmd_proc.save_current_config()
コード例 #36
0
ファイル: vca_cli_network.py プロジェクト: mlees19/vcd-cli
def dhcp(cmd_proc, operation, vdc, gateway, network_name, pool):
    """Operations with Edge Gateway DHCP Service"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers = ['Network', 'IP Range From', 'To',
                   'Enabled', 'Default lease', 'Max Lease']
        table = cmd_proc.dhcp_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'dhcp-pools':
                           utils.table_to_json(headers, table)}
            utils.print_json(json_object, cmd_proc=cmd_proc)
        else:
            utils.print_table("DHCP pools in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers, table, cmd_proc)
    elif 'add' == operation:
        utils.print_message("adding DHCP pool to network '%s'" %
                            network_name,
                            cmd_proc)
        the_gateway.add_dhcp_pool(network_name, pool.split('-')[0],
                                  pool.split('-')[1], default_lease=None,
                                  max_lease=None)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't add DHCP pool: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    elif 'delete' == operation:
        utils.print_message("deleting all DHCP pools in network '%s'" %
                            network_name)
        the_gateway.delete_dhcp_pool(network_name)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            utils.print_error("can't delete DHCP pool", cmd_proc)
            sys.exit(1)
    elif 'enable' == operation or 'disable' == operation:
        utils.print_message("%s DHCP service" % operation)
        the_gateway.enable_dhcp('enable' == operation)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't '%s' the DHCP service: " % operation +
                              error.get_message(), cmd_proc)
    cmd_proc.save_current_config()
コード例 #37
0
ファイル: vca_cli_network.py プロジェクト: digideskio/vca-cli
def vpn(cmd_proc, operation, vdc, gateway, network_name, public_ip, local_ip,
        local_network, peer_ip, peer_network, tunnel, secret):
    """Operations with Edge Gateway VPN"""
    result = cmd_proc.re_login()
    if not result:
        utils.print_error('Not logged in', cmd_proc)
        sys.exit(1)
    if vdc is None:
        vdc = cmd_proc.vdc_name
    the_vdc = cmd_proc.vca.get_vdc(vdc)
    if the_vdc is None:
        utils.print_error("VDC not found '%s'" % vdc, cmd_proc)
        sys.exit(1)
    if gateway is None:
        gateway = cmd_proc.gateway
    the_gateway = cmd_proc.vca.get_gateway(vdc, gateway)
    if the_gateway is None:
        utils.print_error("gateway not found '%s'" % gateway, cmd_proc)
        sys.exit(1)
    if 'list' == operation:
        headers1 = ['EndPoint', 'Public IP']
        table1 = cmd_proc.vpn_endpoints_to_table(the_gateway)
        headers2 = ['Tunnel', 'Local IP', 'Local Networks', 'Peer IP',
                    'Peer Networks', 'Enabled', 'Operational']
        table2 = cmd_proc.vpn_tunnels_to_table(the_gateway)
        if cmd_proc.json_output:
            json_object = {'endpoints': table1, 'tunnels': table2}
            utils.print_json(json_object, 'VPN config',
                             cmd_proc)
        else:
            utils.print_table("VPN endpoints in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers1, table1, cmd_proc)
            utils.print_table("VPN tunnels in gateway '%s', "
                              "VDC '%s', profile '%s':" %
                              (gateway, vdc, cmd_proc.profile),
                              headers2, table2, cmd_proc)
    else:
        if 'enable' == operation or 'disable' == operation:
            utils.print_message("%s VPN service" % operation)
            the_gateway.enable_vpn('enable' == operation)
        elif 'add-endpoint' == operation:
            utils.print_message("add VPN endpoint")
            the_gateway.add_vpn_endpoint(network_name, public_ip)
        elif 'del-endpoint' == operation:
            utils.print_message("delete VPN endpoint")
            the_gateway.del_vpn_endpoint(network_name, public_ip)
        elif 'add-tunnel' == operation:
            utils.print_message("add VPN tunnel")
            the_gateway.add_vpn_tunnel(tunnel, local_ip, local_network,
                                       peer_ip, peer_network, secret)
        elif 'del-tunnel' == operation:
            utils.print_message("delete VPN tunnel")
            the_gateway.delete_vpn_tunnel(tunnel)
        elif 'add-network' == operation:
            utils.print_message("add network to VPN tunnel")
            the_gateway.add_network_to_vpn_tunnel(tunnel, local_network,
                                                  peer_network)
        elif 'del-network' == operation:
            utils.print_message("delete network from VPN tunnel")
            the_gateway.delete_network_from_vpn_tunnel(tunnel, local_network,
                                                       peer_network)
        task = the_gateway.save_services_configuration()
        if task:
            utils.display_progress(task, cmd_proc,
                                   cmd_proc.vca.vcloud_session.
                                   get_vcloud_headers())
        else:
            error = parseString(the_gateway.response.content, True)
            utils.print_error("can't update VPN configuration: " +
                              error.get_message(), cmd_proc)
            sys.exit(1)
    cmd_proc.save_current_config()