Debian Codegraph @

monascaclient/shell.py

raw
1 # (C) Copyright 2014-2015 Hewlett Packard Enterprise Development Company LP
2 # Copyright 2017 Fujitsu LIMITED
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """
18 Command-line interface to the monasca-client API.
19 """
20
21 import argparse
22 import sys
23
24 from osc_lib.api import auth
25 from osc_lib.cli import client_config as cloud_config
26 from osc_lib import shell
27 from osc_lib import utils
28 from oslo_utils import importutils
29
30 from monascaclient.osc import migration
31 from monascaclient import version as mc_version
32
33
34 class MonascaShell(shell.OpenStackShell):
35 def __init__(self):
36 super(MonascaShell, self).__init__(
37 description=__doc__.strip(),
38 version=mc_version.version_string
39 )
40 self.cloud_config = None
41
42 def initialize_app(self, argv):
43 super(MonascaShell, self).initialize_app(argv)
44 self.cloud_config = cloud_config.OSC_Config(
45 override_defaults={
46 'interface': None,
47 'auth_type': self._auth_type,
48 },
49 )
50
51 def build_option_parser(self, description, version):
52 parser = super(MonascaShell, self).build_option_parser(
53 description,
54 version
55 )
56 parser.set_defaults(cloud=None)
57 parser = auth.build_auth_plugins_option_parser(parser)
58 parser = self._append_monasca_args(parser)
59 return parser
60
61 @staticmethod
62 def _append_monasca_args(parser):
63 parser.add_argument('--monasca-api-url',
64 default=utils.env('MONASCA_API_URL'),
65 help='Defaults to env[MONASCA_API_URL].')
66 parser.add_argument('--monasca_api_url',
67 help=argparse.SUPPRESS)
68 parser.add_argument('--monasca-api-version',
69 default=utils.env(
70 'MONASCA_API_VERSION',
71 default='2_0'),
72 help='Defaults to env[MONASCA_API_VERSION] or 2_0')
73 parser.add_argument('--monasca_api_version',
74 help=argparse.SUPPRESS)
75 return parser
76
77 def _load_commands(self):
78 version = self.options.monasca_api_version
79
80 submodule = importutils.import_versioned_module('monascaclient',
81 version,
82 'shell')
83
84 self._find_actions(submodule)
85
86 def _find_actions(self, actions_module):
87 for attr in (a for a in dir(actions_module) if a.startswith('do_')):
88 name, clazz = migration.create_command_class(attr, actions_module)
89
90 if 'help' == name:
91 # help requires no auth
92 clazz.auth_required = False
93
94 self.command_manager.add_command(name, clazz)
95
96
97 def main(args=None):
98 try:
99 if args is None:
100 args = sys.argv[1:]
101 return MonascaShell().run(args)
102 except Exception as e:
103 if '--debug' in args or '-d' in args:
104 raise
105 else:
106 print(e)
107 sys.exit(1)
108
109
110 if __name__ == "__main__":
111 sys.exit(main(sys.argv[1:]))