Skip to content

Commit a742e47

Browse files
committed
Use find_ip from openstacksdk
The find_ip from openstacksdk started being usable by OSC back in 0.9.15 but the local method never got replaced. Change-Id: I18a334280e5f384f8bb96198cdad79c612a02290
1 parent 2ef279a commit a742e47

2 files changed

Lines changed: 29 additions & 143 deletions

File tree

openstackclient/network/v2/floating_ip.py

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
import logging
1717

18-
from openstack import exceptions as sdk_exceptions
19-
from openstack.network.v2 import floating_ip as _floating_ip
2018
from osc_lib.command import command
2119
from osc_lib import utils
2220

@@ -86,54 +84,6 @@ def _get_attrs(client_manager, parsed_args):
8684
return attrs
8785

8886

89-
def _find_floating_ip(
90-
session,
91-
name_or_id,
92-
ignore_missing=True,
93-
**params
94-
):
95-
"""Find a floating IP by IP or ID
96-
97-
The SDK's find_ip() can only locate a floating IP by ID so we have
98-
to do this ourselves.
99-
"""
100-
def _get_one_match(name_or_id):
101-
"""Given a list of results, return the match"""
102-
the_result = None
103-
ip_list = list(_floating_ip.FloatingIP.list(session, **params))
104-
for maybe_result in ip_list:
105-
id_value = maybe_result.id
106-
ip_value = maybe_result.floating_ip_address
107-
108-
if (id_value == name_or_id) or (ip_value == name_or_id):
109-
# Only allow one resource to be found. If we already
110-
# found a match, raise an exception to show it.
111-
if the_result is None:
112-
the_result = maybe_result
113-
else:
114-
msg = "More than one %s exists with the name '%s'."
115-
msg = (msg % (_floating_ip.FloatingIP, name_or_id))
116-
raise sdk_exceptions.DuplicateResource(msg)
117-
118-
return the_result
119-
120-
# Try to short-circuit by looking directly for a matching ID.
121-
try:
122-
match = _floating_ip.FloatingIP.existing(id=name_or_id, **params)
123-
return match.get(session)
124-
except sdk_exceptions.NotFoundException:
125-
pass
126-
127-
result = _get_one_match(name_or_id)
128-
if result is not None:
129-
return result
130-
131-
if ignore_missing:
132-
return None
133-
raise sdk_exceptions.ResourceNotFound(
134-
"No %s found for %s" % (_floating_ip.FloatingIP.__name__, name_or_id))
135-
136-
13787
class CreateFloatingIP(common.NetworkAndComputeShowOne):
13888
_description = _("Create floating IP")
13989

@@ -246,8 +196,7 @@ def update_parser_common(self, parser):
246196
return parser
247197

