Skip to content

Commit

Permalink
Moving to iterators in DNS.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Oct 18, 2016
1 parent e575f81 commit afb851e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 64 deletions.
42 changes: 23 additions & 19 deletions dns/google/cloud/dns/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from google.cloud.client import JSONClient
from google.cloud.dns.connection import Connection
from google.cloud.dns.zone import ManagedZone
from google.cloud.iterator import Iterator


class Client(JSONClient):
Expand Down Expand Up @@ -70,31 +71,19 @@ def list_zones(self, max_results=None, page_token=None):
:param max_results: maximum number of zones to return, If not
passed, defaults to a value set by the API.
:type page_token: string
:type page_token: str
: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:`google.cloud.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``).
:rtype: :class:`~google.cloud.iterator.Iterator`
:returns: Iterator of :class:`~google.cloud.dns.zone.ManagedZone`
belonging to this project.
"""
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')
return Iterator(client=self, path=path, items_key='managedZones',
item_to_value=_item_to_zone, page_token=page_token,
max_results=max_results)

def zone(self, name, dns_name=None, description=None):
"""Construct a zone bound to this client.
Expand All @@ -115,3 +104,18 @@ def zone(self, name, dns_name=None, description=None):
"""
return ManagedZone(name, dns_name, client=self,
description=description)


def _item_to_zone(iterator, resource):
"""Convert a JSON managed zone to the native object.
:type iterator: :class:`~google.cloud.iterator.Iterator`
:param iterator: The iterator that has retrieved the item.
:type resource: dict
:param resource: An item to be converted to a managed zone.
:rtype: :class:`.ManagedZone`
:returns: The next managed zone in the page.
"""
return ManagedZone.from_api_repr(resource, iterator.client)
90 changes: 51 additions & 39 deletions dns/google/cloud/dns/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
# limitations under the License.

"""Define API ManagedZones."""

import six

from google.cloud._helpers import _rfc3339_to_datetime
from google.cloud.exceptions import NotFound
from google.cloud.dns.changes import Changes
from google.cloud.dns.resource_record_set import ResourceRecordSet
from google.cloud.iterator import Iterator


class ManagedZone(object):
Expand Down Expand Up @@ -330,29 +332,19 @@ def list_resource_record_sets(self, max_results=None, page_token=None,
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current zone.
:rtype: tuple, (list, str)
:returns: list of
:class:`~.resource_record_set.ResourceRecordSet`,
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``).
:rtype: :class:`~google.cloud.iterator.Iterator`
:returns: Iterator of :class:`~.resource_record_set.ResourceRecordSet`
belonging to this zone.
"""
params = {}

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

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

client = self._require_client(client)
path = '/projects/%s/managedZones/%s/rrsets' % (
self.project, self.name)
client = self._require_client(client)
conn = client.connection
resp = conn.api_request(method='GET', path=path, query_params=params)
zones = [ResourceRecordSet.from_api_repr(resource, self)
for resource in resp['rrsets']]
return zones, resp.get('nextPageToken')
iterator = Iterator(
client=client, path=path, items_key='rrsets',
item_to_value=_item_to_resource_record_set,
page_token=page_token, max_results=max_results)
iterator.zone = self
return iterator

def list_changes(self, max_results=None, page_token=None, client=None):
"""List change sets for this zone.
Expand All @@ -373,26 +365,46 @@ def list_changes(self, max_results=None, page_token=None, client=None):
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current zone.
:rtype: tuple, (list, str)
:returns: list of
:class:`~.resource_record_set.ResourceRecordSet`,
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``).
:rtype: :class:`~google.cloud.iterator.Iterator`
:returns: Iterator of :class:`~.changes.Changes`
belonging to this zone.
"""
params = {}
client = self._require_client(client)
path = '/projects/%s/managedZones/%s/changes' % (
self.project, self.name)
iterator = Iterator(
client=client, path=path, items_key='changes',
item_to_value=_item_to_changes,
page_token=page_token, max_results=max_results)
iterator.zone = self
return iterator

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

if page_token is not None:
params['pageToken'] = page_token
def _item_to_resource_record_set(iterator, resource):
"""Convert a JSON resource record set value to the native object.
path = '/projects/%s/managedZones/%s/changes' % (
self.project, self.name)
client = self._require_client(client)
conn = client.connection
resp = conn.api_request(method='GET', path=path, query_params=params)
zones = [Changes.from_api_repr(resource, self)
for resource in resp['changes']]
return zones, resp.get('nextPageToken')
:type iterator: :class:`~google.cloud.iterator.Iterator`
:param iterator: The iterator that has retrieved the item.
:type resource: dict
:param resource: An item to be converted to a resource record set.
:rtype: :class:`~.resource_record_set.ResourceRecordSet`
:returns: The next resource record set in the page.
"""
return ResourceRecordSet.from_api_repr(resource, iterator.zone)


def _item_to_changes(iterator, resource):
"""Convert a JSON "changes" value to the native object.
:type iterator: :class:`~google.cloud.iterator.Iterator`
:param iterator: The iterator that has retrieved the item.
:type resource: dict
:param resource: An item to be converted to a "changes".
:rtype: :class:`.Changes`
:returns: The next "changes" in the page.
"""
return Changes.from_api_repr(resource, iterator.zone)
10 changes: 8 additions & 2 deletions dns/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ def test_list_zones_defaults(self):
client = self._makeOne(self.PROJECT, creds)
conn = client.connection = _Connection(DATA)

zones, token = client.list_zones()
iterator = client.list_zones()
iterator.update_page()
zones = list(iterator.page)
token = iterator.next_page_token

self.assertEqual(len(zones), len(DATA['managedZones']))
for found, expected in zip(zones, DATA['managedZones']):
Expand Down Expand Up @@ -173,7 +176,10 @@ def test_list_zones_explicit(self):
client = self._makeOne(self.PROJECT, creds)
conn = client.connection = _Connection(DATA)

zones, token = client.list_zones(max_results=3, page_token=TOKEN)
iterator = client.list_zones(max_results=3, page_token=TOKEN)
iterator.update_page()
zones = list(iterator.page)
token = iterator.next_page_token

self.assertEqual(len(zones), len(DATA['managedZones']))
for found, expected in zip(zones, DATA['managedZones']):
Expand Down
24 changes: 20 additions & 4 deletions dns/unit_tests/test_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,11 @@ def test_list_resource_record_sets_defaults(self):
client = _Client(project=self.PROJECT, connection=conn)
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client)

rrsets, token = zone.list_resource_record_sets()
iterator = zone.list_resource_record_sets()
self.assertIs(zone, iterator.zone)
iterator.update_page()
rrsets = list(iterator.page)
token = iterator.next_page_token

self.assertEqual(len(rrsets), len(DATA['rrsets']))
for found, expected in zip(rrsets, DATA['rrsets']):
Expand Down Expand Up @@ -489,8 +493,12 @@ def test_list_resource_record_sets_explicit(self):
client2 = _Client(project=self.PROJECT, connection=conn2)
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client1)

rrsets, token = zone.list_resource_record_sets(
iterator = zone.list_resource_record_sets(
max_results=3, page_token=TOKEN, client=client2)
self.assertIs(zone, iterator.zone)
iterator.update_page()
rrsets = list(iterator.page)
token = iterator.next_page_token

self.assertEqual(len(rrsets), len(DATA['rrsets']))
for found, expected in zip(rrsets, DATA['rrsets']):
Expand Down Expand Up @@ -551,7 +559,11 @@ def test_list_changes_defaults(self):
client = _Client(project=self.PROJECT, connection=conn)
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client)

changes, token = zone.list_changes()
iterator = zone.list_changes()
self.assertIs(zone, iterator.zone)
iterator.update_page()
changes = list(iterator.page)
token = iterator.next_page_token

self.assertEqual(len(changes), len(DATA['changes']))
for found, expected in zip(changes, DATA['changes']):
Expand Down Expand Up @@ -628,8 +640,12 @@ def test_list_changes_explicit(self):
client2 = _Client(project=self.PROJECT, connection=conn2)
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client1)

changes, token = zone.list_changes(
iterator = zone.list_changes(
max_results=3, page_token=TOKEN, client=client2)
self.assertIs(zone, iterator.zone)
iterator.update_page()
changes = list(iterator.page)
token = iterator.next_page_token

self.assertEqual(len(changes), len(DATA['changes']))
for found, expected in zip(changes, DATA['changes']):
Expand Down

0 comments on commit afb851e

Please sign in to comment.