Search code examples
apiopenstackfloating-ip

How to Create a Floating IP and then assign it to a server using Openstack API


I am currently creating OpenStack virtual machines programmatically using OpenStack API. I need to associate floating IP(s) to the server(s) created. The API documentation on this is not clear to me. The compute API says it is deprecated, though I tried to use it, but I am not sure how to correctly use it. There is no API to first create the floating IP - using the compute API for floating IP. I have also tried using the Neutron networking API for floating IP, also this does not show how to associate the floating IP to a new server. It is asking for a port id, and the response of the create server API does not return a port id, neither does it return an IP address.

Could someone please direct me on this?


Solution

  • You should use the neutron-API for the floating-ip. The nova-version is really depricated and is a leftover from the time, when nova and neutron were one single component until they moved out most of the networking stuff into neutron.

    Creating a Server is an asynchronous process and get its ip address after a few seconds. So you should create a loop, which runs get-requests against the new server-id and checks the state of the server until he becomes active and has the ip (https://docs.openstack.org/api-ref/compute/?expanded=show-server-details-detail#show-server-details and check OS-EXT-STS:vm_state for the active state). After this you can request all ports and check in the result, which port-id belongs to the ip of the new created server (https://docs.openstack.org/api-ref/network/v2/?expanded=list-ports-detail#list-ports and check the fixed_ips for the ip of your server). With this port-id you should be able to create your floating-ip.

    Maybe there is an easier way, but that's the first I would try, when I would have to solve this task.