Ejemplo n.º 1
0
 def test_is_network_exists(self):
     """
         check network exist
     """
     # network exist
     fake_ctx = self.generate_node_context()
     fake_client = self.generate_client()
     fake_client.get_network = mock.MagicMock(
         return_value=self.generate_fake_client_network('test')
     )
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         self.assertTrue(
             network_plugin.is_network_exists(fake_client, 'test')
         )
     fake_client.get_network.assert_called_with('vdc_name', 'test')
     # network not exist
     fake_ctx = self.generate_node_context()
     fake_client = self.generate_client()
     fake_client.get_network = mock.MagicMock(
         return_value=None
     )
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         self.assertFalse(
             network_plugin.is_network_exists(fake_client, 'test')
         )
Ejemplo n.º 2
0
def _create_connections_list(vca_client):
    """
        return full list connections for node
    """
    connections = []
    ports = _get_connected(ctx.instance, 'port')
    networks = _get_connected(ctx.instance, 'network')

    management_network_name = _get_management_network_from_node()

    if not is_network_exists(vca_client, management_network_name):
        raise cfy_exc.NonRecoverableError(
            "Network '{0}' could not be found".format(management_network_name))

    # connection by port
    for port in ports:
        port_properties = port.node.properties['port']
        connections.append(
            _create_connection(port_properties['network'],
                               port_properties.get('ip_address'),
                               port_properties.get('mac_address'),
                               port_properties.get('ip_allocation_mode',
                                                   'POOL').upper(),
                               port_properties.get('primary_interface', False))
        )

    # connection by networks
    for net in networks:
        connections.append(
            _create_connection(get_network_name(net.node.properties),
                               None, None, 'POOL'))

    # add managmenty network if not exist in list
    if not any([conn['network'] == management_network_name
                for conn in connections]):
        connections.append(_create_connection(management_network_name,
                                              None, None, 'POOL'))

    primary_iface_set = len(filter(lambda conn: conn.get('primary_interface',
                                                         False),
                                   connections)) > 0

    # check list of connections and set managment network as primary
    # in case when we dont have any primary networks
    for conn in connections:
        network_name = conn['network']
        if (conn['ip_allocation_mode'] == 'DHCP'
                and not _isDhcpAvailable(vca_client, network_name)):
            raise cfy_exc.NonRecoverableError(
                "DHCP for network {0} is not available"
                .format(network_name))

        if primary_iface_set is False:
            conn['primary_interface'] = \
                (network_name == management_network_name)

    return connections
Ejemplo n.º 3
0
def _create_connections_list(vca_client):
    """
        return full list connections for node
    """
    connections = []
    ports = _get_connected(ctx.instance, 'port')
    networks = _get_connected(ctx.instance, 'network')

    management_network_name = _get_management_network_from_node()

    if not is_network_exists(vca_client, management_network_name):
        raise cfy_exc.NonRecoverableError(
            "Network {0} could not be found".format(management_network_name))

    # connection by port
    for port in ports:
        port_properties = port.node.properties['port']
        connections.append(
            _create_connection(
                port_properties['network'], port_properties.get('ip_address'),
                port_properties.get('mac_address'),
                port_properties.get('ip_allocation_mode', 'POOL').upper(),
                port_properties.get('primary_interface', False)))

    # connection by networks
    for net in networks:
        connections.append(
            _create_connection(get_network_name(net.node.properties), None,
                               None, 'POOL'))

    # add managmenty network if not exist in list
    if not any(
        [conn['network'] == management_network_name for conn in connections]):
        connections.append(
            _create_connection(management_network_name, None, None, 'POOL'))

    primary_iface_set = len(
        filter(lambda conn: conn.get('primary_interface', False),
               connections)) > 0

    # check list of connections and set managment network as primary
    # in case when we dont have any primary networks
    for conn in connections:
        network_name = conn['network']
        if (conn['ip_allocation_mode'] == 'DHCP'
                and not _isDhcpAvailable(vca_client, network_name)):
            raise cfy_exc.NonRecoverableError(
                "DHCP for network {0} is not available".format(network_name))

        if primary_iface_set is False:
            conn['primary_interface'] = \
                (network_name == management_network_name)

    return connections
Ejemplo n.º 4
0
def creation_validation(vca_client, **kwargs):
    """
        check network description from node description
    """
    network_name = get_network_name(ctx.node.properties)
    ctx.logger.info(
        "Validation cloudify.vcloud.nodes.Network node: {0}".format(
            network_name))
    if is_network_exists(vca_client, network_name):
        if ctx.node.properties.get('use_external_resource'):
            # TODO: check: default gateway must exists
            return
        else:
            raise cfy_exc.NonRecoverableError(
                "Network already exsists: {0}".format(network_name))

    net_prop = get_mandatory(ctx.node.properties, "network")
    gateway_name = get_mandatory(net_prop, 'edge_gateway')
    if not vca_client.get_gateway(get_vcloud_config()['vdc'], gateway_name):
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found".format(gateway_name))

    static_ip = _split_adresses(get_mandatory(net_prop, 'static_range'))
    check_ip(static_ip.start)
    check_ip(static_ip.end)
    dns_list = net_prop.get("dns")
    if dns_list:
        for ip in dns_list:
            check_ip(ip)
    gateway_ip = check_ip(get_mandatory(net_prop, "gateway_ip"))
    netmask = check_ip(get_mandatory(net_prop, "netmask"))

    ips = [gateway_ip, static_ip.start, static_ip.end]
    dhcp = net_prop.get("dhcp")
    if dhcp:
        dhcp_range = get_mandatory(net_prop["dhcp"], "dhcp_range")
        dhcp_ip = _split_adresses(dhcp_range)
        if not is_separate_ranges(static_ip, dhcp_ip):
            raise cfy_exc.NonRecoverableError(
                "Static_range and dhcp_range is overlapped.")
        ips.extend([dhcp_ip.start, dhcp_ip.end])
    if not is_ips_in_same_subnet(ips, netmask):
        raise cfy_exc.NonRecoverableError("IP addresses in different subnets.")
Ejemplo n.º 5
0
def creation_validation(vca_client, **kwargs):
    """
        check network description from node description
    """
    network_name = get_network_name(ctx.node.properties)
    ctx.logger.info("Validation cloudify.vcloud.nodes.Network node: {0}"
                    .format(network_name))
    if is_network_exists(vca_client, network_name):
        if ctx.node.properties.get('use_external_resource'):
            # TODO: check: default gateway must exists
            return
        else:
            raise cfy_exc.NonRecoverableError(
                "Network already exsists: {0}".format(network_name))

    net_prop = get_mandatory(ctx.node.properties, "network")
    gateway_name = get_mandatory(net_prop, 'edge_gateway')
    if not vca_client.get_gateway(get_vcloud_config()['vdc'], gateway_name):
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found".format(gateway_name))

    static_ip = _split_adresses(get_mandatory(net_prop, 'static_range'))
    check_ip(static_ip.start)
    check_ip(static_ip.end)
    dns_list = net_prop.get("dns")
    if dns_list:
        for ip in dns_list:
            check_ip(ip)
    gateway_ip = check_ip(get_mandatory(net_prop, "gateway_ip"))
    netmask = check_ip(get_mandatory(net_prop, "netmask"))

    ips = [gateway_ip, static_ip.start, static_ip.end]
    dhcp = net_prop.get("dhcp")
    if dhcp:
        dhcp_range = get_mandatory(net_prop["dhcp"], "dhcp_range")
        dhcp_ip = _split_adresses(dhcp_range)
        if not is_separate_ranges(static_ip, dhcp_ip):
            raise cfy_exc.NonRecoverableError(
                "Static_range and dhcp_range is overlapped.")
        ips.extend([dhcp_ip.start, dhcp_ip.end])
    if not is_ips_in_same_subnet(ips, netmask):
            raise cfy_exc.NonRecoverableError(
                "IP addresses in different subnets.")
Ejemplo n.º 6
0
def _create_connections_list(vca_client):
    """
        return full list connections for node
    """
    connections = []
    ports = _get_connected(ctx.instance, 'port')
    networks = _get_connected(ctx.instance, 'network')

    management_network_name = ctx.node.properties.get('management_network')

    for port in ports:
        port_properties = port.node.properties['port']
        connections.append(
            _create_connection(port_properties['network'],
                               port_properties.get('ip_address'),
                               port_properties.get('mac_address'),
                               port_properties.get('ip_allocation_mode',
                                                   'POOL').upper(),
                               port_properties.get('primary_interface', False))
        )

    for net in networks:
        connections.append(
            _create_connection(get_network_name(net.node.properties),
                               None, None, 'POOL'))

    if management_network_name and not any(
            [conn['network'] == management_network_name
                for conn in connections]):
        connections.append(_create_connection(management_network_name,
                                              None, None, 'POOL'))

    for conn in connections:
        if not is_network_exists(vca_client, conn['network']):
            raise cfy_exc.NonRecoverableError(
                "Network '{0}' could not be found".format(conn['network']))

    primary_iface_set = len(filter(lambda conn: conn.get('primary_interface',
                                                         False),
                                   connections)) > 0
    if not primary_iface_set:
        if management_network_name:
            primary_name = management_network_name
        elif connections:
            primary_name = connections[0]['network']
        else:
            raise cfy_exc.NonRecoverableError(
                "Can't setup primary interface")

    # check list of connections and set managment network as primary
    # in case when we dont have any primary networks
    for conn in connections:
        network_name = conn['network']
        if (conn['ip_allocation_mode'] == 'DHCP'
                and not _isDhcpAvailable(vca_client, network_name)):
            raise cfy_exc.NonRecoverableError(
                "DHCP for network {0} is not available"
                .format(network_name))
        if not primary_iface_set:
            conn['primary_interface'] = \
                (network_name == primary_name)
        if conn['primary_interface']:
            ctx.logger.info(
                "The primary interface has been set to {}".format(
                    network_name))

    return connections
Ejemplo n.º 7
0
def create(vca_client, **kwargs):
    """
        create new vcloud air network, e.g.:
        {
            'use_external_resource': False,
            'resource_id': 'secret_network',
            'network': {
                'dhcp': {
                    'dhcp_range': "10.1.1.128-10.1.1.255"
                },
                'static_range':  "10.1.1.2-10.1.1.127",
                'gateway_ip': "10.1.1.1",
                'edge_gateway': 'gateway',
                'name': 'secret_network',
                "netmask": '255.255.255.0',
                "dns": ["8.8.8.8", "4.4.4.4"]
            }
        }
    """
    vdc_name = get_vcloud_config()['vdc']
    if ctx.node.properties['use_external_resource']:
        network_name = ctx.node.properties['resource_id']
        if not is_network_exists(vca_client, network_name):
            raise cfy_exc.NonRecoverableError(
                "Can't find external resource: {0}".format(network_name))
        ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
        ctx.logger.info(
            "External resource {0} has been used".format(network_name))
        return
    net_prop = ctx.node.properties["network"]
    network_name = get_network_name(ctx.node.properties)
    if network_name in _get_network_list(vca_client,
                                         get_vcloud_config()['vdc']):
        raise cfy_exc.NonRecoverableError(
            "Network {0} already exists, but parameter "
            "'use_external_resource' is 'false' or absent".format(
                network_name))

    ip = _split_adresses(net_prop['static_range'])
    gateway_name = net_prop['edge_gateway']
    if not vca_client.get_gateway(vdc_name, gateway_name):
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found".format(gateway_name))
    start_address = ip.start
    end_address = ip.end
    gateway_ip = net_prop["gateway_ip"]
    netmask = net_prop["netmask"]
    dns1 = ""
    dns2 = ""
    dns_list = net_prop.get("dns")
    if dns_list:
        dns1 = dns_list[0]
        if len(dns_list) > 1:
            dns2 = dns_list[1]
    dns_suffix = net_prop.get("dns_suffix")
    success, result = vca_client.create_vdc_network(vdc_name, network_name,
                                                    gateway_name,
                                                    start_address, end_address,
                                                    gateway_ip, netmask, dns1,
                                                    dns2, dns_suffix)
    if success:
        ctx.logger.info(
            "Network {0} has been successfully created.".format(network_name))
    else:
        raise cfy_exc.NonRecoverableError(
            "Could not create network {0}: {1}".format(network_name, result))
    wait_for_task(vca_client, result)
    ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
    _dhcp_operation(vca_client, network_name, ADD_POOL)
Ejemplo n.º 8
0
def create(vca_client, **kwargs):
    """
        create new vcloud air network, e.g.:
        {
            'use_external_resource': False,
            'resource_id': 'secret_network',
            'network': {
                'dhcp': {
                    'dhcp_range': "10.1.1.128-10.1.1.255"
                },
                'static_range':  "10.1.1.2-10.1.1.127",
                'gateway_ip': "10.1.1.1",
                'edge_gateway': 'gateway',
                'name': 'secret_network',
                "netmask": '255.255.255.0',
                "dns": ["8.8.8.8", "4.4.4.4"]
            }
        }
    """
    vdc_name = get_vcloud_config()['vdc']
    if ctx.node.properties['use_external_resource']:
        network_name = ctx.node.properties['resource_id']
        if not is_network_exists(vca_client, network_name):
            raise cfy_exc.NonRecoverableError(
                "Can't find external resource: {0}".format(network_name))
        ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
        ctx.logger.info(
            "External resource {0} has been used".format(network_name))
        return
    network_name = get_network_name(ctx.node.properties)
    if not ctx.instance.runtime_properties.get(SKIP_CREATE_NETWORK):
        net_prop = ctx.node.properties["network"]
        if network_name in _get_network_list(vca_client,
                                             get_vcloud_config()['vdc']):
            raise cfy_exc.NonRecoverableError(
                "Network {0} already exists, but parameter "
                "'use_external_resource' is 'false' or absent"
                .format(network_name))

        ip = _split_adresses(net_prop['static_range'])
        gateway_name = net_prop['edge_gateway']
        get_gateway(vca_client, gateway_name)
        start_address = ip.start
        end_address = ip.end
        gateway_ip = net_prop["gateway_ip"]
        netmask = net_prop["netmask"]
        dns1 = ""
        dns2 = ""
        dns_list = net_prop.get("dns")
        if dns_list:
            dns1 = dns_list[0]
            if len(dns_list) > 1:
                dns2 = dns_list[1]
        dns_suffix = net_prop.get("dns_suffix")
        ctx.logger.info("Create network {0}."
                        .format(network_name))
        success, result = vca_client.create_vdc_network(
            vdc_name, network_name, gateway_name, start_address,
            end_address, gateway_ip, netmask, dns1, dns2, dns_suffix)
        if success:
            wait_for_task(vca_client, result)
            ctx.logger.info("Network {0} has been successfully created."
                            .format(network_name))
        else:
            raise cfy_exc.NonRecoverableError(
                "Could not create network {0}: {1}".
                format(network_name, result))
        ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
    if not _dhcp_operation(vca_client, network_name, ADD_POOL):
        ctx.instance.runtime_properties[SKIP_CREATE_NETWORK] = True
        return set_retry(ctx)
Ejemplo n.º 9
0
def _create_connections_list(vca_client):
    """
        return full list connections for node
    """
    connections = []
    ports = _get_connected(ctx.instance, 'port')
    networks = _get_connected(ctx.instance, 'network')

    management_network_name = ctx.node.properties.get('management_network')

    for port in ports:
        port_properties = port.node.properties['port']
        connections.append(
            _create_connection(
                port_properties['network'], port_properties.get('ip_address'),
                port_properties.get('mac_address'),
                port_properties.get('ip_allocation_mode', 'POOL').upper(),
                port_properties.get('primary_interface', False)))

    for net in networks:
        connections.append(
            _create_connection(get_network_name(net.node.properties), None,
                               None, 'POOL'))

    if management_network_name and not any(
        [conn['network'] == management_network_name for conn in connections]):
        connections.append(
            _create_connection(management_network_name, None, None, 'POOL'))

    for conn in connections:
        if not is_network_exists(vca_client, conn['network']):
            raise cfy_exc.NonRecoverableError(
                "Network '{0}' could not be found".format(conn['network']))

    primary_iface_set = len(
        filter(lambda conn: conn.get('primary_interface', False),
               connections)) > 0
    if not primary_iface_set:
        if management_network_name:
            primary_name = management_network_name
        elif connections:
            primary_name = connections[0]['network']
        else:
            raise cfy_exc.NonRecoverableError("Can't setup primary interface")

    # check list of connections and set managment network as primary
    # in case when we dont have any primary networks
    for conn in connections:
        network_name = conn['network']
        if (conn['ip_allocation_mode'] == 'DHCP'
                and not _isDhcpAvailable(vca_client, network_name)):
            raise cfy_exc.NonRecoverableError(
                "DHCP for network {0} is not available".format(network_name))
        if not primary_iface_set:
            conn['primary_interface'] = \
                (network_name == primary_name)
        if conn['primary_interface']:
            ctx.logger.info("The primary interface has been set to {}".format(
                network_name))

    return connections