Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Authorize CSC connection #157

Merged
merged 3 commits into from
Aug 25, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions manager/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,6 @@ def get_queryset(self):
| Q(authorized_users__icontains=self.request.user.username)
)

def query_authorize_csc(self, request_data):
url = f"http://{os.environ.get('AUTHORIZE_HOSTNAME')}/manager-connection/"
response = requests.post(url, json=json.dumps(request_data))
return Response(response.json(), status=response.status_code)

@swagger_auto_schema(responses={201: CSCAuthorizationRequestSerializer(many=True)})
def create(self, request, *args, **kwargs):
created_authorizations = []
Expand Down Expand Up @@ -839,7 +834,9 @@ def create(self, request, *args, **kwargs):
authorization_self_remove_obj.resolved_by = request.user
authorization_self_remove_obj.resolved_at = timezone.now()
authorization_self_remove_obj.save()
# authorize_csc_response = self.query_authorize_csc()
query_authorize_csc(
CSCAuthorizationRequestSerializer(authorization_self_remove_obj).data
)
created_authorizations.append(authorization_self_remove_obj)

new_authorized_users = request.data.get("authorized_users").split(",")
Expand All @@ -851,16 +848,19 @@ def create(self, request, *args, **kwargs):
or authorization_obj.unauthorized_cscs != ""
):
authorization_obj.save()
# if authorization_obj.status == "Authorized":
# authorize_csc_response = self.query_authorize_csc()
created_authorizations.append(authorization_obj)

if authorization_obj.duration and int(authorization_obj.duration) > 0:
authlist_revert_authorization_task(
CSCAuthorizationRequestSerializer(authorization_obj).data,
schedule=int(authorization_obj.duration) * 60,
if authorization_obj.status == "Authorized":
query_authorize_csc(
CSCAuthorizationRequestSerializer(authorization_obj).data
)

if authorization_obj.duration and int(authorization_obj.duration) > 0:
authlist_revert_authorization_task(
CSCAuthorizationRequestSerializer(authorization_obj).data,
schedule=(int(authorization_obj.duration) * 60) - 5,
)

if len(created_authorizations) > 0:
return Response(
CSCAuthorizationRequestSerializer(
Expand All @@ -886,12 +886,14 @@ def update(self, request, *args, **kwargs):
updated_instance.resolved_by = request.user
updated_instance.resolved_at = timezone.now()
updated_instance.save()
# authorize_csc_response = self.query_authorize_csc()
query_authorize_csc(
CSCAuthorizationRequestSerializer(updated_instance).data
)

if updated_instance.duration and int(updated_instance.duration) > 0:
authlist_revert_authorization_task(
CSCAuthorizationRequestSerializer(updated_instance).data,
schedule=int(updated_instance.duration) * 60,
schedule=(int(updated_instance.duration) * 60) - 5,
)

return Response(
Expand All @@ -900,22 +902,39 @@ def update(self, request, *args, **kwargs):
return Response({"error": "Bad request"}, status=status.HTTP_400_BAD_REQUEST)


def query_authorize_csc(authorization_dict):
cmd_payload = {
"csc": "Authorize",
"salindex": 0,
"cmd": "cmd_requestAuthorization",
"params": {
"cscsToChange": authorization_dict["cscs_to_change"],
"authorizedUsers": authorization_dict["authorized_users"],
"nonAuthorizedCSCs": authorization_dict["unauthorized_cscs"],
},
}

url = f"http://{os.environ.get('COMMANDER_HOSTNAME')}:{os.environ.get('COMMANDER_PORT')}/cmd"
response = requests.post(url, json=cmd_payload)
return Response(response.json(), status=response.status_code)


@background(schedule=60)
def authlist_revert_authorization_task(request):
def authlist_revert_authorization_task(authorization_dict):
new_authorized_users = (
request["authorized_users"]
authorization_dict["authorized_users"]
.replace("+", "[plus]")
.replace("-", "[minus]")
.replace("[plus]", "-")
.replace("[minus]", "+")
)
new_unauthorized_cscs = (
request["unauthorized_cscs"]
authorization_dict["unauthorized_cscs"]
.replace("+", "[plus]")
.replace("-", "[minus]")
.replace("[plus]", "-")
.replace("[minus]", "+")
)
request["authorized_users"] = new_authorized_users
request["unauthorized_cscs"] = new_unauthorized_cscs
# self.query_authorize_csc(request)
authorization_dict["authorized_users"] = new_authorized_users
authorization_dict["unauthorized_cscs"] = new_unauthorized_cscs
query_authorize_csc(authorization_dict)