| 1 |
# Copyright 2017 FUJITSU LIMITED |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import logging |
| 16 |
|
| 17 |
from osc_lib.command import command |
| 18 |
from osc_lib import utils |
| 19 |
|
| 20 |
from monascaclient import version |
| 21 |
|
| 22 |
LOG = logging.getLogger(__name__) |
| 23 |
|
| 24 |
# NOTE(trebskit) this will be moved to another module |
| 25 |
# once initial migration is up |
| 26 |
# the point is to show how many code can we spare |
| 27 |
# in order to get the client working with minimum effort needed |
| 28 |
VERSION_MAP = { |
| 29 |
'2_0': 'monascaclient.v2_0.client.Client' |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
def make_client(api_version, session=None, |
| 34 |
endpoint=None, service_type='monitoring'): |
| 35 |
"""Returns an monitoring API client.""" |
| 36 |
|
| 37 |
client_cls = utils.get_client_class('monitoring', api_version, VERSION_MAP) |
| 38 |
c = client_cls( |
| 39 |
session=session, |
| 40 |
service_type=service_type, |
| 41 |
endpoint=endpoint, |
| 42 |
app_name='monascaclient', |
| 43 |
app_version=version.version_string, |
| 44 |
) |
| 45 |
|
| 46 |
return c |
| 47 |
|
| 48 |
|
| 49 |
def create_command_class(name, func_module): |
| 50 |
"""Dynamically creates subclass of MigratingCommand. |
| 51 |
|
| 52 |
Method takes name of the function, module it is part of |
| 53 |
and builds the subclass of :py:class:`MigratingCommand`. |
| 54 |
Having a subclass of :py:class:`cliff.command.Command` is mandatory |
| 55 |
for the osc-lib integration. |
| 56 |
|
| 57 |
:param name: name of the function |
| 58 |
:type name: basestring |
| 59 |
:param func_module: the module function is part of |
| 60 |
:type func_module: module |
| 61 |
:return: command name, subclass of :py:class:`MigratingCommand` |
| 62 |
:rtype: tuple(basestring, class) |
| 63 |
|
| 64 |
""" |
| 65 |
|
| 66 |
cmd_name = name[3:].replace('_', '-') |
| 67 |
callback = getattr(func_module, name) |
| 68 |
desc = callback.__doc__ or '' |
| 69 |
help = desc.strip().split('\n')[0] |
| 70 |
|
| 71 |
arguments = getattr(callback, 'arguments', []) |
| 72 |
|
| 73 |
body = { |
| 74 |
'_args': arguments, |
| 75 |
'_callback': staticmethod(callback), |
| 76 |
'_description': desc, |
| 77 |
'_epilog': desc, |
| 78 |
'_help': help |
| 79 |
} |
| 80 |
|
| 81 |
claz = type('%sCommand' % cmd_name.title().replace('-', ''), |
| 82 |
(MigratingCommand,), body) |
| 83 |
|
| 84 |
return cmd_name, claz |
| 85 |
|
| 86 |
|
| 87 |
class MigratingCommandMeta(command.CommandMeta): |
| 88 |
"""Overwrite module name based on osc_lib.CommandMeta requirements.""" |
| 89 |
|
| 90 |
def __new__(mcs, name, bases, cls_dict): |
| 91 |
# NOTE(trebskit) little dirty, but should suffice for migration period |
| 92 |
cls_dict['__module__'] = 'monascaclient.v2_0.shell' |
| 93 |
return super(MigratingCommandMeta, mcs).__new__(mcs, name, |
| 94 |
bases, cls_dict) |
| 95 |
|
| 96 |
|
| 97 |
class MigratingCommand(command.Command, metaclass=MigratingCommandMeta): |
| 98 |
"""MigratingCommand is temporary command. |
| 99 |
|
| 100 |
MigratingCommand allows to map function defined |
| 101 |
shell commands from :py:module:`monascaclient.v2_0.shell` |
| 102 |
into :py:class:`command.Command` instances. |
| 103 |
|
| 104 |
Note: |
| 105 |
This class is temporary solution during migrating |
| 106 |
to osc_lib and will be removed when all |
| 107 |
shell commands are migrated to cliff commands. |
| 108 |
|
| 109 |
""" |
| 110 |
|
| 111 |
_help = None |
| 112 |
_args = None |
| 113 |
_callback = None |
| 114 |
|
| 115 |
def __init__(self, app, app_args, cmd_name=None): |
| 116 |
super(MigratingCommand, self).__init__(app, app_args, cmd_name) |
| 117 |
self._client = None |
| 118 |
self._endpoint = None |
| 119 |
|
| 120 |
def take_action(self, parsed_args): |
| 121 |
return self._callback(self.mon_client, parsed_args) |
| 122 |
|
| 123 |
def get_parser(self, prog_name): |
| 124 |
parser = super(MigratingCommand, self).get_parser(prog_name) |
| 125 |
for (args, kwargs) in self._args: |
| 126 |
parser.add_argument(*args, **kwargs) |
| 127 |
parser.add_argument('-j', '--json', |
| 128 |
action='store_true', |
| 129 |
help='output raw json response') |
| 130 |
return parser |
| 131 |
|
| 132 |
@property |
| 133 |
def mon_client(self): |
| 134 |
if not self._client: |
| 135 |
self.log.debug('Initializing mon-client') |
| 136 |
self._client = make_client(api_version=self.mon_version, |
| 137 |
endpoint=self.mon_url, |
| 138 |
session=self.app.client_manager.session) |
| 139 |
return self._client |
| 140 |
|
| 141 |
@property |
| 142 |
def mon_version(self): |
| 143 |
return self.app_args.monasca_api_version |
| 144 |
|
| 145 |
@property |
| 146 |
def mon_url(self): |
| 147 |
if self._endpoint: |
| 148 |
return self._endpoint |
| 149 |
|
| 150 |
app_args = self.app_args |
| 151 |
cm = self.app.client_manager |
| 152 |
|
| 153 |
endpoint = app_args.monasca_api_url |
| 154 |
|
| 155 |
if not endpoint: |
| 156 |
req_data = { |
| 157 |
'service_type': 'monitoring', |
| 158 |
'region_name': cm.region_name, |
| 159 |
'interface': cm.interface, |
| 160 |
} |
| 161 |
LOG.debug('Discovering monasca endpoint using %s' % req_data) |
| 162 |
endpoint = cm.get_endpoint_for_service_type(**req_data) |
| 163 |
else: |
| 164 |
LOG.debug('Using supplied endpoint=%s' % endpoint) |
| 165 |
|
| 166 |
self._endpoint = endpoint |
| 167 |
return self._endpoint |