Skip to content

Commit

Permalink
ADCM-6118: Implement Host object (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: astarovo <[email protected]>
  • Loading branch information
2 people authored and a-alferov committed Jan 24, 2025
1 parent 0e2d57d commit b99a45c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
6 changes: 5 additions & 1 deletion adcm_aio_client/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from functools import cached_property
from typing import Self

from adcm_aio_client.core.objects.cm import ClustersNode
from adcm_aio_client.core.objects.cm import ClustersNode, HostsNode
from adcm_aio_client.core.requesters import Requester
from adcm_aio_client.core.types import AuthToken, Cert, Credentials, Verify

Expand All @@ -26,6 +26,10 @@ def __init__(self: Self, requester: Requester) -> None:
def clusters(self: Self) -> ClustersNode:
return ClustersNode(path=(), requester=self._requester)

@cached_property
def hosts(self: Self) -> HostsNode:
return HostsNode(path=(), requester=self._requester)


async def build_client(
url: str | list[str],
Expand Down
14 changes: 13 additions & 1 deletion adcm_aio_client/core/objects/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ class RootInteractiveObject(InteractiveObject):
PATH_PREFIX: str

def get_own_path(self: Self) -> Endpoint:
return self.PATH_PREFIX, self.id
# change here
return self._build_own_path(self.id)

# let's add this one
@classmethod
async def with_id(cls: type[Self], requester: Requester, object_id: int) -> Self:
object_path = cls._build_own_path(object_id)
response = await requester.get(*object_path)
return cls(requester=requester, data=response.as_dict())

@classmethod
def _build_own_path(cls: type[Self], object_id: int) -> Endpoint:
return cls.PATH_PREFIX, object_id


class InteractiveChildObject[Parent](InteractiveObject):
Expand Down
78 changes: 58 additions & 20 deletions adcm_aio_client/core/objects/cm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from enum import Enum
from functools import cached_property
from typing import Literal, Self
from typing import Self

from adcm_aio_client.core.objects._accessors import (
NonPaginatedChildAccessor,
PaginatedAccessor,
PaginatedChildAccessor,
)
from adcm_aio_client.core.objects._base import InteractiveChildObject, InteractiveObject
from adcm_aio_client.core.objects._base import InteractiveChildObject, InteractiveObject, RootInteractiveObject
from adcm_aio_client.core.objects._common import (
Deletable,
WithActionHostGroups,
Expand All @@ -20,15 +21,18 @@
from adcm_aio_client.core.types import Endpoint


class Bundle(Deletable, InteractiveObject): ...
class ADCMEntityStatus(str, Enum):
UP = "up"
DOWN = "down"


class Host(Deletable, InteractiveObject): ...
class Bundle(Deletable, InteractiveObject): ...


class Cluster(
Deletable, WithActions, WithUpgrades, WithConfig, WithActionHostGroups, WithConfigGroups, InteractiveObject
Deletable, WithActions, WithUpgrades, WithConfig, WithActionHostGroups, WithConfigGroups, RootInteractiveObject
):
PATH_PREFIX = "clusters"
# data-based properties

@property
Expand Down Expand Up @@ -56,9 +60,9 @@ async def bundle(self: Self) -> Bundle:

# object-specific methods

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

async def set_ansible_forks(self: Self, value: int) -> Self:
await self._requester.post(
Expand All @@ -85,7 +89,7 @@ def imports(self: Self) -> ClusterImports:
return ClusterImports()

def get_own_path(self: Self) -> Endpoint:
return "clusters", self.id
return self.PATH_PREFIX, self.id


class ClustersNode(PaginatedAccessor[Cluster, None]):
Expand All @@ -95,15 +99,7 @@ def get_own_path(self: Self) -> Endpoint:
return ("clusters",)


class HostsInClusterNode(PaginatedAccessor[Host, None]):
class_type = Host


class Service(InteractiveChildObject[Cluster]):
@property
def id(self: Self) -> int:
return int(self._data["id"])

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

Expand All @@ -117,13 +113,55 @@ class ServicesNode(PaginatedChildAccessor[Cluster, Service, None]):


class Component(InteractiveChildObject[Service]):
@property
def id(self: Self) -> int:
return int(self._data["id"])

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


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


class HostProvider(Deletable, WithActions, WithUpgrades, WithConfig, RootInteractiveObject):
PATH_PREFIX = "hostproviders"


class HostProvidersNode(PaginatedChildAccessor): ...


class Host(Deletable, RootInteractiveObject):
PATH_PREFIX = "hosts"

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

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

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

@cached_property
async def cluster(self: Self) -> Cluster | None:
if not self._data["cluster"]:
return None
return await Cluster.with_id(requester=self._requester, object_id=self._data["cluster"]["id"])

@cached_property
async def hostprovider(self: Self) -> HostProvider:
return await HostProvider.with_id(requester=self._requester, object_id=self._data["hostprovider"]["id"])

def get_own_path(self: Self) -> Endpoint:
return self.PATH_PREFIX, self.id


class HostsNode(PaginatedAccessor[Host, None]):
class_type = Host

# TODO: define def __init__(self, hostprovider: Hostprovider, name: str, cluster: Cluster = None): ...


class HostsInClusterNode(PaginatedAccessor[Host, None]):
class_type = Host

0 comments on commit b99a45c

Please sign in to comment.