Skip to content

Commit a7344b7

Browse files
authored
fix(CES): added list method to catch empty lists (#656)
fix(CES): added list method to catch empty lists Reviewed-by: Anton Sidelnikov
1 parent d5913da commit a7344b7

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

otcextensions/sdk/ces/v1/metric.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
12+
from openstack import exceptions
1213
from openstack import resource
1314

1415

@@ -44,3 +45,34 @@ class Metric(resource.Resource):
4445
namespace = resource.Body("namespace")
4546
#: Indicates the metric unit
4647
unit = resource.Body("unit")
48+
49+
@classmethod
50+
def list(
51+
cls,
52+
session,
53+
paginated=True,
54+
base_path=None,
55+
allow_unknown_params=False,
56+
**params
57+
):
58+
if base_path is None:
59+
base_path = cls.base_path
60+
query_params = cls._query_mapping._transpose(params, cls)
61+
uri = base_path.format(**params)
62+
response = session.get(
63+
uri,
64+
headers={"Accept": "application/json"},
65+
params=query_params,
66+
)
67+
exceptions.raise_from_response(response)
68+
data = response.json()
69+
resources = data.get(cls.resources_key) or []
70+
for raw in resources:
71+
if not isinstance(raw, dict):
72+
continue
73+
raw.pop("self", None)
74+
yield cls.existing(
75+
microversion=None,
76+
connection=session._get_connection(),
77+
**raw,
78+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
from openstack import _log
13+
from otcextensions.tests.functional import base
14+
15+
_logger = _log.setup_logging("openstack")
16+
17+
18+
class TestMetric(base.BaseFunctionalTest):
19+
20+
def test_list(self):
21+
metrics = list(self.conn.ces.metrics())
22+
self.assertGreater(len(metrics), 0)
23+
24+
def test_list_with_non_existing_params(self):
25+
metrics = list(
26+
self.conn.ces.metrics(
27+
namespace="SYS.ECS", metric_name="totally_fake_metric_xyz"
28+
)
29+
)
30+
self.assertEqual(len(metrics), 0)
31+
32+
def test_list_with_existing_metric(self):
33+
metrics = list(
34+
self.conn.ces.metrics(
35+
namespace="SYS.ECS",
36+
)
37+
)
38+
self.assertIsNotNone(metrics)

0 commit comments

Comments
 (0)