support for floating_ips + D4

This commit is contained in:
Anthony Young 2011-08-24 22:51:53 -07:00
parent 541d578b90
commit 8ec0ae6ab0
6 changed files with 178 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from novaclient import client
from novaclient.v1_1 import flavors
from novaclient.v1_1 import floating_ips
from novaclient.v1_1 import images
from novaclient.v1_1 import keypairs
from novaclient.v1_1 import servers
@ -27,6 +28,7 @@ class Client(object):
# FIXME(jesse): project_id isn't required to autenticate
def __init__(self, username, api_key, project_id, auth_url, timeout=None):
self.flavors = flavors.FlavorManager(self)
self.floating_ips = floating_ips.FloatingIPManager(self)
self.images = images.ImageManager(self)
self.servers = servers.ServerManager(self)

View File

@ -0,0 +1,55 @@
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# 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.
from novaclient.v1_1 import base
class FloatingIP(base.Resource):
def delete(self):
"""
Delete this floating ip
"""
self.manager.delete(self)
class FloatingIPManager(base.ManagerWithFind):
resource_class = FloatingIP
def list(self):
"""
List floating ips for a tenant
"""
return self._list("/os-floating-ips", "floating_ips")
def create(self):
"""
Create (allocate) a floating ip for a tenant
"""
return self._create("/os-floating-ips", {}, "floating_ip")
def delete(self, floating_ip):
"""
Delete (deallocate) a floating ip for a tenant
:param key: The :class:`Keypair` (or its ID) to delete.
"""
return self._delete("/os-floating-ips/%s" % base.getid(floating_ip))
def get(self, floating_ip):
"""
Retrieve a floating ip
"""
return self._get("/os-floating-ips/%s" % base.getid(floating_ip),
"floating_ip")

View File

@ -55,6 +55,22 @@ class Server(base.Resource):
"""
self.manager.add_fixed_ip(self, network_id)
def add_floating_ip(self, address):
"""
Add floating IP to an instance
:param address: The ip address or FloatingIP to add to the instance
"""
self.manager.add_floating_ip(self, address)
def remove_floating_ip(self, address):
"""
Add floating IP to an instance
:param address: The ip address or FloatingIP to add to remove
"""
self.manager.remove_floating_ip(self, address)
def pause(self):
"""
Pause -- Pause the running server.
@ -239,6 +255,33 @@ class ServerManager(local_base.BootingManagerWithFind):
"""
self._action('removeFixedIp', server, {'address': address})
def add_floating_ip(self, server, address):
"""
Add a floating ip to an instance
:param server: The :class:`Server` (or its ID) to add an IP to.
:param address: The FloatingIP or string floating address to add.
"""
try:
address = address.ip
except:
pass
self._action('addFloatingIp', server, {'address': address})
def remove_floating_ip(self, server, address):
"""
Remove a floating IP address.
:param server: The :class:`Server` (or its ID) to remove an IP from.
:param address: The FloatingIP or string floating address to remove.
"""
try:
address = address.ip
except:
pass
self._action('removeFloatingIp', server, {'address': address})
def pause(self, server):
"""
Pause the server.

View File

@ -264,6 +264,10 @@ class FakeHTTPClient(base_client.HTTPClient):
assert body[action].keys() == ['networkId']
elif action == 'removeFixedIp':
assert body[action].keys() == ['address']
elif action == 'addFloatingIp':
assert body[action].keys() == ['address']
elif action == 'removeFloatingIp':
assert body[action].keys() == ['address']
elif action == 'createImage':
assert set(body[action].keys()) == set(['name', 'metadata'])
elif action == 'changePassword':
@ -294,6 +298,27 @@ class FakeHTTPClient(base_client.HTTPClient):
def get_flavors_2(self, **kw):
return (200, {'flavor': self.get_flavors_detail()[1]['flavors'][1]})
#
# Floating ips
#
def get_os_floating_ips(self, **kw):
return (200, {'floating_ips': [
{'id': 1, 'fixed_ip': '10.0.0.1', 'address': '11.0.0.1'},
{'id': 2, 'fixed_ip': '10.0.0.2', 'address': '11.0.0.2'},
]})
def get_os_floating_ips_1(self, **kw):
return (200, {'floating_ip':
{'id': 1, 'fixed_ip': '10.0.0.1', 'address': '11.0.0.1'}
})
def post_os_floating_ips(self, body, **kw):
return (202, self.get_os_floating_ips_1()[1])
def delete_os_floating_ips_1(self, **kw):
return (204, None)
#
# Images
#

View File

@ -0,0 +1,29 @@
from novaclient import exceptions
from novaclient.v1_1 import floating_ips
from tests.v1_1 import fakes
from tests import utils
cs = fakes.FakeClient()
class FloatingIPsTest(utils.TestCase):
def test_list_floating_ips(self):
fl = cs.floating_ips.list()
cs.assert_called('GET', '/os-floating-ips')
[self.assertTrue(isinstance(f, floating_ips.FloatingIP)) for f in fl]
def test_delete_floating_ip(self):
fl = cs.floating_ips.list()[0]
fl.delete()
cs.assert_called('DELETE', '/os-floating-ips/1')
cs.floating_ips.delete(1)
cs.assert_called('DELETE', '/os-floating-ips/1')
cs.floating_ips.delete(fl)
cs.assert_called('DELETE', '/os-floating-ips/1')
def test_create_floating_ip(self):
fl = cs.floating_ips.create()
cs.assert_called('POST', '/os-floating-ips')
self.assertTrue(isinstance(fl, floating_ips.FloatingIP))

View File

@ -133,6 +133,30 @@ class ServersTest(utils.TestCase):
cs.servers.remove_fixed_ip(s, '10.0.0.1')
cs.assert_called('POST', '/servers/1234/action')
def test_add_floating_ip(self):
s = cs.servers.get(1234)
s.add_floating_ip('11.0.0.1')
cs.assert_called('POST', '/servers/1234/action')
cs.servers.add_floating_ip(s, '11.0.0.1')
cs.assert_called('POST', '/servers/1234/action')
f = cs.floating_ips.list()[0]
cs.servers.add_floating_ip(s, f)
cs.assert_called('POST', '/servers/1234/action')
s.add_floating_ip(f)
cs.assert_called('POST', '/servers/1234/action')
def test_remove_floating_ip(self):
s = cs.servers.get(1234)
s.remove_floating_ip('11.0.0.1')
cs.assert_called('POST', '/servers/1234/action')
cs.servers.remove_floating_ip(s, '11.0.0.1')
cs.assert_called('POST', '/servers/1234/action')
f = cs.floating_ips.list()[0]
cs.servers.remove_floating_ip(s, f)
cs.assert_called('POST', '/servers/1234/action')
s.remove_floating_ip(f)
cs.assert_called('POST', '/servers/1234/action')
def test_rescue(self):
s = cs.servers.get(1234)
s.rescue()