Skip to content

Commit 0b2987f

Browse files
committed
Fix find_resource for keystone and cinder
The find_resource method had two hacks in in to support cinder and keystone and I have removed those in favor of a monkey patch for cinder. The find_resource method used to attempt to UUID parse the id, but it would do a manager.get anyway. I changed it to skip the UUID parsing. This will make things run minorly faster and it supports LDAP for keystone. The find_resource used to attempt to use display_name=name_or_id when finding. This was a hack for cinder support, but it breaks keystone because keystone totally messes up with the bogus filter and keystone refuses to fix it. Change-Id: I66e45a6341f704900f1d5321a0e70eac3d051665 Closes-Bug: #1306699
1 parent 6380b8b commit 0b2987f

4 files changed

Lines changed: 78 additions & 15 deletions

File tree

‎openstackclient/common/utils.py‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import six
2222
import sys
2323
import time
24-
import uuid
2524

2625
from openstackclient.common import exceptions
2726
from openstackclient.openstack.common import strutils
@@ -37,13 +36,6 @@ def find_resource(manager, name_or_id):
3736
except exceptions.NotFound:
3837
pass
3938

40-
# Try to get entity as uuid
41-
try:
42-
uuid.UUID(str(name_or_id))
43-
return manager.get(name_or_id)
44-
except (ValueError, exceptions.NotFound):
45-
pass
46-
4739
# Try directly using the passed value
4840
try:
4941
return manager.get(name_or_id)
@@ -65,11 +57,6 @@ def find_resource(manager, name_or_id):
6557
# Eventually this should be pulled from a common set
6658
# of client exceptions.
6759
except Exception as ex:
68-
try:
69-
return manager.find(display_name=name_or_id)
70-
except Exception:
71-
pass
72-
7360
if type(ex).__name__ == 'NotFound':
7461
msg = "No %s with a name or ID of '%s' exists." % \
7562
(manager.resource_class.__name__.lower(), name_or_id)

‎openstackclient/tests/common/test_utils.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_find_resource_find_not_found(self):
117117
self.assertEqual("No lego with a name or ID of 'legos' exists.",
118118
str(result))
119119
self.manager.get.assert_called_with(self.name)
120-
self.manager.find.assert_called_with(display_name=self.name)
120+
self.manager.find.assert_called_with(name=self.name)
121121

122122
def test_find_resource_find_no_unique(self):
123123
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
@@ -129,4 +129,4 @@ def test_find_resource_find_no_unique(self):
129129
self.assertEqual("More than one lego exists with the name 'legos'.",
130130
str(result))
131131
self.manager.get.assert_called_with(self.name)
132-
self.manager.find.assert_called_with(display_name=self.name)
132+
self.manager.find.assert_called_with(name=self.name)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2013 Nebula Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
import mock
17+
18+
from cinderclient.v1 import volume_snapshots
19+
from cinderclient.v1 import volumes
20+
21+
from openstackclient.common import exceptions
22+
from openstackclient.common import utils
23+
from openstackclient.tests import utils as test_utils
24+
25+
26+
ID = '1after909'
27+
NAME = 'PhilSpector'
28+
29+
30+
class TestFindResourceVolumes(test_utils.TestCase):
31+
32+
def setUp(self):
33+
super(TestFindResourceVolumes, self).setUp()
34+
api = mock.Mock()
35+
api.client = mock.Mock()
36+
api.client.get = mock.Mock()
37+
resp = mock.Mock()
38+
body = {"volumes": [{"id": ID, 'display_name': NAME}]}
39+
api.client.get.side_effect = [Exception("Not found"), (resp, body)]
40+
self.manager = volumes.VolumeManager(api)
41+
42+
def test_find(self):
43+
result = utils.find_resource(self.manager, NAME)
44+
self.assertEqual(ID, result.id)
45+
self.assertEqual(NAME, result.display_name)
46+
47+
def test_not_find(self):
48+
self.assertRaises(exceptions.CommandError, utils.find_resource,
49+
self.manager, 'GeorgeMartin')
50+
51+
52+
class TestFindResourceVolumeSnapshots(test_utils.TestCase):
53+
54+
def setUp(self):
55+
super(TestFindResourceVolumeSnapshots, self).setUp()
56+
api = mock.Mock()
57+
api.client = mock.Mock()
58+
api.client.get = mock.Mock()
59+
resp = mock.Mock()
60+
body = {"snapshots": [{"id": ID, 'display_name': NAME}]}
61+
api.client.get.side_effect = [Exception("Not found"), (resp, body)]
62+
self.manager = volume_snapshots.SnapshotManager(api)
63+
64+
def test_find(self):
65+
result = utils.find_resource(self.manager, NAME)
66+
self.assertEqual(ID, result.id)
67+
self.assertEqual(NAME, result.display_name)
68+
69+
def test_not_find(self):
70+
self.assertRaises(exceptions.CommandError, utils.find_resource,
71+
self.manager, 'GeorgeMartin')

‎openstackclient/volume/client.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515

1616
import logging
1717

18+
from cinderclient.v1 import volume_snapshots
19+
from cinderclient.v1 import volumes
1820
from openstackclient.common import utils
1921

22+
# Monkey patch for v1 cinderclient
23+
volumes.Volume.NAME_ATTR = 'display_name'
24+
volume_snapshots.Snapshot.NAME_ATTR = 'display_name'
2025

2126
LOG = logging.getLogger(__name__)
2227

0 commit comments

Comments
 (0)