Skip to content

Commit

Permalink
Allow passing 'description' 'ManagedZone.__init__'.
Browse files Browse the repository at this point in the history
Likewise, to 'Client.zone'.

Closes: #1721.
  • Loading branch information
tseaver committed Apr 19, 2016
1 parent dd2e90a commit 3e7f10a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
9 changes: 7 additions & 2 deletions gcloud/dns/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def list_zones(self, max_results=None, page_token=None):
for resource in resp['managedZones']]
return zones, resp.get('nextPageToken')

def zone(self, name, dns_name=None):
def zone(self, name, dns_name=None, description=None):
"""Construct a zone bound to this client.
:type name: string
Expand All @@ -105,7 +105,12 @@ def zone(self, name, dns_name=None):
:param dns_name: DNS name of the zone. If not passed, then calls
to :meth:`zone.create` will fail.
:type description: string or :class:`NoneType`
:param description: the description for the zone. If not passed,
defaults to the value of 'dns_name'.
:rtype: :class:`gcloud.dns.zone.ManagedZone`
:returns: a new ``ManagedZone`` instance
"""
return ManagedZone(name, dns_name, client=self)
return ManagedZone(name, dns_name, client=self,
description=description)
17 changes: 16 additions & 1 deletion gcloud/dns/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,20 @@ def test_list_zones_explicit(self):
self.assertEqual(req['query_params'],
{'maxResults': 3, 'pageToken': TOKEN})

def test_zone_w_explicit_dns_name(self):
def test_zone_explicit(self):
from gcloud.dns.zone import ManagedZone
DESCRIPTION = 'DESCRIPTION'
DNS_NAME = 'test.example.com'
creds = _Credentials()
client = self._makeOne(self.PROJECT, creds)
zone = client.zone(self.ZONE_NAME, DNS_NAME, DESCRIPTION)
self.assertTrue(isinstance(zone, ManagedZone))
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, DNS_NAME)
self.assertEqual(zone.description, DESCRIPTION)
self.assertTrue(zone._client is client)

def test_zone_w_dns_name_wo_description(self):
from gcloud.dns.zone import ManagedZone
DNS_NAME = 'test.example.com'
creds = _Credentials()
Expand All @@ -199,6 +212,7 @@ def test_zone_w_explicit_dns_name(self):
self.assertTrue(isinstance(zone, ManagedZone))
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, DNS_NAME)
self.assertEqual(zone.description, DNS_NAME)
self.assertTrue(zone._client is client)

def test_zone_wo_dns_name(self):
Expand All @@ -209,6 +223,7 @@ def test_zone_wo_dns_name(self):
self.assertTrue(isinstance(zone, ManagedZone))
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, None)
self.assertEqual(zone.description, None)
self.assertTrue(zone._client is client)


Expand Down
49 changes: 47 additions & 2 deletions gcloud/dns/test_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_ctor_defaults(self):
self.assertEqual(zone.created, None)
self.assertEqual(zone.description, None)

def test_ctor_explicit(self):
def test_ctor_wo_description(self):
client = _Client(self.PROJECT)
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client)
self.assertEqual(zone.name, self.ZONE_NAME)
Expand All @@ -112,7 +112,23 @@ def test_ctor_explicit(self):
'/projects/%s/managedZones/%s' % (self.PROJECT, self.ZONE_NAME))
self.assertEqual(zone.zone_id, None)
self.assertEqual(zone.created, None)
self.assertEqual(zone.description, None)
self.assertEqual(zone.description, self.DNS_NAME)

def test_ctor_explicit(self):
DESCRIPTION = 'DESCRIPTION'
client = _Client(self.PROJECT)
zone = self._makeOne(
self.ZONE_NAME, self.DNS_NAME, client, DESCRIPTION)
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, self.DNS_NAME)
self.assertTrue(zone._client is client)
self.assertEqual(zone.project, client.project)
self.assertEqual(
zone.path,
'/projects/%s/managedZones/%s' % (self.PROJECT, self.ZONE_NAME))
self.assertEqual(zone.zone_id, None)
self.assertEqual(zone.created, None)
self.assertEqual(zone.description, DESCRIPTION)

def test_from_api_repr_missing_identity(self):
self._setUpConstants()
Expand Down Expand Up @@ -207,6 +223,7 @@ def test_create_w_bound_client(self):
SENT = {
'name': self.ZONE_NAME,
'dnsName': self.DNS_NAME,
'description': self.DNS_NAME,
}
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(zone, RESOURCE)
Expand Down Expand Up @@ -242,6 +259,33 @@ def test_create_w_alternate_client(self):
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(zone, RESOURCE)

def test_create_wo_dns_name_or_description(self):
from gcloud.exceptions import BadRequest
PATH = 'projects/%s/managedZones' % self.PROJECT

_requested = []

def _api_request(**kw):
_requested.append(kw)
raise BadRequest('missing dns_name / description')

conn = _Connection()
conn.api_request = _api_request
client = _Client(project=self.PROJECT, connection=conn)
zone = self._makeOne(self.ZONE_NAME, client=client)

with self.assertRaises(BadRequest):
zone.create()

self.assertEqual(len(_requested), 1)
req = _requested[0]
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/%s' % PATH)
SENT = {
'name': self.ZONE_NAME,
}
self.assertEqual(req['data'], SENT)

def test_create_w_missing_output_properties(self):
# In the wild, the resource returned from 'zone.create' sometimes
# lacks 'creationTime' / 'lastModifiedTime'
Expand All @@ -264,6 +308,7 @@ def test_create_w_missing_output_properties(self):
SENT = {
'name': self.ZONE_NAME,
'dnsName': self.DNS_NAME,
'description': self.DNS_NAME,
}
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(zone, RESOURCE)
Expand Down
13 changes: 11 additions & 2 deletions gcloud/dns/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ class ManagedZone(object):
:type client: :class:`gcloud.dns.client.Client`
:param client: A client which holds credentials and project configuration
for the zone (which requires a project).
:type description: string or :class:`NoneType`
:param description: the description for the zone. If not passed, defaults
to the value of 'dns_name'.
"""

def __init__(self, name, dns_name=None, client=None):
def __init__(self, name, dns_name=None, client=None, description=None):
self.name = name
self.dns_name = dns_name
self._client = client
self._properties = {}
if description is None:
description = dns_name
self.description = description

@classmethod
def from_api_repr(cls, resource, client):
Expand Down Expand Up @@ -221,9 +228,11 @@ def _build_resource(self):
"""Generate a resource for ``create`` or ``update``."""
resource = {
'name': self.name,
'dnsName': self.dns_name,
}

if self.dns_name is not None:
resource['dnsName'] = self.dns_name

if self.description is not None:
resource['description'] = self.description

Expand Down

0 comments on commit 3e7f10a

Please sign in to comment.