-
Notifications
You must be signed in to change notification settings - Fork 1
use simplyblock v2 API to contact storage backend #292
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
| from uuid import UUID | ||
|
|
||
| if TYPE_CHECKING: | ||
| import httpx | ||
|
|
||
| SIMPLYBLOCK_API_TIMEOUT_SECONDS = 10.0 | ||
| SIMPLYBLOCK_STORAGE_POOL_NAME = "testing1" | ||
|
|
||
|
|
||
| class SimplyblockApi: | ||
| def __init__( | ||
| self, | ||
| client: httpx.AsyncClient, | ||
| endpoint: str, | ||
| cluster_id: str, | ||
| cluster_secret: str, | ||
| ) -> None: | ||
| self._client = client | ||
| self._endpoint = endpoint.rstrip("/") | ||
| self._cluster_id = cluster_id | ||
| self._cluster_secret = cluster_secret | ||
| self._pool_id_cache: dict[str, UUID] = {} | ||
|
|
||
| @property | ||
| def _cluster_base(self) -> str: | ||
| return f"{self._endpoint}/api/v2/clusters/{self._cluster_id}" | ||
|
|
||
| def _headers(self) -> dict[str, str]: | ||
| return { | ||
| "Authorization": f"Bearer {self._cluster_secret}", | ||
| "Accept": "application/json", | ||
| } | ||
|
|
||
| async def _cluster_pool_base(self) -> str: | ||
| pool_id = await self.pool_id() | ||
| return f"{self._endpoint}/api/v2/clusters/{self._cluster_id}/storage-pools/{pool_id}" | ||
|
|
||
| async def pool(self, name: str = SIMPLYBLOCK_STORAGE_POOL_NAME) -> dict[str, Any]: | ||
| url = f"{self._cluster_base}/storage-pools/" | ||
| response = await self._client.get(url, headers=self._headers(), timeout=SIMPLYBLOCK_API_TIMEOUT_SECONDS) | ||
| response.raise_for_status() | ||
|
|
||
| pools = response.json() | ||
| if isinstance(pools, list): | ||
| for pool in pools: | ||
| if isinstance(pool, dict) and pool.get("name") == name: | ||
| return pool | ||
| raise KeyError(f"Storage pool {name!r} not found") | ||
|
|
||
| async def pool_id(self, name: str = SIMPLYBLOCK_STORAGE_POOL_NAME) -> UUID: | ||
| cached = self._pool_id_cache.get(name) | ||
| if cached: | ||
| return cached | ||
| pool = await self.pool(name) | ||
| identifier = UUID(str(pool["id"])) | ||
| self._pool_id_cache[name] = identifier | ||
| return identifier | ||
|
|
||
| async def volume_iostats(self, volume_uuid: str) -> dict[str, Any]: | ||
| base_url = await self._cluster_pool_base() | ||
| url = f"{base_url}/volumes/{volume_uuid}/iostats" | ||
| response = await self._client.get(url, headers=self._headers(), timeout=SIMPLYBLOCK_API_TIMEOUT_SECONDS) | ||
| response.raise_for_status() | ||
| payload = response.json() | ||
| return payload[0] # return the most recent one | ||
|
|
||
| async def update_volume( | ||
| self, | ||
| volume_uuid: str, | ||
| payload: dict[str, Any], | ||
| ) -> None: | ||
| headers = self._headers() | ||
| headers["Content-Type"] = "application/json" | ||
| base_url = await self._cluster_pool_base() | ||
| url = f"{base_url}/volumes/{volume_uuid}" | ||
| response = await self._client.put( | ||
| url, | ||
| headers=headers, | ||
| json=payload, | ||
| timeout=SIMPLYBLOCK_API_TIMEOUT_SECONDS, | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
|
|
||
| async def create_simplyblock_api(client: httpx.AsyncClient) -> SimplyblockApi: | ||
| from . import load_simplyblock_credentials | ||
|
|
||
| endpoint, cluster_id, cluster_secret = await load_simplyblock_credentials() | ||
| return SimplyblockApi( | ||
| client=client, | ||
| endpoint=endpoint, | ||
| cluster_id=cluster_id, | ||
| cluster_secret=cluster_secret, | ||
| ) | ||
boddumanohar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.