248198
def take_action_network(self, client, parsed_args):
249-
obj = _find_floating_ip(
250-
self.app.client_manager.sdk_connection.session,
199+
obj = client.find_ip(
251200
self.r,
252201
ignore_missing=False,
253202
)
@@ -487,9 +436,7 @@ def get_parser(self, prog_name):
487436
def take_action(self, parsed_args):
488437
client = self.app.client_manager.network
489438
attrs = {}
490-
# TODO(sindhu) Use client.find_ip() once SDK 0.9.15 is released
491-
obj = _find_floating_ip(
492-
self.app.client_manager.sdk_connection.session,
439+
obj = client.find_ip(
493440
parsed_args.floating_ip,
494441
ignore_missing=False,
495442
)
@@ -521,8 +468,7 @@ def update_parser_common(self, parser):
521468
return parser
522469

523470
def take_action_network(self, client, parsed_args):
524-
obj = _find_floating_ip(
525-
self.app.client_manager.sdk_connection.session,
471+
obj = client.find_ip(
526472
parsed_args.floating_ip,
527473
ignore_missing=False,
528474
)
@@ -586,9 +532,7 @@ def get_parser(self, prog_name):
586532

587533
def take_action(self, parsed_args):
588534
client = self.app.client_manager.network
589-
# TODO(sindhu) Use client.find_ip() once SDK 0.9.15 is released
590-
obj = _find_floating_ip(
591-
self.app.client_manager.sdk_connection.session,
535+
obj = client.find_ip(
592536
parsed_args.floating_ip,
593537
ignore_missing=False,
594538
)

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

Lines changed: 25 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,14 @@ class TestDeleteFloatingIPNetwork(TestFloatingIPNetwork):
230230
def setUp(self):
231231
super(TestDeleteFloatingIPNetwork, self).setUp()
232232

233+
self.network.find_ip = mock.Mock()
233234
self.network.delete_ip = mock.Mock(return_value=None)
234235

235236
# Get the command object to test
236237
self.cmd = fip.DeleteFloatingIP(self.app, self.namespace)
237238

238-
@mock.patch.object(fip, '_find_floating_ip')
239-
def test_floating_ip_delete(self, find_floating_ip_mock):
240-
find_floating_ip_mock.side_effect = [
239+
def test_floating_ip_delete(self):
240+
self.network.find_ip.side_effect = [
241241
self.floating_ips[0],
242242
self.floating_ips[1],
243243
]
@@ -251,17 +251,15 @@ def test_floating_ip_delete(self, find_floating_ip_mock):
251251

252252
result = self.cmd.take_action(parsed_args)
253253

254-
find_floating_ip_mock.assert_called_once_with(
255-
mock.ANY,
254+
self.network.find_ip.assert_called_once_with(
256255
self.floating_ips[0].id,
257256
ignore_missing=False,
258257
)
259258
self.network.delete_ip.assert_called_once_with(self.floating_ips[0])
260259
self.assertIsNone(result)
261260

262-
@mock.patch.object(fip, '_find_floating_ip')
263-
def test_floating_ip_delete_multi(self, find_floating_ip_mock):
264-
find_floating_ip_mock.side_effect = [
261+
def test_floating_ip_delete_multi(self):
262+
self.network.find_ip.side_effect = [
265263
self.floating_ips[0],
266264
self.floating_ips[1],
267265
]
@@ -279,27 +277,24 @@ def test_floating_ip_delete_multi(self, find_floating_ip_mock):
279277

280278
calls = [
281279
call(
282-
mock.ANY,
283280
self.floating_ips[0].id,
284281
ignore_missing=False,
285282
),
286283
call(
287-
mock.ANY,
288284
self.floating_ips[1].id,
289285
ignore_missing=False,
290286
),
291287
]
292-
find_floating_ip_mock.assert_has_calls(calls)
288+
self.network.find_ip.assert_has_calls(calls)
293289

294290
calls = []
295291
for f in self.floating_ips:
296292
calls.append(call(f))
297293
self.network.delete_ip.assert_has_calls(calls)
298294
self.assertIsNone(result)
299295

300-
@mock.patch.object(fip, '_find_floating_ip')
301-
def test_floating_ip_delete_multi_exception(self, find_floating_ip_mock):
302-
find_floating_ip_mock.side_effect = [
296+
def test_floating_ip_delete_multi_exception(self):
297+
self.network.find_ip.side_effect = [
303298
self.floating_ips[0],
304299
exceptions.CommandError,
305300
]
@@ -319,13 +314,11 @@ def test_floating_ip_delete_multi_exception(self, find_floating_ip_mock):
319314
except exceptions.CommandError as e:
320315
self.assertEqual('1 of 2 floating_ips failed to delete.', str(e))
321316

322-
find_floating_ip_mock.assert_any_call(
323-
mock.ANY,
317+
self.network.find_ip.assert_any_call(
324318
self.floating_ips[0].id,
325319
ignore_missing=False,
326320
)
327-
find_floating_ip_mock.assert_any_call(
328-
mock.ANY,
321+
self.network.find_ip.assert_any_call(
329322
'unexist_floating_ip',
330323
ignore_missing=False,
331324
)
@@ -590,9 +583,7 @@ def setUp(self):
590583
# Get the command object to test
591584
self.cmd = fip.ShowFloatingIP(self.app, self.namespace)
592585

593-
@mock.patch.object(fip, '_find_floating_ip')
594-
def test_floating_ip_show(self, find_floating_ip_mock):
595-
find_floating_ip_mock.return_value = self.floating_ip
586+
def test_floating_ip_show(self):
596587
arglist = [
597588
self.floating_ip.id,
598589
]
@@ -603,8 +594,7 @@ def test_floating_ip_show(self, find_floating_ip_mock):
603594

604595
columns, data = self.cmd.take_action(parsed_args)
605596

606-
find_floating_ip_mock.assert_called_once_with(
607-
mock.ANY,
597+
self.network.find_ip.assert_called_once_with(
608598
self.floating_ip.id,
609599
ignore_missing=False,
610600
)
@@ -636,14 +626,7 @@ def setUp(self):
636626
# Get the command object to test
637627
self.cmd = fip.SetFloatingIP(self.app, self.namespace)
638628

639-
@mock.patch(
640-
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
641-
"fip._find_floating_ip"
642-
)
643-
def test_port_option(self, find_floating_ip_mock):
644-
find_floating_ip_mock.side_effect = [
645-
self.floating_ip,
646-
]
629+
def test_port_option(self):
647630
arglist = [
648631
self.floating_ip.id,
649632
'--port', self.floating_ip.port_id,
@@ -660,23 +643,15 @@ def test_port_option(self, find_floating_ip_mock):
660643
'port_id': self.floating_ip.port_id,
661644
}
662645

663-
find_floating_ip_mock.assert_called_once_with(
664-
mock.ANY,
646+
self.network.find_ip.assert_called_once_with(
665647
self.floating_ip.id,
666648
ignore_missing=False,
667649
)
668650

669651
self.network.update_ip.assert_called_once_with(
670652
self.floating_ip, **attrs)
671653

672-
@mock.patch(
673-
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
674-
"fip._find_floating_ip"
675-
)
676-
def test_fixed_ip_option(self, find_floating_ip_mock):
677-
find_floating_ip_mock.side_effect = [
678-
self.floating_ip,
679-
]
654+
def test_fixed_ip_option(self):
680655
arglist = [
681656
self.floating_ip.id,
682657
'--port', self.floating_ip.port_id,
@@ -695,24 +670,16 @@ def test_fixed_ip_option(self, find_floating_ip_mock):
695670
'port_id': self.floating_ip.port_id,
696671
'fixed_ip_address': self.floating_ip.fixed_ip_address,
697672
}
698-
find_floating_ip_mock.assert_called_once_with(
699-
mock.ANY,
673+
self.network.find_ip.assert_called_once_with(
700674
self.floating_ip.id,
701675
ignore_missing=False,
702676
)
703677
self.network.update_ip.assert_called_once_with(
704678
self.floating_ip, **attrs)
705679

706-
@mock.patch(
707-
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
708-
"fip._find_floating_ip"
709-
)
710-
def test_port_and_qos_policy_option(self, find_floating_ip_mock):
680+
def test_port_and_qos_policy_option(self):
711681
qos_policy = network_fakes.FakeNetworkQosPolicy.create_one_qos_policy()
712682
self.network.find_qos_policy = mock.Mock(return_value=qos_policy)
713-
find_floating_ip_mock.side_effect = [
714-
self.floating_ip,
715-
]
716683
arglist = [
717684
"--qos-policy", qos_policy.id,
718685
'--port', self.floating_ip.port_id,
@@ -731,22 +698,14 @@ def test_port_and_qos_policy_option(self, find_floating_ip_mock):
731698
'qos_policy_id': qos_policy.id,
732699
'port_id': self.floating_ip.port_id,
733700
}
734-
find_floating_ip_mock.assert_called_once_with(
735-
mock.ANY,
701+
self.network.find_ip.assert_called_once_with(
736702
self.floating_ip.id,
737703
ignore_missing=False,
738704
)
739705
self.network.update_ip.assert_called_once_with(
740706
self.floating_ip, **attrs)
741707

742-
@mock.patch(
743-
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
744-
"fip._find_floating_ip"
745-
)
746-
def test_port_and_no_qos_policy_option(self, find_floating_ip_mock):
747-
find_floating_ip_mock.side_effect = [
748-
self.floating_ip,
749-
]
708+
def test_port_and_no_qos_policy_option(self):
750709
arglist = [
751710
"--no-qos-policy",
752711
'--port', self.floating_ip.port_id,
@@ -765,8 +724,7 @@ def test_port_and_no_qos_policy_option(self, find_floating_ip_mock):
765724
'qos_policy_id': None,
766725
'port_id': self.floating_ip.port_id,
767726
}
768-
find_floating_ip_mock.assert_called_once_with(
769-
mock.ANY,
727+
self.network.find_ip.assert_called_once_with(
770728
self.floating_ip.id,
771729
ignore_missing=False,
772730
)
@@ -796,14 +754,7 @@ def setUp(self):
796754
# Get the command object to test
797755
self.cmd = fip.UnsetFloatingIP(self.app, self.namespace)
798756

799-
@mock.patch(
800-
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
801-
"fip._find_floating_ip"
802-
)
803-
def test_floating_ip_unset_port(self, find_floating_ip_mock):
804-
find_floating_ip_mock.side_effect = [
805-
self.floating_ip,
806-
]
757+
def test_floating_ip_unset_port(self):
807758
arglist = [
808759
self.floating_ip.id,
809760
"--port",
@@ -819,8 +770,7 @@ def test_floating_ip_unset_port(self, find_floating_ip_mock):
819770
attrs = {
820771
'port_id': None,
821772
}
822-
find_floating_ip_mock.assert_called_once_with(
823-
mock.ANY,
773+
self.network.find_ip.assert_called_once_with(
824774
self.floating_ip.id,
825775
ignore_missing=False,
826776
)
@@ -829,14 +779,7 @@ def test_floating_ip_unset_port(self, find_floating_ip_mock):
829779

830780
self.assertIsNone(result)
831781

832-
@mock.patch(
833-
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
834-
"fip._find_floating_ip"
835-
)
836-
def test_floating_ip_unset_qos_policy(self, find_floating_ip_mock):
837-
find_floating_ip_mock.side_effect = [
838-
self.floating_ip,
839-
]
782+
def test_floating_ip_unset_qos_policy(self):
840783
arglist = [
841784
self.floating_ip.id,
842785
"--qos-policy",
@@ -852,8 +795,7 @@ def test_floating_ip_unset_qos_policy(self, find_floating_ip_mock):
852795
attrs = {
853796
'qos_policy_id': None,
854797
}
855-
find_floating_ip_mock.assert_called_once_with(
856-
mock.ANY,
798+
self.network.find_ip.assert_called_once_with(
857799
self.floating_ip.id,
858800
ignore_missing=False,
859801
)

0 commit comments

Comments
 (0)