Skip to content

Commit

Permalink
Add dns.Client.get_zone
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott committed Apr 12, 2016
1 parent cefff49 commit 486de17
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
26 changes: 26 additions & 0 deletions gcloud/dns/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,29 @@ def zone(self, name, dns_name):
:returns: a new ``ManagedZone`` instance
"""
return ManagedZone(name, dns_name, client=self)

def get_zone(self, zone_name):
"""Get a zone by name.
If the zone isn't found, this will raise a
:class:`gcloud.exceptions.NotFound`.
For example::
>>> try:
>>> zone = client.get_zone('my-zone')
>>> except gcloud.exceptions.NotFound:
>>> print 'Sorry, that zone does not exist!'
This implements "dns.managedZones.get".
:type zone_name: string
:param zone_name: The name of the zone to get.
:rtype: :class:`gcloud.dns.zone.ManagedZone`
:returns: The zone matching the name provided.
:raises: :class:`gcloud.exceptions.NotFound`
"""
zone = ManagedZone(name=zone_name, dns_name=None, client=self)
zone.reload(client=self)
return zone
70 changes: 70 additions & 0 deletions gcloud/dns/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,62 @@ def test_zone(self):
self.assertEqual(zone.dns_name, DNS_NAME)
self.assertTrue(zone._client is client)

def test_get_zone_miss(self):
from gcloud.exceptions import NotFound

PROJECT = 'PROJECT'
CREDENTIALS = _Credentials()
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)

NONESUCH = 'nonesuch'
URI = '/'.join([
client.connection.API_BASE_URL,
'dns',
client.connection.API_VERSION,
'projects',
PROJECT,
'managedZones',
'nonesuch',
])
http = client.connection._http = _Http(
{'status': '404', 'content-type': 'application/json'},
b'{}',
)
self.assertRaises(NotFound, client.get_zone, NONESUCH)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)

def test_get_zone_hit(self):
from gcloud.dns.zone import ManagedZone

PROJECT = 'PROJECT'
CREDENTIALS = _Credentials()
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)

ZONE_NAME = 'zone-name'
ZONE_DNS_NAME = 'example.com.'
URI = '/'.join([
client.connection.API_BASE_URL,
'dns',
client.connection.API_VERSION,
'projects',
PROJECT,
'managedZones',
ZONE_NAME
])
http = client.connection._http = _Http(
{'status': '200', 'content-type': 'application/json'},
'{{"name": "{0}", "dnsName": "{1}"}}'.format(
ZONE_NAME, ZONE_DNS_NAME).encode('utf-8'),
)

zone = client.get_zone(ZONE_NAME)
self.assertTrue(isinstance(zone, ManagedZone))
self.assertEqual(zone.name, ZONE_NAME)
self.assertEqual(zone.dns_name, ZONE_DNS_NAME)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)


class _Credentials(object):

Expand All @@ -228,3 +284,17 @@ def api_request(self, **kw):
self._requested.append(kw)
response, self._responses = self._responses[0], self._responses[1:]
return response


class _Http(object):

_called_with = None

def __init__(self, headers, content):
from httplib2 import Response
self._response = Response(headers)
self._content = content

def request(self, **kw):
self._called_with = kw
return self._response, self._content
1 change: 1 addition & 0 deletions gcloud/dns/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def reload(self, client=None):

api_response = client.connection.api_request(
method='GET', path=self.path)
self.dns_name = api_response.get('dnsName')
self._set_properties(api_response)

def delete(self, client=None):
Expand Down

0 comments on commit 486de17

Please sign in to comment.