Merge "Make floatingip can be saved to db and can be displayed"

This commit is contained in:
Jenkins 2017-07-06 02:37:02 +00:00 committed by Gerrit Code Review
commit c9e4fe340b
2 changed files with 32 additions and 7 deletions

View File

@ -222,11 +222,13 @@ class FloatingIPController(ServerControllerBase):
msg, status_code=http_client.BAD_REQUEST)
fixed_address = None
nic_to_associate = None
if 'fixed_address' in floatingip:
fixed_address = floatingip['fixed_address']
for nic in server_nics:
for port_address in nic.fixed_ips:
if port_address['ip_address'] == fixed_address:
nic_to_associate = nic
break
else:
continue
@ -235,12 +237,19 @@ class FloatingIPController(ServerControllerBase):
msg = _('Specified fixed address not assigned to server')
raise wsme.exc.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
if nic_to_associate and nic_to_associate.floating_ip:
msg = _('The specified fixed ip has already been associated with '
'a floating ip.')
raise wsme.exc.ClientSideError(
msg, status_code=http_client.CONFLICT)
if not fixed_address:
for nic in server_nics:
if nic.floating_ip:
continue
for port_address in nic.fixed_ips:
if netutils.is_valid_ipv4(port_address['ip_address']):
fixed_address = port_address['ip_address']
nic_to_associate = nic
break
else:
continue
@ -249,14 +258,14 @@ class FloatingIPController(ServerControllerBase):
msg = _('Unable to associate floating IP %(address)s '
'to any fixed IPs for server %(id)s. '
'Server has no fixed IPv4 addresses to '
'associate.') % ({'address': address,
'id': server.uuid})
'associate or all fixed ips have already been '
'associated with floating ips.') % (
{'address': address, 'id': server.uuid})
raise wsme.exc.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
if len(server_nics) > 1:
LOG.warning('multiple ports exist, using the first '
'IPv4 fixed_ip: %s', fixed_address)
try:
self.network_api.associate_floating_ip(
pecan.request.context, floating_address=address,
@ -276,6 +285,8 @@ class FloatingIPController(ServerControllerBase):
LOG.exception(msg)
raise wsme.exc.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
nic_to_associate.floating_ip = address
nic_to_associate.save(pecan.request.context)
@policy.authorize_wsgi("mogan:server", "disassociate_floatingip")
@expose.expose(None, types.uuid, wtypes.text,
@ -327,6 +338,11 @@ class FloatingIPController(ServerControllerBase):
msg = _('Floating IP is not associated')
raise wsme.exc.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
server = self._resource or self._get_resource(server_uuid)
for nic in server.nics:
if nic.floating_ip == address:
nic.floating_ip = None
nic.save(pecan.request.context)
else:
msg = _("Floating IP %(address)s is not associated with server "
"%(id)s.") % {'address': address, 'id': server_uuid}

View File

@ -191,10 +191,19 @@ class BaremetalComputeAPIServersTest(base.BaseBaremetalComputeTest):
resp = self.network_floatingip_client.create_floatingip(
floating_network_id=self.ext_net_id)
floatingip = resp['floatingip']
nics = self.baremetal_compute_client.server_get_networks(
self.server_ids[0])
for nic in nics:
self.assertIsNone(nic['floating_ip'])
self.baremetal_compute_client.server_associate_floatingip(
self.server_ids[0], floatingip['floating_ip_address'])
# TODO(liusheng) for now we cannot query the server info including
# floatingip, that maybe a bug and the future fix need also to
# update this test.
nics = self.baremetal_compute_client.server_get_networks(
self.server_ids[0])
server_floatingips = [nic['floating_ip'] for nic in nics]
self.assertIn(floatingip['floating_ip_address'], server_floatingips)
self.baremetal_compute_client.server_disassociate_floatingip(
self.server_ids[0], floatingip['floating_ip_address'])
nics = self.baremetal_compute_client.server_get_networks(
self.server_ids[0])
for nic in nics:
self.assertIsNone(nic['floating_ip'])