Skip to content

Commit 897418e

Browse files
author
Dean Troyer
committed
Move plugin stuff to clientmanager
The OSC plugins work by adding an object as an attribute to a ClientManager instance. The initialization and management of thos plugins belongs in clientmanager.py. At this point the only part not moved is the API version dict initialization bcause the timing and connection to the CommandManager initialization. It gets refactored anyway when API discovery becomes operational. Change-Id: If9cb9a0c45a3a577082a5cdbb793769211f20ebb
1 parent 36212c4 commit 897418e

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

‎openstackclient/common/clientmanager.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
LOG = logging.getLogger(__name__)
3131

32+
PLUGIN_MODULES = []
33+
3234

3335
class ClientCache(object):
3436
"""Descriptor class for caching created client handles."""
@@ -123,11 +125,13 @@ def get_endpoint_for_service_type(self, service_type):
123125
return endpoint
124126

125127

126-
def get_extension_modules(group):
127-
"""Add extension clients"""
128+
# Plugin Support
129+
130+
def get_plugin_modules(group):
131+
"""Find plugin entry points"""
128132
mod_list = []
129133
for ep in pkg_resources.iter_entry_points(group):
130-
LOG.debug('found extension %r', ep.name)
134+
LOG.debug('Found plugin %r', ep.name)
131135

132136
__import__(ep.module_name)
133137
module = sys.modules[ep.module_name]
@@ -136,6 +140,7 @@ def get_extension_modules(group):
136140
if init_func:
137141
init_func('x')
138142

143+
# Add the plugin to the ClientManager
139144
setattr(
140145
ClientManager,
141146
module.API_NAME,
@@ -144,3 +149,22 @@ def get_extension_modules(group):
144149
),
145150
)
146151
return mod_list
152+
153+
154+
def build_plugin_option_parser(parser):
155+
"""Add plugin options to the parser"""
156+
157+
# Loop through extensions to get parser additions
158+
for mod in PLUGIN_MODULES:
159+
parser = mod.build_option_parser(parser)
160+
return parser
161+
162+
163+
# Get list of base plugin modules
164+
PLUGIN_MODULES = get_plugin_modules(
165+
'openstack.cli.base',
166+
)
167+
# Append list of external plugin modules
168+
PLUGIN_MODULES.extend(get_plugin_modules(
169+
'openstack.cli.extension',
170+
))

‎openstackclient/shell.py‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,6 @@ def __init__(self):
6868
# Assume TLS host certificate verification is enabled
6969
self.verify = True
7070

71-
# Get list of base modules
72-
self.ext_modules = clientmanager.get_extension_modules(
73-
'openstack.cli.base',
74-
)
75-
# Append list of extension modules
76-
self.ext_modules.extend(clientmanager.get_extension_modules(
77-
'openstack.cli.extension',
78-
))
79-
80-
# Loop through extensions to get parser additions
81-
for mod in self.ext_modules:
82-
self.parser = mod.build_option_parser(self.parser)
83-
8471
# NOTE(dtroyer): This hack changes the help action that Cliff
8572
# automatically adds to the parser so we can defer
8673
# its execution until after the api-versioned commands
@@ -170,6 +157,7 @@ def build_option_parser(self, description, version):
170157
parser = super(OpenStackShell, self).build_option_parser(
171158
description,
172159
version)
160+
173161
# service token auth argument
174162
parser.add_argument(
175163
'--os-url',
@@ -214,7 +202,7 @@ def build_option_parser(self, description, version):
214202
help="Print API call timing info",
215203
)
216204

217-
return parser
205+
return clientmanager.build_plugin_option_parser(parser)
218206

219207
def authenticate_user(self):
220208
"""Verify the required authentication credentials are present"""
@@ -332,7 +320,7 @@ def initialize_app(self, argv):
332320
self.default_domain = self.options.os_default_domain
333321

334322
# Loop through extensions to get API versions
335-
for mod in self.ext_modules:
323+
for mod in clientmanager.PLUGIN_MODULES:
336324
version_opt = getattr(self.options, mod.API_VERSION_OPTION, None)
337325
if version_opt:
338326
api = mod.API_NAME

0 commit comments

Comments
 (0)