Skip to content

Commit

Permalink
Allow constructing 'ManagedZone' instance w/o 'dns_name'.
Browse files Browse the repository at this point in the history
Closes: #1719.
  • Loading branch information
tseaver committed Apr 18, 2016
1 parent 5d6c3c6 commit 89d8a5e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
7 changes: 4 additions & 3 deletions gcloud/dns/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ 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):
def zone(self, name, dns_name=None):
"""Construct a zone bound to this client.
:type name: string
:param name: Name of the zone.
:type dns_name: string
:param dns_name: DNS name of the zone.
:type dns_name: string or :class:`NoneType`
:param dns_name: DNS name of the zone. If not passed, then calls
to :meth:`zone.create` will fail.
:rtype: :class:`gcloud.dns.zone.ManagedZone`
:returns: a new ``ManagedZone`` instance
Expand Down
47 changes: 27 additions & 20 deletions gcloud/dns/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

class TestClient(unittest2.TestCase):

PROJECT = 'PROJECT'
ZONE_NAME = 'zone-name'

def _getTargetClass(self):
from gcloud.dns.client import Client
return Client
Expand All @@ -26,17 +29,16 @@ def _makeOne(self, *args, **kw):

def test_ctor(self):
from gcloud.dns.connection import Connection
PROJECT = 'PROJECT'
creds = _Credentials()
http = object()
client = self._makeOne(project=PROJECT, credentials=creds, http=http)
client = self._makeOne(project=self.PROJECT, credentials=creds,
http=http)
self.assertTrue(isinstance(client.connection, Connection))
self.assertTrue(client.connection.credentials is creds)
self.assertTrue(client.connection.http is http)

def test_quotas_defaults(self):
PROJECT = 'PROJECT'
PATH = 'projects/%s' % PROJECT
PATH = 'projects/%s' % (self.PROJECT,)
MANAGED_ZONES = 1234
RRS_PER_RRSET = 23
RRSETS_PER_ZONE = 345
Expand All @@ -56,7 +58,7 @@ def test_quotas_defaults(self):
CONVERTED = dict([(key, int(value))
for key, value in DATA['quota'].items()])
creds = _Credentials()
client = self._makeOne(PROJECT, creds)
client = self._makeOne(self.PROJECT, creds)
conn = client.connection = _Connection(DATA)

quotas = client.quotas()
Expand All @@ -69,8 +71,7 @@ def test_quotas_defaults(self):
self.assertEqual(req['path'], '/%s' % PATH)

def test_quotas_w_kind_key(self):
PROJECT = 'PROJECT'
PATH = 'projects/%s' % PROJECT
PATH = 'projects/%s' % (self.PROJECT,)
MANAGED_ZONES = 1234
RRS_PER_RRSET = 23
RRSETS_PER_ZONE = 345
Expand All @@ -92,7 +93,7 @@ def test_quotas_w_kind_key(self):
WITH_KIND = {'quota': DATA['quota'].copy()}
WITH_KIND['quota']['kind'] = 'dns#quota'
creds = _Credentials()
client = self._makeOne(PROJECT, creds)
client = self._makeOne(self.PROJECT, creds)
conn = client.connection = _Connection(WITH_KIND)

quotas = client.quotas()
Expand All @@ -106,14 +107,13 @@ def test_quotas_w_kind_key(self):

def test_list_zones_defaults(self):
from gcloud.dns.zone import ManagedZone
PROJECT = 'PROJECT'
ID_1 = '123'
ZONE_1 = 'zone_one'
DNS_1 = 'one.example.com'
ID_2 = '234'
ZONE_2 = 'zone_two'
DNS_2 = 'two.example.com'
PATH = 'projects/%s/managedZones' % PROJECT
PATH = 'projects/%s/managedZones' % (self.PROJECT,)
TOKEN = 'TOKEN'
DATA = {
'nextPageToken': TOKEN,
Expand All @@ -129,7 +129,7 @@ def test_list_zones_defaults(self):
]
}
creds = _Credentials()
client = self._makeOne(PROJECT, creds)
client = self._makeOne(self.PROJECT, creds)
conn = client.connection = _Connection(DATA)

zones, token = client.list_zones()
Expand All @@ -149,14 +149,13 @@ def test_list_zones_defaults(self):

def test_list_zones_explicit(self):
from gcloud.dns.zone import ManagedZone
PROJECT = 'PROJECT'
ID_1 = '123'
ZONE_1 = 'zone_one'
DNS_1 = 'one.example.com'
ID_2 = '234'
ZONE_2 = 'zone_two'
DNS_2 = 'two.example.com'
PATH = 'projects/%s/managedZones' % PROJECT
PATH = 'projects/%s/managedZones' % (self.PROJECT,)
TOKEN = 'TOKEN'
DATA = {
'managedZones': [
Expand All @@ -171,7 +170,7 @@ def test_list_zones_explicit(self):
]
}
creds = _Credentials()
client = self._makeOne(PROJECT, creds)
client = self._makeOne(self.PROJECT, creds)
conn = client.connection = _Connection(DATA)

zones, token = client.list_zones(max_results=3, page_token=TOKEN)
Expand All @@ -191,19 +190,27 @@ def test_list_zones_explicit(self):
self.assertEqual(req['query_params'],
{'maxResults': 3, 'pageToken': TOKEN})

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

def test_zone_wo_dns_name(self):
from gcloud.dns.zone import ManagedZone
creds = _Credentials()
client = self._makeOne(self.PROJECT, creds)
zone = client.zone(self.ZONE_NAME)
self.assertTrue(isinstance(zone, ManagedZone))
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, None)
self.assertTrue(zone._client is client)


class _Credentials(object):

Expand Down
20 changes: 17 additions & 3 deletions gcloud/dns/test_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,23 @@ def _verifyResourceProperties(self, zone, resource):
self.assertEqual(zone.zone_id, resource.get('id'))
self.assertEqual(zone.name_server_set, resource.get('nameServerSet'))

def test_ctor(self):
def test_ctor_defaults(self):
zone = self._makeOne(self.ZONE_NAME)
self.assertEqual(zone.name, self.ZONE_NAME)
self.assertEqual(zone.dns_name, None)
self.assertTrue(zone._client is None)

with self.assertRaises(AttributeError):
_ = zone.project

with self.assertRaises(AttributeError):
_ = zone.path

self.assertEqual(zone.zone_id, None)
self.assertEqual(zone.created, None)
self.assertEqual(zone.description, None)

def test_ctor_explicit(self):
client = _Client(self.PROJECT)
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client)
self.assertEqual(zone.name, self.ZONE_NAME)
Expand All @@ -94,10 +110,8 @@ def test_ctor(self):
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, None)

def test_from_api_repr_missing_identity(self):
Expand Down
7 changes: 4 additions & 3 deletions gcloud/dns/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ class ManagedZone(object):
:type name: string
:param name: the name of the zone
:type dns_name: string
:param dns_name: the DNS name of the zone
:type dns_name: string or :class:`NoneType`
:param dns_name: the DNS name of the zone. If not passed, then calls
to :meth:`create` will fail.
:type client: :class:`gcloud.dns.client.Client`
:param client: A client which holds credentials and project configuration
for the zone (which requires a project).
"""

def __init__(self, name, dns_name, client):
def __init__(self, name, dns_name=None, client=None):
self.name = name
self.dns_name = dns_name
self._client = client
Expand Down

0 comments on commit 89d8a5e

Please sign in to comment.