Example #1
0
    def _check_network_overlap(self,
                               context,
                               user_network=None,
                               user_subnet=None):
        """Check if the network contains IP address belongs to reserved
        network.

        :param context: User context.
        :param user_network: Network ID.
        :param user_subnet: Subnet ID.
        """
        neutron_client = clients.create_neutron_client(context)
        user_cidrs = neutron.get_subnet_cidrs(neutron_client, user_network,
                                              user_subnet)

        reserved_cidrs = CONF.reserved_network_cidrs
        mgmt_cidrs = neutron.get_mamangement_subnet_cidrs(neutron_client)
        reserved_cidrs.extend(mgmt_cidrs)

        LOG.debug(
            "Cidrs of the user network: %s, cidrs of the reserved "
            "network: %s", user_cidrs, reserved_cidrs)

        for user_cidr in user_cidrs:
            user_net = ipaddress.ip_network(user_cidr)
            for reserved_cidr in reserved_cidrs:
                res_net = ipaddress.ip_network(reserved_cidr)
                if user_net.overlaps(res_net):
                    raise exception.NetworkConflict()
Example #2
0
    def _check_nic(self, context, nic):
        """Check user provided nic.

        :param context: User context.
        :param nic: A dict may contain network_id(net-id), subnet_id or
            ip_address.
        """
        neutron_client = clients.create_neutron_client(context)
        network_id = nic.get('network_id', nic.get('net-id'))
        subnet_id = nic.get('subnet_id')
        ip_address = nic.get('ip_address')

        if not network_id and not subnet_id:
            raise exception.NetworkNotProvided(resource='network or subnet')

        if not subnet_id and ip_address:
            raise exception.NetworkNotProvided(resource='subnet')

        if subnet_id:
            actual_network = neutron_client.show_subnet(
                subnet_id)['subnet']['network_id']
            if network_id and actual_network != network_id:
                raise exception.SubnetNotFound(subnet_id=subnet_id,
                                               network_id=network_id)
            network_id = actual_network

        nic['network_id'] = network_id
        nic.pop('net-id', None)

        self._check_network_overlap(context, network_id, subnet_id)
Example #3
0
 def _check_network_overlap(self, context, user_network):
     neutron_client = clients.create_neutron_client(context)
     user_cidrs = neutron.get_subnet_cidrs(neutron_client, user_network)
     mgmt_cidrs = neutron.get_mamangement_subnet_cidrs(neutron_client)
     LOG.debug("Cidrs of the user network: %s, cidrs of the management "
               "network: %s", user_cidrs, mgmt_cidrs)
     for user_cidr in user_cidrs:
         user_net = ipaddress.ip_network(user_cidr)
         for mgmt_cidr in mgmt_cidrs:
             mgmt_net = ipaddress.ip_network(mgmt_cidr)
             if user_net.overlaps(mgmt_net):
                 raise exception.NetworkConflict()
Example #4
0
def validate_instance_nics(context, instances):
    """Checking networks are same for the cluster."""
    instance_nics = []
    for instance in instances:
        nics = instance.get('nics')
        if nics:
            instance_nics.append(nics[0].get('net-id'))
    if len(set(instance_nics)) > 1:
        raise exception.ClusterNetworksNotEqual()
    if not instance_nics:
        return
    instance_nic = instance_nics[0]
    try:
        neutron_client = clients.create_neutron_client(context)
        neutron_client.find_resource('network', instance_nic)
    except neutron_exceptions.NotFound:
        raise exception.NetworkNotFound(uuid=instance_nic)
Example #5
0
def get_management_networks(context):
    """Cache the management network names.

    When CONF.management_networks is changed, the Trove service needs to
    restart so the global cache will be refreshed.
    """
    global MGMT_NETWORKS

    if MGMT_NETWORKS is not None:
        return MGMT_NETWORKS

    MGMT_NETWORKS = []
    if len(CONF.management_networks) > 0:
        neutron_client = clients.create_neutron_client(context)

        for net_id in CONF.management_networks:
            MGMT_NETWORKS.append(
                neutron_client.show_network(net_id)['network']['name'])

    return MGMT_NETWORKS
Example #6
0
 def __init__(self, context, region_name):
     try:
         self.client = clients.create_neutron_client(context, region_name)
     except neutron_exceptions.NeutronClientException as e:
         raise exception.TroveError(str(e))