[Openstack] Fwd: how to allocate floating IP with Python API?

Riccardo Murri riccardo.murri at gmail.com
Sun Jan 28 09:53:36 UTC 2018


Hello,

I am trying to figure out how floating IPs should be allocated with the
Python APIs.

Code like the following used to work::

        # `self.nova_client` is a `novaclient.client.Client` instance
        free_ips = [ip for ip in self.nova_client.floating_ips.list()
if not ip.fixed_ip]
        if not free_ips:
            free_ips.append(self.nova_client.floating_ips.create())
        floating_ip = free_ips.pop()
        # `instance` is a Nova client "server manager" object
        instance.add_floating_ip(floating_ip)

However, Nova client's `add_floating_ip()` method is marked as
deprecated but no alternative is given in the documentation.

I presumed that I could use the Neutron API client to do the following,
but it doesn't work either (the call to update the port with the
floating IP raises an error)::

        #
        # 1. get or create a floating IP address
        #
        # `self.neutron_client` is a `neutronclient.v2_0.client.Client` instance
        free_ips = [
            ip for ip in
            # `fixed_ip_address=''` seems to act as wildcard
            self.neutron_client.list_floatingips(fixed_ip_address='').get('floatingips')
            if (ip['fixed_ip_address'] is None
                and ip['floating_network_id'] == network_id
                and ip['port_id'] is None)
        ]
        if not free_ips:
            floating_ip = self.neutron_client.create_floatingip({
                'floatingip': {
                    'floating_network_id':network_id,
                }}).get('floatingip')
        floating_ip = free_ips.pop()
        #
        # 2. wait until at least one interface of the server is up
        #
        interfaces = []
        while not interfaces:
            interfaces = instance.interface_list()
            sleep(2)  ## FIXME: hard-coded value
        #
        # 3. get port ID
        #
        port_id = interface.port_id
        #
        # 4. assign floating IP to port
        #
        floating_ip = self.neutron_client.update_floatingip(
            floating_ip, {
                'floatingip_id': floating_ip['id'],
                'floatingip': {
                    'port_id': port_id,
                },
            }).get('floatingip')

What's the correct way of allocating floating IPs with the current
OpenStack API Python clients?

Thanks,
Riccardo


--
Riccardo Murri / Email: riccardo.murri at gmail.com / Tel.: +41 77 458 98 32



More information about the Openstack mailing list