Remove dupl. for get_resources in adv. services
There is duplication of logic related to creating API resources for advanced services extensions. Extracted the common logic out into a new module that can be used by the services. There are already UT cases in the services that exercises the logic, so no new tests were added. Change-Id: Ib52e2de6f215a1755f11a382dc4451a05ce3b147 Closes-Bug: 1258656
This commit is contained in:
parent
eb12fd9aeb
commit
e0f4a5d27c
75
neutron/api/v2/resource_helper.py
Normal file
75
neutron/api/v2/resource_helper.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
# (c) Copyright 2014 Cisco Systems Inc.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
# @author: Paul Michali Cisco Systems, Inc.
|
||||||
|
|
||||||
|
from oslo.config import cfg
|
||||||
|
|
||||||
|
from neutron.api import extensions
|
||||||
|
from neutron.api.v2 import base
|
||||||
|
from neutron import manager
|
||||||
|
from neutron.plugins.common import constants
|
||||||
|
from neutron import quota
|
||||||
|
|
||||||
|
|
||||||
|
def build_plural_mappings(special_mappings, resource_map):
|
||||||
|
"""Create plural to singular mapping for all resources.
|
||||||
|
|
||||||
|
Allows for special mappings to be provided, like policies -> policy.
|
||||||
|
Otherwise, will strip off the last character for normal mappings, like
|
||||||
|
routers -> router.
|
||||||
|
"""
|
||||||
|
plural_mappings = {}
|
||||||
|
for plural in resource_map:
|
||||||
|
singular = special_mappings.get(plural, plural[:-1])
|
||||||
|
plural_mappings[plural] = singular
|
||||||
|
return plural_mappings
|
||||||
|
|
||||||
|
|
||||||
|
def build_resource_info(plural_mappings, resource_map, which_service,
|
||||||
|
action_map=None, register_quota=False,
|
||||||
|
translate_name=False, allow_bulk=False):
|
||||||
|
"""Build resources for advanced services.
|
||||||
|
|
||||||
|
Takes the resource information, and singular/plural mappings, and creates
|
||||||
|
API resource objects for advanced services extensions. Will optionally
|
||||||
|
translate underscores to dashes in resource names, register the resource,
|
||||||
|
and accept action information for resources.
|
||||||
|
"""
|
||||||
|
resources = []
|
||||||
|
if action_map is None:
|
||||||
|
action_map = {}
|
||||||
|
plugin = manager.NeutronManager.get_service_plugins()[which_service]
|
||||||
|
for collection_name in resource_map:
|
||||||
|
resource_name = plural_mappings[collection_name]
|
||||||
|
params = resource_map.get(collection_name, {})
|
||||||
|
if translate_name:
|
||||||
|
collection_name = collection_name.replace('_', '-')
|
||||||
|
if register_quota:
|
||||||
|
quota.QUOTAS.register_resource_by_name(resource_name)
|
||||||
|
member_actions = action_map.get(resource_name, {})
|
||||||
|
controller = base.create_resource(
|
||||||
|
collection_name, resource_name, plugin, params,
|
||||||
|
member_actions=member_actions,
|
||||||
|
allow_bulk=allow_bulk,
|
||||||
|
allow_pagination=cfg.CONF.allow_pagination,
|
||||||
|
allow_sorting=cfg.CONF.allow_sorting)
|
||||||
|
resource = extensions.ResourceExtension(
|
||||||
|
collection_name,
|
||||||
|
controller,
|
||||||
|
path_prefix=constants.COMMON_PREFIXES[which_service],
|
||||||
|
member_actions=member_actions,
|
||||||
|
attr_map=params)
|
||||||
|
resources.append(resource)
|
||||||
|
return resources
|
@ -24,9 +24,8 @@ import six
|
|||||||
|
|
||||||
from neutron.api import extensions
|
from neutron.api import extensions
|
||||||
from neutron.api.v2 import attributes as attr
|
from neutron.api.v2 import attributes as attr
|
||||||
from neutron.api.v2 import base
|
from neutron.api.v2 import resource_helper
|
||||||
from neutron.common import exceptions as qexception
|
from neutron.common import exceptions as qexception
|
||||||
from neutron import manager
|
|
||||||
from neutron.openstack.common import log as logging
|
from neutron.openstack.common import log as logging
|
||||||
from neutron.plugins.common import constants
|
from neutron.plugins.common import constants
|
||||||
from neutron.services.service_base import ServicePluginBase
|
from neutron.services.service_base import ServicePluginBase
|
||||||
@ -321,46 +320,16 @@ class Firewall(extensions.ExtensionDescriptor):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_resources(cls):
|
def get_resources(cls):
|
||||||
my_plurals = []
|
special_mappings = {'firewall_policies': 'firewall_policy'}
|
||||||
for plural in RESOURCE_ATTRIBUTE_MAP:
|
plural_mappings = resource_helper.build_plural_mappings(
|
||||||
if plural == 'firewall_policies':
|
special_mappings, RESOURCE_ATTRIBUTE_MAP)
|
||||||
singular = 'firewall_policy'
|
attr.PLURALS.update(plural_mappings)
|
||||||
else:
|
action_map = {'firewall_policy': {'insert_rule': 'PUT',
|
||||||
singular = plural[:-1]
|
'remove_rule': 'PUT'}}
|
||||||
my_plurals.append((plural, singular))
|
return resource_helper.build_resource_info(plural_mappings,
|
||||||
attr.PLURALS.update(dict(my_plurals))
|
RESOURCE_ATTRIBUTE_MAP,
|
||||||
resources = []
|
constants.FIREWALL,
|
||||||
plugin = manager.NeutronManager.get_service_plugins()[
|
action_map=action_map)
|
||||||
constants.FIREWALL]
|
|
||||||
for collection_name in RESOURCE_ATTRIBUTE_MAP:
|
|
||||||
# Special handling needed for resources with 'y' ending
|
|
||||||
if collection_name == 'firewall_policies':
|
|
||||||
resource_name = 'firewall_policy'
|
|
||||||
else:
|
|
||||||
resource_name = collection_name[:-1]
|
|
||||||
|
|
||||||
params = RESOURCE_ATTRIBUTE_MAP[collection_name]
|
|
||||||
|
|
||||||
member_actions = {}
|
|
||||||
if resource_name == 'firewall_policy':
|
|
||||||
member_actions = {'insert_rule': 'PUT',
|
|
||||||
'remove_rule': 'PUT'}
|
|
||||||
|
|
||||||
controller = base.create_resource(
|
|
||||||
collection_name, resource_name, plugin, params,
|
|
||||||
member_actions=member_actions,
|
|
||||||
allow_pagination=cfg.CONF.allow_pagination,
|
|
||||||
allow_sorting=cfg.CONF.allow_sorting)
|
|
||||||
|
|
||||||
resource = extensions.ResourceExtension(
|
|
||||||
collection_name,
|
|
||||||
controller,
|
|
||||||
path_prefix=constants.COMMON_PREFIXES[constants.FIREWALL],
|
|
||||||
member_actions=member_actions,
|
|
||||||
attr_map=params)
|
|
||||||
resources.append(resource)
|
|
||||||
|
|
||||||
return resources
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_plugin_interface(cls):
|
def get_plugin_interface(cls):
|
||||||
|
@ -24,11 +24,9 @@ from oslo.config import cfg
|
|||||||
|
|
||||||
from neutron.api import extensions
|
from neutron.api import extensions
|
||||||
from neutron.api.v2 import attributes as attr
|
from neutron.api.v2 import attributes as attr
|
||||||
from neutron.api.v2 import base
|
from neutron.api.v2 import resource_helper
|
||||||
from neutron.common import exceptions as qexception
|
from neutron.common import exceptions as qexception
|
||||||
from neutron import manager
|
|
||||||
from neutron.plugins.common import constants
|
from neutron.plugins.common import constants
|
||||||
from neutron import quota
|
|
||||||
|
|
||||||
|
|
||||||
# L3 Exceptions
|
# L3 Exceptions
|
||||||
@ -176,35 +174,16 @@ class L3(extensions.ExtensionDescriptor):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def get_resources(cls):
|
def get_resources(cls):
|
||||||
"""Returns Ext Resources."""
|
"""Returns Ext Resources."""
|
||||||
my_plurals = [(key, key[:-1]) for key in RESOURCE_ATTRIBUTE_MAP.keys()]
|
plural_mappings = resource_helper.build_plural_mappings(
|
||||||
attr.PLURALS.update(dict(my_plurals))
|
{}, RESOURCE_ATTRIBUTE_MAP)
|
||||||
exts = []
|
attr.PLURALS.update(plural_mappings)
|
||||||
plugin = manager.NeutronManager.get_service_plugins()[
|
action_map = {'router': {'add_router_interface': 'PUT',
|
||||||
constants.L3_ROUTER_NAT]
|
'remove_router_interface': 'PUT'}}
|
||||||
for resource_name in ['router', 'floatingip']:
|
return resource_helper.build_resource_info(plural_mappings,
|
||||||
collection_name = resource_name + "s"
|
RESOURCE_ATTRIBUTE_MAP,
|
||||||
params = RESOURCE_ATTRIBUTE_MAP.get(collection_name, dict())
|
constants.L3_ROUTER_NAT,
|
||||||
|
action_map=action_map,
|
||||||
member_actions = {}
|
register_quota=True)
|
||||||
if resource_name == 'router':
|
|
||||||
member_actions = {'add_router_interface': 'PUT',
|
|
||||||
'remove_router_interface': 'PUT'}
|
|
||||||
|
|
||||||
quota.QUOTAS.register_resource_by_name(resource_name)
|
|
||||||
|
|
||||||
controller = base.create_resource(
|
|
||||||
collection_name, resource_name, plugin, params,
|
|
||||||
member_actions=member_actions,
|
|
||||||
allow_pagination=cfg.CONF.allow_pagination,
|
|
||||||
allow_sorting=cfg.CONF.allow_sorting)
|
|
||||||
|
|
||||||
ex = extensions.ResourceExtension(collection_name,
|
|
||||||
controller,
|
|
||||||
member_actions=member_actions,
|
|
||||||
attr_map=params)
|
|
||||||
exts.append(ex)
|
|
||||||
|
|
||||||
return exts
|
|
||||||
|
|
||||||
def update_attributes_map(self, attributes):
|
def update_attributes_map(self, attributes):
|
||||||
super(L3, self).update_attributes_map(
|
super(L3, self).update_attributes_map(
|
||||||
|
@ -17,12 +17,12 @@
|
|||||||
|
|
||||||
import abc
|
import abc
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from neutron.api import extensions
|
from neutron.api import extensions
|
||||||
from neutron.api.v2 import attributes as attr
|
from neutron.api.v2 import attributes as attr
|
||||||
from neutron.api.v2 import base
|
from neutron.api.v2 import base
|
||||||
|
from neutron.api.v2 import resource_helper
|
||||||
from neutron.common import exceptions as qexception
|
from neutron.common import exceptions as qexception
|
||||||
from neutron import manager
|
from neutron import manager
|
||||||
from neutron.plugins.common import constants
|
from neutron.plugins.common import constants
|
||||||
@ -315,36 +315,17 @@ class Loadbalancer(extensions.ExtensionDescriptor):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_resources(cls):
|
def get_resources(cls):
|
||||||
my_plurals = [(key, key[:-1]) for key in RESOURCE_ATTRIBUTE_MAP.keys()]
|
plural_mappings = resource_helper.build_plural_mappings(
|
||||||
my_plurals.append(('health_monitors_status', 'health_monitor_status'))
|
{}, RESOURCE_ATTRIBUTE_MAP)
|
||||||
attr.PLURALS.update(dict(my_plurals))
|
plural_mappings['health_monitors_status'] = 'health_monitor_status'
|
||||||
resources = []
|
attr.PLURALS.update(plural_mappings)
|
||||||
|
action_map = {'pool': {'stats': 'GET'}}
|
||||||
|
resources = resource_helper.build_resource_info(plural_mappings,
|
||||||
|
RESOURCE_ATTRIBUTE_MAP,
|
||||||
|
constants.LOADBALANCER,
|
||||||
|
action_map=action_map)
|
||||||
plugin = manager.NeutronManager.get_service_plugins()[
|
plugin = manager.NeutronManager.get_service_plugins()[
|
||||||
constants.LOADBALANCER]
|
constants.LOADBALANCER]
|
||||||
for collection_name in RESOURCE_ATTRIBUTE_MAP:
|
|
||||||
# Special handling needed for resources with 'y' ending
|
|
||||||
# (e.g. proxies -> proxy)
|
|
||||||
resource_name = collection_name[:-1]
|
|
||||||
params = RESOURCE_ATTRIBUTE_MAP[collection_name]
|
|
||||||
|
|
||||||
member_actions = {}
|
|
||||||
if resource_name == 'pool':
|
|
||||||
member_actions = {'stats': 'GET'}
|
|
||||||
|
|
||||||
controller = base.create_resource(
|
|
||||||
collection_name, resource_name, plugin, params,
|
|
||||||
member_actions=member_actions,
|
|
||||||
allow_pagination=cfg.CONF.allow_pagination,
|
|
||||||
allow_sorting=cfg.CONF.allow_sorting)
|
|
||||||
|
|
||||||
resource = extensions.ResourceExtension(
|
|
||||||
collection_name,
|
|
||||||
controller,
|
|
||||||
path_prefix=constants.COMMON_PREFIXES[constants.LOADBALANCER],
|
|
||||||
member_actions=member_actions,
|
|
||||||
attr_map=params)
|
|
||||||
resources.append(resource)
|
|
||||||
|
|
||||||
for collection_name in SUB_RESOURCE_ATTRIBUTE_MAP:
|
for collection_name in SUB_RESOURCE_ATTRIBUTE_MAP:
|
||||||
# Special handling needed for sub-resources with 'y' ending
|
# Special handling needed for sub-resources with 'y' ending
|
||||||
# (e.g. proxies -> proxy)
|
# (e.g. proxies -> proxy)
|
||||||
|
@ -20,9 +20,8 @@ import six
|
|||||||
|
|
||||||
from neutron.api import extensions
|
from neutron.api import extensions
|
||||||
from neutron.api.v2 import attributes as attr
|
from neutron.api.v2 import attributes as attr
|
||||||
from neutron.api.v2 import base
|
from neutron.api.v2 import resource_helper
|
||||||
from neutron.common import exceptions as qexception
|
from neutron.common import exceptions as qexception
|
||||||
from neutron import manager
|
|
||||||
from neutron.openstack.common import log as logging
|
from neutron.openstack.common import log as logging
|
||||||
from neutron.plugins.common import constants
|
from neutron.plugins.common import constants
|
||||||
from neutron.services import service_base
|
from neutron.services import service_base
|
||||||
@ -112,31 +111,16 @@ class Metering(extensions.ExtensionDescriptor):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def get_resources(cls):
|
def get_resources(cls):
|
||||||
"""Returns Ext Resources."""
|
"""Returns Ext Resources."""
|
||||||
my_plurals = [(key, key[:-1]) for key in RESOURCE_ATTRIBUTE_MAP.keys()]
|
plural_mappings = resource_helper.build_plural_mappings(
|
||||||
attr.PLURALS.update(dict(my_plurals))
|
{}, RESOURCE_ATTRIBUTE_MAP)
|
||||||
exts = []
|
attr.PLURALS.update(plural_mappings)
|
||||||
plugin = manager.NeutronManager.get_service_plugins()[
|
# PCM: Metering sets pagination and sorting to True. Do we have cfg
|
||||||
constants.METERING]
|
# entries for these so can be read? Else, must pass in.
|
||||||
for resource_name in ['metering_label', 'metering_label_rule']:
|
return resource_helper.build_resource_info(plural_mappings,
|
||||||
collection_name = resource_name + "s"
|
RESOURCE_ATTRIBUTE_MAP,
|
||||||
|
constants.METERING,
|
||||||
collection_name = collection_name.replace('_', '-')
|
translate_name=True,
|
||||||
params = RESOURCE_ATTRIBUTE_MAP.get(resource_name + "s", dict())
|
allow_bulk=True)
|
||||||
|
|
||||||
controller = base.create_resource(collection_name,
|
|
||||||
resource_name,
|
|
||||||
plugin, params, allow_bulk=True,
|
|
||||||
allow_pagination=True,
|
|
||||||
allow_sorting=True)
|
|
||||||
|
|
||||||
ex = extensions.ResourceExtension(
|
|
||||||
collection_name,
|
|
||||||
controller,
|
|
||||||
path_prefix=constants.COMMON_PREFIXES[constants.METERING],
|
|
||||||
attr_map=params)
|
|
||||||
exts.append(ex)
|
|
||||||
|
|
||||||
return exts
|
|
||||||
|
|
||||||
def update_attributes_map(self, attributes):
|
def update_attributes_map(self, attributes):
|
||||||
super(Metering, self).update_attributes_map(
|
super(Metering, self).update_attributes_map(
|
||||||
|
@ -19,16 +19,13 @@
|
|||||||
|
|
||||||
import abc
|
import abc
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from neutron.api import extensions
|
from neutron.api import extensions
|
||||||
from neutron.api.v2 import attributes as attr
|
from neutron.api.v2 import attributes as attr
|
||||||
from neutron.api.v2 import base
|
from neutron.api.v2 import resource_helper
|
||||||
from neutron.common import exceptions as qexception
|
from neutron.common import exceptions as qexception
|
||||||
from neutron import manager
|
|
||||||
from neutron.plugins.common import constants
|
from neutron.plugins.common import constants
|
||||||
from neutron import quota
|
|
||||||
from neutron.services.service_base import ServicePluginBase
|
from neutron.services.service_base import ServicePluginBase
|
||||||
|
|
||||||
|
|
||||||
@ -359,38 +356,17 @@ class Vpnaas(extensions.ExtensionDescriptor):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_resources(cls):
|
def get_resources(cls):
|
||||||
plural_mapping = {
|
special_mappings = {'ikepolicies': 'ikepolicy',
|
||||||
'ikepolicies': 'ikepolicy',
|
'ipsecpolicies': 'ipsecpolicy'}
|
||||||
'ipsecpolicies': 'ipsecpolicy'
|
plural_mappings = resource_helper.build_plural_mappings(
|
||||||
}
|
special_mappings, RESOURCE_ATTRIBUTE_MAP)
|
||||||
my_plurals = []
|
plural_mappings['peer_cidrs'] = 'peer_cidr'
|
||||||
for plural in RESOURCE_ATTRIBUTE_MAP:
|
attr.PLURALS.update(plural_mappings)
|
||||||
singular = plural_mapping.get(plural, plural[:-1])
|
return resource_helper.build_resource_info(plural_mappings,
|
||||||
my_plurals.append((plural, singular))
|
RESOURCE_ATTRIBUTE_MAP,
|
||||||
my_plurals.append(('peer_cidrs', 'peer_cidr'))
|
constants.VPN,
|
||||||
attr.PLURALS.update(dict(my_plurals))
|
register_quota=True,
|
||||||
resources = []
|
translate_name=True)
|
||||||
plugin = manager.NeutronManager.get_service_plugins()[
|
|
||||||
constants.VPN]
|
|
||||||
for collection_name in RESOURCE_ATTRIBUTE_MAP:
|
|
||||||
resource_name = plural_mapping.get(
|
|
||||||
collection_name, collection_name[:-1])
|
|
||||||
params = RESOURCE_ATTRIBUTE_MAP[collection_name]
|
|
||||||
collection_name = collection_name.replace('_', '-')
|
|
||||||
|
|
||||||
quota.QUOTAS.register_resource_by_name(resource_name)
|
|
||||||
controller = base.create_resource(
|
|
||||||
collection_name, resource_name, plugin, params,
|
|
||||||
allow_pagination=cfg.CONF.allow_pagination,
|
|
||||||
allow_sorting=cfg.CONF.allow_sorting)
|
|
||||||
|
|
||||||
resource = extensions.ResourceExtension(
|
|
||||||
collection_name,
|
|
||||||
controller,
|
|
||||||
path_prefix=constants.COMMON_PREFIXES[constants.VPN],
|
|
||||||
attr_map=params)
|
|
||||||
resources.append(resource)
|
|
||||||
return resources
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_plugin_interface(cls):
|
def get_plugin_interface(cls):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user