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

[RHCLOUD-37936] refactored /principals/ endpoint by splitting it into separate methods #1516

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

petracihalova
Copy link
Contributor

@petracihalova petracihalova commented Feb 17, 2025

Link(s) to Jira

Description of Intent of Change(s)

motivation: some cleanup before I will start the work on the adding principal_type=all query param option to list both principal types with GET /groups/<uuid>/principals/ endpoint

Refactored /principals/ endpoint by splitting it into separate methods

  • get_principals for retrieving group principals (GET)
  • add_principal for adding a principal to a group (POST)
  • remove_principal for removing a principal from a group (DELETE)

the principals metod started to be huge and complicated

def principals(self, request: Request, uuid: Optional[UUID] = None):
    if request.method == "POST":
        ... logic for POST
    elif request.method == "GET":
        ... logic for GET
    else:
        ... logic for DELETE
    return response

so I created new methods for each HTTP method and then redirected the requests from principals()

@action(detail=True, methods=["get", "post", "delete"])
def principals(self, request: Request, uuid: Optional[UUID] = None):
    """Alias for individual methods based on the HTTP method."""
    if request.method == "GET":
        return self.get_principals(request, uuid)
    elif request.method == "POST":
        return self.add_principal(request, uuid)
    elif request.method == "DELETE":
        return self.remove_principal(request, uuid)
    return Response({"error": "Method not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

def get_principals(self, request: Request, uuid: Optional[UUID] = None):
    """Get principals from a group."""
   ... logic for GET

def add_principal(self, request: Request, uuid: Optional[UUID] = None):
    """Add a principal to a group."""
    ... logic for POST

def remove_principal(self, request: Request, uuid: Optional[UUID] = None):
    """Remove a principal from a group."""
    ... logic for DELETE

there is no need to add tests or update open api spec because there is no logic change

Local Testing

run the unit tests

petracihalova and others added 2 commits February 17, 2025 14:27
- `get_principals` for retrieving group principals (GET)
- `add_principal` for adding a principal to a group (POST)
- `remove_principal` for removing a principal from a group (DELETE)
Copy link
Contributor

@Ellen-Yi-Dong Ellen-Yi-Dong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Locally tested with unit tests as mentioned in description and everything worked as expected on my end!!

@petracihalova
Copy link
Contributor Author

/retest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants