
osc-lib library is foundation on which a CLI client for openstack can be built. It is meant to facilitate several aspects, that were previously hard-coded in client: * keystone communication handling * supporting multiple authentication methods (not only password) * common authentication parameters (i.e. environmental OS_*) * communicating over http with service endpoint * interactive CLI mode Thanks to those items, it was possible not only to drop nearly 3k lines of code and replace them with osc-lib but also increase reliabity of the client in terms of new openstack releases. Also it allowed to greatly simpify existing set of unit-tests. They are now testing only actual logic instead of mocking entire process of calling shell (i.e. MonascaShell.run(args)) or mocking HTTP communication. Both items are handled by osc-lib thus not they are not subject of monascaclient unit tests layers. Note: This change is partial integration with osc-lib and its main purpose is to move the responsibility of: * keystone communication * rest-ful communication with service endpoint to underlying library thus allowing client to implement only necessary functionality and not supporting boilerplate code, mentioned above. Story: 2000995 Task: 4172 Change-Id: I1712a24739438e2d8331a495f18f357749a633c5
118 lines
3.9 KiB
Python
118 lines
3.9 KiB
Python
# (C) Copyright 2014-2015 Hewlett Packard Enterprise Development Company LP
|
|
# Copyright 2017 Fujitsu LIMITED
|
|
#
|
|
# 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.
|
|
|
|
"""
|
|
Command-line interface to the monasca-client API.
|
|
"""
|
|
|
|
import argparse
|
|
import locale
|
|
import sys
|
|
|
|
from osc_lib.api import auth
|
|
from osc_lib.cli import client_config as cloud_config
|
|
from osc_lib import shell
|
|
from osc_lib import utils as utils
|
|
from oslo_utils import importutils
|
|
import six
|
|
|
|
from monascaclient.osc import migration
|
|
from monascaclient import version as mc_version
|
|
|
|
|
|
class MonascaShell(shell.OpenStackShell):
|
|
def __init__(self):
|
|
super(MonascaShell, self).__init__(
|
|
description=__doc__.strip(),
|
|
version=mc_version.version_string
|
|
)
|
|
self.cloud_config = None
|
|
|
|
def initialize_app(self, argv):
|
|
super(MonascaShell, self).initialize_app(argv)
|
|
self.cloud_config = cloud_config.OSC_Config(
|
|
override_defaults={
|
|
'interface': None,
|
|
'auth_type': self._auth_type,
|
|
},
|
|
)
|
|
|
|
def build_option_parser(self, description, version):
|
|
parser = super(MonascaShell, self).build_option_parser(
|
|
description,
|
|
version
|
|
)
|
|
parser = auth.build_auth_plugins_option_parser(parser)
|
|
parser = self._append_monasca_args(parser)
|
|
return parser
|
|
|
|
@staticmethod
|
|
def _append_monasca_args(parser):
|
|
parser.add_argument('--monasca-api-url',
|
|
default=utils.env('MONASCA_API_URL'),
|
|
help='Defaults to env[MONASCA_API_URL].')
|
|
parser.add_argument('--monasca_api_url',
|
|
help=argparse.SUPPRESS)
|
|
parser.add_argument('--monasca-api-version',
|
|
default=utils.env(
|
|
'MONASCA_API_VERSION',
|
|
default='2_0'),
|
|
help='Defaults to env[MONASCA_API_VERSION] or 2_0')
|
|
parser.add_argument('--monasca_api_version',
|
|
help=argparse.SUPPRESS)
|
|
return parser
|
|
|
|
def _load_commands(self):
|
|
version = self.options.monasca_api_version
|
|
|
|
submodule = importutils.import_versioned_module('monascaclient',
|
|
version,
|
|
'shell')
|
|
|
|
self._find_actions(submodule)
|
|
|
|
def _find_actions(self, actions_module):
|
|
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
|
|
name, clazz = migration.create_command_class(attr, actions_module)
|
|
|
|
if 'help' == name:
|
|
# help requires no auth
|
|
clazz.auth_required = False
|
|
|
|
self.command_manager.add_command(name, clazz)
|
|
|
|
|
|
def main(args=None):
|
|
try:
|
|
if args is None:
|
|
args = sys.argv[1:]
|
|
if six.PY2:
|
|
# Emulate Py3, decode argv into Unicode based on locale so that
|
|
# commands always see arguments as text instead of binary data
|
|
encoding = locale.getpreferredencoding()
|
|
if encoding:
|
|
args = map(lambda arg: arg.decode(encoding), args)
|
|
MonascaShell().run(args)
|
|
except Exception as e:
|
|
if '--debug' in args or '-d' in args:
|
|
raise
|
|
else:
|
|
print(e)
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main(sys.argv[1:]))
|