Skip to content

Commit 25a77f7

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Structure FindFloatingIP() to work without ip_cache"
2 parents abb2498 + 0e42ea3 commit 25a77f7

2 files changed

Lines changed: 13 additions & 33 deletions

File tree

openstackclient/network/v2/floating_ip.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def _get_attrs(client_manager, parsed_args):
8383

8484
def _find_floating_ip(
8585
session,
86-
ip_cache,
8786
name_or_id,
8887
ignore_missing=True,
8988
**params
@@ -93,11 +92,11 @@ def _find_floating_ip(
9392
The SDK's find_ip() can only locate a floating IP by ID so we have
9493
to do this ourselves.
9594
"""
96-
9795
def _get_one_match(name_or_id):
9896
"""Given a list of results, return the match"""
9997
the_result = None
100-
for maybe_result in ip_cache:
98+
ip_list = list(_floating_ip.FloatingIP.list(session, **params))
99+
for maybe_result in ip_list:
101100
id_value = maybe_result.id
102101
ip_value = maybe_result.floating_ip_address
103102

@@ -116,19 +115,16 @@ def _get_one_match(name_or_id):
116115
# Try to short-circuit by looking directly for a matching ID.
117116
try:
118117
match = _floating_ip.FloatingIP.existing(id=name_or_id, **params)
119-
return (match.get(session), ip_cache)
118+
return match.get(session)
120119
except sdk_exceptions.NotFoundException:
121120
pass
122121

123-
if len(ip_cache) == 0:
124-
ip_cache = list(_floating_ip.FloatingIP.list(session, **params))
125-
126122
result = _get_one_match(name_or_id)
127123
if result is not None:
128-
return (result, ip_cache)
124+
return result
129125

130126
if ignore_missing:
131-
return (None, ip_cache)
127+
return None
132128
raise sdk_exceptions.ResourceNotFound(
133129
"No %s found for %s" % (_floating_ip.FloatingIP.__name__, name_or_id))
134130

@@ -240,9 +236,8 @@ def update_parser_common(self, parser):
240236
return parser
241237

242238
def take_action_network(self, client, parsed_args):
243-
(obj, self.ip_cache) = _find_floating_ip(
239+
obj = _find_floating_ip(
244240
self.app.client_manager.sdk_connection.session,
245-
self.ip_cache,
246241
self.r,
247242
ignore_missing=False,
248243
)
@@ -255,11 +250,6 @@ def take_action_compute(self, client, parsed_args):
255250
def take_action(self, parsed_args):
256251
"""Implements a naive cache for the list of floating IPs"""
257252

258-
# NOTE(dtroyer): This really only prevents multiple list()
259-
# calls when performing multiple resource deletes
260-
# in a single command. In an interactive session
261-
# each delete command will call list().
262-
self.ip_cache = []
263253
super(DeleteFloatingIP, self).take_action(parsed_args)
264254

265255

@@ -459,9 +449,6 @@ def take_action_compute(self, client, parsed_args):
459449
class ShowFloatingIP(common.NetworkAndComputeShowOne):
460450
_description = _("Display floating IP details")
461451

462-
# ip_cache is unused here but is a side effect of _find_floating_ip()
463-
ip_cache = []
464-
465452
def update_parser_common(self, parser):
466453
parser.add_argument(
467454
'floating_ip',
@@ -471,9 +458,8 @@ def update_parser_common(self, parser):
471458
return parser
472459

473460
def take_action_network(self, client, parsed_args):
474-
(obj, self.ip_cache) = _find_floating_ip(
461+
obj = _find_floating_ip(
475462
self.app.client_manager.sdk_connection.session,
476-
[],
477463
parsed_args.floating_ip,
478464
ignore_missing=False,
479465
)

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ def setUp(self):
218218
)
219219
def test_floating_ip_delete(self, find_floating_ip_mock):
220220
find_floating_ip_mock.side_effect = [
221-
(self.floating_ips[0], []),
222-
(self.floating_ips[1], []),
221+
self.floating_ips[0],
222+
self.floating_ips[1],
223223
]
224224
arglist = [
225225
self.floating_ips[0].id,
@@ -233,7 +233,6 @@ def test_floating_ip_delete(self, find_floating_ip_mock):
233233

234234
find_floating_ip_mock.assert_called_once_with(
235235
mock.ANY,
236-
[],
237236
self.floating_ips[0].id,
238237
ignore_missing=False,
239238
)
@@ -246,8 +245,8 @@ def test_floating_ip_delete(self, find_floating_ip_mock):
246245
)
247246
def test_floating_ip_delete_multi(self, find_floating_ip_mock):
248247
find_floating_ip_mock.side_effect = [
249-
(self.floating_ips[0], []),
250-
(self.floating_ips[1], []),
248+
self.floating_ips[0],
249+
self.floating_ips[1],
251250
]
252251
arglist = []
253252
verifylist = []
@@ -264,13 +263,11 @@ def test_floating_ip_delete_multi(self, find_floating_ip_mock):
264263
calls = [
265264
call(
266265
mock.ANY,
267-
[],
268266
self.floating_ips[0].id,
269267
ignore_missing=False,
270268
),
271269
call(
272270
mock.ANY,
273-
[],
274271
self.floating_ips[1].id,
275272
ignore_missing=False,
276273
),
@@ -289,7 +286,7 @@ def test_floating_ip_delete_multi(self, find_floating_ip_mock):
289286
)
290287
def test_floating_ip_delete_multi_exception(self, find_floating_ip_mock):
291288
find_floating_ip_mock.side_effect = [
292-
(self.floating_ips[0], []),
289+
self.floating_ips[0],
293290
exceptions.CommandError,
294291
]
295292
arglist = [
@@ -310,13 +307,11 @@ def test_floating_ip_delete_multi_exception(self, find_floating_ip_mock):
310307

311308
find_floating_ip_mock.assert_any_call(
312309
mock.ANY,
313-
[],
314310
self.floating_ips[0].id,
315311
ignore_missing=False,
316312
)
317313
find_floating_ip_mock.assert_any_call(
318314
mock.ANY,
319-
[],
320315
'unexist_floating_ip',
321316
ignore_missing=False,
322317
)
@@ -584,7 +579,7 @@ def setUp(self):
584579
"floating_ip._find_floating_ip"
585580
)
586581
def test_floating_ip_show(self, find_floating_ip_mock):
587-
find_floating_ip_mock.return_value = (self.floating_ip, [])
582+
find_floating_ip_mock.return_value = self.floating_ip
588583
arglist = [
589584
self.floating_ip.id,
590585
]
@@ -597,7 +592,6 @@ def test_floating_ip_show(self, find_floating_ip_mock):
597592

598593
find_floating_ip_mock.assert_called_once_with(
599594
mock.ANY,
600-
[],
601595
self.floating_ip.id,
602596
ignore_missing=False,
603597
)

0 commit comments

Comments
 (0)