diff --git a/gcloud/dns/client.py b/gcloud/dns/client.py index ddc4ded769beb..4b2d3b6268a39 100644 --- a/gcloud/dns/client.py +++ b/gcloud/dns/client.py @@ -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 bucket 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 diff --git a/gcloud/dns/test_client.py b/gcloud/dns/test_client.py index 44b168f82efe1..62b3d6ec0b143 100644 --- a/gcloud/dns/test_client.py +++ b/gcloud/dns/test_client.py @@ -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): @@ -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 diff --git a/gcloud/dns/zone.py b/gcloud/dns/zone.py index 52f8e28187595..7d0df36a1024c 100644 --- a/gcloud/dns/zone.py +++ b/gcloud/dns/zone.py @@ -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):