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
This commit is contained in:
parent
2ef279ab71
commit
a742e47ecf
@ -15,8 +15,6 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from openstack import exceptions as sdk_exceptions
|
|
||||||
from openstack.network.v2 import floating_ip as _floating_ip
|
|
||||||
from osc_lib.command import command
|
from osc_lib.command import command
|
||||||
from osc_lib import utils
|
from osc_lib import utils
|
||||||
|
|
||||||
@ -86,54 +84,6 @@ def _get_attrs(client_manager, parsed_args):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
def _find_floating_ip(
|
|
||||||
session,
|
|
||||||
name_or_id,
|
|
||||||
ignore_missing=True,
|
|
||||||
**params
|
|
||||||
):
|
|
||||||
"""Find a floating IP by IP or ID
|
|
||||||
|
|
||||||
The SDK's find_ip() can only locate a floating IP by ID so we have
|
|
||||||
to do this ourselves.
|
|
||||||
"""
|
|
||||||
def _get_one_match(name_or_id):
|
|
||||||
"""Given a list of results, return the match"""
|
|
||||||
the_result = None
|
|
||||||
ip_list = list(_floating_ip.FloatingIP.list(session, **params))
|
|
||||||
for maybe_result in ip_list:
|
|
||||||
id_value = maybe_result.id
|
|
||||||
ip_value = maybe_result.floating_ip_address
|
|
||||||
|
|
||||||
if (id_value == name_or_id) or (ip_value == name_or_id):
|
|
||||||
# Only allow one resource to be found. If we already
|
|
||||||
# found a match, raise an exception to show it.
|
|
||||||
if the_result is None:
|
|
||||||
the_result = maybe_result
|
|
||||||
else:
|
|
||||||
msg = "More than one %s exists with the name '%s'."
|
|
||||||
msg = (msg % (_floating_ip.FloatingIP, name_or_id))
|
|
||||||
raise sdk_exceptions.DuplicateResource(msg)
|
|
||||||
|
|
||||||
return the_result
|
|
||||||
|
|
||||||
# Try to short-circuit by looking directly for a matching ID.
|
|
||||||
try:
|
|
||||||
match = _floating_ip.FloatingIP.existing(id=name_or_id, **params)
|
|
||||||
return match.get(session)
|
|
||||||
except sdk_exceptions.NotFoundException:
|
|
||||||
pass
|
|
||||||
|
|
||||||
result = _get_one_match(name_or_id)
|
|
||||||
if result is not None:
|
|
||||||
return result
|
|
||||||
|
|
||||||
if ignore_missing:
|
|
||||||
return None
|
|
||||||
raise sdk_exceptions.ResourceNotFound(
|
|
||||||
"No %s found for %s" % (_floating_ip.FloatingIP.__name__, name_or_id))
|
|
||||||
|
|
||||||
|
|
||||||
class CreateFloatingIP(common.NetworkAndComputeShowOne):
|
class CreateFloatingIP(common.NetworkAndComputeShowOne):
|
||||||
_description = _("Create floating IP")
|
_description = _("Create floating IP")
|
||||||
|
|
||||||
@ -246,8 +196,7 @@ class DeleteFloatingIP(common.NetworkAndComputeDelete):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action_network(self, client, parsed_args):
|
def take_action_network(self, client, parsed_args):
|
||||||
obj = _find_floating_ip(
|
obj = client.find_ip(
|
||||||
self.app.client_manager.sdk_connection.session,
|
|
||||||
self.r,
|
self.r,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -487,9 +436,7 @@ class SetFloatingIP(command.Command):
|
|||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
client = self.app.client_manager.network
|
||||||
attrs = {}
|
attrs = {}
|
||||||
# TODO(sindhu) Use client.find_ip() once SDK 0.9.15 is released
|
obj = client.find_ip(
|
||||||
obj = _find_floating_ip(
|
|
||||||
self.app.client_manager.sdk_connection.session,
|
|
||||||
parsed_args.floating_ip,
|
parsed_args.floating_ip,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -521,8 +468,7 @@ class ShowFloatingIP(common.NetworkAndComputeShowOne):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action_network(self, client, parsed_args):
|
def take_action_network(self, client, parsed_args):
|
||||||
obj = _find_floating_ip(
|
obj = client.find_ip(
|
||||||
self.app.client_manager.sdk_connection.session,
|
|
||||||
parsed_args.floating_ip,
|
parsed_args.floating_ip,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -586,9 +532,7 @@ class UnsetFloatingIP(command.Command):
|
|||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
client = self.app.client_manager.network
|
||||||
# TODO(sindhu) Use client.find_ip() once SDK 0.9.15 is released
|
obj = client.find_ip(
|
||||||
obj = _find_floating_ip(
|
|
||||||
self.app.client_manager.sdk_connection.session,
|
|
||||||
parsed_args.floating_ip,
|
parsed_args.floating_ip,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
|
@ -230,14 +230,14 @@ class TestDeleteFloatingIPNetwork(TestFloatingIPNetwork):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestDeleteFloatingIPNetwork, self).setUp()
|
super(TestDeleteFloatingIPNetwork, self).setUp()
|
||||||
|
|
||||||
|
self.network.find_ip = mock.Mock()
|
||||||
self.network.delete_ip = mock.Mock(return_value=None)
|
self.network.delete_ip = mock.Mock(return_value=None)
|
||||||
|
|
||||||
# Get the command object to test
|
# Get the command object to test
|
||||||
self.cmd = fip.DeleteFloatingIP(self.app, self.namespace)
|
self.cmd = fip.DeleteFloatingIP(self.app, self.namespace)
|
||||||
|
|
||||||
@mock.patch.object(fip, '_find_floating_ip')
|
def test_floating_ip_delete(self):
|
||||||
def test_floating_ip_delete(self, find_floating_ip_mock):
|
self.network.find_ip.side_effect = [
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ips[0],
|
self.floating_ips[0],
|
||||||
self.floating_ips[1],
|
self.floating_ips[1],
|
||||||
]
|
]
|
||||||
@ -251,17 +251,15 @@ class TestDeleteFloatingIPNetwork(TestFloatingIPNetwork):
|
|||||||
|
|
||||||
result = self.cmd.take_action(parsed_args)
|
result = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ips[0].id,
|
self.floating_ips[0].id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
self.network.delete_ip.assert_called_once_with(self.floating_ips[0])
|
self.network.delete_ip.assert_called_once_with(self.floating_ips[0])
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
|
||||||
@mock.patch.object(fip, '_find_floating_ip')
|
def test_floating_ip_delete_multi(self):
|
||||||
def test_floating_ip_delete_multi(self, find_floating_ip_mock):
|
self.network.find_ip.side_effect = [
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ips[0],
|
self.floating_ips[0],
|
||||||
self.floating_ips[1],
|
self.floating_ips[1],
|
||||||
]
|
]
|
||||||
@ -279,17 +277,15 @@ class TestDeleteFloatingIPNetwork(TestFloatingIPNetwork):
|
|||||||
|
|
||||||
calls = [
|
calls = [
|
||||||
call(
|
call(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ips[0].id,
|
self.floating_ips[0].id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
),
|
),
|
||||||
call(
|
call(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ips[1].id,
|
self.floating_ips[1].id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
find_floating_ip_mock.assert_has_calls(calls)
|
self.network.find_ip.assert_has_calls(calls)
|
||||||
|
|
||||||
calls = []
|
calls = []
|
||||||
for f in self.floating_ips:
|
for f in self.floating_ips:
|
||||||
@ -297,9 +293,8 @@ class TestDeleteFloatingIPNetwork(TestFloatingIPNetwork):
|
|||||||
self.network.delete_ip.assert_has_calls(calls)
|
self.network.delete_ip.assert_has_calls(calls)
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
|
||||||
@mock.patch.object(fip, '_find_floating_ip')
|
def test_floating_ip_delete_multi_exception(self):
|
||||||
def test_floating_ip_delete_multi_exception(self, find_floating_ip_mock):
|
self.network.find_ip.side_effect = [
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ips[0],
|
self.floating_ips[0],
|
||||||
exceptions.CommandError,
|
exceptions.CommandError,
|
||||||
]
|
]
|
||||||
@ -319,13 +314,11 @@ class TestDeleteFloatingIPNetwork(TestFloatingIPNetwork):
|
|||||||
except exceptions.CommandError as e:
|
except exceptions.CommandError as e:
|
||||||
self.assertEqual('1 of 2 floating_ips failed to delete.', str(e))
|
self.assertEqual('1 of 2 floating_ips failed to delete.', str(e))
|
||||||
|
|
||||||
find_floating_ip_mock.assert_any_call(
|
self.network.find_ip.assert_any_call(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ips[0].id,
|
self.floating_ips[0].id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
find_floating_ip_mock.assert_any_call(
|
self.network.find_ip.assert_any_call(
|
||||||
mock.ANY,
|
|
||||||
'unexist_floating_ip',
|
'unexist_floating_ip',
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -590,9 +583,7 @@ class TestShowFloatingIPNetwork(TestFloatingIPNetwork):
|
|||||||
# Get the command object to test
|
# Get the command object to test
|
||||||
self.cmd = fip.ShowFloatingIP(self.app, self.namespace)
|
self.cmd = fip.ShowFloatingIP(self.app, self.namespace)
|
||||||
|
|
||||||
@mock.patch.object(fip, '_find_floating_ip')
|
def test_floating_ip_show(self):
|
||||||
def test_floating_ip_show(self, find_floating_ip_mock):
|
|
||||||
find_floating_ip_mock.return_value = self.floating_ip
|
|
||||||
arglist = [
|
arglist = [
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
]
|
]
|
||||||
@ -603,8 +594,7 @@ class TestShowFloatingIPNetwork(TestFloatingIPNetwork):
|
|||||||
|
|
||||||
columns, data = self.cmd.take_action(parsed_args)
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -636,14 +626,7 @@ class TestSetFloatingIP(TestFloatingIPNetwork):
|
|||||||
# Get the command object to test
|
# Get the command object to test
|
||||||
self.cmd = fip.SetFloatingIP(self.app, self.namespace)
|
self.cmd = fip.SetFloatingIP(self.app, self.namespace)
|
||||||
|
|
||||||
@mock.patch(
|
def test_port_option(self):
|
||||||
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
|
|
||||||
"fip._find_floating_ip"
|
|
||||||
)
|
|
||||||
def test_port_option(self, find_floating_ip_mock):
|
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ip,
|
|
||||||
]
|
|
||||||
arglist = [
|
arglist = [
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
'--port', self.floating_ip.port_id,
|
'--port', self.floating_ip.port_id,
|
||||||
@ -660,8 +643,7 @@ class TestSetFloatingIP(TestFloatingIPNetwork):
|
|||||||
'port_id': self.floating_ip.port_id,
|
'port_id': self.floating_ip.port_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -669,14 +651,7 @@ class TestSetFloatingIP(TestFloatingIPNetwork):
|
|||||||
self.network.update_ip.assert_called_once_with(
|
self.network.update_ip.assert_called_once_with(
|
||||||
self.floating_ip, **attrs)
|
self.floating_ip, **attrs)
|
||||||
|
|
||||||
@mock.patch(
|
def test_fixed_ip_option(self):
|
||||||
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
|
|
||||||
"fip._find_floating_ip"
|
|
||||||
)
|
|
||||||
def test_fixed_ip_option(self, find_floating_ip_mock):
|
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ip,
|
|
||||||
]
|
|
||||||
arglist = [
|
arglist = [
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
'--port', self.floating_ip.port_id,
|
'--port', self.floating_ip.port_id,
|
||||||
@ -695,24 +670,16 @@ class TestSetFloatingIP(TestFloatingIPNetwork):
|
|||||||
'port_id': self.floating_ip.port_id,
|
'port_id': self.floating_ip.port_id,
|
||||||
'fixed_ip_address': self.floating_ip.fixed_ip_address,
|
'fixed_ip_address': self.floating_ip.fixed_ip_address,
|
||||||
}
|
}
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
self.network.update_ip.assert_called_once_with(
|
self.network.update_ip.assert_called_once_with(
|
||||||
self.floating_ip, **attrs)
|
self.floating_ip, **attrs)
|
||||||
|
|
||||||
@mock.patch(
|
def test_port_and_qos_policy_option(self):
|
||||||
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
|
|
||||||
"fip._find_floating_ip"
|
|
||||||
)
|
|
||||||
def test_port_and_qos_policy_option(self, find_floating_ip_mock):
|
|
||||||
qos_policy = network_fakes.FakeNetworkQosPolicy.create_one_qos_policy()
|
qos_policy = network_fakes.FakeNetworkQosPolicy.create_one_qos_policy()
|
||||||
self.network.find_qos_policy = mock.Mock(return_value=qos_policy)
|
self.network.find_qos_policy = mock.Mock(return_value=qos_policy)
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ip,
|
|
||||||
]
|
|
||||||
arglist = [
|
arglist = [
|
||||||
"--qos-policy", qos_policy.id,
|
"--qos-policy", qos_policy.id,
|
||||||
'--port', self.floating_ip.port_id,
|
'--port', self.floating_ip.port_id,
|
||||||
@ -731,22 +698,14 @@ class TestSetFloatingIP(TestFloatingIPNetwork):
|
|||||||
'qos_policy_id': qos_policy.id,
|
'qos_policy_id': qos_policy.id,
|
||||||
'port_id': self.floating_ip.port_id,
|
'port_id': self.floating_ip.port_id,
|
||||||
}
|
}
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
self.network.update_ip.assert_called_once_with(
|
self.network.update_ip.assert_called_once_with(
|
||||||
self.floating_ip, **attrs)
|
self.floating_ip, **attrs)
|
||||||
|
|
||||||
@mock.patch(
|
def test_port_and_no_qos_policy_option(self):
|
||||||
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
|
|
||||||
"fip._find_floating_ip"
|
|
||||||
)
|
|
||||||
def test_port_and_no_qos_policy_option(self, find_floating_ip_mock):
|
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ip,
|
|
||||||
]
|
|
||||||
arglist = [
|
arglist = [
|
||||||
"--no-qos-policy",
|
"--no-qos-policy",
|
||||||
'--port', self.floating_ip.port_id,
|
'--port', self.floating_ip.port_id,
|
||||||
@ -765,8 +724,7 @@ class TestSetFloatingIP(TestFloatingIPNetwork):
|
|||||||
'qos_policy_id': None,
|
'qos_policy_id': None,
|
||||||
'port_id': self.floating_ip.port_id,
|
'port_id': self.floating_ip.port_id,
|
||||||
}
|
}
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -796,14 +754,7 @@ class TestUnsetFloatingIP(TestFloatingIPNetwork):
|
|||||||
# Get the command object to test
|
# Get the command object to test
|
||||||
self.cmd = fip.UnsetFloatingIP(self.app, self.namespace)
|
self.cmd = fip.UnsetFloatingIP(self.app, self.namespace)
|
||||||
|
|
||||||
@mock.patch(
|
def test_floating_ip_unset_port(self):
|
||||||
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
|
|
||||||
"fip._find_floating_ip"
|
|
||||||
)
|
|
||||||
def test_floating_ip_unset_port(self, find_floating_ip_mock):
|
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ip,
|
|
||||||
]
|
|
||||||
arglist = [
|
arglist = [
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
"--port",
|
"--port",
|
||||||
@ -819,8 +770,7 @@ class TestUnsetFloatingIP(TestFloatingIPNetwork):
|
|||||||
attrs = {
|
attrs = {
|
||||||
'port_id': None,
|
'port_id': None,
|
||||||
}
|
}
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
@ -829,14 +779,7 @@ class TestUnsetFloatingIP(TestFloatingIPNetwork):
|
|||||||
|
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
|
||||||
@mock.patch(
|
def test_floating_ip_unset_qos_policy(self):
|
||||||
"openstackclient.tests.unit.network.v2.test_floating_ip_network." +
|
|
||||||
"fip._find_floating_ip"
|
|
||||||
)
|
|
||||||
def test_floating_ip_unset_qos_policy(self, find_floating_ip_mock):
|
|
||||||
find_floating_ip_mock.side_effect = [
|
|
||||||
self.floating_ip,
|
|
||||||
]
|
|
||||||
arglist = [
|
arglist = [
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
"--qos-policy",
|
"--qos-policy",
|
||||||
@ -852,8 +795,7 @@ class TestUnsetFloatingIP(TestFloatingIPNetwork):
|
|||||||
attrs = {
|
attrs = {
|
||||||
'qos_policy_id': None,
|
'qos_policy_id': None,
|
||||||
}
|
}
|
||||||
find_floating_ip_mock.assert_called_once_with(
|
self.network.find_ip.assert_called_once_with(
|
||||||
mock.ANY,
|
|
||||||
self.floating_ip.id,
|
self.floating_ip.id,
|
||||||
ignore_missing=False,
|
ignore_missing=False,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user