Skip to content

Commit

Permalink
Implement Quantum support for addition and removal of fixed IPs
Browse files Browse the repository at this point in the history
Fixes bug 1099378

Change-Id: I696d1f702cf99ef15af47c261844febd8a6e7ffc
  • Loading branch information
Gary Kotton committed Jan 14, 2013
1 parent ca4b130 commit 1ff3afc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
55 changes: 53 additions & 2 deletions nova/network/quantumv2/api.py
Expand Up @@ -204,11 +204,62 @@ def _get_instance_nw_info(self, context, instance, networks=None):

def add_fixed_ip_to_instance(self, context, instance, network_id):
"""Add a fixed ip to the instance from specified network."""
raise NotImplementedError()
search_opts = {'network_id': network_id}
data = quantumv2.get_client(context).list_subnets(**search_opts)
ipam_subnets = data.get('subnets', [])
if not ipam_subnets:
raise exception.NetworkNotFoundForInstance(
instance_id=instance['uuid'])

zone = 'compute:%s' % instance['availability_zone']
search_opts = {'device_id': instance['uuid'],
'device_owner': zone,
'network_id': network_id}
data = quantumv2.get_client(context).list_ports(**search_opts)
ports = data['ports']
for p in ports:
fixed_ips = p['fixed_ips']
for subnet in ipam_subnets:
fixed_ip = {'subnet_id': subnet['id']}
fixed_ips.append(fixed_ip)
port_req_body = {'port': {'fixed_ips': fixed_ips}}
try:
quantumv2.get_client(context).update_port(p['id'],
port_req_body)
except Exception as ex:
msg = _("Unable to update port %(portid)s with"
" failure: %(exception)s")
LOG.debug(msg, {'portid': p['id'], 'exception': ex})
return
raise exception.NetworkNotFoundForInstance(
instance_id=instance['uuid'])

def remove_fixed_ip_from_instance(self, context, instance, address):
"""Remove a fixed ip from the instance."""
raise NotImplementedError()
zone = 'compute:%s' % instance['availability_zone']
search_opts = {'device_id': instance['uuid'],
'device_owner': zone,
'fixed_ips': 'ip_address=%s' % address}
data = quantumv2.get_client(context).list_ports(**search_opts)
ports = data['ports']
for p in ports:
fixed_ips = p['fixed_ips']
new_fixed_ips = []
for fixed_ip in fixed_ips:
if fixed_ip['ip_address'] != address:
new_fixed_ips.append(fixed_ip)
port_req_body = {'port': {'fixed_ips': new_fixed_ips}}
try:
quantumv2.get_client(context).update_port(p['id'],
port_req_body)
except Exception as ex:
msg = _("Unable to update port %(portid)s with"
" failure: %(exception)s")
LOG.debug(msg, {'portid': p['id'], 'exception': ex})
return

raise exception.FixedIpNotFoundForSpecificInstance(
instance_uuid=instance['uuid'], ip=address)

def validate_networks(self, context, requested_networks):
"""Validate that the tenant can use the requested networks."""
Expand Down
48 changes: 48 additions & 0 deletions nova/tests/network/test_quantumv2.py
Expand Up @@ -916,6 +916,54 @@ def test_disassociate_floating_ip(self):
self.mox.ReplayAll()
api.disassociate_floating_ip(self.context, self.instance, address)

def test_add_fixed_ip_to_instance(self):
api = quantumapi.API()
network_id = 'my_netid1'
search_opts = {'network_id': network_id}
self.moxed_client.list_subnets(
**search_opts).AndReturn({'subnets': self.subnet_data1})

zone = 'compute:%s' % self.instance['availability_zone']
search_opts = {'device_id': self.instance['uuid'],
'device_owner': 'compute:nova',
'network_id': network_id}
self.moxed_client.list_ports(
**search_opts).AndReturn({'ports': self.port_data1})
port_req_body = {
'port': {
'fixed_ips': [{'subnet_id': 'my_subid1'}],
},
}
port = self.port_data1[0]
port['fixed_ips'] = [{'subnet_id': 'my_subid1'}]
self.moxed_client.update_port('my_portid1',
MyComparator(port_req_body)).AndReturn({'port': port})

self.mox.ReplayAll()
api.add_fixed_ip_to_instance(self.context, self.instance, network_id)

def test_remove_fixed_ip_from_instance(self):
api = quantumapi.API()
address = '10.0.0.3'
zone = 'compute:%s' % self.instance['availability_zone']
search_opts = {'device_id': self.instance['uuid'],
'device_owner': zone,
'fixed_ips': 'ip_address=%s' % address}
self.moxed_client.list_ports(
**search_opts).AndReturn({'ports': self.port_data1})
port_req_body = {
'port': {
'fixed_ips': [],
},
}
port = self.port_data1[0]
port['fixed_ips'] = []
self.moxed_client.update_port('my_portid1',
MyComparator(port_req_body)).AndReturn({'port': port})

self.mox.ReplayAll()
api.remove_fixed_ip_from_instance(self.context, self.instance, address)


class TestQuantumv2ModuleMethods(test.TestCase):
def test_ensure_requested_network_ordering_no_preference(self):
Expand Down

0 comments on commit 1ff3afc

Please sign in to comment.