Skip to content

Commit d2fb83e

Browse files
committed
Adapt the client for the v2 API
This adds a v2 client class allowing to add support for upcoming v2 API endpoints. The v2 client class implements all v1 endpoints. The cloudkitty API version can be specified with the "--os-rating-api-version" option or the "OS_RATING_API_VERSION" environment variable. Change-Id: If38730da3baed59c93543a08f8a4989f919611db
1 parent d77526b commit d2fb83e

19 files changed

Lines changed: 172 additions & 34 deletions

cloudkittyclient/common/__init__.py

Whitespace-only changes.

cloudkittyclient/common/client.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2018 Objectif Libre
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# 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, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
#
16+
from keystoneauth1 import adapter
17+
from keystoneauth1 import session as ks_session
18+
19+
20+
class BaseClient(object):
21+
22+
def __init__(self,
23+
session=None,
24+
adapter_options={},
25+
cacert=None,
26+
insecure=False,
27+
**kwargs):
28+
adapter_options.setdefault('service_type', 'rating')
29+
30+
if insecure:
31+
verify_cert = False
32+
else:
33+
if cacert:
34+
verify_cert = cacert
35+
else:
36+
verify_cert = True
37+
38+
self.session = session
39+
if self.session is None:
40+
self.session = ks_session.Session(
41+
verify=verify_cert, **kwargs)
42+
43+
self.api_client = adapter.Adapter(
44+
session=self.session, **adapter_options)

cloudkittyclient/osc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414

1515
from osc_lib import utils
1616

17-
DEFAULT_API_VERSION = '1'
17+
DEFAULT_API_VERSION = '2'
1818
API_VERSION_OPTION = 'os_rating_api_version'
1919
API_NAME = "rating"
2020
API_VERSIONS = {
2121
"1": "cloudkittyclient.v1.client.Client",
22+
"2": "cloudkittyclient.v2.client.Client",
2223
}
2324

2425

@@ -40,4 +41,9 @@ def make_client(instance):
4041

4142
def build_option_parser(parser):
4243
"""Hook to add global options."""
44+
parser.add_argument(
45+
'--rating-api-version', type=int, default=utils.env(
46+
'OS_RATING_API_VERSION',
47+
default=DEFAULT_API_VERSION)
48+
)
4349
return parser

cloudkittyclient/shell.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from oslo_log import log
2323

2424
from cloudkittyclient import client
25+
from cloudkittyclient.osc import DEFAULT_API_VERSION
2526
from cloudkittyclient import utils
2627

2728

@@ -105,9 +106,6 @@ def build_option_parser(self, description, version):
105106
description,
106107
version,
107108
argparse_kwargs={'allow_abbrev': False})
108-
parser.add_argument(
109-
'--ck-api-version', type=int, default=1, dest='ck_version',
110-
help='Cloudkitty API version (defaults to 1)')
111109
if 'OS_AUTH_TYPE' not in os.environ.keys() \
112110
and 'OS_PASSWORD' in os.environ.keys():
113111
os.environ['OS_AUTH_TYPE'] = 'password'
@@ -133,9 +131,10 @@ def client(self):
133131
self.options.os_rating_endpoint_override or
134132
self.options.os_endpoint_override),
135133
)
136-
self._client = client.Client(str(self.options.ck_version),
137-
session=session,
138-
adapter_options=adapter_options)
134+
self._client = client.Client(
135+
str(self.options.os_rating_api_version or DEFAULT_API_VERSION),
136+
session=session,
137+
adapter_options=adapter_options)
139138
return self._client
140139

141140

cloudkittyclient/v1/client.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,30 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515
#
16-
from keystoneauth1 import adapter
17-
from keystoneauth1 import session as ks_session
18-
16+
from cloudkittyclient.common import client
1917
from cloudkittyclient.v1 import collector
2018
from cloudkittyclient.v1 import info
2119
from cloudkittyclient.v1 import rating
2220
from cloudkittyclient.v1 import report
2321
from cloudkittyclient.v1 import storage
2422

2523

26-
class Client(object):
24+
class Client(client.BaseClient):
2725

2826
def __init__(self,
2927
session=None,
3028
adapter_options={},
3129
cacert=None,
3230
insecure=False,
3331
**kwargs):
34-
adapter_options.setdefault('service_type', 'rating')
35-
36-
if insecure:
37-
verify_cert = False
38-
else:
39-
if cacert:
40-
verify_cert = cacert
41-
else:
42-
verify_cert = True
43-
44-
self.session = session
45-
if self.session is None:
46-
self.session = ks_session.Session(
47-
verify=verify_cert, **kwargs)
32+
super(Client, self).__init__(
33+
session=session,
34+
adapter_options=adapter_options,
35+
cacert=cacert,
36+
insecure=insecure,
37+
**kwargs
38+
)
4839

49-
self.api_client = adapter.Adapter(
50-
session=self.session, **adapter_options)
5140
self.info = info.InfoManager(self.api_client)
5241
self.collector = collector.CollectorManager(self.api_client)
5342
self.rating = rating.RatingManager(self.api_client)

cloudkittyclient/v1/collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#
1616
from oslo_log import log
1717

18+
from cloudkittyclient.common import base
1819
from cloudkittyclient import exc
19-
from cloudkittyclient.v1 import base
2020

2121

2222
LOG = log.getLogger(__name__)

cloudkittyclient/v1/info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515
#
16-
from cloudkittyclient.v1 import base
16+
from cloudkittyclient.common import base
1717

1818

1919
class InfoManager(base.BaseManager):

cloudkittyclient/v1/rating/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#
1616
from cliff import lister
1717

18+
from cloudkittyclient.common import base
1819
from cloudkittyclient import exc
1920
from cloudkittyclient import utils
20-
from cloudkittyclient.v1 import base
2121
from cloudkittyclient.v1.rating import hashmap
2222
from cloudkittyclient.v1.rating import pyscripts
2323

cloudkittyclient/v1/rating/hashmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515
#
16+
from cloudkittyclient.common import base
1617
from cloudkittyclient import exc
17-
from cloudkittyclient.v1 import base
1818

1919

2020
class HashmapManager(base.BaseManager):

0 commit comments

Comments
 (0)