示例#1
0
def get_ip_info_for_instance(context, instance):
    """Return a list of all fixed IPs for an instance"""

    ip_info = dict(fixed_ips=[], fixed_ip6s=[], floating_ips=[])

    fixed_ips = instance['fixed_ips']
    for fixed_ip in fixed_ips:
        fixed_addr = fixed_ip['address']
        network = fixed_ip.get('network')
        vif = fixed_ip.get('virtual_interface')
        if not network or not vif:
            name = instance['name']
            ip = fixed_ip['address']
            LOG.warn(_("Instance %(name)s has stale IP "
                    "address: %(ip)s (no network or vif)") % locals())
            continue
        cidr_v6 = network.get('cidr_v6')
        if FLAGS.use_ipv6 and cidr_v6:
            ipv6_addr = ipv6.to_global(cidr_v6, vif['address'],
                    network['project_id'])
            if ipv6_addr not in ip_info['fixed_ip6s']:
                ip_info['fixed_ip6s'].append(ipv6_addr)

        for floating_ip in fixed_ip.get('floating_ips', []):
            float_addr = floating_ip['address']
            ip_info['floating_ips'].append(float_addr)
        ip_info['fixed_ips'].append(fixed_addr)
    return ip_info
示例#2
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 []
示例#3
0
def get_networks_for_instance(context, instance):
    """Returns a prepared nw_info list for passing into the view
    builders

    We end up with a data structure like:
    {'public': {'ips': [{'addr': '10.0.0.1', 'version': 4},
                        {'addr': '2001::1', 'version': 6}],
                'floating_ips': [{'addr': '172.16.0.1', 'version': 4},
                                 {'addr': '172.16.2.1', 'version': 4}]},
     ...}
    """

    def _emit_addr(ip, version):
        return {'addr': ip, 'version': version}

    networks = {}
    fixed_ips = instance['fixed_ips']
    ipv6_addrs_seen = {}
    for fixed_ip in fixed_ips:
        fixed_addr = fixed_ip['address']
        network = fixed_ip['network']
        vif = fixed_ip.get('virtual_interface')
        if not network or not vif:
            name = instance['name']
            ip = fixed_ip['address']
            LOG.warn(_("Instance %(name)s has stale IP "
                    "address: %(ip)s (no network or vif)") % locals())
            continue
        label = network.get('label', None)
        if label is None:
            continue
        if label not in networks:
            networks[label] = {'ips': [], 'floating_ips': []}
        nw_dict = networks[label]
        cidr_v6 = network.get('cidr_v6')
        if FLAGS.use_ipv6 and cidr_v6:
            ipv6_addr = ipv6.to_global(cidr_v6, vif['address'],
                    network['project_id'])
            # Only add same IPv6 address once.  It's possible we've
            # seen it before if there was a previous fixed_ip with
            # same network and vif as this one
            if not ipv6_addrs_seen.get(ipv6_addr):
                nw_dict['ips'].append(_emit_addr(ipv6_addr, 6))
                ipv6_addrs_seen[ipv6_addr] = True
        nw_dict['ips'].append(_emit_addr(fixed_addr, 4))
        for floating_ip in fixed_ip.get('floating_ips', []):
            float_addr = floating_ip['address']
            nw_dict['floating_ips'].append(_emit_addr(float_addr, 4))
    return networks
示例#4
0
def get_networks_for_instance(context, instance):
    """Returns a prepared nw_info list for passing into the view
    builders

    We end up with a data structure like:
    {'public': {'ips': [{'addr': '10.0.0.1', 'version': 4},
                        {'addr': '2001::1', 'version': 6}],
                'floating_ips': [{'addr': '172.16.0.1', 'version': 4},
                                 {'addr': '172.16.2.1', 'version': 4}]},
     ...}
    """
    def _emit_addr(ip, version):
        return {'addr': ip, 'version': version}

    networks = {}
    fixed_ips = instance['fixed_ips']
    ipv6_addrs_seen = {}
    for fixed_ip in fixed_ips:
        fixed_addr = fixed_ip['address']
        network = fixed_ip['network']
        vif = fixed_ip.get('virtual_interface')
        if not network or not vif:
            name = instance['name']
            ip = fixed_ip['address']
            LOG.warn(
                _("Instance %(name)s has stale IP "
                  "address: %(ip)s (no network or vif)") % locals())
            continue
        label = network.get('label', None)
        if label is None:
            continue
        if label not in networks:
            networks[label] = {'ips': [], 'floating_ips': []}
        nw_dict = networks[label]
        cidr_v6 = network.get('cidr_v6')
        if FLAGS.use_ipv6 and cidr_v6:
            ipv6_addr = ipv6.to_global(cidr_v6, vif['address'],
                                       network['project_id'])
            # Only add same IPv6 address once.  It's possible we've
            # seen it before if there was a previous fixed_ip with
            # same network and vif as this one
            if not ipv6_addrs_seen.get(ipv6_addr):
                nw_dict['ips'].append(_emit_addr(ipv6_addr, 6))
                ipv6_addrs_seen[ipv6_addr] = True
        nw_dict['ips'].append(_emit_addr(fixed_addr, 4))
        for floating_ip in fixed_ip.get('floating_ips', []):
            float_addr = floating_ip['address']
            nw_dict['floating_ips'].append(_emit_addr(float_addr, 4))
    return networks
示例#5
0
 def _get_fixed_ipv6_dict(cidr, mac, project_id):
     ip_string = ipv6.to_global(cidr, mac, project_id)
     return {'version': 6,
             'address': ip_string,
             'floating_ips': []}
示例#6
0
 def test_to_global(self):
     addr = ipv6.to_global('2001:db8::', '02:16:3e:33:44:55', 'test')
     self.assertEquals(addr, '2001:db8::a94a:8fe5:ff33:4455')
示例#7
0
 def test_to_global(self):
     addr = ipv6.to_global('2001:db8::', '02:16:3e:33:44:55', 'test')
     self.assertEquals(addr, '2001:db8::a94a:8fe5:ff33:4455')