Skip to content

Commit 8358f90

Browse files
rupa deadwylerrupa
authored andcommitted
add a listZones method to "high-level" interface
returns a bunch of "unloaded" Zone objects
1 parent 9ed3cbd commit 8358f90

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

examples/zones.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,28 @@
2121
config = api.config
2222
config["follow_pagination"] = True
2323

24+
######################
25+
# LISTING ZONES #
26+
######################
27+
28+
# The zone list endpoint does not have full information about each zone,
29+
# so the high-level `listZone` method returns a list of "unloaded" Zone
30+
# objects, and you need to call the `.load` method on any that you intend
31+
# use further.
32+
# It's often easier to use the rest interface directly for list() calls,
33+
# and/or skip right to loadZone, but this could be a reasonable method to use
34+
# for actions that require operating on most of all of your zones.
35+
zone_list = api.listZones()
36+
2437
######################
2538
# LOAD / CREATE ZONE #
2639
######################
2740

41+
# get a zone from the list and load it. this is not a practical way to fetch
42+
# a single zone
43+
listed_zone = [x for x in zone_list if x["zone"] == "test.com"][0]
44+
listed_zone.load()
45+
2846
# to load an existing zone, get a Zone object back
2947
test_zone = api.loadZone("test.com")
3048

ns1/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ def apikey(self):
234234
return ns1.rest.apikey.APIKey(self.config)
235235

236236
# HIGH LEVEL INTERFACE
237+
def listZones(self, callback=None, errback=None):
238+
"""
239+
Returns a list of Zone objects for all of your zones. Note that
240+
to avoid needless API traffic, these are NOT "loaded", and you
241+
will need to call the `.load` method on any that you intend to use
242+
further.
243+
244+
:rtype: list of :py:class:`ns1.zones.Zone`
245+
"""
246+
import ns1.zones
247+
248+
zones = ns1.rest.zones.Zones(self.config)
249+
return [ns1.zones.Zone(self.config, z["zone"]) for z in zones.list()]
250+
237251
def loadZone(self, zone, callback=None, errback=None):
238252
"""
239253
Load an existing zone into a high level Zone object.

tests/unit/test_init.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import mock
12
import pytest
23

34
from ns1 import NS1
@@ -18,6 +19,7 @@
1819
from ns1.rest.team import Team
1920
from ns1.rest.user import User
2021
from ns1.rest.zones import Zones
22+
from ns1.zones import Zone
2123

2224

2325
@pytest.fixture
@@ -67,3 +69,28 @@ def test_rest_interface(ns1_config, method, want):
6769
client = NS1(config=ns1_config)
6870
got = getattr(client, method)()
6971
assert isinstance(got, want)
72+
73+
74+
@mock.patch.object(Zone, "load")
75+
@mock.patch.object(Zones, "list")
76+
def test_listZones(zones_list, zone_load, ns1_config):
77+
zones_list.return_value = [{"zone": "a.com"}, {"zone": "b.com"}]
78+
client = NS1(config=ns1_config)
79+
80+
result = client.listZones()
81+
zones_list.assert_called_once_with()
82+
zone_load.assert_not_called()
83+
assert sorted([x.zone for x in result]) == ["a.com", "b.com"]
84+
85+
result[0].load()
86+
zone_load.assert_called_once_with()
87+
88+
89+
@mock.patch.object(Zone, "load")
90+
def test_loadZone(zone_load, ns1_config):
91+
zone_load.return_value = "LOADED_ZONE_OBJECT"
92+
client = NS1(config=ns1_config)
93+
94+
result = client.loadZone("a.com")
95+
zone_load.assert_called_once_with(callback=None, errback=None)
96+
assert result == "LOADED_ZONE_OBJECT"

0 commit comments

Comments
 (0)