Skip to content

Commit 87c1bcc

Browse files
committed
Moving to iterators in DNS.
1 parent 0aca3f6 commit 87c1bcc

File tree

4 files changed

+165
-65
lines changed

4 files changed

+165
-65
lines changed

dns/google/cloud/dns/client.py

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from google.cloud.client import JSONClient
1919
from google.cloud.dns.connection import Connection
2020
from google.cloud.dns.zone import ManagedZone
21+
from google.cloud.iterator import Iterator
2122

2223

2324
class Client(JSONClient):
@@ -75,26 +76,12 @@ def list_zones(self, max_results=None, page_token=None):
7576
not passed, the API will return the first page of
7677
zones.
7778
78-
:rtype: tuple, (list, str)
79-
:returns: list of :class:`google.cloud.dns.zone.ManagedZone`, plus a
80-
"next page token" string: if the token is not None,
81-
indicates that more zones can be retrieved with another
82-
call (pass that value as ``page_token``).
79+
:rtype: :class:`_ManagedZoneIterator`
80+
:returns: An iterator of :class:`~google.cloud.dns.zone.ManagedZone`
81+
objects.
8382
"""
84-
params = {}
85-
86-
if max_results is not None:
87-
params['maxResults'] = max_results
88-
89-
if page_token is not None:
90-
params['pageToken'] = page_token
91-
92-
path = '/projects/%s/managedZones' % (self.project,)
93-
resp = self.connection.api_request(method='GET', path=path,
94-
query_params=params)
95-
zones = [ManagedZone.from_api_repr(resource, self)
96-
for resource in resp['managedZones']]
97-
return zones, resp.get('nextPageToken')
83+
return _ManagedZoneIterator(self, page_token=page_token,
84+
max_results=max_results)
9885

9986
def zone(self, name, dns_name=None, description=None):
10087
"""Construct a zone bound to this client.
@@ -115,3 +102,40 @@ def zone(self, name, dns_name=None, description=None):
115102
"""
116103
return ManagedZone(name, dns_name, client=self,
117104
description=description)
105+
106+
107+
class _ManagedZoneIterator(Iterator):
108+
"""An iterator listing all managed zones.
109+
110+
:type client: :class:`~google.cloud.dns.client.Client`
111+
:param client: The client to use for making connections.
112+
113+
:type page_token: str
114+
:param page_token: (Optional) A token identifying a page in a result set.
115+
116+
:type max_results: int
117+
:param max_results: (Optional) The maximum number of results to fetch.
118+
119+
:type extra_params: dict or ``NoneType``
120+
:param extra_params: Extra query string parameters for the API call.
121+
"""
122+
123+
ITEMS_KEY = 'managedZones'
124+
125+
def __init__(self, client, page_token=None, max_results=None,
126+
extra_params=None):
127+
path = '/projects/%s/managedZones' % (client.project,)
128+
super(_ManagedZoneIterator, self).__init__(
129+
client=client, path=path, page_token=page_token,
130+
max_results=max_results, extra_params=extra_params)
131+
132+
def _item_to_value(self, resource):
133+
"""Convert a JSON managed zone to the native object.
134+
135+
:type resource: dict
136+
:param resource: An item to be converted to a managed zone.
137+
138+
:rtype: :class:`.ManagedZone`
139+
:returns: The next managed zone in the page.
140+
"""
141+
return ManagedZone.from_api_repr(resource, self.client)

dns/google/cloud/dns/zone.py

Lines changed: 98 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
# limitations under the License.
1414

1515
"""Define API ManagedZones."""
16+
1617
import six
1718

1819
from google.cloud._helpers import _rfc3339_to_datetime
1920
from google.cloud.exceptions import NotFound
2021
from google.cloud.dns.changes import Changes
2122
from google.cloud.dns.resource_record_set import ResourceRecordSet
23+
from google.cloud.iterator import Iterator
2224

2325

2426
class ManagedZone(object):
@@ -330,29 +332,13 @@ def list_resource_record_sets(self, max_results=None, page_token=None,
330332
:param client: the client to use. If not passed, falls back to the
331333
``client`` stored on the current zone.
332334
333-
:rtype: tuple, (list, str)
334-
:returns: list of
335-
:class:`~.resource_record_set.ResourceRecordSet`,
336-
plus a "next page token" string: if the token is not None,
337-
indicates that more zones can be retrieved with another
338-
call (pass that value as ``page_token``).
335+
:rtype: :class:`_ResourceRecordSetIterator`
336+
:returns: An iterator of
337+
:class:`~.resource_record_set.ResourceRecordSet` objects.
339338
"""
340-
params = {}
341-
342-
if max_results is not None:
343-
params['maxResults'] = max_results
344-
345-
if page_token is not None:
346-
params['pageToken'] = page_token
347-
348-
path = '/projects/%s/managedZones/%s/rrsets' % (
349-
self.project, self.name)
350-
client = self._require_client(client)
351-
conn = client.connection
352-
resp = conn.api_request(method='GET', path=path, query_params=params)
353-
zones = [ResourceRecordSet.from_api_repr(resource, self)
354-
for resource in resp['rrsets']]
355-
return zones, resp.get('nextPageToken')
339+
return _ResourceRecordSetIterator(
340+
self, page_token=page_token,
341+
max_results=max_results, client=client)
356342

