Beispiel #1
0
 def verify_subnet_exists(self, context, tenant_id, quantum_net_id):
     """Confirms that a subnet exists that is associated with the
        specified Quantum Network UUID.  Raises an exception if no
        such subnet exists.
     """
     admin_context = context.elevated()
     net = db.network_get_by_uuid(admin_context, quantum_net_id)
     return net is not None
Beispiel #2
0
 def get_networks_by_tenant(self, admin_context, tenant_id):
     nets = []
     blocks = self.m_conn.get_blocks(tenant_id)
     for ip_block in blocks['ip_blocks']:
         network_id = ip_block['network_id']
         network = db.network_get_by_uuid(admin_context, network_id)
         nets.append(network)
     return nets
 def get_networks_by_tenant(self, admin_context, tenant_id):
     nets = []
     blocks = self.m_conn.get_blocks(tenant_id)
     for ip_block in blocks['ip_blocks']:
         network_id = ip_block['network_id']
         network = db.network_get_by_uuid(admin_context, network_id)
         nets.append(network)
     return nets
Beispiel #4
0
 def delete_subnets_by_net_id(self, context, net_id, project_id):
     """Deletes a network based on Quantum UUID.  Uses FlatManager
        delete_network to avoid duplication.
     """
     admin_context = context.elevated()
     network = db.network_get_by_uuid(admin_context, net_id)
     if not network:
         raise Exception(_("No network with net_id = %s" % net_id))
     manager.FlatManager.delete_network(self.net_manager,
                                        admin_context, None,
                                        network['uuid'],
                                        require_disassociated=False)
Beispiel #5
0
    def delete_subnets_by_net_id(self, context, net_id, project_id):
        """Find Melange block associated with the Quantum UUID,
           then tell Melange to delete that block.
        """
        admin_context = context.elevated()
        tenant_id = project_id or FLAGS.quantum_default_tenant_id
        all_blocks = self.m_conn.get_blocks(tenant_id)
        for b in all_blocks['ip_blocks']:
            if b['network_id'] == net_id:
                self.m_conn.delete_block(b['id'], tenant_id)

        network = db.network_get_by_uuid(admin_context, net_id)
        db.network_delete_safe(context, network['id'])
    def delete_subnets_by_net_id(self, context, net_id, project_id):
        """Find Melange block associated with the Quantum UUID,
           then tell Melange to delete that block.
        """
        admin_context = context.elevated()
        tenant_id = project_id or FLAGS.quantum_default_tenant_id
        all_blocks = self.m_conn.get_blocks(tenant_id)
        for b in all_blocks['ip_blocks']:
            if b['network_id'] == net_id:
                self.m_conn.delete_block(b['id'], tenant_id)

        network = db.network_get_by_uuid(admin_context, net_id)
        db.network_delete_safe(context, network['id'])
Beispiel #7
0
 def get_v6_ips_by_interface(self, context, net_id, vif_id, project_id):
     """Returns a list containing a single IPv6 address strings
        associated with the specified virtual interface.
     """
     admin_context = context.elevated()
     network = db.network_get_by_uuid(admin_context, net_id)
     vif_rec = db.virtual_interface_get_by_uuid(context, vif_id)
     if network['cidr_v6']:
         ip = ipv6.to_global(network['cidr_v6'],
                             vif_rec['address'],
                             project_id)
         return [ip]
     return []
Beispiel #8
0
 def allocate_fixed_ip(self, context, tenant_id, quantum_net_id, vif_rec):
     """Allocates a single fixed IPv4 address for a virtual interface."""
     admin_context = context.elevated()
     network = db.network_get_by_uuid(admin_context, quantum_net_id)
     address = None
     if network['cidr']:
         address = db.fixed_ip_associate_pool(admin_context,
                                              network['id'],
                                              vif_rec['instance_id'])
         values = {'allocated': True,
                   'virtual_interface_id': vif_rec['id']}
         db.fixed_ip_update(admin_context, address, values)
     return address
Beispiel #9
0
 def get_allocated_ips(self, context, subnet_id, project_id):
     """Returns a list of (ip, vif_id) pairs"""
     admin_context = context.elevated()
     ips = db.fixed_ip_get_all(admin_context)
     allocated_ips = []
     # Get all allocated IPs that are part of this subnet
     network = db.network_get_by_uuid(admin_context, subnet_id)
     for ip in ips:
         # Skip unallocated IPs
         if not ip['allocated'] == 1:
             continue
         if ip['network_id'] == network['id']:
             vif = db.virtual_interface_get(admin_context,
                 ip['virtual_interface_id'])
             allocated_ips.append((ip['address'], vif['uuid']))
     return allocated_ips
Beispiel #10
0
 def get_subnets_by_net_id(self, context, tenant_id, net_id, _vif_id=None):
     """Returns information about the IPv4 and IPv6 subnets
        associated with a Quantum Network UUID.
     """
     n = db.network_get_by_uuid(context.elevated(), net_id)
     subnet_data_v4 = {
         'network_id': n['uuid'],
         'cidr': n['cidr'],
         'gateway': n['gateway'],
         'broadcast': n['broadcast'],
         'netmask': n['netmask'],
         'dns1': n['dns1'],
         'dns2': n['dns2']}
     subnet_data_v6 = {
         'network_id': n['uuid'],
         'cidr': n['cidr_v6'],
         'gateway': n['gateway_v6'],
         'broadcast': None,
         'netmask': None,
         'dns1': None,
         'dns2': None}
     return (subnet_data_v4, subnet_data_v6)
Beispiel #11
0
    def allocate_for_instance(self, context, **kwargs):
        """Called by compute when it is creating a new VM.

           There are three key tasks:
                - Determine the number and order of vNICs to create
                - Allocate IP addresses
                - Create ports on a Quantum network and attach vNICs.

           We support two approaches to determining vNICs:
                - By default, a VM gets a vNIC for any network belonging
                  to the VM's project, and a vNIC for any "global" network
                  that has a NULL project_id.  vNIC order is determined
                  by the network's 'priority' field.
                - If the 'os-create-server-ext' was used to create the VM,
                  only the networks in 'requested_networks' are used to
                  create vNICs, and the vNIC order is determiend by the
                  order in the requested_networks array.

           For each vNIC, use the FlatManager to create the entries
           in the virtual_interfaces table, contact Quantum to
           create a port and attachment the vNIC, and use the IPAM
           lib to allocate IP addresses.
        """
        instance_id = kwargs.pop('instance_id')
        instance_type_id = kwargs['instance_type_id']
        host = kwargs.pop('host')
        project_id = kwargs.pop('project_id')
        LOG.debug(_("network allocations for instance %s"), project_id)

        requested_networks = kwargs.get('requested_networks')

        if requested_networks:
            net_proj_pairs = [(net_id, project_id) \
                for (net_id, _i) in requested_networks]
        else:
            net_proj_pairs = self.ipam.get_project_and_global_net_ids(context,
                                                                project_id)

        # Quantum may also know about networks that aren't in the networks
        # table so we need to query Quanutm for any tenant networks and add
        # them to net_proj_pairs.
        qnets = self.q_conn.get_networks(project_id)
        for qn in qnets['networks']:
            pair = (qn['id'], project_id)
            if pair not in net_proj_pairs:
                net_proj_pairs.append(pair)

        # Create a port via quantum and attach the vif
        for (quantum_net_id, project_id) in net_proj_pairs:
            # FIXME(danwent): We'd like to have the manager be
            # completely decoupled from the engine networks table.
            # However, other parts of engine sometimes go behind our
            # back and access network data directly from the DB.  So
            # for now, the quantum manager knows that there is a engine
            # networks DB table and accesses it here.  updating the
            # virtual_interfaces table to use UUIDs would be one
            # solution, but this would require significant work
            # elsewhere.
            admin_context = context.elevated()

            # We may not be able to get a network_ref here if this network
            # isn't in the database (i.e. it came from Quantum).
            network_ref = db.network_get_by_uuid(admin_context,
                                                 quantum_net_id)
            if network_ref is None:
                network_ref = {}
                network_ref = {"uuid": quantum_net_id,
                    "project_id": project_id,
                    # NOTE(bgh): We need to document this somewhere but since
                    # we don't know the priority of any networks we get from
                    # quantum we just give them a priority of 0.  If its
                    # necessary to specify the order of the vifs and what
                    # network they map to then the user will have to use the
                    # OSCreateServer extension and specify them explicitly.
                    #
                    # In the future users will be able to tag quantum networks
                    # with a priority .. and at that point we can update the
                    # code here to reflect that.
                    "priority": 0,
                    "id": 'NULL',
                    "label": "quantum-net-%s" % quantum_net_id}

            vif_rec = self.add_virtual_interface(context,
                                                 instance_id,
                                                 network_ref['id'])

            # talk to Quantum API to create and attach port.
            instance = db.instance_get(context, instance_id)
            instance_type = instance_types.get_instance_type(instance_type_id)
            rxtx_factor = instance_type['rxtx_factor']
            engine_id = self._get_engine_id(context)
            q_tenant_id = project_id or FLAGS.quantum_default_tenant_id
            self.q_conn.create_and_attach_port(q_tenant_id, quantum_net_id,
                                               vif_rec['uuid'],
                                               vm_id=instance['uuid'],
                                               rxtx_factor=rxtx_factor,
                                               engine_id=engine_id)
            # Tell melange to allocate an IP
            ip = self.ipam.allocate_fixed_ip(context, project_id,
                    quantum_net_id, vif_rec)
            # Set up/start the dhcp server for this network if necessary
            if FLAGS.quantum_use_dhcp:
                self.enable_dhcp(context, quantum_net_id, network_ref,
                    vif_rec, project_id)
        return self.get_instance_nw_info(context, instance_id,
                                         instance_type_id, host)