Add the python api for floating IP DNS.

For blueprint public-and-private-dns.

Change-Id: I73d64f9e0ea6a1c913f427d1fd07b76d19e9f6a3
This commit is contained in:
Andrew Bogott 2011-12-28 17:33:43 -06:00
parent 90212e9317
commit c9d87f9bac
4 changed files with 196 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_ip_dns
from novaclient.v1_1 import floating_ips
from novaclient.v1_1 import images
from novaclient.v1_1 import keypairs
@ -44,6 +45,7 @@ class Client(object):
# extensions
self.floating_ips = floating_ips.FloatingIPManager(self)
self.floating_ip_dns = floating_ip_dns.FloatingIPDNSManager(self)
self.volumes = volumes.VolumeManager(self)
self.volume_snapshots = volume_snapshots.SnapshotManager(self)
self.keypairs = keypairs.KeypairManager(self)

View File

@ -0,0 +1,84 @@
# Copyright 2011 Andrew Bogott for The Wikimedia Foundation
# 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.
import urllib
from novaclient import base
def _quote_zone(zone):
"""Special quoting rule for placing zone names on a url line.
Zone names tend to have .'s in them. Urllib doesn't quote dots,
but Routes tends to choke on them, so we need an extra level of
by-hand quoting here.
"""
return urllib.quote(zone.replace('.', '%2E'))
class FloatingIPDNS(base.Resource):
def delete(self):
self.manager.delete_entry(self.name, self.zone)
def create(self):
self.manager.create_entry(self.zone, self.name,
self.ip, self.dns_type)
def get(self):
entries = self.manager.get_entries(self.zone, self.ip, self.name)
if entries:
return entries[0]
else:
return None
class FloatingIPDNSManager(base.ManagerWithFind):
resource_class = FloatingIPDNS
def zones(self):
"""Return the list of available dns zones."""
return self._list("/os-floating-ip-dns", "zones")
def get_entries(self, zone, name=None, ip=None):
"""Return a list of entries for the given zone and ip or name."""
qparams = {}
if name:
qparams['name'] = name
if ip:
qparams['ip'] = ip
params = "?%s" % urllib.urlencode(qparams) if qparams else ""
return self._list("/os-floating-ip-dns/%s%s" %
(_quote_zone(zone), params),
"dns_entries")
def create_entry(self, zone, name, ip, dns_type):
"""Add a new DNS entry."""
body = {'dns_entry':
{'name': name,
'ip': ip,
'dns_type': dns_type,
'zone': zone}}
return self._create("/os-floating-ip-dns", body, "dns_entry")
def delete_entry(self, zone, name):
"""Delete entry specified by name and zone."""
qparams = {'name': name}
params = "?%s" % urllib.urlencode(qparams) if qparams else ""
return self._delete("/os-floating-ip-dns/%s%s" %
(_quote_zone(zone), params))

View File

@ -352,6 +352,47 @@ class FakeHTTPClient(base_client.HTTPClient):
def delete_os_floating_ips_1(self, **kw):
return (204, None)
def get_os_floating_ip_dns(self, **kw):
return (205, {'zones':
[{'zone': 'example.org'},
{'zone': 'example.com'}]})
def get_os_floating_ip_dns_zone1(self, **kw):
if kw.get('ip'):
return (205, {'dns_entries':
[{'dns_entry':
{'ip': kw.get('ip'),
'name': "host1",
'type': "A",
'zone': 'zone1'}},
{'dns_entry':
{'ip': kw.get('ip'),
'name': "host2",
'type': "A",
'zone': 'zone1'}}]})
if kw.get('name'):
return (205, {'dns_entries':
[{'dns_entry':
{'ip': "10.10.10.10",
'name': kw.get('name'),
'type': "A",
'zone': 'zone1'}}]})
else:
return (404, None)
def post_os_floating_ip_dns(self, body, **kw):
fakes.assert_has_keys(body['dns_entry'],
required=['name', 'ip', 'dns_type', 'zone'])
return (205, {'dns_entry':
{'ip': body['dns_entry'].get('ip'),
'name': body['dns_entry'].get('name'),
'type': body['dns_entry'].get('dns_type'),
'zone': body['dns_entry'].get('zone')}})
def delete_os_floating_ip_dns_zone1(self, **kw):
assert 'name' in kw
return (200, None)
#
# Images
#

View File

@ -0,0 +1,69 @@
from novaclient import exceptions
from novaclient.v1_1 import floating_ip_dns
from tests.v1_1 import fakes
from tests import utils
cs = fakes.FakeClient()
def _quote_zone(zone):
"""
Zone names tend to have .'s in them. Urllib doesn't quote dots,
but Routes tends to choke on them, so we need an extra level of
by-hand quoting here. This function needs to duplicate the one in
python-novaclient/novaclient/v1_1/floating_ip_dns.py
"""
return urllib.quote(zone.replace('.', '%2E'))
class FloatingIPDNSTest(utils.TestCase):
testname = "somehostname"
testip = "1.2.3.4"
testzone = "zone1"
testtype = "A"
def test_dns_zones(self):
zonelist = cs.floating_ip_dns.zones()
self.assertEqual(len(zonelist), 2)
for entry in zonelist:
self.assertTrue(isinstance(entry, floating_ip_dns.FloatingIPDNS))
self.assertEqual(zonelist[1].zone, 'example.com')
def test_get_dns_entries_by_ip(self):
entries = cs.floating_ip_dns.get_entries(self.testzone, ip=self.testip)
self.assertEqual(len(entries), 2)
for entry in entries:
self.assertTrue(isinstance(entry, floating_ip_dns.FloatingIPDNS))
self.assertEqual(entries[1].dns_entry['name'], 'host2')
self.assertEqual(entries[1].dns_entry['ip'], self.testip)
def test_get_dns_entries_by_name(self):
entries = cs.floating_ip_dns.get_entries(self.testzone,
name=self.testname)
self.assertEqual(len(entries), 1)
self.assertTrue(isinstance(entries[0], floating_ip_dns.FloatingIPDNS))
self.assertEqual(entries[0].dns_entry['name'], self.testname)
def test_create_entry(self):
response = cs.floating_ip_dns.create_entry(self.testzone,
self.testname,
self.testip,
self.testtype)
self.assertEqual(response.name, self.testname)
self.assertEqual(response.ip, self.testip)
self.assertEqual(response.zone, self.testzone)
self.assertEqual(response.type, self.testtype)
def test_delete_entry(self):
response = cs.floating_ip_dns.delete_entry(self.testzone,
self.testname)
cs.assert_called('DELETE', '/os-floating-ip-dns/%s?name=%s' %
(self.testzone, self.testname))