357343
def list_changes(self, max_results=None, page_token=None, client=None):
358344
"""List change sets for this zone.
@@ -373,26 +359,98 @@ def list_changes(self, max_results=None, page_token=None, client=None):
373359
:param client: the client to use. If not passed, falls back to the
374360
``client`` stored on the current zone.
375361
376-
:rtype: tuple, (list, str)
377-
:returns: list of
378-
:class:`~.resource_record_set.ResourceRecordSet`,
379-
plus a "next page token" string: if the token is not None,
380-
indicates that more zones can be retrieved with another
381-
call (pass that value as ``page_token``).
362+
:rtype: :class:`_ChangesIterator`
363+
:returns: An iterator of :class:`~.changes.Changes` objects.
382364
"""
383-
params = {}
365+
return _ChangesIterator(self, page_token=page_token,
366+
max_results=max_results, client=client)
367+
368+
369+
class _ResourceRecordSetIterator(Iterator):
370+
"""An iterator listing all resource record sets.
371+
372+
:type zone: :class:`ManagedZone`
373+
:param zone: The managed zone from which to list resource record sets.
374+
375+
:type page_token: str
376+
:param page_token: (Optional) A token identifying a page in a result set.
384377
385-
if max_results is not None:
386-
params['maxResults'] = max_results
378+
:type max_results: int
379+
:param max_results: (Optional) The maximum number of results to fetch.
387380
388-
if page_token is not None:
389-
params['pageToken'] = page_token
381+
:type extra_params: dict or ``NoneType``
382+
:param extra_params: Extra query string parameters for the API call.
383+
384+
:type client: :class:`~google.cloud.dns.client.Client`
385+
:param client: (Optional) The client to use for making connections.
386+
Defaults to the zone's client.
387+
"""
388+
389+
ITEMS_KEY = 'rrsets'
390+
391+
def __init__(self, zone, page_token=None, max_results=None,
392+
extra_params=None, client=None):
393+
if client is None:
394+
client = zone._client
395+
self.zone = zone
396+
path = '/projects/%s/managedZones/%s/rrsets' % (
397+
zone.project, zone.name)
398+
super(_ResourceRecordSetIterator, self).__init__(
399+
client=client, path=path, page_token=page_token,
400+
max_results=max_results, extra_params=extra_params)
401+
402+
def _item_to_value(self, resource):
403+
"""Convert a JSON resource record set value to the native object.
404+
405+
:type resource: dict
406+
:param resource: An item to be converted to a resource record set.
390407
408+
:rtype: :class:`~.resource_record_set.ResourceRecordSet`
409+
:returns: The next resource record set in the page.
410+
"""
411+
return ResourceRecordSet.from_api_repr(resource, self.zone)
412+
413+
414+
class _ChangesIterator(Iterator):
415+
"""An iterator listing all changes.
416+
417+
:type zone: :class:`ManagedZone`
418+
:param zone: The managed zone from which to list changes.
419+
420+
:type page_token: str
421+
:param page_token: (Optional) A token identifying a page in a result set.
422+
423+
:type max_results: int
424+
:param max_results: (Optional) The maximum number of results to fetch.
425+
426+
:type extra_params: dict or ``NoneType``
427+
:param extra_params: Extra query string parameters for the API call.
428+
429+
:type client: :class:`~google.cloud.dns.client.Client`
430+
:param client: (Optional) The client to use for making connections.
431+
Defaults to the zone's client.
432+
"""
433+
434+
ITEMS_KEY = 'changes'
435+
436+
def __init__(self, zone, page_token=None, max_results=None,
437+
extra_params=None, client=None):
438+
if client is None:
439+
client = zone._client
440+
self.zone = zone
391441
path = '/projects/%s/managedZones/%s/changes' % (
392-
self.project, self.name)
393-
client = self._require_client(client)
394-
conn = client.connection
395-
resp = conn.api_request(method='GET', path=path, query_params=params)
396-
zones = [Changes.from_api_repr(resource, self)
397-
for resource in resp['changes']]
398-
return zones, resp.get('nextPageToken')
442+
zone.project, zone.name)
443+
super(_ChangesIterator, self).__init__(
444+
client=client, path=path, page_token=page_token,
445+
max_results=max_results, extra_params=extra_params)
446+
447+
def _item_to_value(self, resource):
448+
"""Convert a JSON "changes" value to the native object.
449+
450+
:type resource: dict
451+
:param resource: An item to be converted to a "changes".
452+
453+
:rtype: :class:`.Changes`
454+
:returns: The next "changes" in the page.
455+
"""
456+
return Changes.from_api_repr(resource, self.zone)

