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

Permission commands migrated. #228

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions mreg_cli/api/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,15 @@ def get_by_query_unique_and_raise(
return None

@classmethod
def get_by_query_unique(cls, data: dict[str, str]) -> Self:
def get_by_query_unique(cls, data: dict[str, str]) -> Self | None:
"""Get an object with the given data.

:param data: The data to search for.
:returns: The object if found, None otherwise.
"""
obj_dict = get_list_unique(cls.endpoint(), params=data)
if not obj_dict:
raise EntityNotFound(f"{cls.__name__} record for {data} not found.")
return None
return cls(**obj_dict)

def refetch(self) -> Self:
Expand Down
14 changes: 9 additions & 5 deletions mreg_cli/commands/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mreg_cli.api.models import Label, NetworkOrIP, Permission
from mreg_cli.commands.base import BaseCommand
from mreg_cli.commands.registry import CommandRegistry
from mreg_cli.exceptions import EntityNotFound
from mreg_cli.exceptions import DeleteError, EntityNotFound
from mreg_cli.log import cli_info
from mreg_cli.outputmanager import OutputManager
from mreg_cli.types import Flag
Expand Down Expand Up @@ -142,8 +142,11 @@ def network_remove(args: argparse.Namespace) -> None:
"regex": args.regex,
}

Permission.get_by_query_unique(query).delete()
cli_info(f"Removed permission for {args.range}", True)
permission = Permission.get_by_query_unique_or_raise(query)
if permission.delete():
cli_info(f"Removed permission for {args.range}", True)
else:
raise DeleteError(f"Failed to remove permission for {args.range}")


@command_registry.register_command(
Expand All @@ -168,7 +171,7 @@ def add_label_to_permission(args: argparse.Namespace) -> None:
"range": args.range,
"regex": args.regex,
}
permission = Permission.get_by_query_unique(query)
permission = Permission.get_by_query_unique_or_raise(query)
permission.add_label(args.label)
cli_info(f"Added the label {args.label!r} to the permission.", print_msg=True)

Expand All @@ -195,5 +198,6 @@ def remove_label_from_permission(args: argparse.Namespace) -> None:
"range": args.range,
"regex": args.regex,
}
Permission.get_by_query_unique(query).remove_label(args.label)
permission = Permission.get_by_query_unique_or_raise(query)
permission.remove_label(args.label)
terjekv marked this conversation as resolved.
Show resolved Hide resolved
cli_info(f"Removed the label {args.label!r} from the permission.", print_msg=True)
Loading