File: shell.py

package info (click to toggle)
python-monascaclient 2.8.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 484 kB
  • sloc: python: 2,487; sh: 111; makefile: 21
file content (111 lines) | stat: -rw-r--r-- 3,677 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# (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 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
from oslo_utils import importutils

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.set_defaults(cloud=None)
        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:]
        return 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:]))