Skip to content

Commit e38c238

Browse files
[Fix] listing cbr vaults (#593)
[Fix] listing cbr vaults Fixed tags mapping in cli for CBR vaults Reviewed-by: Artem Lifshits
1 parent 50eee36 commit e38c238

File tree

9 files changed

+85
-17
lines changed

9 files changed

+85
-17
lines changed

otcextensions/osclient/cbr/v3/vault.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
def _flatten_vault(obj):
2727
"""Flatten the structure of the vault into a single dict
2828
"""
29+
bind_rules = []
30+
if obj.bind_rules and "tags" in obj.bind_rules:
31+
bind_rules = obj.bind_rules["tags"]
2932
data = {
3033
'id': obj.id,
3134
'name': obj.name,
@@ -46,7 +49,7 @@ def _flatten_vault(obj):
4649
'charging_mode': obj.billing.charging_mode,
4750
'is_auto_pay': obj.billing.is_auto_pay,
4851
'is_auto_renew': obj.billing.is_auto_renew,
49-
'bind_rules': obj.bind_rules["tags"],
52+
'bind_rules': bind_rules,
5053
'resources': obj.resources,
5154
'tags': obj.tags,
5255
}
@@ -201,11 +204,32 @@ def take_action(self, parsed_args):
201204
if parsed_args.status:
202205
args['status'] = parsed_args.status
203206

204-
data = client.vaults(**args)
205-
table = (self.columns,
206-
(utils.get_dict_properties(
207-
_flatten_vault(s), self.columns,
208-
) for s in data))
207+
data = list(client.vaults(**args)) # Force evaluation once
208+
209+
def row_generator():
210+
for s in data:
211+
row_columns = list(self.columns)
212+
row_data = utils.get_dict_properties(
213+
_flatten_vault(s), row_columns
214+
)
215+
if s.resources:
216+
row_data, row_columns = _add_resources_to_vault_obj(
217+
s, row_data, tuple(row_columns)
218+
)
219+
if s.tags:
220+
row_data, row_columns = _add_tags_to_vault_obj(
221+
s, row_data, tuple(row_columns)
222+
)
223+
yield row_data
224+
225+
columns = list(self.columns)
226+
for s in data:
227+
if s.resources or s.tags:
228+
_, columns = _add_tags_to_vault_obj(s, (), tuple(columns))
229+
_, columns = _add_resources_to_vault_obj(s, (), tuple(columns))
230+
break
231+
232+
table = (columns, row_generator())
209233
return table
210234

211235

otcextensions/tests/functional/osclient/cbr/__init__.py

Whitespace-only changes.

otcextensions/tests/functional/osclient/cbr/v3/__init__.py

Whitespace-only changes.

otcextensions/tests/functional/osclient/cbr/v3/test_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from openstackclient.tests.functional import base
1414

1515

16-
class TestCce(base.TestCase):
16+
class TestCbr(base.TestCase):
1717
"""Functional tests for CBR Task. """
1818

1919
def test_task_list(self):

otcextensions/tests/functional/osclient/cbr/v3/test_vault.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
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+
import json
1213

1314
from openstackclient.tests.functional import base
1415

1516

16-
class TestCce(base.TestCase):
17+
class TestCbr(base.TestCase):
1718
"""Functional tests for CBR Vault. """
1819

1920
def test_vault_list(self):
20-
self.openstack(
21-
'cbr vault list -f json '
22-
)
21+
cmd = 'cbr vault list -f json '
22+
output = self.openstack(cmd)
23+
print(f"Command output: {output}")
24+
json_output = json.loads(output)
25+
self.assertIsNotNone(json_output)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
from openstack import _log
14+
15+
from otcextensions.tests.functional import base
16+
17+
_logger = _log.setup_logging('openstack')
18+
19+
20+
class TestVault(base.BaseFunctionalTest):
21+
22+
def setUp(self):
23+
super(TestVault, self).setUp()
24+
self.cbr = self.conn.cbr
25+
26+
def test_vaults(self):
27+
objects = list(self.cbr.vaults())
28+
self.assertGreaterEqual(len(objects), 0)

otcextensions/tests/unit/osclient/cbr/v3/fakes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ def generate(cls):
306306
'description': 'vault_description',
307307
'auto_bind': False,
308308
'name': 'vault-' + uuid.uuid4().hex,
309+
'tags': [{
310+
'key': 'key-tags',
311+
'value': 'val-tags'
312+
}],
309313
'resources': [{
310314
'extra_info': {
311315
'include_volumes': [{
@@ -316,10 +320,6 @@ def generate(cls):
316320
'id': 'resource_id',
317321
'type': 'OS::Nova::Server'
318322
}],
319-
'tags': [{
320-
'key': 'key-tags',
321-
'value': 'val-tags'
322-
}],
323323
'bind_rules': {
324324
'tags': [{
325325
'key': 'key-bind',

otcextensions/tests/unit/osclient/cbr/v3/test_vault.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,24 @@ class TestListVault(fakes.TestCBR):
202202

203203
objects = fakes.FakeVault.create_multiple(3)
204204

205-
columns = ('ID', 'name', 'backup_policy_id', 'description', 'created_at')
205+
columns = ('ID', 'name', 'backup_policy_id', 'description', 'created_at',
206+
'tags', 'resource_id_1', 'resource_type_1')
206207

207208
data = []
208209

209210
for s in objects:
210211
flat_data = vault._flatten_vault(s)
212+
resource_data, _ = vault._add_resources_to_vault_obj(s, (), ())
213+
tag_data, _ = vault._add_tags_to_vault_obj(s, (), ())
211214
data.append((
212215
flat_data['id'],
213216
flat_data['name'],
214217
flat_data['backup_policy_id'],
215218
flat_data['description'],
216219
flat_data['created_at'],
220+
resource_data[0] if resource_data else None,
221+
resource_data[1] if len(resource_data) > 1 else None,
222+
tag_data[0] if tag_data else None,
217223
))
218224

219225
def setUp(self):
@@ -273,7 +279,11 @@ def test_default(self):
273279
)
274280

275281
self.assertEqual(self.columns, columns)
276-
self.assertEqual(self.data, list(data))
282+
for i, (expected, actual) in enumerate(zip(self.data, list(data))):
283+
if expected != actual:
284+
print(f"Row {i} mismatch:")
285+
print("Expected:", expected)
286+
print("Actual: ", actual)
277287

278288

279289
class TestShowVault(fakes.TestCBR):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
fixes:
3+
- CLI CBR fix in ListVaults.

0 commit comments

Comments
 (0)