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

ADCM-6115: Implement Service object #12

Merged
merged 10 commits into from
Nov 21, 2024
6 changes: 4 additions & 2 deletions adcm_aio_client/core/objects/_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import deque
from contextlib import suppress
from functools import cached_property
from typing import Any, Self

Expand Down Expand Up @@ -47,8 +48,9 @@ def _construct_child[Child: "InteractiveChildObject"](

def _clear_cache(self: Self) -> None:
for name in self._delete_on_refresh:
# works for cached_property
delattr(self, name)
# Works for cached_property. Suppresses errors on deleting values not yet cached (absent in self.__dict__)
with suppress(AttributeError):
Sealwing marked this conversation as resolved.
Show resolved Hide resolved
delattr(self, name)


class RootInteractiveObject(InteractiveObject):
Expand Down
7 changes: 7 additions & 0 deletions adcm_aio_client/core/objects/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
from typing import Self

from adcm_aio_client.core.objects._base import AwareOfOwnPath, WithRequester
from adcm_aio_client.core.types import ADCMEntityStatus


class Deletable(WithRequester, AwareOfOwnPath):
async def delete(self: Self) -> None:
await self._requester.delete(*self.get_own_path())


class WithStatus(WithRequester, AwareOfOwnPath):
async def get_status(self: Self) -> ADCMEntityStatus:
response = await self._requester.get(*self.get_own_path())
return ADCMEntityStatus(response.as_dict()["status"])


# todo whole section lacking implementation (and maybe code move is required)
class WithConfig(WithRequester, AwareOfOwnPath):
@cached_property
Expand Down
52 changes: 35 additions & 17 deletions adcm_aio_client/core/objects/cm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from enum import Enum
from functools import cached_property
from typing import Self

from adcm_aio_client.core.objects._accessors import (
NonPaginatedChildAccessor,
PaginatedAccessor,
PaginatedChildAccessor,
)
Expand All @@ -14,23 +12,26 @@
WithActions,
WithConfig,
WithConfigGroups,
WithStatus,
WithUpgrades,
)
from adcm_aio_client.core.objects._imports import ClusterImports
from adcm_aio_client.core.objects._mapping import ClusterMapping
from adcm_aio_client.core.types import Endpoint


class ADCMEntityStatus(str, Enum):
UP = "up"
DOWN = "down"
from adcm_aio_client.core.types import ADCMEntityStatus, Endpoint


class Bundle(Deletable, InteractiveObject): ...


class Cluster(
Deletable, WithActions, WithUpgrades, WithConfig, WithActionHostGroups, WithConfigGroups, RootInteractiveObject
WithStatus,
Deletable,
WithActions,
WithUpgrades,
WithConfig,
WithActionHostGroups,
WithConfigGroups,
RootInteractiveObject,
):
PATH_PREFIX = "clusters"
# data-based properties
Expand Down Expand Up @@ -59,11 +60,6 @@ async def bundle(self: Self) -> Bundle:
return self._construct(what=Bundle, from_data=response.as_dict())

# object-specific methods

async def get_status(self: Self) -> ADCMEntityStatus:
response = await self._requester.get(*self.get_own_path())
return ADCMEntityStatus(response.as_dict()["status"])

async def set_ansible_forks(self: Self, value: int) -> Self:
await self._requester.post(
*self.get_own_path(), "ansible-config", data={"config": {"defaults": {"forks": value}}, "adcmMeta": {}}
Expand Down Expand Up @@ -99,9 +95,31 @@ def get_own_path(self: Self) -> Endpoint:
return ("clusters",)


class Service(InteractiveChildObject[Cluster]):
class Service(
WithStatus,
Deletable,
WithActions,
WithConfig,
WithActionHostGroups,
WithConfigGroups,
InteractiveChildObject[Cluster],
):
PATH_PREFIX = "services"

@property
def name(self: Self) -> str:
return self._data["name"]

@property
def display_name(self: Self) -> str:
return self._data["displayName"]

@cached_property
def cluster(self: Self) -> Cluster:
return self._parent

def get_own_path(self: Self) -> Endpoint:
return (*self._parent.get_own_path(), "services", self.id)
return *self._parent.get_own_path(), "services", self.id

@cached_property
def components(self: Self) -> "ComponentsNode":
Expand All @@ -117,7 +135,7 @@ def get_own_path(self: Self) -> Endpoint:
return (*self._parent.get_own_path(), "components", self.id)


class ComponentsNode(NonPaginatedChildAccessor[Service, Component, None]):
class ComponentsNode(PaginatedChildAccessor[Service, Component, None]):
class_type = Component


Expand Down
6 changes: 6 additions & 0 deletions adcm_aio_client/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# limitations under the License.

from dataclasses import asdict, dataclass
from enum import Enum
from typing import Optional, Protocol, Self

# Init / Authorization
Expand Down Expand Up @@ -67,3 +68,8 @@ class WithRequester(Protocol):

class AwareOfOwnPath(Protocol):
def get_own_path(self: Self) -> Endpoint: ...


class ADCMEntityStatus(str, Enum):
UP = "up"
DOWN = "down"