Add floating IP pool resource methods

This change add floating IP pool resource list method.
floating IP pool is a Nova API read-only resource.

Change-Id: I1c68a696926f45e93050f441404726d92a717900
This commit is contained in:
Davide Guerri 2015-05-05 18:46:29 +01:00
parent 3328cc77da
commit 569fe40caf
6 changed files with 149 additions and 0 deletions

View File

@ -878,6 +878,10 @@ class OpenStackCloud(object):
images = self.list_images()
return self._filter_list(images, name_or_id, filters)
def search_floating_ip_pools(self, name=None, filters=None):
pools = self.list_floating_ip_pools()
return self._filter_list(pools, name, filters)
def list_networks(self):
return self.manager.submitTask(_tasks.NetworkList())['networks']
@ -964,6 +968,23 @@ class OpenStackCloud(object):
images.append(image)
return images
def list_floating_ip_pools(self):
if not self._has_nova_extension('os-floating-ip-pools'):
raise OpenStackCloudUnavailableExtension(
'Floating IP pools extension is not available on target cloud')
try:
return meta.obj_list_to_dict(
self.manager.submitTask(_tasks.FloatingIPPoolList())
)
except Exception as e:
self.log.debug(
"nova could not list floating IP pools: {msg}".format(
msg=str(e)), exc_info=True)
raise OpenStackCloudException(
"error fetching floating IP pool list: {msg}".format(
msg=str(e)))
def get_network(self, name_or_id, filters=None):
return self._get_entity(self.search_networks, name_or_id, filters)

View File

@ -213,6 +213,11 @@ class FloatingIPAttach(task_manager.Task):
return client.nova_client.servers.add_floating_ip(**self.args)
class FloatingIPPoolList(task_manager.Task):
def main(self, client):
return client.nova_client.floating_ip_pools.list()
class ContainerGet(task_manager.Task):
def main(self, client):
return client.swift_client.head_container(**self.args)

View File

@ -34,3 +34,7 @@ class OpenStackCloudTimeout(OpenStackCloudException):
class OpenStackCloudUnavailableService(OpenStackCloudException):
pass
class OpenStackCloudUnavailableExtension(OpenStackCloudException):
pass

View File

@ -26,6 +26,12 @@ class FakeFlavor(object):
self.name = name
class FakeFloatingIPPool(object):
def __init__(self, id, name):
self.id = id
self.name = name
class FakeImage(object):
def __init__(self, id, name, status):
self.id = id

View File

@ -0,0 +1,53 @@
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
test_floating_ip_pool
----------------------------------
Functional tests for floating IP pool resource (managed by nova)
"""
from shade import openstack_cloud
from shade.tests import base
# When using nova-network, floating IP pools are created with nova-manage
# command.
# When using Neutron, floating IP pools in Nova are mapped from external
# network names. This only if the floating-ip-pools nova extension is
# available.
# For instance, for current implementation of hpcloud that's not true:
# nova floating-ip-pool-list returns 404.
class TestFloatingIPPool(base.TestCase):
def setUp(self):
super(TestFloatingIPPool, self).setUp()
# Shell should have OS-* envvars from openrc, typically loaded by job
self.cloud = openstack_cloud()
if not self.cloud._has_nova_extension('os-floating-ip-pools'):
# Skipping this test is floating-ip-pool extension is not
# available on the testing cloud
self.skip(
'Floating IP pools extension is not available')
def test_list_floating_ip_pools(self):
pools = self.cloud.list_floating_ip_pools()
if not pools:
self.assertFalse('no floating-ip pool available')
for pool in pools:
self.assertTrue('name' in pool)

View File

@ -0,0 +1,60 @@
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
test_floating_ip_pool
----------------------------------
Test floating IP pool resource (managed by nova)
"""
from mock import patch
from shade import OpenStackCloud
from shade import OpenStackCloudException
from shade.tests.unit import base
from shade.tests.fakes import FakeFloatingIPPool
class TestFloatingIPPool(base.TestCase):
mock_pools = [
{'id': 'pool1_id', 'name': 'pool1'},
{'id': 'pool2_id', 'name': 'pool2'}]
def setUp(self):
super(TestFloatingIPPool, self).setUp()
self.client = OpenStackCloud('cloud', {})
@patch.object(OpenStackCloud, '_has_nova_extension')
@patch.object(OpenStackCloud, 'nova_client')
def test_list_floating_ip_pools(
self, mock_nova_client, mock__has_nova_extension):
mock_nova_client.floating_ip_pools.list.return_value = [
FakeFloatingIPPool(**p) for p in self.mock_pools
]
mock__has_nova_extension.return_value = True
floating_ip_pools = self.client.list_floating_ip_pools()
self.assertItemsEqual(floating_ip_pools, self.mock_pools)
@patch.object(OpenStackCloud, '_has_nova_extension')
@patch.object(OpenStackCloud, 'nova_client')
def test_list_floating_ip_pools_exception(
self, mock_nova_client, mock__has_nova_extension):
mock_nova_client.floating_ip_pools.list.side_effect = \
Exception('whatever')
mock__has_nova_extension.return_value = True
self.assertRaises(
OpenStackCloudException, self.client.list_floating_ip_pools)