Skip to content

Commit

Permalink
Merge pull request #2560 from dhermes/dns-iterators-alt
Browse files Browse the repository at this point in the history
Moving to iterators in DNS.
  • Loading branch information
dhermes authored Oct 19, 2016
2 parents b402216 + ea9c653 commit 3d28a5c
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 136 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
Loading

0 comments on commit 3d28a5c

Please sign in to comment.