Compare commits
17 Commits
4acb5ac3bb
...
72f738a816
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72f738a816 | ||
|
|
fe13e49172 | ||
|
|
d90a18053f | ||
|
|
22419ecf84 | ||
|
|
cf09ba6431 | ||
|
|
774fa3484c | ||
| b0d70e2120 | |||
|
|
fc71950039 | ||
|
|
e8efde9892 | ||
| 1592333ef8 | |||
|
|
73a42aae3b | ||
|
|
615d290773 | ||
|
|
af4b60a0e3 | ||
|
|
63bcd9b931 | ||
| d564710004 | |||
|
|
76c5353afa | ||
|
|
19e9cd6625 |
8
.env
Normal file
8
.env
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Environment variables for NetApp ONTAP clusters
|
||||||
|
CLUSTER1_HOSTNAME=172.16.57.2
|
||||||
|
CLUSTER1_USERNAME=admin
|
||||||
|
CLUSTER1_PASSWORD=Netapp12
|
||||||
|
|
||||||
|
CLUSTER2_HOSTNAME=172.16.56.2
|
||||||
|
CLUSTER2_USERNAME=admin
|
||||||
|
CLUSTER2_PASSWORD=Netapp12
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -129,7 +129,7 @@ celerybeat.pid
|
|||||||
*.sage.py
|
*.sage.py
|
||||||
|
|
||||||
# Environments
|
# Environments
|
||||||
.env
|
#.env
|
||||||
.venv
|
.venv
|
||||||
env/
|
env/
|
||||||
venv/
|
venv/
|
||||||
|
|||||||
@@ -12,5 +12,6 @@ requires-python = ">=3.13"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"fastapi[standard]>=0.116.2",
|
"fastapi[standard]>=0.116.2",
|
||||||
"httpx>=0.28.1",
|
"httpx>=0.28.1",
|
||||||
|
"python-dotenv>=1.1.1",
|
||||||
"redis>=6.4.0",
|
"redis>=6.4.0",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
fastapi[standard]>=0.116.2
|
fastapi[standard]>=0.116.2
|
||||||
httpx>=0.28.1
|
httpx>=0.28.1
|
||||||
redis>=6.4.0
|
redis>=6.4.0
|
||||||
|
python-dotenv>=1.1.1
|
||||||
pydantic
|
pydantic
|
||||||
redis[hiredis]
|
redis[hiredis]
|
||||||
dotenv
|
dotenv
|
||||||
4
src/aggregate/__init__.py
Normal file
4
src/aggregate/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from src.example.router import router as example_router
|
||||||
|
from .aggregate_router import router as aggregate_router
|
||||||
|
|
||||||
|
__all__ = ["example_router", "aggregate_router"]
|
||||||
16
src/aggregate/aggregate_router.py
Normal file
16
src/aggregate/aggregate_router.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# contains the router for the aggregates endpoint
|
||||||
|
from fastapi import APIRouter, Query, Request
|
||||||
|
from typing import List
|
||||||
|
from .aggregate_schema import AggregateSchema, MetricEnum
|
||||||
|
from .aggregate_service import get_aggregates
|
||||||
|
|
||||||
|
|
||||||
|
router = APIRouter(tags=["aggregates"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/aggregates", response_model=List[AggregateSchema])
|
||||||
|
async def aggregates_endpoint(
|
||||||
|
request: Request,
|
||||||
|
metric: MetricEnum = Query(MetricEnum.relative, description="Metric type"),
|
||||||
|
):
|
||||||
|
return await get_aggregates(request, metric)
|
||||||
15
src/aggregate/aggregate_schema.py
Normal file
15
src/aggregate/aggregate_schema.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# contains the schema definitions for aggregates
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
class AggregateSchema(BaseModel):
|
||||||
|
aggregate: str
|
||||||
|
node: str
|
||||||
|
available: int
|
||||||
|
available_str: str
|
||||||
|
|
||||||
|
|
||||||
|
class MetricEnum(str, Enum):
|
||||||
|
relative = "relative"
|
||||||
|
absolute = "absolute"
|
||||||
38
src/aggregate/aggregate_service.py
Normal file
38
src/aggregate/aggregate_service.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# contains the business logic for aggregates
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from fastapi import Request
|
||||||
|
from .aggregate_schema import AggregateSchema, MetricEnum
|
||||||
|
from logging import getLogger
|
||||||
|
from ..utils import round_bytes, get_data_from_ontap
|
||||||
|
|
||||||
|
logger = getLogger("uvicorn")
|
||||||
|
logger.setLevel("DEBUG")
|
||||||
|
|
||||||
|
|
||||||
|
async def get_aggregates(request: Request, metric: str = "relative") -> List[AggregateSchema]:
|
||||||
|
# Dummy data for demonstration
|
||||||
|
# You can use the metric parameter to filter or modify results as needed
|
||||||
|
# For now, just return the same data and show metric usage
|
||||||
|
logger.debug(f"Metric used: {metric}")
|
||||||
|
client = request.app.requests_client
|
||||||
|
__aggregates = await get_data_from_ontap(client, logger, "172.16.57.2", "admin", "Netapp12", "storage/aggregates", "fields=name,uuid,space,node,home_node")
|
||||||
|
logger.debug(__aggregates)
|
||||||
|
__aggregates = __aggregates.get("records")
|
||||||
|
if metric == MetricEnum.relative:
|
||||||
|
__aggregates = sorted(__aggregates, key=lambda r: r["space"]["block_storage"].get("used_percent"), reverse=True)
|
||||||
|
elif metric == MetricEnum.absolute:
|
||||||
|
__aggregates = sorted(__aggregates, key=lambda r: r["space"]["block_storage"].get("available"), reverse=False)
|
||||||
|
|
||||||
|
aggregates: list = [
|
||||||
|
AggregateSchema(
|
||||||
|
aggregate=a["name"],
|
||||||
|
node=a["node"]["name"],
|
||||||
|
available=a["space"]["block_storage"]["available"],
|
||||||
|
available_str=round_bytes(a["space"]["block_storage"]["available"]),
|
||||||
|
)
|
||||||
|
for a in __aggregates
|
||||||
|
]
|
||||||
|
|
||||||
|
return aggregates
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
from .router import router as example_router
|
|
||||||
|
|
||||||
__all__ = ["example_router"]
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
# contains the router for the example endpoint
|
# contains the router for the aggregate endpoint
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from .schema import ExampleSchema
|
from .schema import ExampleSchema
|
||||||
|
|
||||||
router = APIRouter(tags=["example"])
|
router = APIRouter(tags=["aggregate"])
|
||||||
|
|
||||||
|
|
||||||
@router.get("/example")
|
@router.get("/example")
|
||||||
async def example_endpoint() -> ExampleSchema:
|
async def example_endpoint() -> ExampleSchema:
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# contains the schema definitions for the example service
|
# contains the schema definitions for the aggregate service
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class ExampleSchema(BaseModel):
|
class ExampleSchema(BaseModel):
|
||||||
example_field: str
|
example_field: str
|
||||||
another_field: int
|
another_field: int
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
# contains the business logic for the example service
|
# contains the business logic for the aggregate service
|
||||||
async def example_service() -> str:
|
async def example_service() -> str:
|
||||||
return "This is an example service"
|
return "This is an aggregate service"
|
||||||
|
|||||||
16
src/service.py
Normal file
16
src/service.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
from dotenv import dotenv_values
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from src.schema import ConfigSchema
|
||||||
|
|
||||||
|
logger = logging.getLogger("uvicorn")
|
||||||
|
|
||||||
|
|
||||||
|
def load_config() -> ConfigSchema:
|
||||||
|
logger.info("Loading config from .env file")
|
||||||
|
config = dotenv_values(".env")
|
||||||
|
return ConfigSchema(
|
||||||
|
hostname=config["CLUSTER1_HOSTNAME"],
|
||||||
|
username=config["CLUSTER1_USERNAME"],
|
||||||
|
password=config["CLUSTER1_PASSWORD"],
|
||||||
|
)
|
||||||
24
src/utils.py
24
src/utils.py
@@ -1,4 +1,28 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
def round_bytes(size_in_bytes: int) -> str:
|
||||||
|
# Helper function to convert bytes to a human-readable format
|
||||||
|
for unit in ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]:
|
||||||
|
if size_in_bytes < 1024:
|
||||||
|
return f"{size_in_bytes:.2f}{unit}"
|
||||||
|
size_in_bytes /= 1024
|
||||||
|
return f"{size_in_bytes:.2f}EB"
|
||||||
|
|
||||||
|
|
||||||
|
async def get_data_from_ontap(client, logger, hostname: str, username: str, password: str, endpoint: str, query_string: str = ""):
|
||||||
|
url = f"https://{hostname}/api/{endpoint}"
|
||||||
|
if query_string:
|
||||||
|
url += f"?{query_string}"
|
||||||
|
async with client as _client:
|
||||||
|
try:
|
||||||
|
logger.debug(f"Fetching data from ONTAP: {url}")
|
||||||
|
response = await _client.get(url, auth=(username, password))
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"HTTP error occurred: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
def setup_logging() -> None:
|
def setup_logging() -> None:
|
||||||
"""Configure logging for the application"""
|
"""Configure logging for the application"""
|
||||||
|
|||||||
Reference in New Issue
Block a user