Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- `APIMixin.endpoint_with_id` property for getting the endpoint for the object with its ID, used for operations on the object itself (PATCH, DELETE, etc.). Used by default in `APIMixin.patch()` and `APIMixin.delete()`. Subclasses can override `endpoint_with_id` to provide custom endpoint resolution for these operations.

### Removed

- `Community.patch()`. Now uses `APIMixin.patch()`.
- `Community.delete()`. Now uses `APIMixin.delete()`.

### Deprecated

- `APIMixin.patch()` parameter `validate`. Has no effect and will be removed in a future release.
Expand Down
14 changes: 11 additions & 3 deletions mreg_api/models/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,17 @@ def id_for_endpoint(self) -> int | str:
@classmethod
@abstractmethod
def endpoint(cls) -> Endpoint:
"""Return the endpoint for the method."""
"""Return the endpoint for the resource."""
raise NotImplementedError("You must define an endpoint.")

@property
def endpoint_with_id(self) -> str:
"""Return the endpoint for the object with its ID.

Used for operations on the object itself (PATCH, DELETE, etc.).
"""
return self.endpoint().with_id(self.id_for_endpoint())

@classmethod
def get(cls, _id: int) -> Self | None:
"""Get an object.
Expand Down Expand Up @@ -481,7 +489,7 @@ def patch(
"""
from mreg_api.client import MregClient # noqa: PLC0415

MregClient().patch(self.endpoint().with_id(self.id_for_endpoint()), json=data, params=params)
MregClient().patch(self.endpoint_with_id, json=data, params=params)
new_object = self.refetch()

return new_object
Expand All @@ -494,7 +502,7 @@ def delete(self) -> bool:
"""
from mreg_api.client import MregClient # noqa: PLC0415

response = MregClient().delete(self.endpoint().with_id(self.id_for_endpoint()))
response = MregClient().delete(self.endpoint_with_id)

if response and response.is_success:
return True
Expand Down
37 changes: 1 addition & 36 deletions mreg_api/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,7 @@ def endpoint(cls) -> Endpoint:
return Endpoint.NetworkCommunity

@property
@override
def endpoint_with_id(self) -> str:
"""Return the endpoint with the community ID."""
return self.endpoint().with_params(self.network_address, self.id)
Expand All @@ -2088,42 +2089,6 @@ def refetch(self) -> Self:

return MregClient().get_typed(self.endpoint_with_id, self.__class__)

@override
def patch(
self,
data: JsonMapping,
*,
params: QueryParams | None = None,
validate: bool | None = None, # noqa: ARG002
) -> Self:
"""Patch the community.

Args:
data: The data to patch.
params: Optional query parameters.
validate: Whether to validate the response. (Deprecated and ignored)

Returns:
The updated Community object.
"""
from mreg_api.client import MregClient # noqa: PLC0415

try:
MregClient().patch(self.endpoint_with_id, json=data, params=params)
except PatchError as e:
# TODO: implement after mreg-cli parity
# raise PatchError(f"Failed to patch community {self.name!r}", e.response) from e
raise e
new_object = self.refetch()
return new_object

def delete(self) -> bool:
"""Delete the community."""
from mreg_api.client import MregClient # noqa: PLC0415

resp = MregClient().delete(self.endpoint_with_id)
return resp.is_success if resp else False

def get_hosts(self) -> list[Host]:
"""Get a list of hosts in the community.

Expand Down
Loading