Debian Codegraph @

watcherclient/osc/plugin.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13
14 import logging
15
16 from osc_lib import utils
17
18 import watcherclient
19 from watcherclient.common import api_versioning
20 from watcherclient.common import httpclient
21 from watcherclient import exceptions
22
23 LOG = logging.getLogger(__name__)
24
25 DEFAULT_API_VERSION = httpclient.LATEST_VERSION
26 API_VERSION_OPTION = 'os_infra_optim_api_version'
27 API_NAME = 'infra-optim'
28 API_VERSIONS = {
29 '1': 'watcherclient.v1.client.Client',
30 }
31
32
33 def make_client(instance):
34 """Returns an infra-optim service client."""
35
36 version = api_versioning.APIVersion(instance._api_version[API_NAME])
37
38 infraoptim_client_class = utils.get_client_class(
39 API_NAME,
40 version.ver_major,
41 API_VERSIONS)
42 LOG.debug('Instantiating infraoptim client: %s', infraoptim_client_class)
43
44 client = infraoptim_client_class(
45 os_infra_optim_api_version=instance._api_version[API_NAME],
46 session=instance.session,
47 region_name=instance._region_name,
48 )
49
50 return client
51
52
53 def build_option_parser(parser):
54 """Hook to add global options."""
55 parser.add_argument('--os-infra-optim-api-version',
56 metavar='<infra-optim-api-version>',
57 default=utils.env(
58 'OS_INFRA_OPTIM_API_VERSION',
59 default=DEFAULT_API_VERSION),
60 help=('Watcher API version, default=' +
61 DEFAULT_API_VERSION +
62 ' (Env: OS_INFRA_OPTIM_API_VERSION)'))
63 return parser
64
65
66 def check_api_version(check_version):
67 """Validate version supplied by user
68
69 Returns:
70 * True if version is OK
71 * False if the version has not been checked and the previous plugin
72 check should be performed
73 * throws an exception if the version is no good
74 """
75
76 infra_api_version = api_versioning.get_api_version(check_version)
77
78 # Bypass X.latest format microversion
79 if not infra_api_version.is_latest():
80 if infra_api_version > api_versioning.APIVersion("2.0"):
81 if not infra_api_version.matches(
82 watcherclient.API_MIN_VERSION,
83 watcherclient.API_MAX_VERSION,
84 ):
85 msg = "versions supported by client: %(min)s - %(max)s" % {
86 "min": watcherclient.API_MIN_VERSION.get_string(),
87 "max": watcherclient.API_MAX_VERSION.get_string(),
88 }
89 raise exceptions.CommandError(msg)
90 return True