Пример #1
0
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()
Пример #2
0
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()
Пример #3
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()
Пример #4
0
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()
Пример #5
0
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()
Пример #6
0
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()
Пример #7
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()
Пример #8
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()
Пример #9
0
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()
Пример #10
0
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()
Пример #11
0
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()
Пример #12
0
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()
Пример #13
0
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()
Пример #14
0
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()
Пример #15
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()
Пример #16
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', '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()
Пример #17
0
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()
Пример #18
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()
Пример #19
0
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()
Пример #20
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()
Пример #21
0
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()