Skip to content

Commit a065dd0

Browse files
committed
Allow network find to use alternate name
Add the name_attr to the network find method so it can search for things like floating_ip_address for floating IP addresses rather than just id. Change-Id: I827e3745b06397a54555d1286e477bf2e05bf789
1 parent b157dc9 commit a065dd0

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎openstackclient/network/common.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@
1414
from openstackclient.common import exceptions
1515

1616

17-
def find(client, resource, resources, name_or_id):
17+
def find(client, resource, resources, name_or_id, name_attr='name'):
1818
"""Find a network resource
1919
2020
:param client: network client
2121
:param resource: name of the resource
2222
:param resources: plural name of resource
2323
:param name_or_id: name or id of resource user is looking for
24+
:param name_attr: key to the name attribute for the resource
2425
2526
For example:
2627
n = find(netclient, 'network', 'networks', 'matrix')
2728
"""
2829
list_method = getattr(client, "list_%s" % resources)
2930
# Search for by name
30-
data = list_method(name=name_or_id, fields='id')
31+
kwargs = {name_attr: name_or_id, 'fields': 'id'}
32+
data = list_method(**kwargs)
3133
info = data[resources]
3234
if len(info) == 1:
3335
return info[0]['id']
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
14+
import mock
15+
16+
from openstackclient.common import exceptions
17+
from openstackclient.network import common
18+
from openstackclient.tests import utils
19+
20+
RESOURCE = 'resource'
21+
RESOURCES = 'resources'
22+
NAME = 'matrix'
23+
ID = 'Fishburne'
24+
25+
26+
class TestFind(utils.TestCase):
27+
def setUp(self):
28+
super(TestFind, self).setUp()
29+
self.mock_client = mock.Mock()
30+
self.list_resources = mock.Mock()
31+
self.mock_client.list_resources = self.list_resources
32+
self.matrix = {'id': ID}
33+
34+
def test_name(self):
35+
self.list_resources.return_value = {RESOURCES: [self.matrix]}
36+
37+
result = common.find(self.mock_client, RESOURCE, RESOURCES, NAME)
38+
39+
self.assertEqual(ID, result)
40+
self.list_resources.assert_called_with(fields='id', name=NAME)
41+
42+
def test_id(self):
43+
self.list_resources.side_effect = [{RESOURCES: []},
44+
{RESOURCES: [self.matrix]}]
45+
46+
result = common.find(self.mock_client, RESOURCE, RESOURCES, NAME)
47+
48+
self.assertEqual(ID, result)
49+
self.list_resources.assert_called_with(fields='id', id=NAME)
50+
51+
def test_nameo(self):
52+
self.list_resources.return_value = {RESOURCES: [self.matrix]}
53+
54+
result = common.find(self.mock_client, RESOURCE, RESOURCES, NAME,
55+
name_attr='nameo')
56+
57+
self.assertEqual(ID, result)
58+
self.list_resources.assert_called_with(fields='id', nameo=NAME)
59+
60+
def test_dups(self):
61+
dup = {'id': 'Larry'}
62+
self.list_resources.return_value = {RESOURCES: [self.matrix, dup]}
63+
64+
self.assertRaises(exceptions.CommandError, common.find,
65+
self.mock_client, RESOURCE, RESOURCES, NAME)
66+
67+
def test_nada(self):
68+
self.list_resources.side_effect = [{RESOURCES: []},
69+
{RESOURCES: []}]
70+
71+
self.assertRaises(exceptions.CommandError, common.find,
72+
self.mock_client, RESOURCE, RESOURCES, NAME)

0 commit comments

Comments
 (0)