Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
#5 Update UP Restrictions
Browse files Browse the repository at this point in the history
Signed-off-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
uilianries committed Jul 13, 2019
1 parent e0d078f commit 5ef8e1e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
53 changes: 53 additions & 0 deletions bintray/bintray.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,3 +967,56 @@ def get_ip_restrictions(self, subject, repo):
response = self._requester.get(url)
self._logger.info("Get successfully")
return response

def set_ip_restrictions(self, subject, repo, white_cidrs=None, black_cidrs=None):
""" Update ip restrictions with the given white list and black list of CIDRs.
:param subject: repository owner
:param repo: repository name
:param white_cidrs: white list for CIDRs
:param black_cidrs: black list for CIDRs
:return: request response
"""
url = "{}/repos/{}/{}/ip_restrictions".format(Bintray.BINTRAY_URL, subject, repo)
json_data = {}
if isinstance(white_cidrs, list):
json_data["white_cidrs"] = white_cidrs
if isinstance(black_cidrs, list):
json_data["black_cidrs"] = black_cidrs
if not json_data:
raise ValueError("At lease one parameter must be filled.")

response = self._requester.put(url, json=json_data)
self._logger.info("Set successfully")
return response

def update_ip_restrictions(self, subject, repo, add_white_cidrs=None, rm_white_cidrs=None,
add_black_cidrs=None, rm_black_cidrs=None):
""" Add or remove CIDRs from black/white list restrictions.
:param subject: repository owner
:param repo: repository name
:param add_white_cidrs: CIDRs to be added in the white list
:param rm_white_cidrs: CIDRs to be removed from the white list
:param add_black_cidrs: CIDRs to be added in the black list
:param rm_black_cidrs: CIDRs to be removed from the black list
:return: request response
"""
url = "{}/repos/{}/{}/ip_restrictions".format(Bintray.BINTRAY_URL, subject, repo)
json_data = {}
if isinstance(add_white_cidrs, list):
json_data["add"] = {"white_cidrs": add_white_cidrs}
if isinstance(add_black_cidrs, list):
json_data["add"] = {"black_cidrs": add_black_cidrs}

if isinstance(rm_white_cidrs, list):
json_data["remove"] = {"white_cidrs": rm_white_cidrs}
if isinstance(rm_black_cidrs, list):
json_data["remove"] = {"black_cidrs": rm_black_cidrs}

if not json_data:
raise ValueError("At lease one parameter must be filled.")

response = self._requester.patch(url, json=json_data)
self._logger.info("Update successfully")
return response
38 changes: 38 additions & 0 deletions tests/test_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,41 @@ def test_get_ip_restrictions():
'hasWritePermission': None,
'statusCode': 200,
'white_cidrs': []} == response


def test_set_ip_restrictions():
bintray = Bintray()
error_message = ""
try:
bintray.set_ip_restrictions("uilianries", "generic", white_cidrs=["10.0.0.1/32"],
black_cidrs=["192.168.0.1/32"])
except Exception as error:
error_message = str(error)
assert "Could not PUT (403): 403 Client Error: Forbidden for url: " \
"https://api.bintray.com/repos/uilianries/generic/ip_restrictions" == error_message

try:
bintray.set_ip_restrictions("uilianries", "generic")
except Exception as error:
error_message = str(error)
assert "At lease one parameter must be filled." == error_message


def test_update_ip_restrictions():
bintray = Bintray()
error_message = ""
try:
bintray.update_ip_restrictions("uilianries", "generic", add_white_cidrs=["10.0.0.1/32"],
rm_white_cidrs=["10.0.0.2/32"],
add_black_cidrs=["192.168.0.1/32"],
rm_black_cidrs=["192.168.0.2/32"])
except Exception as error:
error_message = str(error)
assert "Could not PATCH (403): 403 Client Error: Forbidden for url: " \
"https://api.bintray.com/repos/uilianries/generic/ip_restrictions" == error_message

try:
bintray.update_ip_restrictions("uilianries", "generic")
except Exception as error:
error_message = str(error)
assert "At lease one parameter must be filled." == error_message

0 comments on commit 5ef8e1e

Please sign in to comment.