dns/unit_tests/test_client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ def test_list_zones_defaults(self):
132132
client = self._makeOne(self.PROJECT, creds)
133133
conn = client.connection = _Connection(DATA)
134134

135-
zones, token = client.list_zones()
135+
iterator = client.list_zones()
136+
iterator.update_page()
137+
zones = list(iterator.page)
138+
token = iterator.next_page_token
136139

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

176-
zones, token = client.list_zones(max_results=3, page_token=TOKEN)
179+
iterator = client.list_zones(max_results=3, page_token=TOKEN)
180+
iterator.update_page()
181+
zones = list(iterator.page)
182+
token = iterator.next_page_token
177183

178184
self.assertEqual(len(zones), len(DATA['managedZones']))
179185
for found, expected in zip(zones, DATA['managedZones']):

dns/unit_tests/test_zone.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ def test_list_resource_record_sets_defaults(self):
440440
client = _Client(project=self.PROJECT, connection=conn)
441441
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client)
442442

443-
rrsets, token = zone.list_resource_record_sets()
443+
iterator = zone.list_resource_record_sets()
444+
iterator.update_page()
445+
rrsets = list(iterator.page)
446+
token = iterator.next_page_token
444447

445448
self.assertEqual(len(rrsets), len(DATA['rrsets']))
446449
for found, expected in zip(rrsets, DATA['rrsets']):
@@ -489,8 +492,11 @@ def test_list_resource_record_sets_explicit(self):
489492
client2 = _Client(project=self.PROJECT, connection=conn2)
490493
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client1)
491494

492-
rrsets, token = zone.list_resource_record_sets(
495+
iterator = zone.list_resource_record_sets(
493496
max_results=3, page_token=TOKEN, client=client2)
497+
iterator.update_page()
498+
rrsets = list(iterator.page)
499+
token = iterator.next_page_token
494500

495501
self.assertEqual(len(rrsets), len(DATA['rrsets']))
496502
for found, expected in zip(rrsets, DATA['rrsets']):
@@ -551,7 +557,10 @@ def test_list_changes_defaults(self):
551557
client = _Client(project=self.PROJECT, connection=conn)
552558
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client)
553559

554-
changes, token = zone.list_changes()
560+
iterator = zone.list_changes()
561+
iterator.update_page()
562+
changes = list(iterator.page)
563+
token = iterator.next_page_token
555564

556565
self.assertEqual(len(changes), len(DATA['changes']))
557566
for found, expected in zip(changes, DATA['changes']):
@@ -628,8 +637,11 @@ def test_list_changes_explicit(self):
628637
client2 = _Client(project=self.PROJECT, connection=conn2)
629638
zone = self._makeOne(self.ZONE_NAME, self.DNS_NAME, client1)
630639

631-
changes, token = zone.list_changes(
640+
iterator = zone.list_changes(
632641
max_results=3, page_token=TOKEN, client=client2)
642+
iterator.update_page()
643+
changes = list(iterator.page)
644+
token = iterator.next_page_token
633645

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

0 commit comments

Comments
 (0)