Skip to content

Commit

Permalink
add new monitoring permissions support
Browse files Browse the repository at this point in the history
  • Loading branch information
hmodi-ns1 committed Jul 19, 2024
1 parent 65954a7 commit 35c15b0
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 134 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.21.0 (July 19th, 2024)

ENHANCEMENTS:
* Adds support for new split monitoring permissions create_jobs, update_jobs and delete_jobs

## 0.20.0 (June 19th, 2024)

ENHANCEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion ns1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
from .config import Config

version = "0.20.0"
version = "0.21.0"


class NS1:
Expand Down
14 changes: 14 additions & 0 deletions ns1/rest/apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def create(self, name, callback=None, errback=None, **kwargs):

self._buildStdBody(body, kwargs)

# Replace `manage_jobs` with the new split permissions
if body["permissions"].get("monitoring", {}).get("manage_jobs", False):
body["permissions"]["monitoring"]["manage_jobs"] = False
body["permissions"]["monitoring"]["create_jobs"] = True
body["permissions"]["monitoring"]["update_jobs"] = True
body["permissions"]["monitoring"]["delete_jobs"] = True

return self._make_request(
"PUT",
"%s" % (self.ROOT),
Expand All @@ -40,6 +47,13 @@ def update(self, apikey_id, callback=None, errback=None, **kwargs):
body = {}
self._buildStdBody(body, kwargs)

# Replace `manage_jobs` with the new split permissions
if body["permissions"].get("monitoring", {}).get("manage_jobs", False):
body["permissions"]["monitoring"]["manage_jobs"] = False
body["permissions"]["monitoring"]["create_jobs"] = True
body["permissions"]["monitoring"]["update_jobs"] = True
body["permissions"]["monitoring"]["delete_jobs"] = True

return self._make_request(
"POST",
"%s/%s" % (self.ROOT, apikey_id),
Expand Down
3 changes: 3 additions & 0 deletions ns1/rest/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
},
"monitoring": {
"manage_jobs": False,
"create_jobs": False,
"update_jobs": False,
"delete_jobs": False,
"manage_lists": False,
"view_jobs": False,
},
Expand Down
14 changes: 14 additions & 0 deletions ns1/rest/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def create(self, name, callback=None, errback=None, **kwargs):

self._buildStdBody(body, kwargs)

# Replace `manage_jobs` with the new split permissions
if body["permissions"].get("monitoring", {}).get("manage_jobs", False):
body["permissions"]["monitoring"]["manage_jobs"] = False
body["permissions"]["monitoring"]["create_jobs"] = True
body["permissions"]["monitoring"]["update_jobs"] = True
body["permissions"]["monitoring"]["delete_jobs"] = True

return self._make_request(
"PUT",
"%s" % (self.ROOT),
Expand All @@ -34,6 +41,13 @@ def update(self, team_id, callback=None, errback=None, **kwargs):
body = {"id": team_id}
self._buildStdBody(body, kwargs)

# Replace `manage_jobs` with the new split permissions
if body["permissions"].get("monitoring", {}).get("manage_jobs", False):
body["permissions"]["monitoring"]["manage_jobs"] = False
body["permissions"]["monitoring"]["create_jobs"] = True
body["permissions"]["monitoring"]["update_jobs"] = True
body["permissions"]["monitoring"]["delete_jobs"] = True

return self._make_request(
"POST",
"%s/%s" % (self.ROOT, team_id),
Expand Down
16 changes: 15 additions & 1 deletion ns1/rest/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class User(resource.BaseResource):
]

def create(
self, name, username, email, callback=None, errback=None, **kwargs
self, name, username, email, callback=None, errback=None, **kwargs
):
body = {"name": name, "username": username, "email": email}

Expand All @@ -33,6 +33,13 @@ def create(

self._buildStdBody(body, kwargs)

# Replace `manage_jobs` with the new split permissions
if body["permissions"].get("monitoring", {}).get("manage_jobs", False):
body["permissions"]["monitoring"]["manage_jobs"] = False
body["permissions"]["monitoring"]["create_jobs"] = True
body["permissions"]["monitoring"]["update_jobs"] = True
body["permissions"]["monitoring"]["delete_jobs"] = True

return self._make_request(
"PUT",
"%s" % (self.ROOT),
Expand All @@ -45,6 +52,13 @@ def update(self, username, callback=None, errback=None, **kwargs):
body = {"username": username}
self._buildStdBody(body, kwargs)

# Replace `manage_jobs` with the new split permissions
if body["permissions"].get("monitoring", {}).get("manage_jobs", False):
body["permissions"]["monitoring"]["manage_jobs"] = False
body["permissions"]["monitoring"]["create_jobs"] = True
body["permissions"]["monitoring"]["update_jobs"] = True
body["permissions"]["monitoring"]["delete_jobs"] = True

return self._make_request(
"POST",
"%s/%s" % (self.ROOT, username),
Expand Down
150 changes: 107 additions & 43 deletions tests/unit/test_apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,54 +45,118 @@ def test_rest_apikey_retrieve(apikey_config, apikey_id, url):
)


@pytest.mark.parametrize("name, url", [("test-apikey", "account/apikeys")])
def test_rest_apikey_create(apikey_config, name, url):
z = ns1.rest.apikey.APIKey(apikey_config)
z._make_request = mock.MagicMock()
z.create(name)
z._make_request.assert_called_once_with(
"PUT",
url,
callback=None,
errback=None,
body={"name": name, "permissions": permissions._default_perms},
)
class TestApiKeyCreate:
@pytest.mark.parametrize("name, url", [("test-apikey", "account/apikeys")])
def test_no_manage_jobs_set(self, apikey_config, name, url):
z = ns1.rest.apikey.APIKey(apikey_config)
z._make_request = mock.MagicMock()
z.create(name)
z._make_request.assert_called_once_with(
"PUT",
url,
callback=None,
errback=None,
body={"name": name, "permissions": permissions._default_perms},
)

@pytest.mark.parametrize("name, url", [("test-apikey", "account/apikeys")])
def test_manage_jobs_set_to_true(self, apikey_config, name, url):
z = ns1.rest.apikey.APIKey(apikey_config)
z._make_request = mock.MagicMock()

@pytest.mark.parametrize(
"apikey_id, name, ip_whitelist, permissions, url",
[
(
"test-apikey_id",
"test-apikey",
["1.1.1.1", "2.2.2.2"],
{"data": {"push_to_datafeeds": True}},
"account/apikeys/test-apikey_id",
z.create(name, permissions={"monitoring": {"manage_jobs": True}})

expected_perms = {"monitoring": {
"manage_jobs": False,
"create_jobs": True,
"update_jobs": True,
"delete_jobs": True,
}}
z._make_request.assert_called_once_with(
"PUT",
url,
callback=None,
errback=None,
body={"name": name, "permissions": expected_perms},
)
],
)
def test_rest_apikey_update(
apikey_config, apikey_id, name, ip_whitelist, permissions, url
):
z = ns1.rest.apikey.APIKey(apikey_config)
z._make_request = mock.MagicMock()
z.update(
apikey_id,
name=name,
ip_whitelist=ip_whitelist,
permissions=permissions,


class TestApiKeyUpdate:
@pytest.mark.parametrize(
"apikey_id, name, ip_whitelist, perms, url",
[
(
"test-apikey_id",
"test-apikey",
["1.1.1.1", "2.2.2.2"],
{"data": {"push_to_datafeeds": True}},
"account/apikeys/test-apikey_id",
)
],
)
z._make_request.assert_called_once_with(
"POST",
url,
callback=None,
errback=None,
body={
"name": name,
"ip_whitelist": ip_whitelist,
"permissions": permissions,
},
def test_no_manage_jobs_set(
self, apikey_config, apikey_id, name, ip_whitelist, perms, url
):
z = ns1.rest.apikey.APIKey(apikey_config)
z._make_request = mock.MagicMock()
z.update(
apikey_id,
name=name,
ip_whitelist=ip_whitelist,
permissions=perms,
)
z._make_request.assert_called_once_with(
"POST",
url,
callback=None,
errback=None,
body={
"name": name,
"ip_whitelist": ip_whitelist,
"permissions": perms,
},
)

@pytest.mark.parametrize(
"apikey_id, name, ip_whitelist, perms, url",
[
(
"test-apikey_id",
"test-apikey",
["1.1.1.1", "2.2.2.2"],
{"monitoring": {"manage_jobs": True}},
"account/apikeys/test-apikey_id",
)
],
)
def test_mange_jobs_set_to_true(
self, apikey_config, apikey_id, name, ip_whitelist, perms, url
):
z = ns1.rest.apikey.APIKey(apikey_config)
z._make_request = mock.MagicMock()
z.update(
apikey_id,
name=name,
ip_whitelist=ip_whitelist,
permissions=perms,
)
expected_perms = {"monitoring": {
"manage_jobs": False,
"create_jobs": True,
"update_jobs": True,
"delete_jobs": True,
}}
z._make_request.assert_called_once_with(
"POST",
url,
callback=None,
errback=None,
body={
"name": name,
"ip_whitelist": ip_whitelist,
"permissions": expected_perms,
},
)


@pytest.mark.parametrize(
Expand Down
Loading

0 comments on commit 35c15b0

Please sign in to comment.