a gazillion things to implement cosmos db healthcheck
This commit is contained in:
parent
7af3b12c81
commit
91c79357f8
11 changed files with 421 additions and 15 deletions
12
anaxi/cli.py
12
anaxi/cli.py
|
|
@ -1,18 +1,12 @@
|
|||
import importlib.metadata
|
||||
import logging
|
||||
|
||||
import click
|
||||
import pyfiglet
|
||||
|
||||
from anaxi.logging import get_anaxi_logger
|
||||
from anaxi.processes import run_cosmos_db_healthcheck_process
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format="%(asctime)s - [%(levelname)s] - %(filename)s - L%(lineno)d - %(message)s",
|
||||
handlers=[logging.StreamHandler()],
|
||||
)
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger = get_anaxi_logger(__name__)
|
||||
|
||||
|
||||
@click.group()
|
||||
|
|
@ -37,7 +31,7 @@ def smoke_test():
|
|||
|
||||
|
||||
@cli.command()
|
||||
@click.option("--cosmos-db-id", type=click.STRING)
|
||||
@click.option("--cosmos-db-id", type=click.STRING, required=True)
|
||||
def cosmos_db_healthcheck(cosmos_db_id):
|
||||
logger.info("Starting a Cosmos DB healthcheck.")
|
||||
run_cosmos_db_healthcheck_process(cosmos_db_id)
|
||||
|
|
|
|||
40
anaxi/config.py
Normal file
40
anaxi/config.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import logging
|
||||
import pathlib
|
||||
from typing import Dict
|
||||
|
||||
from anaxi.file_persistence import read_yaml
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
class CosmosDBDatabaseConfig:
|
||||
|
||||
config_root_key: str = "cosmos-databases"
|
||||
|
||||
def __init__(self, host: str, database_id: str, master_key: str) -> None:
|
||||
self.host = host
|
||||
self.database_id = database_id
|
||||
self.master_key = master_key
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, a_dict) -> "CosmosDBDatabaseConfig":
|
||||
return CosmosDBDatabaseConfig(**a_dict)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, path: pathlib.Path) -> Dict[str, "CosmosDBDatabaseConfig"]:
|
||||
yaml_contents = read_yaml(path)
|
||||
|
||||
cosmos_db_contents = yaml_contents[CosmosDBDatabaseConfig.config_root_key]
|
||||
|
||||
try:
|
||||
database_configs = {
|
||||
db_id: CosmosDBDatabaseConfig.from_dict(db_config)
|
||||
for db_id, db_config in cosmos_db_contents.items()
|
||||
}
|
||||
except KeyError as e:
|
||||
logger.error(
|
||||
"Error reading Cosmos DB config yaml file. The file seems malformed. Review the example in the repository."
|
||||
)
|
||||
raise e
|
||||
|
||||
return database_configs
|
||||
13
anaxi/constants.py
Normal file
13
anaxi/constants.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import pathlib
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class PATHS:
|
||||
logging_file: pathlib.Path = pathlib.Path("anaxi.log")
|
||||
# Expand user is important. It will replace the user and give you the full
|
||||
# path. Stuff breaks without it.
|
||||
config_home_path: pathlib.Path = pathlib.Path("~/.anaxi/").expanduser()
|
||||
cosmos_db_config_file_path: pathlib.Path = config_home_path / pathlib.Path(
|
||||
"cosmos-db.yml"
|
||||
)
|
||||
12
anaxi/cosmos_tools.py
Normal file
12
anaxi/cosmos_tools.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import azure.cosmos.cosmos_client as cosmos_client
|
||||
|
||||
from anaxi.config import CosmosDBDatabaseConfig
|
||||
|
||||
|
||||
def create_cosmos_client_from_config(
|
||||
config: CosmosDBDatabaseConfig,
|
||||
) -> cosmos_client.CosmosClient:
|
||||
return cosmos_client.CosmosClient(
|
||||
url=config.host,
|
||||
credential={"masterKey": config.master_key},
|
||||
)
|
||||
8
anaxi/file_persistence.py
Normal file
8
anaxi/file_persistence.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import pathlib
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
def read_yaml(path: pathlib.Path) -> dict:
|
||||
with open(path) as f:
|
||||
return yaml.safe_load(f)
|
||||
15
anaxi/logging.py
Normal file
15
anaxi/logging.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import logging
|
||||
|
||||
|
||||
def get_anaxi_logger(name: str) -> None:
|
||||
logger = logging.getLogger(name)
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(
|
||||
logging.Formatter(
|
||||
fmt="%(asctime)s - [%(levelname)s] - %(filename)s - L%(lineno)d - %(message)s"
|
||||
)
|
||||
)
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
return logger
|
||||
|
|
@ -1,10 +1,32 @@
|
|||
import logging
|
||||
from anaxi.config import CosmosDBDatabaseConfig
|
||||
from anaxi.constants import PATHS
|
||||
from anaxi.cosmos_tools import create_cosmos_client_from_config
|
||||
from anaxi.logging import get_anaxi_logger
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger = get_anaxi_logger(__name__)
|
||||
|
||||
|
||||
def run_cosmos_db_healthcheck_process(cosmos_db_id: str) -> None:
|
||||
|
||||
logger.info(
|
||||
f"If I had a cosmos db connector, I would really try to hit this database: {cosmos_db_id}"
|
||||
logger.info("Reading Cosmos DB config file...")
|
||||
cosmos_db_configs = CosmosDBDatabaseConfig.from_yaml(
|
||||
PATHS.cosmos_db_config_file_path
|
||||
)
|
||||
logger.info(f"Found file with {len(cosmos_db_configs)} entries.")
|
||||
try:
|
||||
relevant_cosmos_db_config = cosmos_db_configs[cosmos_db_id]
|
||||
except KeyError as e:
|
||||
logger.error(
|
||||
f"Couldn't find a config entry for database with id: {cosmos_db_id}"
|
||||
)
|
||||
raise e
|
||||
|
||||
logger.info("Creating client...")
|
||||
cosmos_client = create_cosmos_client_from_config(relevant_cosmos_db_config)
|
||||
logger.info("Client created.")
|
||||
|
||||
logger.info("Throwing a SELECT 1;")
|
||||
response = cosmos_client.query_databases(query="SELECT 1")
|
||||
logger.info(f"Response: {response.next()}")
|
||||
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue