Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 18, 2023
1 parent c0830ea commit 25c9b38
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
53 changes: 53 additions & 0 deletions dsp_permissions_scripts/ap/ap_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import warnings
from urllib.parse import quote_plus

import requests

from dsp_permissions_scripts.ap.ap_model import Ap
from dsp_permissions_scripts.models.api_error import ApiError
from dsp_permissions_scripts.utils.authentication import get_protocol
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.try_request import http_call_with_retry

logger = get_logger(__name__)


def _delete_ap_on_server(
ap: Ap,
host: str,
token: str,
) -> None:
headers = {"Authorization": f"Bearer {token}"}
ap_iri = quote_plus(ap.iri, safe="")
protocol = get_protocol(host)
url = f"{protocol}://{host}/admin/permissions/{ap_iri}"
response = http_call_with_retry(
action=lambda: requests.delete(url, headers=headers, timeout=10),
err_msg=f"Could not delete Administrative Permission {ap.iri}",
)
if response.status_code != 200:
raise ApiError(f"Could not delete Administrative Permission {ap.iri}", response.text, response.status_code)


def delete_ap_of_group_on_server(
host: str,
token: str,
existing_aps: list[Ap],
forGroup: str,
) -> list[Ap]:
aps_to_delete = [ap for ap in existing_aps if ap.forGroup == forGroup]
if not aps_to_delete:
logger.warning(f"There are no APs to delete on {host} for group {forGroup}")
warnings.warn(f"There are no APs to delete on {host} for group {forGroup}")
return existing_aps
print(f"Deleting the Administrative Permissions for group {forGroup} on server {host}")
logger.info(f"Deleting the Administrative Permissions for group {forGroup} on server {host}")
for ap in aps_to_delete:
_delete_ap_on_server(
ap=ap,
host=host,
token=token,
)
existing_aps.remove(ap)
logger.info(f"Deleted Administrative Permission {ap.iri} on host {host}")
return existing_aps
41 changes: 0 additions & 41 deletions dsp_permissions_scripts/ap/ap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@
logger = get_logger(__name__)


def _delete_ap_on_server(
ap: Ap,
host: str,
token: str,
) -> None:
headers = {"Authorization": f"Bearer {token}"}
ap_iri = quote_plus(ap.iri, safe="")
protocol = get_protocol(host)
url = f"{protocol}://{host}/admin/permissions/{ap_iri}"
response = http_call_with_retry(
action=lambda: requests.delete(url, headers=headers, timeout=10),
err_msg=f"Could not delete Administrative Permission {ap.iri}",
)
if response.status_code != 200:
raise ApiError(f"Could not delete Administrative Permission {ap.iri}", response.text, response.status_code)


def _update_ap_on_server(
ap: Ap,
host: str,
Expand Down Expand Up @@ -82,27 +65,3 @@ def apply_updated_aps_on_server(
except ApiError as err:
logger.error(err)
warnings.warn(err.message)


def delete_ap_of_group_on_server(
host: str,
token: str,
existing_aps: list[Ap],
forGroup: str,
) -> list[Ap]:
aps_to_delete = [ap for ap in existing_aps if ap.forGroup == forGroup]
if not aps_to_delete:
logger.warning(f"There are no APs to delete on {host} for group {forGroup}")
warnings.warn(f"There are no APs to delete on {host} for group {forGroup}")
return existing_aps
print(f"Deleting the Administrative Permissions for group {forGroup} on server {host}")
logger.info(f"Deleting the Administrative Permissions for group {forGroup} on server {host}")
for ap in aps_to_delete:
_delete_ap_on_server(
ap=ap,
host=host,
token=token,
)
existing_aps.remove(ap)
logger.info(f"Deleted Administrative Permission {ap.iri} on host {host}")
return existing_aps

0 comments on commit 25c9b38

Please sign in to comment.