def _get_fixed_ip_address(self, port_uuid, client): """Get a Neutron port's fixed ip address. :param port_uuid: Neutron port id. :param client: Neutron client instance. :returns: Neutron port ip address. :raises: FailedToGetIPAddressOnPort :raises: InvalidIPv4Address """ ip_address = None try: neutron_port = client.show_port(port_uuid).get('port') except neutron_client_exc.NeutronClientException: LOG.exception("Failed to Get IP address on Neutron port %s.", port_uuid) raise exception.FailedToGetIPAddressOnPort(port_id=port_uuid) fixed_ips = neutron_port.get('fixed_ips') # NOTE(faizan) At present only the first fixed_ip assigned to this # neutron port will be used, since nova allocates only one fixed_ip # for the instance. if fixed_ips: ip_address = fixed_ips[0].get('ip_address', None) if ip_address: if netutils.is_valid_ipv4(ip_address): return ip_address else: LOG.error("Neutron returned invalid IPv4 address %s.", ip_address) raise exception.InvalidIPv4Address(ip_address=ip_address) else: LOG.error("No IP address assigned to Neutron port %s.", port_uuid) raise exception.FailedToGetIPAddressOnPort(port_id=port_uuid)
def _get_fixed_ip_address(self, port_id, client): """Get a Neutron port's fixed ip address. :param port_id: Neutron port id. :param client: Neutron client instance. :returns: Neutron port ip address. :raises: NetworkError :raises: InvalidIPv4Address :raises: FailedToGetIPAddressOnPort """ ip_address = None try: neutron_port = client.get_port(port_id) except openstack_exc.OpenStackCloudException: raise exception.NetworkError( _('Could not retrieve neutron port: %s') % port_id) fixed_ips = neutron_port.get('fixed_ips') # NOTE(faizan) At present only the first fixed_ip assigned to this # neutron port will be used, since nova allocates only one fixed_ip # for the instance. if fixed_ips: ip_address = fixed_ips[0].get('ip_address', None) if ip_address: try: if (ipaddress.ip_address(ip_address).version == 4 or ipaddress.ip_address(ip_address).version == 6): return ip_address else: LOG.error( "Neutron returned invalid IP " "address %(ip_address)s on port %(port_id)s.", { 'ip_address': ip_address, 'port_id': port_id }) raise exception.InvalidIPv4Address(ip_address=ip_address) except ValueError as exc: LOG.error( "An Invalid IP address was supplied and failed " "basic validation: %s", exc) raise exception.InvalidIPAddress(ip_address=ip_address) else: LOG.error("No IP address assigned to Neutron port %s.", port_id) raise exception.FailedToGetIPAddressOnPort(port_id=port_id)