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 Neutron Network UUID. """ n = objects.Network.get_by_uuid(context.elevated(), net_id) subnet_v4 = { 'network_id': n.uuid, 'cidr': n.cidr, 'gateway': n.gateway, 'dhcp_server': getattr(n, 'dhcp_server'), 'broadcast': n.broadcast, 'netmask': n.netmask, 'version': 4, 'dns1': n.dns1 if utils.is_valid_ipv4(n.dns1) else None, 'dns2': n.dns2 if utils.is_valid_ipv4(n.dns2) else None} subnet_v6 = { 'network_id': n.uuid, 'cidr': n.cidr_v6, 'gateway': n.gateway_v6, 'dhcp_server': None, 'broadcast': None, 'netmask': n.netmask_v6, 'version': 6, 'dns1': n.dns1 if utils.is_valid_ipv6(n.dns1) else None, 'dns2': n.dns2 if utils.is_valid_ipv6(n.dns2) else None} def ips_to_strs(net): for key, value in net.items(): if isinstance(value, netaddr.ip.BaseIP): net[key] = str(value) return net return [ips_to_strs(subnet_v4), ips_to_strs(subnet_v6)]
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 Neutron Network UUID. """ n = objects.Network.get_by_uuid(context.elevated(), net_id) subnet_v4 = { 'network_id': n.uuid, 'cidr': n.cidr, 'gateway': n.gateway, 'dhcp_server': getattr(n, 'dhcp_server'), 'broadcast': n.broadcast, 'netmask': n.netmask, 'version': 4, 'dns1': n.dns1 if utils.is_valid_ipv4(n.dns1) else None, 'dns2': n.dns2 if utils.is_valid_ipv4(n.dns2) else None } subnet_v6 = { 'network_id': n.uuid, 'cidr': n.cidr_v6, 'gateway': n.gateway_v6, 'dhcp_server': None, 'broadcast': None, 'netmask': n.netmask_v6, 'version': 6, 'dns1': n.dns1 if utils.is_valid_ipv6(n.dns1) else None, 'dns2': n.dns2 if utils.is_valid_ipv6(n.dns2) else None } def ips_to_strs(net): for key, value in net.items(): if isinstance(value, netaddr.ip.BaseIP): net[key] = str(value) return net return [ips_to_strs(subnet_v4), ips_to_strs(subnet_v6)]
def show(self, req, domain_id, id): """Return the DNS entry that corresponds to domain_id and id.""" context = req.environ['nova.context'] authorize(context) domain = _unquote_domain(domain_id) floating_ip = None # Check whether id is a valid ipv4/ipv6 address. if utils.is_valid_ipv4(id) or utils.is_valid_ipv6(id): floating_ip = id if floating_ip: entries = self.network_api.get_dns_entries_by_address( context, floating_ip, domain) else: entries = self.network_api.get_dns_entries_by_name( context, id, domain) if not entries: explanation = _("DNS entries not found.") raise webob.exc.HTTPNotFound(explanation=explanation) if floating_ip: entrylist = [ _create_dns_entry(floating_ip, entry, domain) for entry in entries ] dns_entries = _translate_dns_entries_view(entrylist) return wsgi.ResponseObject(dns_entries, xml=FloatingIPDNSsTemplate) entry = _create_dns_entry(entries[0], id, domain) return _translate_dns_entry_view(entry)
def show(self, req, domain_id, id): """Return the DNS entry that corresponds to domain_id and id.""" context = req.environ['nova.context'] authorize(context) domain = _unquote_domain(domain_id) floating_ip = None # Check whether id is a valid ipv4/ipv6 address. if utils.is_valid_ipv4(id) or utils.is_valid_ipv6(id): floating_ip = id if floating_ip: entries = self.network_api.get_dns_entries_by_address(context, floating_ip, domain) else: entries = self.network_api.get_dns_entries_by_name(context, id, domain) if not entries: explanation = _("DNS entries not found.") raise webob.exc.HTTPNotFound(explanation=explanation) if floating_ip: entrylist = [_create_dns_entry(floating_ip, entry, domain) for entry in entries] dns_entries = _translate_dns_entries_view(entrylist) return wsgi.ResponseObject(dns_entries, xml=FloatingIPDNSsTemplate) entry = _create_dns_entry(entries[0], id, domain) return _translate_dns_entry_view(entry)
def validate_networks(self, context, networks): """check if the networks exists and host is set to each network. """ if networks is None or len(networks) == 0: return network_uuids = [uuid for (uuid, fixed_ip) in networks] self._get_networks_by_uuids(context, network_uuids) for network_uuid, address in networks: # check if the fixed IP address is valid and # it actually belongs to the network if address is not None: if not utils.is_valid_ipv4(address): raise exception.FixedIpInvalid(address=address) fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address) if fixed_ip_ref['network']['uuid'] != network_uuid: raise exception.FixedIpNotFoundForNetwork(address=address, network_uuid=network_uuid) if fixed_ip_ref['instance'] is not None: raise exception.FixedIpAlreadyInUse(address=address)
def _get_requested_networks(self, requested_networks): """Create a list of requested networks from the networks attribute.""" networks = [] for network in requested_networks: try: port_id = network.get('port', None) if port_id: network_uuid = None if not self._is_quantum_v2(): # port parameter is only for qunatum v2.0 msg = _("Unknown argment : port") raise exc.HTTPBadRequest(explanation=msg) if not utils.is_uuid_like(port_id): msg = _("Bad port format: port uuid is " "not in proper format " "(%s)") % port_id raise exc.HTTPBadRequest(explanation=msg) else: network_uuid = network['uuid'] if not port_id and not utils.is_uuid_like(network_uuid): br_uuid = network_uuid.split('-', 1)[-1] if not utils.is_uuid_like(br_uuid): msg = _("Bad networks format: network uuid is " "not in proper format " "(%s)") % network_uuid raise exc.HTTPBadRequest(explanation=msg) #fixed IP address is optional #if the fixed IP address is not provided then #it will use one of the available IP address from the network address = network.get('fixed_ip', None) if address is not None and not utils.is_valid_ipv4(address): msg = _("Invalid fixed IP address (%s)") % address raise exc.HTTPBadRequest(explanation=msg) # For quantumv2, requestd_networks # should be tuple of (network_uuid, fixed_ip, port_id) if self._is_quantum_v2(): networks.append((network_uuid, address, port_id)) else: # check if the network id is already present in the list, # we don't want duplicate networks to be passed # at the boot time for id, ip in networks: if id == network_uuid: expl = (_("Duplicate networks" " (%s) are not allowed") % network_uuid) raise exc.HTTPBadRequest(explanation=expl) networks.append((network_uuid, address)) except KeyError as key: expl = _('Bad network format: missing %s') % key raise exc.HTTPBadRequest(explanation=expl) except TypeError: expl = _('Bad networks format') raise exc.HTTPBadRequest(explanation=expl) return networks
def _get_requested_networks(self, requested_networks): """Create a list of requested networks from the networks attribute.""" networks = [] for network in requested_networks: try: network_uuid = network['uuid'] if not utils.is_uuid_like(network_uuid): br_uuid = network_uuid.split('-', 1)[-1] if not utils.is_uuid_like(br_uuid): msg = _("Bad networks format: network uuid is " "not in proper format " "(%s)") % network_uuid raise exc.HTTPBadRequest(explanation=msg) #fixed IP address is optional #if the fixed IP address is not provided then #it will use one of the available IP address from the network address = network.get('fixed_ip', None) if address is not None and not utils.is_valid_ipv4(address): msg = _("Invalid fixed IP address (%s)") % address raise exc.HTTPBadRequest(explanation=msg) # check if the network id is already present in the list, # we don't want duplicate networks to be passed # at the boot time for id, ip in networks: if id == network_uuid: expl = (_("Duplicate networks (%s) are not allowed") % network_uuid) raise exc.HTTPBadRequest(explanation=expl) networks.append((network_uuid, address)) except KeyError as key: expl = _('Bad network format: missing %s') % key raise exc.HTTPBadRequest(explanation=expl) except TypeError: expl = _('Bad networks format') raise exc.HTTPBadRequest(explanation=expl) return networks
def _validate_access_ipv4(self, address): if not utils.is_valid_ipv4(address): expl = _('access_ip_v4 is not proper IPv4 format') raise exc.HTTPBadRequest(explanation=expl)
def test_is_valid_ipv4(self): self.assertTrue(utils.is_valid_ipv4('127.0.0.1')) self.assertFalse(utils.is_valid_ipv4('::1')) self.assertFalse(utils.is_valid_ipv4('bacon')) self.assertFalse(utils.is_valid_ipv4("")) self.assertFalse(utils.is_valid_ipv4(10))
def test_is_valid_ipv4(self): self.assertTrue(utils.is_valid_ipv4("127.0.0.1")) self.assertFalse(utils.is_valid_ipv4("::1")) self.assertFalse(utils.is_valid_ipv4("bacon")) self.assertFalse(utils.is_valid_ipv4("")) self.assertFalse(utils.is_valid_ipv4(10))