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

Zone commands #227

Merged
merged 31 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
39eae19
Zone commands
pederhan May 14, 2024
971e618
Use illegal hack to create zones
pederhan May 14, 2024
c3ad114
Re-add zone_basepath for development
pederhan May 14, 2024
b51e855
Delegation abomination
pederhan May 14, 2024
a02b08a
Make `with_*` methods available outside `Endpoint`
pederhan May 15, 2024
bf8b3e3
Get/create/delete delegations
pederhan May 15, 2024
a828f88
Add Zone.get_delegation_{and,or}_raise`
pederhan May 15, 2024
cdf3774
Remove WithName mixin from `Delegation`
pederhan May 15, 2024
c15d155
Add `zone list`
pederhan May 15, 2024
8db55ea
Move `with_*` methods back into `Endpoints`
pederhan May 15, 2024
4553e0a
Add `zone set_ns`
pederhan May 15, 2024
d940812
Fix `ReverseZoneDelegation.is_reverse()`
pederhan May 15, 2024
7421687
Remove unused Delegation.type_by_name
pederhan May 15, 2024
2cf3c85
Replace `WithName` with `APIMixin`in `Zone`
pederhan May 15, 2024
9dd919e
Fix invalid imports
pederhan May 15, 2024
2bbfbd2
Add `zone info`
pederhan May 15, 2024
157dbc4
Fix incorrect endpoint values, reorder endpoints
pederhan May 16, 2024
eaffe75
Require force for `delegation_create` if zone does not exist
pederhan May 16, 2024
f47adf8
Add `Zone.get_zone_{and,or}_raise`
pederhan May 16, 2024
e72d312
Simplify Zone.delete_delegation
pederhan May 16, 2024
c572961
Add `delegation_list`
pederhan May 16, 2024
3548827
Add `zone set_soa`
pederhan May 16, 2024
470d584
Add `zone delegation_comment_set`
pederhan May 16, 2024
56bd66f
Add `zone delegation_comment_remove`
pederhan May 16, 2024
71f49dd
Add `zone set_default_ttl`
pederhan May 16, 2024
ac33039
Improve TTL printing
pederhan May 16, 2024
571267b
Remove unused zone functions
pederhan May 16, 2024
460e1e6
Change `output_ttl` param order for consistency
pederhan May 16, 2024
7f25bc4
Use `get_by_name*` instead of `get_by_field*`
pederhan May 16, 2024
4afe505
Remove unused imports
pederhan May 16, 2024
ebebe75
Comments, docstrings, CLI messages
pederhan May 19, 2024
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
2 changes: 1 addition & 1 deletion mreg_cli/api/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def delete(self) -> bool:

@classmethod
def create(
cls, params: Mapping[str, str | None], fetch_after_create: bool = True
cls, params: Mapping[str, str | list[str] | None], fetch_after_create: bool = True
) -> Self | None:
"""Create the object.

Expand Down
79 changes: 47 additions & 32 deletions mreg_cli/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,46 @@ def __call__(self, *args: Any, **kwargs: Any):
return self.func(*args, **kwargs)


def with_id(endpoint: str, identity: str | int) -> str:
"""Return the endpoint with an ID."""
id_field = quote(str(identity))

return f"{endpoint}{id_field}"


def with_params(endpoint: str, *params: str | int) -> str:
"""Construct and return an endpoint URL by inserting parameters.

:param endpoint: The endpoint path.
:param params: A sequence of parameters to be inserted into the URL.
:raises ValueError: If the number of provided parameters does not match the
number of placeholders.
:returns: A fully constructed endpoint URL with parameters.
"""
placeholders_count = endpoint.count("{}")
if placeholders_count != len(params):
if isinstance(endpoint, Endpoint):
name = endpoint.name
else:
name = endpoint
raise ValueError(
f"Endpoint {name} expects {placeholders_count} parameters, got {len(params)}."
)
encoded_params = (quote(str(param)) for param in params)
return endpoint.format(*encoded_params)


def with_query(endpoint: str, query: dict[str, str]) -> str:
"""Construct and return an endpoint URL with a query string.

:param endpoint: The endpoint path.
:param query: A dictionary of query parameters.
:returns: A fully constructed endpoint URL with a query string.
"""
query_string = "&".join(f"{quote(key)}={quote(value)}" for key, value in query.items())
return f"{endpoint}?{query_string}"


class Endpoint(str, Enum):
"""API endpoints."""

Expand Down Expand Up @@ -77,8 +117,10 @@ class Endpoint(str, Enum):
PermissionNetgroupRegex = "/api/v1/permissions/netgroupregex/"

ForwardZones = f"{Zones}forward/"
ReverseZones = f"{Zones}reverse/"
ForwardZonesDelegations = f"{ForwardZones}{{}}/delegations/"
ForwardZoneForHost = f"{ForwardZones}hostname/"
ReverseZones = f"{Zones}reverse/"
ReverseZonesDelegations = f"{ReverseZones}{{}}/delegations/"

def __str__(self):
"""Prevent direct usage without parameters where needed."""
Expand Down Expand Up @@ -109,34 +151,7 @@ def external_id_field(self) -> str:
return "host"
return "id"

def with_id(self, identity: str | int) -> str:
"""Return the endpoint with an ID."""
id_field = quote(str(identity))

return f"{self.value}{id_field}"

def with_params(self, *params: str | int) -> str:
"""Construct and return an endpoint URL by inserting parameters.

:param params: A sequence of parameters to be inserted into the URL.
:raises ValueError: If the number of provided parameters does not match the
number of placeholders.
:returns: A fully constructed endpoint URL with parameters.
"""
placeholders_count = self.value.count("{}")
if placeholders_count != len(params):
raise ValueError(
f"{self.name} endpoint expects {placeholders_count} parameters, got {len(params)}."
)

encoded_params = (quote(str(param)) for param in params)
return self.value.format(*encoded_params)

def with_query(self, query: dict[str, str]) -> str:
"""Construct and return an endpoint URL with a query string.

:param query: A dictionary of query parameters.
:returns: A fully constructed endpoint URL with a query string.
"""
query_string = "&".join(f"{quote(key)}={quote(value)}" for key, value in query.items())
return f"{self.value}?{query_string}"
# Add methods via composition
with_id = with_id
with_params = with_params
with_query = with_query
Loading
Loading