Skip to content

Commit

Permalink
starting redis integration for get configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuhtz committed Jul 25, 2024
1 parent c84bda9 commit 7eed54c
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 17 deletions.
6 changes: 4 additions & 2 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import logging
import sys

from globals import env
from internal import logger_init, check_env_vars, set_log_level
from routers.configuration.operations import router as configuration_router

# check if python version is what this is written with:
# python version sanity check

if sys.version_info < (3, 10):
print('ERROR: Python 3.10+ required.')
Expand All @@ -31,7 +32,8 @@
logger_init()

# check env and use defaults if not present
env = check_env_vars()
# this populates env dict global
check_env_vars()

# set logger level based on what we got back
set_log_level(env['LOG_LEVEL'])
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions api/internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from .logger_init import logger_init
from .check_env_vars import check_env_vars
from .set_log_level import set_log_level
from .check_conf_server import check_conf_server

37 changes: 37 additions & 0 deletions api/internal/check_conf_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

from typing import Tuple
from xmlrpc.client import Boolean
import redis
from globals import env


def check_conf_server(
host_param: str = None,
port_param: str = None) -> Tuple:
"""Check if the Redis configuration endpoint is alive.
If host and/or port aren't provided, pull values from REDIS_HOST and REDIS_port
environment variables
Arguments:
host_param (Optional): the Redis endpoint (.e.g, 'redis://localhost' or '127.0.0.1')
port_param (Optional): the Redis port
Return:
(status_code, r) tuple where status_code is boolean for the check success,
and r is the Redis handle if successful
"""

if not host_param:
host_param = env['REDIS_HOST']
if not port_param:
port_param = env['REDIS_PORT']

try:
r = redis.StrictRedis(host=host_param, port=port_param, decode_responses=True )
if r.ping():
return True, r
return False, None

except redis.ConnectionError:
return False, None

22 changes: 9 additions & 13 deletions api/internal/check_env_vars.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from ast import Dict
import os
import logging
from typing import Dict
from globals import env

def check_env_vars() -> Dict:
"""
Checks if environment variables we care about are present.
def check_env_vars():
"""Checks if environment variables we care about are present.
If so, accept them.
If not, substitute a reasonable default.
Returns:
Dict: returns a dictionary of environment variables and their assigned
values. NOT typesafe. Consumer is resonsible for typecasting as needed.
Sets globals.env Dict of environment variables and their assigned
values.
NOT typesafe. Consumer is resonsible for typecasting as needed.
"""

# access the 'global' logger
logger = logging.getLogger('api')

Expand All @@ -36,8 +36,6 @@ def check_env_vars() -> Dict:

all_env_vars = dict(os.environ)

env = {}

logger.debug(f'all_env_vars: {all_env_vars}')
logger.debug(f'env = {env}')

Expand All @@ -50,5 +48,3 @@ def check_env_vars() -> Dict:
logger.debug(f'env: {var}={env[var]} (default)')

logger.debug(f'env = {env}')

return env
2 changes: 2 additions & 0 deletions api/routers/configuration/delete.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Configuration DELETE

from ast import Dict
from fastapi import Response, status

def delete(
response: Response,
instance_param: str = None,
section_param: str = None
) -> Dict:
Expand Down
30 changes: 29 additions & 1 deletion api/routers/configuration/get.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
# Configuration GET

from fastapi import Response, status
from globals import env
from ast import Dict
from internal.check_conf_server import check_conf_server

def get(
response: Response,
instance_param: str = None,
section_param: str = None
) -> Dict:
"""Implement GET method for
/v1/configuration
/v1/configuration/{instance}
/v1/configuration/{instance}/{section}
Args:
response (Response): pass through for setting specific status
instance_param (str, optional): Name of the instance. Defaults to None.
section_param (str, optional): Name of the configuration section. Defaults to None.
Returns:
Dict: response from the operation
HTTP Status Codes:
424: Redis failure
"""
return_dict = {'instance': instance_param, 'section': section_param}

# redis code goes here
# do we have a working Redis connection?
redis_status, r = check_conf_server()
if not redis_status:
response.status_code = status.HTTP_424_FAILED_DEPENDENCY
return { 'message': 'redis connection failed' }
# we have a working redis connection

# do something useful with redis

return return_dict
16 changes: 15 additions & 1 deletion api/routers/configuration/operations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Configuration operations

from enum import Enum
from fastapi import APIRouter, Body
from fastapi import APIRouter, Body, Response

from .get import get as configuration_get
from .put import put as configuration_put
Expand All @@ -27,10 +27,22 @@ class SectionName(str, Enum):
@router.get('/configuration/{instance}')
@router.get('/configuration/{instance}/{section}')
async def get_config(
response: Response,
instance: str = None,
section: SectionName = None
):
"""Handler for GET operation on configuration
Args:
response (Response): HTTP status code pass-through
instance (str, optional): Name of the instance. Defaults to None.
section (SectionName, optional): Name of the configuration section. Defaults to None.
Returns:
Dict: HTTP operation response
"""
return configuration_get(
response=response,
instance_param=instance,
section_param=section
)
Expand All @@ -43,12 +55,14 @@ async def get_config(
@router.put('/configuration/{instance}')
@router.put('/configuration/{instance}/{section}')
async def put_config(
response: Response,
instance: str = None,
section: SectionName = None,
body: dict = Body (...)
):

return configuration_put(
response=response,
instance_param=instance,
section_param=section
)
Expand Down
2 changes: 2 additions & 0 deletions api/routers/configuration/patch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Configuration PATCH

from ast import Dict
from fastapi import Response, status

def patch(
response: Response,
instance_param: str = None,
section_param: str = None
) -> Dict:
Expand Down
2 changes: 2 additions & 0 deletions api/routers/configuration/put.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Configuration PUT

from ast import Dict
from fastapi import Response, status

def put(
response: Response,
instance_param: str = None,
section_param: str = None
) -> Dict:
Expand Down

0 comments on commit 7eed54c

Please sign in to comment.