def test_is_separate_ranges(self):
     """
         ips in separate ranges
     """
     IPRange = collections.namedtuple('IPRange', 'start end')
     # positive case
     self.assertTrue(
         network_plugin.is_separate_ranges(
             IPRange(start='1.1.1.1', end='1.1.1.11'),
             IPRange(start='1.1.1.12', end='1.1.1.23')))
     # negative case
     self.assertFalse(
         network_plugin.is_separate_ranges(
             IPRange(start='1.1.1.1', end='1.1.1.15'),
             IPRange(start='1.1.1.9', end='1.1.1.23')))
 def test_is_separate_ranges(self):
     """
         ips in separate ranges
     """
     IPRange = collections.namedtuple('IPRange', 'start end')
     # positive case
     self.assertTrue(
         network_plugin.is_separate_ranges(
             IPRange(start='1.1.1.1', end='1.1.1.11'),
             IPRange(start='1.1.1.12', end='1.1.1.23')
         )
     )
     # negative case
     self.assertFalse(
         network_plugin.is_separate_ranges(
             IPRange(start='1.1.1.1', end='1.1.1.15'),
             IPRange(start='1.1.1.9', end='1.1.1.23')
         )
     )
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.")
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.")