Merge "Move plugin stuff to clientmanager"

This commit is contained in:
Jenkins 2014-10-17 20:13:55 +00:00 committed by Gerrit Code Review
commit b40fa49809
2 changed files with 30 additions and 18 deletions
openstackclient

@ -29,6 +29,8 @@ from openstackclient.identity import client as identity_client
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
PLUGIN_MODULES = []
class ClientCache(object): class ClientCache(object):
"""Descriptor class for caching created client handles.""" """Descriptor class for caching created client handles."""
@ -123,11 +125,13 @@ class ClientManager(object):
return endpoint return endpoint
def get_extension_modules(group): # Plugin Support
"""Add extension clients"""
def get_plugin_modules(group):
"""Find plugin entry points"""
mod_list = [] mod_list = []
for ep in pkg_resources.iter_entry_points(group): for ep in pkg_resources.iter_entry_points(group):
LOG.debug('found extension %r', ep.name) LOG.debug('Found plugin %r', ep.name)
__import__(ep.module_name) __import__(ep.module_name)
module = sys.modules[ep.module_name] module = sys.modules[ep.module_name]
@ -136,6 +140,7 @@ def get_extension_modules(group):
if init_func: if init_func:
init_func('x') init_func('x')
# Add the plugin to the ClientManager
setattr( setattr(
ClientManager, ClientManager,
module.API_NAME, module.API_NAME,
@ -144,3 +149,22 @@ def get_extension_modules(group):
), ),
) )
return mod_list return mod_list
def build_plugin_option_parser(parser):
"""Add plugin options to the parser"""
# Loop through extensions to get parser additions
for mod in PLUGIN_MODULES:
parser = mod.build_option_parser(parser)
return parser
# Get list of base plugin modules
PLUGIN_MODULES = get_plugin_modules(
'openstack.cli.base',
)
# Append list of external plugin modules
PLUGIN_MODULES.extend(get_plugin_modules(
'openstack.cli.extension',
))

@ -68,19 +68,6 @@ class OpenStackShell(app.App):
# Assume TLS host certificate verification is enabled # Assume TLS host certificate verification is enabled
self.verify = True self.verify = True
# Get list of base modules
self.ext_modules = clientmanager.get_extension_modules(
'openstack.cli.base',
)
# Append list of extension modules
self.ext_modules.extend(clientmanager.get_extension_modules(
'openstack.cli.extension',
))
# Loop through extensions to get parser additions
for mod in self.ext_modules:
self.parser = mod.build_option_parser(self.parser)
# NOTE(dtroyer): This hack changes the help action that Cliff # NOTE(dtroyer): This hack changes the help action that Cliff
# automatically adds to the parser so we can defer # automatically adds to the parser so we can defer
# its execution until after the api-versioned commands # its execution until after the api-versioned commands
@ -170,6 +157,7 @@ class OpenStackShell(app.App):
parser = super(OpenStackShell, self).build_option_parser( parser = super(OpenStackShell, self).build_option_parser(
description, description,
version) version)
# service token auth argument # service token auth argument
parser.add_argument( parser.add_argument(
'--os-url', '--os-url',
@ -214,7 +202,7 @@ class OpenStackShell(app.App):
help="Print API call timing info", help="Print API call timing info",
) )
return parser return clientmanager.build_plugin_option_parser(parser)
def authenticate_user(self): def authenticate_user(self):
"""Verify the required authentication credentials are present""" """Verify the required authentication credentials are present"""
@ -332,7 +320,7 @@ class OpenStackShell(app.App):
self.default_domain = self.options.os_default_domain self.default_domain = self.options.os_default_domain
# Loop through extensions to get API versions # Loop through extensions to get API versions
for mod in self.ext_modules: for mod in clientmanager.PLUGIN_MODULES:
version_opt = getattr(self.options, mod.API_VERSION_OPTION, None) version_opt = getattr(self.options, mod.API_VERSION_OPTION, None)
if version_opt: if version_opt:
api = mod.API_NAME api = mod.API_NAME