Refactor the port update code

Introduce a port_update method in kuryr_network.py.
This method allows caller to pass an admin context to construct
the neutorn client.

Change-Id: I40349fb65bf49b3ffa80ab2da9c803bbcd2d2a1e
This commit is contained in:
Hongbin Lu 2019-01-20 00:04:22 +00:00
parent 332f9827e1
commit 0589e16f9b
3 changed files with 33 additions and 23 deletions

View File

@ -19,7 +19,6 @@ from neutronclient.common import exceptions
from oslo_log import log as logging
from oslo_utils import excutils
from zun.common import context as zun_context
from zun.common import exception
from zun.common.i18n import _
import zun.conf
@ -223,9 +222,8 @@ class KuryrNetwork(network.Network):
# NOTE(hongbin): Use admin context here because non-admin
# context might not be able to update some attributes
# (i.e. binding:profile).
admin_context = zun_context.get_admin_context()
neutron_api = neutron.NeutronAPI(admin_context)
neutron_api.update_port(neutron_port_id, port_req_body)
self.neutron_api.update_port(neutron_port_id, port_req_body,
admin=True)
else:
network = self.inspect_network(network_name)
neutron_net_id = network['Options']['neutron.net.uuid']
@ -313,9 +311,8 @@ class KuryrNetwork(network.Network):
try:
# Requires admin creds to set port bindings
admin_context = zun_context.get_admin_context()
neutron_api = neutron.NeutronAPI(admin_context)
neutron_api.update_port(port_id, port_req_body)
self.neutron_api.update_port(port_id, port_req_body,
admin=True)
except exception.PortNotFound:
LOG.debug('Unable to unbind port %s as it no longer '
'exists.', port_id)
@ -396,9 +393,8 @@ class KuryrNetwork(network.Network):
try:
# Requires admin creds to set port bindings
admin_context = zun_context.get_admin_context()
neutron_api = neutron.NeutronAPI(admin_context)
neutron_api.update_port(port_id, port_req_body)
self.neutron_api.update_port(port_id, port_req_body,
admin=True)
except exception.PortNotFound:
LOG.debug('Unable to unbind port %s as it no longer '
'exists.', port_id)
@ -431,10 +427,9 @@ class KuryrNetwork(network.Network):
"to port %(port_id)s",
{'security_group_ids': security_group_ids,
'port_id': port['id']})
admin_context = zun_context.get_admin_context()
neutron_api = neutron.NeutronAPI(admin_context)
neutron_api.update_port(port['id'],
{'port': updated_port})
self.neutron_api.update_port(port['id'],
{'port': updated_port},
admin=True)
except exceptions.NeutronClientException as e:
exc_info = sys.exc_info()
if e.status_code == 400:
@ -469,10 +464,9 @@ class KuryrNetwork(network.Network):
"from port %(port_id)s",
{'security_group_ids': security_group_ids,
'port_id': port['id']})
admin_context = zun_context.get_admin_context()
neutron_api = neutron.NeutronAPI(admin_context)
neutron_api.update_port(port['id'],
{'port': updated_port})
self.neutron_api.update_port(port['id'],
{'port': updated_port},
admin=True)
except exceptions.NeutronClientException as e:
exc_info = sys.exc_info()
if e.status_code == 400:

View File

@ -17,6 +17,7 @@ from oslo_log import log as logging
from oslo_utils import uuidutils
from zun.common import clients
from zun.common import context as zun_context
from zun.common import exception
from zun.common.i18n import _
@ -28,15 +29,29 @@ class NeutronAPI(object):
def __init__(self, context):
self.context = context
self.neutron = clients.OpenStackClients(self.context).neutron()
self.client = clients.OpenStackClients(self.context).neutron()
self.admin_client = None
def __getattr__(self, key):
return getattr(self.neutron, key)
return getattr(self.client, key)
def _get_admin_client(self):
if self.admin_client is None:
context = zun_context.get_admin_context()
self.admin_client = clients.OpenStackClients(context).neutron()
return self.admin_client
def update_port(self, port, body=None, admin=False):
if admin:
client = self._get_admin_client()
else:
client = self.client
return client.update_port(port, body)
def find_resourceid_by_name_or_id(self, resource, name_or_id,
project_id=None):
return neutronv20.find_resourceid_by_name_or_id(
self.neutron, resource, name_or_id, project_id)
self.client, resource, name_or_id, project_id)
def get_available_network(self):
search_opts = {'tenant_id': self.context.project_id, 'shared': False}

View File

@ -69,7 +69,7 @@ class FakeNeutronClient(object):
self.ports.append(port_data)
return port
def update_port(self, port_id, port):
def update_port(self, port_id, port, **kwargs):
port_data = copy.deepcopy(port['port'])
for port in self.ports:
if port['id'] == port_id:
@ -339,4 +339,5 @@ class KuryrNetworkTestCase(base.TestCase):
mock_update_port.assert_called_once_with(
'fake-port-id',
{'port': {'security_groups': ['sg1', 'sg2']}})
{'port': {'security_groups': ['sg1', 'sg2']}},
admin=True)