Skip to content

Commit

Permalink
kernelci.cli: add kci user group delete command
Browse files Browse the repository at this point in the history
Add a command to delete a user group. Only admin
users will be allowed to perform the delete operation.
Implement `Base._delete` method to send HTTP delete
requests.

Signed-off-by: Jeny Sadadia <[email protected]>
  • Loading branch information
Jeny Sadadia committed Nov 28, 2023
1 parent 75b310d commit df7c80e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions kernelci/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ def _get_paginated(self, input_params, path, offset=None, limit=None):
offset += limit
return objs

def _delete(self, path):
url = self.make_url(path)
resp = requests.delete(
url, headers=self.data.headers,
timeout=self.data.timeout
)
resp.raise_for_status()
return resp


class API(abc.ABC, Base): # pylint: disable=too-many-public-methods
"""KernelCI API Python bindings abstraction"""
Expand Down Expand Up @@ -240,6 +249,10 @@ def get_groups(
def create_group(self, name: str) -> dict:
"""Create a new group"""

@abc.abstractmethod
def delete_group(self, group_id: str):
"""Delete a new group"""

# -------------
# User accounts
# -------------
Expand Down
3 changes: 3 additions & 0 deletions kernelci/api/latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def update_password(self, username: str, current_password: str,
def create_group(self, name: str) -> dict:
return self._post('group', {"name": name}).json()

def delete_group(self, group_id: str):
return self._delete(f'group/{group_id}')


def get_api(config, token):
"""Get an API object for the latest version"""
Expand Down
16 changes: 16 additions & 0 deletions kernelci/cli/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,19 @@ def leave(name, config, api, secrets):
if name in groups:
groups.remove(name)
api.update_user({"groups": groups}, None)


@user_group.command(secrets=True)
@click.argument('name')
@Args.config
@Args.api
@catch_http_error
def delete(name, config, api, secrets):
"""Delete a user group (admin only)"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
groups = api.get_groups({"name": name})
if not groups:
raise click.ClickException(f"Group not found: {name}")
api.delete_group(groups[0]['id'])

0 comments on commit df7c80e

Please sign in to comment.