Add ability to find an available floating ip
Add method to find the first available floating IP address. Change-Id: I7b1ff09e0df69f43ddbf20c1b49e26ae649f6cd3
This commit is contained in:
parent
5f3ca6406d
commit
6e94743987
@ -10,6 +10,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
from openstack import exceptions
|
||||||
from openstack.network import network_service
|
from openstack.network import network_service
|
||||||
from openstack import resource
|
from openstack import resource
|
||||||
|
|
||||||
@ -37,3 +38,14 @@ class FloatingIP(resource.Resource):
|
|||||||
port_id = resource.prop('port_id')
|
port_id = resource.prop('port_id')
|
||||||
project_id = resource.prop('tenant_id')
|
project_id = resource.prop('tenant_id')
|
||||||
router_id = resource.prop('router_id')
|
router_id = resource.prop('router_id')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def find_available(cls, session):
|
||||||
|
args = {
|
||||||
|
'port_id': '',
|
||||||
|
'fields': cls.id_attribute,
|
||||||
|
}
|
||||||
|
info = cls.list(session, **args)
|
||||||
|
if len(info) > 0:
|
||||||
|
return info[0]
|
||||||
|
raise exceptions.ResourceNotFound("No available floating ips exist.")
|
||||||
|
@ -10,8 +10,10 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import mock
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
|
from openstack import exceptions
|
||||||
from openstack.network.v2 import floatingip
|
from openstack.network.v2 import floatingip
|
||||||
|
|
||||||
IDENTIFIER = '10.0.0.1'
|
IDENTIFIER = '10.0.0.1'
|
||||||
@ -50,3 +52,31 @@ class TestFloatingIP(testtools.TestCase):
|
|||||||
self.assertEqual(EXAMPLE['port_id'], sot.port_id)
|
self.assertEqual(EXAMPLE['port_id'], sot.port_id)
|
||||||
self.assertEqual(EXAMPLE['tenant_id'], sot.project_id)
|
self.assertEqual(EXAMPLE['tenant_id'], sot.project_id)
|
||||||
self.assertEqual(EXAMPLE['router_id'], sot.router_id)
|
self.assertEqual(EXAMPLE['router_id'], sot.router_id)
|
||||||
|
|
||||||
|
def test_find_available(self):
|
||||||
|
mock_session = mock.Mock()
|
||||||
|
mock_get = mock.Mock()
|
||||||
|
mock_session.get = mock_get
|
||||||
|
data = {'floating_ip_address': '10.0.0.1'}
|
||||||
|
fake_response = mock.Mock()
|
||||||
|
fake_response.body = {floatingip.FloatingIP.resources_key: [data]}
|
||||||
|
mock_get.return_value = fake_response
|
||||||
|
|
||||||
|
result = floatingip.FloatingIP.find_available(mock_session)
|
||||||
|
|
||||||
|
self.assertEqual('10.0.0.1', result.id)
|
||||||
|
p = {'fields': 'floating_ip_address', 'port_id': ''}
|
||||||
|
mock_get.assert_called_with(floatingip.FloatingIP.base_path,
|
||||||
|
params=p,
|
||||||
|
service=floatingip.FloatingIP.service)
|
||||||
|
|
||||||
|
def test_find_available_nada(self):
|
||||||
|
mock_session = mock.Mock()
|
||||||
|
mock_get = mock.Mock()
|
||||||
|
mock_session.get = mock_get
|
||||||
|
fake_response = mock.Mock()
|
||||||
|
fake_response.body = {floatingip.FloatingIP.resources_key: []}
|
||||||
|
mock_get.return_value = fake_response
|
||||||
|
|
||||||
|
self.assertRaises(exceptions.ResourceNotFound,
|
||||||
|
floatingip.FloatingIP.find_available, mock_session)
|
||||||
|
Loading…
Reference in New Issue
Block a user