| 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 |
from urllib import parse |
| 19 |
|
| 20 |
|
| 21 |
class MonascaManager(object): |
| 22 |
base_url = None |
| 23 |
|
| 24 |
def __init__(self, client): |
| 25 |
self.client = client |
| 26 |
|
| 27 |
def _parse_body(self, body): |
| 28 |
if type(body) is dict: |
| 29 |
self.next = None |
| 30 |
for link in body['links']: |
| 31 |
if link['rel'] == 'next': |
| 32 |
self.next = link['href'] |
| 33 |
return body['elements'] |
| 34 |
else: |
| 35 |
return body |
| 36 |
|
| 37 |
def _list(self, path, dim_key=None, **kwargs): |
| 38 |
"""Get a list of metrics.""" |
| 39 |
url_str = self.base_url + path |
| 40 |
if dim_key and dim_key in kwargs: |
| 41 |
dim_str = self.get_dimensions_url_string(kwargs[dim_key]) |
| 42 |
kwargs[dim_key] = dim_str |
| 43 |
|
| 44 |
if kwargs: |
| 45 |
url_str += '?%s' % parse.urlencode(kwargs, True) |
| 46 |
|
| 47 |
body = self.client.list( |
| 48 |
path=url_str |
| 49 |
) |
| 50 |
|
| 51 |
return self._parse_body(body) |
| 52 |
|
| 53 |
@staticmethod |
| 54 |
def get_dimensions_url_string(dimensions): |
| 55 |
dim_list = list() |
| 56 |
for k, v in dimensions.items(): |
| 57 |
# In case user specifies a dimension multiple times |
| 58 |
if isinstance(v, (list, tuple)): |
| 59 |
v = v[-1] |
| 60 |
if v: |
| 61 |
dim_str = k + ':' + v |
| 62 |
else: |
| 63 |
dim_str = k |
| 64 |
dim_list.append(dim_str) |
| 65 |
return ','.join(dim_list) |