Skip to content

Commit f96e177

Browse files
authored
INT-131: support monitoring regions and jobtypes endpoint (#55)
* INT-131: support monitoring regions and jobtypes endpoint Make separate classes for JobTypes and Regions, with list methods, rather than trying to shoehorn them in to the Monitors class and get fancy with properties to delineate these methods from the standard CRUD methods of another resource. * Add endpoints to "high level interface": * monitoring_jobtypes: returns ns1.rest.monitoring.JobTypes * monitoring_regions: returns ns1.rest.monitoring.Regions Also started some tests for ns1/__init__
1 parent b751023 commit f96e177

4 files changed

Lines changed: 255 additions & 0 deletions

File tree

ns1/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ def notifylists(self):
173173

174174
return ns1.rest.monitoring.NotifyLists(self.config)
175175

176+
def monitoring_jobtypes(self):
177+
"""
178+
Return a new raw REST interface to monitoring jobtypes resources
179+
180+
:rtype: :py:class:`ns1.rest.monitoring.JobTypes`
181+
"""
182+
import ns1.rest.monitoring
183+
184+
return ns1.rest.monitoring.JobTypes(self.config)
185+
186+
def monitoring_regions(self):
187+
"""
188+
Return a new raw REST interface to monitoring regions resources
189+
190+
:rtype: :py:class:`ns1.rest.monitoring.Regions`
191+
"""
192+
import ns1.rest.monitoring
193+
194+
return ns1.rest.monitoring.Regions(self.config)
195+
176196
def plan(self):
177197
"""
178198
Return a new raw REST interface to account plan

ns1/rest/monitoring.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,25 @@ def delete(self, nlid, callback=None, errback=None):
113113
callback=callback,
114114
errback=errback,
115115
)
116+
117+
118+
class JobTypes(resource.BaseResource):
119+
120+
ROOT = "monitoring/jobtypes"
121+
PASSTHRU_FIELDS = []
122+
123+
def list(self, callback=None, errback=None):
124+
return self._make_request(
125+
"GET", self.ROOT, callback=callback, errback=errback,
126+
)
127+
128+
129+
class Regions(resource.BaseResource):
130+
131+
ROOT = "monitoring/regions"
132+
PASSTHRU_FIELDS = []
133+
134+
def list(self, callback=None, errback=None):
135+
return self._make_request(
136+
"GET", self.ROOT, callback=callback, errback=errback,
137+
)

tests/unit/test_init.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import pytest
2+
3+
from ns1 import NS1
4+
from ns1.rest.account import Plan
5+
from ns1.rest.apikey import APIKey
6+
from ns1.rest.data import Feed, Source
7+
from ns1.rest.ipam import (
8+
Addresses,
9+
Networks,
10+
Reservations,
11+
Scopegroups,
12+
Scopes,
13+
Optiondefs,
14+
)
15+
from ns1.rest.monitoring import JobTypes, Monitors, NotifyLists, Regions
16+
from ns1.rest.records import Records
17+
from ns1.rest.stats import Stats
18+
from ns1.rest.team import Team
19+
from ns1.rest.user import User
20+
from ns1.rest.zones import Zones
21+
22+
23+
@pytest.fixture
24+
def ns1_config(config):
25+
config.loadFromDict(
26+
{
27+
"endpoint": "api.nsone.net",
28+
"default_key": "test1",
29+
"keys": {
30+
"test1": {
31+
"key": "key-1",
32+
"desc": "test key number 1",
33+
"writeLock": True,
34+
}
35+
},
36+
}
37+
)
38+
39+
return config
40+
41+
42+
@pytest.mark.parametrize(
43+
"method, want",
44+
[
45+
("zones", Zones),
46+
("records", Records),
47+
("addresses", Addresses),
48+
("networks", Networks),
49+
("scope_groups", Scopegroups),
50+
("reservations", Reservations),
51+
("scopes", Scopes),
52+
("optiondefs", Optiondefs),
53+
("stats", Stats),
54+
("datasource", Source),
55+
("datafeed", Feed),
56+
("monitors", Monitors),
57+
("notifylists", NotifyLists),
58+
("monitoring_jobtypes", JobTypes),
59+
("monitoring_regions", Regions),
60+
("plan", Plan),
61+
("team", Team),
62+
("user", User),
63+
("apikey", APIKey),
64+
],
65+
)
66+
def test_rest_interface(ns1_config, method, want):
67+
client = NS1(config=ns1_config)
68+
got = getattr(client, method)()
69+
assert isinstance(got, want)

tests/unit/test_monitoring.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import ns1.rest.monitoring
2+
import pytest
3+
4+
try: # Python 3.3 +
5+
import unittest.mock as mock
6+
except ImportError:
7+
import mock
8+
9+
10+
@pytest.fixture
11+
def monitoring_config(config):
12+
config.loadFromDict(
13+
{
14+
"endpoint": "api.nsone.net",
15+
"default_key": "test1",
16+
"keys": {
17+
"test1": {
18+
"key": "key-1",
19+
"desc": "test key number 1",
20+
"writeLock": True,
21+
}
22+
},
23+
}
24+
)
25+
26+
return config
27+
28+
29+
@pytest.mark.parametrize(
30+
"op, args, method, url, kwargs",
31+
[
32+
(
33+
"list",
34+
None,
35+
"GET",
36+
"monitoring/jobs",
37+
{"callback": None, "errback": None},
38+
),
39+
(
40+
"create",
41+
[{}],
42+
"PUT",
43+
"monitoring/jobs",
44+
{"body": {}, "callback": None, "errback": None},
45+
),
46+
(
47+
"retrieve",
48+
["my-job-id"],
49+
"GET",
50+
"monitoring/jobs/my-job-id",
51+
{"callback": None, "errback": None},
52+
),
53+
(
54+
"update",
55+
["my-job-id", {}],
56+
"POST",
57+
"monitoring/jobs/my-job-id",
58+
{"body": {}, "callback": None, "errback": None},
59+
),
60+
(
61+
"delete",
62+
["my-job-id"],
63+
"DELETE",
64+
"monitoring/jobs/my-job-id",
65+
{"callback": None, "errback": None},
66+
),
67+
],
68+
)
69+
def test_rest_monitoring_monitors(
70+
monitoring_config, op, args, method, url, kwargs
71+
):
72+
m = ns1.rest.monitoring.Monitors(monitoring_config)
73+
m._make_request = mock.MagicMock()
74+
operation = getattr(m, op)
75+
if args is not None:
76+
operation(*args)
77+
else:
78+
operation()
79+
m._make_request.assert_called_once_with(method, url, **kwargs)
80+
81+
82+
@pytest.mark.parametrize(
83+
"op, args, method, url, kwargs",
84+
[
85+
("list", None, "GET", "lists", {"callback": None, "errback": None}),
86+
(
87+
"create",
88+
[{}],
89+
"PUT",
90+
"lists",
91+
{"body": {}, "callback": None, "errback": None},
92+
),
93+
(
94+
"retrieve",
95+
["my-list-id"],
96+
"GET",
97+
"lists/my-list-id",
98+
{"callback": None, "errback": None},
99+
),
100+
(
101+
"update",
102+
["my-list-id", {}],
103+
"POST",
104+
"lists/my-list-id",
105+
{"body": {}, "callback": None, "errback": None},
106+
),
107+
(
108+
"delete",
109+
["my-list-id"],
110+
"DELETE",
111+
"lists/my-list-id",
112+
{"callback": None, "errback": None},
113+
),
114+
],
115+
)
116+
def test_rest_monitoring_notifylists(
117+
monitoring_config, op, args, method, url, kwargs
118+
):
119+
m = ns1.rest.monitoring.NotifyLists(monitoring_config)
120+
m._make_request = mock.MagicMock()
121+
operation = getattr(m, op)
122+
if args is not None:
123+
operation(*args)
124+
else:
125+
operation()
126+
m._make_request.assert_called_once_with(method, url, **kwargs)
127+
128+
129+
def test_rest_monitoring_jobtypes(monitoring_config):
130+
m = ns1.rest.monitoring.JobTypes(monitoring_config)
131+
m._make_request = mock.MagicMock()
132+
m.list()
133+
m._make_request.assert_called_once_with(
134+
"GET", "monitoring/jobtypes", callback=None, errback=None
135+
)
136+
137+
138+
def test_rest_monitoring_regions(monitoring_config):
139+
m = ns1.rest.monitoring.Regions(monitoring_config)
140+
m._make_request = mock.MagicMock()
141+
m.list()
142+
m._make_request.assert_called_once_with(
143+
"GET", "monitoring/regions", callback=None, errback=None
144+
)

0 commit comments

Comments
 (0)