Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'ManagedZone' and related support #1140

Merged
merged 6 commits into from
Sep 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gcloud/dns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from gcloud.dns.client import Client
from gcloud.dns.connection import Connection
from gcloud.dns.zone import ManagedZone


SCOPE = Connection.SCOPE
53 changes: 52 additions & 1 deletion gcloud/dns/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

from gcloud.client import JSONClient
from gcloud.dns.connection import Connection
from gcloud.dns.zone import ManagedZone


class Client(JSONClient):
"""Client to bundle configuration needed for API requests.

:type project: string
:param project: the project which the client acts on behalf of. Will be
passed when creating a dataset / job. If not passed,
passed when creating a zone. If not passed,

This comment was marked as spam.

falls back to the default inferred from the environment.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
Expand Down Expand Up @@ -56,3 +57,53 @@ def quotas(self):
resp = self.connection.api_request(method='GET', path=path)
return dict([(key, int(value))
for key, value in resp['quota'].items()])

def list_zones(self, max_results=None, page_token=None):
"""List zones for the project associated with this client.

See:
https://cloud.google.com/dns/api/v1/managedZones/list

:type max_results: int
:param max_results: maximum number of zones to return, If not
passed, defaults to a value set by the API.

:type page_token: string
:param page_token: opaque marker for the next "page" of zones. If
not passed, the API will return the first page of
zones.

:rtype: tuple, (list, str)
:returns: list of :class:`gcloud.dns.zone.ManagedZone`, plus a
"next page token" string: if the token is not None,
indicates that more zones can be retrieved with another
call (pass that value as ``page_token``).
"""
params = {}

if max_results is not None:
params['maxResults'] = max_results

if page_token is not None:
params['pageToken'] = page_token

path = '/projects/%s/managedZones' % (self.project,)
resp = self.connection.api_request(method='GET', path=path,
query_params=params)
zones = [ManagedZone.from_api_repr(resource, self)
for resource in resp['managedZones']]
return zones, resp.get('nextPageToken')

def zone(self, name, dns_name):
"""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.

:rtype: :class:`gcloud.dns.zone.ManagedZone`
:returns: a new ``ManagedZone`` instance
"""
return ManagedZone(name, dns_name, client=self)
100 changes: 100 additions & 0 deletions gcloud/dns/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,106 @@ def test_quotas_defaults(self):
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)

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
TOKEN = 'TOKEN'
DATA = {
'nextPageToken': TOKEN,
'managedZones': [
{'kind': 'dns#managedZone',
'id': ID_1,
'name': ZONE_1,
'dnsName': DNS_1},
{'kind': 'dns#managedZone',
'id': ID_2,
'name': ZONE_2,
'dnsName': DNS_2},
]
}
creds = _Credentials()
client = self._makeOne(PROJECT, creds)
conn = client.connection = _Connection(DATA)

zones, token = client.list_zones()

self.assertEqual(len(zones), len(DATA['managedZones']))
for found, expected in zip(zones, DATA['managedZones']):
self.assertTrue(isinstance(found, ManagedZone))
self.assertEqual(found.zone_id, expected['id'])
self.assertEqual(found.name, expected['name'])
self.assertEqual(found.dns_name, expected['dnsName'])
self.assertEqual(token, TOKEN)

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)

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
TOKEN = 'TOKEN'
DATA = {
'managedZones': [
{'kind': 'dns#managedZone',
'id': ID_1,
'name': ZONE_1,
'dnsName': DNS_1},
{'kind': 'dns#managedZone',
'id': ID_2,
'name': ZONE_2,
'dnsName': DNS_2},
]
}
creds = _Credentials()
client = self._makeOne(PROJECT, creds)
conn = client.connection = _Connection(DATA)

zones, token = client.list_zones(max_results=3, page_token=TOKEN)

self.assertEqual(len(zones), len(DATA['managedZones']))
for found, expected in zip(zones, DATA['managedZones']):
self.assertTrue(isinstance(found, ManagedZone))
self.assertEqual(found.zone_id, expected['id'])
self.assertEqual(found.name, expected['name'])
self.assertEqual(found.dns_name, expected['dnsName'])
self.assertEqual(token, None)

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)
self.assertEqual(req['query_params'],
{'maxResults': 3, 'pageToken': TOKEN})

def test_zone(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)
dataset = client.zone(ZONE_NAME, DNS_NAME)
self.assertTrue(isinstance(dataset, ManagedZone))
self.assertEqual(dataset.name, ZONE_NAME)
self.assertEqual(dataset.dns_name, DNS_NAME)
self.assertTrue(dataset._client is client)


class _Credentials(object):

Expand Down
Loading