Files
group-based-policy/gbpservice/common/utils.py
Sumit Naiksatam d649785c9e Pike sync
The following changes have been made to coordinate with the changes
made in Neutron for Pike:

* Partial use of Neutron context has been completely moved to neutron_lib's
context.

* The patching of neutron.db.api.get_session() has been replaced with
patching of sqlalchemy.orm.session to add the notification_queue attribute.
This significantly reduces the earlier complexity of patching.

* Use of top-level start of transaction in GBP plugins:
with context.session.begin(subtransactions=True):
has been migrated to use of:
with db_api.context_manager.writer.using(context):
or
with db_api.context_manager.reader.using(context)
as relevant.

* Calls to _make_resource_xxx_dict() in GBP plugins have been moved
to inside the transaction.

* The use of:
neutron.callbacks.events
neutron.callbacks.exceptions
neutron.callbacks.registry
to
neutron_lib.callbacks.events
neutron_lib.callbacks.exceptions
neutron_lib.callbacks.registry

* The use of:
neutron.api.v2.attributes.resource_xxx
neutron.extensions.extension_xxx
to:
from neutron_lib.api.definitions.resource_xxx
from neutron_lib.api.definitions.extension_xxx
resp.

* The use of:
neutron.db.db_base_plugin_v2.NeutronDbPluginV2.register_dict_extend_funcs
to:
neutron.db._resource_extend.resource_extend
(the latter is a decorator)

* The use of:
neutron.db.db_base_plugin_v2.NeutronDbPluginV2.register_model_query_hook()
to:
from neutron.db import _model_query as model_query.register_hook()

* The use of:
neutron.db.segments_db.NetworkSegment
to:
neutron.db.models.segment.NetworkSegment

* In the case of Neutron ml2plus plugin (used by APIC/AIM solution),
the use of get_admin_context() has been patched to return elevated
version of the current context in use. This helps to preserve the session
and transaction semantics. Ideally, context.elevated() would have been
directly used in all these places, however the current context is not
available in these places, and hence getting the current context and elevating
it is wrapped in the get_admin_context() patched method.

* In the case of the components used by the APIC/AIM solution (including
the ml2plus and l3_plugin) the use of:
with context.session.begin(subtransactions=True):
to
with db_api.context_manager.writer.using(context):
or
with db_api.context_manager.reader.using(context):
as relevant.

* Patching of methods from Neutron which is no longer relevant have been
removed from gbpservice.neutron.extensions.patch module.

* Setting up of UTs has been fixed to load and reset configurations
appropriately. This helps to eleminate some failures when tests are
run in non-deterministic orders.

* In tree devstack plugin has been updated (aim repo commit pin needs
to be reverted).

* Gate jobs have been updated as relevant (including fixes to the exercise
scripts and job configurations).

The associated repos, namely, client, UI and automation have also been
updated (the reference to the client's gerrit patch needs to be updated
once the patch has been merged).

Change-Id: I11dd089effbf40cf104afd720dc40a9911dcf28d
2018-01-06 19:36:36 -08:00

110 lines
3.0 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
from neutron_lib import context as n_context
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
from stevedore import driver
from gbpservice._i18n import _
LOG = logging.getLogger(__name__)
cfg.CONF.import_group('keystone_authtoken', 'keystonemiddleware.auth_token')
def get_obj_from_stack(cls):
i = 1
try:
while True:
for val in sys._getframe(i).f_locals.itervalues():
if isinstance(val, cls):
return val
i = i + 1
except Exception:
return
def get_current_context():
return get_obj_from_stack(n_context.Context)
def get_current_session():
ctx = get_current_context()
if ctx:
return ctx.session
def get_resource_plural(resource):
if resource.endswith('y'):
resource_plural = resource.replace('y', 'ies')
else:
resource_plural = resource + 's'
return resource_plural
def load_plugin(namespace, plugin):
try:
# Try to resolve plugin by name
mgr = driver.DriverManager(namespace, plugin)
plugin_class = mgr.driver
except RuntimeError as e1:
# fallback to class name
try:
plugin_class = importutils.import_class(plugin)
except ImportError as e2:
LOG.exception("Error loading plugin by name, %s", e1)
LOG.exception("Error loading plugin by class, %s", e2)
raise ImportError(_("Plugin not found."))
return plugin_class()
def admin_context(context):
admin_context = n_context.get_admin_context()
admin_context._session = context.session
return admin_context
class DictClass(dict):
def __getattr__(self, item):
return self[item]
__setattr__ = dict.__setattr__
__delattr__ = dict.__delattr__
def get_keystone_creds():
keystone_conf = cfg.CONF.keystone_authtoken
user = keystone_conf.username
pw = keystone_conf.password
tenant = keystone_conf.project_name
if keystone_conf.get('auth_uri'):
auth_url = keystone_conf.auth_uri.rstrip('/')
if not auth_url.endswith('/v2.0'):
auth_url += '/v2.0'
else:
auth_url = ('%s://%s:%s/v2.0' % (
keystone_conf.auth_protocol,
keystone_conf.auth_host,
keystone_conf.auth_port))
return user, pw, tenant, auth_url + '/'
def set_difference(iterable_1, iterable_2):
set1 = set(iterable_1)
set2 = set(iterable_2)
return (set1 - set2), (set2 - set1)