Skip to content

Commit 70cb628

Browse files
committed
SDK Refactor: Prepare address scope commands
Prepare the OSC "address scope" commands for the SDK refactor. See [1] for details. [1] https://etherpad.openstack.org/p/osc-network-command-sdk-support Change-Id: I4e253e01f9b0b10452354f4e4152468090c76958 Partially-Implements: blueprint network-command-sdk-support
1 parent 62bf9e2 commit 70cb628

4 files changed

Lines changed: 116 additions & 12 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
import six
14+
15+
16+
# Get the OSC show command display and attribute columns for an SDK resource.
17+
def get_osc_show_columns_for_sdk_resource(sdk_resource, osc_column_map):
18+
if getattr(sdk_resource, 'allow_get', None) is not None:
19+
resource_dict = sdk_resource.to_dict(
20+
body=True, headers=False, ignore_none=False)
21+
else:
22+
resource_dict = sdk_resource
23+
24+
# Build the OSC column names to display for the SDK resource.
25+
attr_map = {}
26+
display_columns = list(resource_dict.keys())
27+
for sdk_attr, osc_attr in six.iteritems(osc_column_map):
28+
if sdk_attr in display_columns:
29+
attr_map[osc_attr] = sdk_attr
30+
display_columns.remove(sdk_attr)
31+
if osc_attr not in display_columns:
32+
display_columns.append(osc_attr)
33+
sorted_display_columns = sorted(display_columns)
34+
35+
# Build the SDK attribute names for the OSC column names.
36+
attr_columns = []
37+
for column in sorted_display_columns:
38+
new_column = attr_map[column] if column in attr_map else column
39+
attr_columns.append(new_column)
40+
return tuple(sorted_display_columns), tuple(attr_columns)

openstackclient/network/v2/address_scope.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121

2222
from openstackclient.i18n import _
2323
from openstackclient.identity import common as identity_common
24+
from openstackclient.network import sdk_utils
2425

2526

2627
LOG = logging.getLogger(__name__)
2728

2829

2930
def _get_columns(item):
30-
columns = list(item.keys())
31-
if 'tenant_id' in columns:
32-
columns.remove('tenant_id')
33-
columns.append('project_id')
34-
35-
return tuple(sorted(columns))
31+
column_map = {
32+
'is_shared': 'shared',
33+
'tenant_id': 'project_id',
34+
}
35+
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
3636

3737

3838
def _get_attrs(client_manager, parsed_args):
@@ -55,6 +55,8 @@ def _get_attrs(client_manager, parsed_args):
5555
return attrs
5656

5757

58+
# TODO(rtheis): Use the SDK resource mapped attribute names once the
59+
# OSC minimum requirements include SDK 1.0.
5860
class CreateAddressScope(command.ShowOne):
5961
"""Create a new Address Scope"""
6062

@@ -97,10 +99,10 @@ def take_action(self, parsed_args):
9799
client = self.app.client_manager.network
98100
attrs = _get_attrs(self.app.client_manager, parsed_args)
99101
obj = client.create_address_scope(**attrs)
100-
columns = _get_columns(obj)
102+
display_columns, columns = _get_columns(obj)
101103
data = utils.get_item_properties(obj, columns, formatters={})
102104

103-
return (columns, data)
105+
return (display_columns, data)
104106

105107

106108
class DeleteAddressScope(command.Command):
@@ -147,8 +149,8 @@ def take_action(self, parsed_args):
147149
'id',
148150
'name',
149151
'ip_version',
150-
'shared',
151-
'tenant_id',
152+
'is_shared',
153+
'project_id',
152154
)
153155
column_headers = (
154156
'ID',
@@ -165,6 +167,8 @@ def take_action(self, parsed_args):
165167
) for s in data))
166168

167169

170+
# TODO(rtheis): Use the SDK resource mapped attribute names once the
171+
# OSC minimum requirements include SDK 1.0.
168172
class SetAddressScope(command.Command):
169173
"""Set address scope properties"""
170174

@@ -227,7 +231,7 @@ def take_action(self, parsed_args):
227231
obj = client.find_address_scope(
228232
parsed_args.address_scope,
229233
ignore_missing=False)
230-
columns = _get_columns(obj)
234+
display_columns, columns = _get_columns(obj)
231235
data = utils.get_item_properties(obj, columns, formatters={})
232236

233-
return (columns, data)
237+
return (display_columns, data)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 openstackclient.network import sdk_utils
14+
from openstackclient.tests.unit import utils as tests_utils
15+
16+
17+
class TestSDKUtils(tests_utils.TestCase):
18+
19+
def setUp(self):
20+
super(TestSDKUtils, self).setUp()
21+
22+
def _test_get_osc_show_columns_for_sdk_resource(
23+
self, sdk_resource, column_map,
24+
expected_display_columns, expected_attr_columns):
25+
display_columns, attr_columns = \
26+
sdk_utils.get_osc_show_columns_for_sdk_resource(
27+
sdk_resource, column_map)
28+
self.assertEqual(expected_display_columns, display_columns)
29+
self.assertEqual(expected_attr_columns, attr_columns)
30+
31+
def test_get_osc_show_columns_for_sdk_resource_empty(self):
32+
self._test_get_osc_show_columns_for_sdk_resource(
33+
{}, {}, tuple(), tuple())
34+
35+
def test_get_osc_show_columns_for_sdk_resource_empty_map(self):
36+
self._test_get_osc_show_columns_for_sdk_resource(
37+
{'foo': 'foo1'}, {},
38+
('foo',), ('foo',))
39+
40+
def test_get_osc_show_columns_for_sdk_resource_empty_data(self):
41+
self._test_get_osc_show_columns_for_sdk_resource(
42+
{}, {'foo': 'foo_map'},
43+
('foo_map',), ('foo_map',))
44+
45+
def test_get_osc_show_columns_for_sdk_resource_map(self):
46+
self._test_get_osc_show_columns_for_sdk_resource(
47+
{'foo': 'foo1'}, {'foo': 'foo_map'},
48+
('foo_map',), ('foo',))
49+
50+
def test_get_osc_show_columns_for_sdk_resource_map_dup(self):
51+
self._test_get_osc_show_columns_for_sdk_resource(
52+
{'foo': 'foo1', 'foo_map': 'foo1'}, {'foo': 'foo_map'},
53+
('foo_map',), ('foo',))
54+
55+
def test_get_osc_show_columns_for_sdk_resource_map_full(self):
56+
self._test_get_osc_show_columns_for_sdk_resource(
57+
{'foo': 'foo1', 'bar': 'bar1'},
58+
{'foo': 'foo_map', 'new': 'bar'},
59+
('bar', 'foo_map'), ('bar', 'foo'))

openstackclient/tests/unit/network/v2/fakes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def create_one_address_scope(attrs=None):
9898
loaded=True)
9999

100100
# Set attributes with special mapping in OpenStack SDK.
101+
address_scope.is_shared = address_scope_attrs['shared']
101102
address_scope.project_id = address_scope_attrs['tenant_id']
102103

103104
return address_scope

0 commit comments

Comments
 (0)