338 lines
14 KiB
Python
338 lines
14 KiB
Python
import datetime
|
|
#from datetime import datetime, time, timedelta
|
|
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Response, status
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
from typing import Dict, List, Optional, Set, Union
|
|
|
|
from app.config import settings
|
|
from app.db_sql import sql_enable_part, sql_insert, sql_update, sql_insert_or_update, sql_limit_offset_part, sql_select, sql_delete, redis_lookup_id_random
|
|
from app.lib_general import log, logging, common_route_params, Common_Route_Params, common_route_params_min, Common_Route_Params_Min
|
|
|
|
from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
|
|
|
from app.methods.site_domain_methods import load_site_domain_obj, lookup_site_domain_fqdn
|
|
|
|
from app.models.response_models import Resp_Body_Base, mk_resp
|
|
from app.models.site_models import Site_Base
|
|
from app.models.site_domain_models import Site_Domain_Base
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post('/site/domain', response_model=Resp_Body_Base)
|
|
async def post_site_domain_obj(
|
|
obj: Site_Domain_Base,
|
|
x_account_id: str = Header(...),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
exclude_none: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site_domain'
|
|
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
|
result = post_obj_template(
|
|
obj_type=obj_type,
|
|
data=obj_data_dict,
|
|
return_obj=True,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.patch('/site/domain/{obj_id}', response_model=Resp_Body_Base)
|
|
async def patch_site_domain_obj(
|
|
obj: Site_Domain_Base,
|
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
|
x_account_id: str = Header(..., ),
|
|
return_obj: bool = True,
|
|
by_alias: bool = True,
|
|
exclude_unset: bool = True,
|
|
exclude_none: bool = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site_domain'
|
|
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
|
obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=obj_type)
|
|
obj_data_dict['id_random'] = obj_id
|
|
result = patch_obj_template(
|
|
obj_type=obj_type,
|
|
data=obj_data_dict,
|
|
obj_id=obj_id,
|
|
return_obj=True,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.get('/site/domain/fqdn/{fqdn}', response_model=Resp_Body_Base)
|
|
async def lookup_site_domain_obj(
|
|
fqdn: str,
|
|
# x_account_id: str = Header(...),
|
|
# response: Response = Response,
|
|
|
|
commons: Common_Route_Params_Min = Depends(common_route_params_min),
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
|
|
# Updated 2021-12-13
|
|
if site_domain_rec_list_result := lookup_site_domain_fqdn(
|
|
fqdn = fqdn,
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
offset = commons.offset
|
|
):
|
|
site_domain_result_list = []
|
|
for site_domain_rec in site_domain_rec_list_result:
|
|
if load_site_domain_result := load_site_domain_obj(
|
|
site_domain_id = site_domain_rec.get('site_domain_id', None),
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
# by_alias = commons.by_alias,
|
|
# exclude_unset = commons.exclude_unset,
|
|
# model_as_dict = commons.model_as_dict,
|
|
):
|
|
site_domain_result_list.append(load_site_domain_result)
|
|
else:
|
|
site_domain_result_list.append(None)
|
|
response_data = site_domain_result_list
|
|
elif isinstance(site_domain_rec_list_result, list) or site_domain_rec_list_result is None: # Empty list or None
|
|
log.info('No results')
|
|
return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
|
|
return mk_resp(data=response_data, response=commons.response)
|
|
|
|
|
|
# data = {}
|
|
# data['fqdn'] = fqdn
|
|
|
|
# log.debug(data)
|
|
|
|
# sql = f"""
|
|
# SELECT *, site_enable_from AS 'enable_from', site_enable_to AS 'enable_to'
|
|
# FROM `v_site_domain` AS site_domain
|
|
# WHERE site_domain.fqdn = :fqdn;
|
|
# """
|
|
# log.debug(sql)
|
|
|
|
# # NOTE: This need to broken out into a methods function for site and or site_domain
|
|
# if site_domain_obj_result := sql_select(data=data, sql=sql):
|
|
# if isinstance(site_domain_obj_result, dict):
|
|
# log.info('Got a site domain result')
|
|
# try:
|
|
# site_obj = Site_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
|
|
# except ValidationError as e:
|
|
# log.error(e.json())
|
|
# log.debug(site_obj)
|
|
# try:
|
|
# site_domain_obj = Site_Domain_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
|
|
# except ValidationError as e:
|
|
# log.error(e.json())
|
|
# log.debug(site_domain_obj)
|
|
|
|
# site_domain_obj['site'] = site_obj
|
|
|
|
# return mk_resp(data=site_domain_obj, response=response)
|
|
# elif isinstance(site_domain_obj_result, list):
|
|
# log.info('Got multiple site domain results')
|
|
# log.warning(f'More than one site domain records were returned. This was unexpected and needs to be corrected. FQDN: {fqdn}')
|
|
# site_domain_obj_li = site_domain_obj_result
|
|
# return mk_resp(data=site_domain_obj_li, response=response)
|
|
# else:
|
|
# log.error(f'Unexpected results for the site domain records was returned. FQDN: {fqdn}; Access Key {access_key}; Referrer: {referrer}')
|
|
# return mk_resp(data=False, status_code=500, status_message=f'Unexpected results for the site domain records was returned. FQDN: {fqdn}; Access Key {access_key}; Referrer: {referrer}', response=response)
|
|
# else:
|
|
# log.info('No site domain results')
|
|
# log.debug(site_domain_obj_result)
|
|
# return mk_resp(data=False, status_code=404, response=response)
|
|
|
|
|
|
@router.get('/site/domain/lookup_fqdn', response_model=Resp_Body_Base)
|
|
async def lookup_site_domain_obj(
|
|
fqdn: str,
|
|
access_key: Optional[str] = Query(None, min_length=4, max_length=50),
|
|
referrer: Optional[str] = Query(None, min_length=8, max_length=150),
|
|
referer: Optional[str] = Query(None, min_length=8, max_length=150),
|
|
x_account_id: str = Header(...),
|
|
by_alias: bool = True,
|
|
exclude_unset: bool = True,
|
|
exclude_none: bool = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
data = {}
|
|
data['fqdn'] = fqdn
|
|
data['domain_access_key'] = access_key
|
|
if referrer: # This should work...
|
|
data['required_referrer'] = referrer
|
|
data['required_referer'] = referrer
|
|
elif referer: # This should not be needed...
|
|
referrer = referer
|
|
data['required_referrer'] = referer
|
|
data['required_refer'] = referer
|
|
log.info(f'Looking up site domain with FQDN: {fqdn}; Access Key {access_key}; Referrer: {referrer}')
|
|
log.debug(data)
|
|
|
|
if access_key and referrer:
|
|
sql_access_key_referrer = """
|
|
AND site_domain.domain_access_key = :domain_access_key
|
|
AND site_domain.required_referrer = :required_referrer
|
|
"""
|
|
elif access_key:
|
|
sql_access_key_referrer = """
|
|
AND site_domain.domain_access_key = :domain_access_key
|
|
AND (site_domain.required_referrer IS NULL OR site_domain.required_referrer = '')
|
|
"""
|
|
# elif referrer:
|
|
# sql_access_key_referrer = """
|
|
# AND site_domain.required_referrer = :required_referrer
|
|
# """
|
|
else:
|
|
sql_access_key_referrer = """
|
|
AND (site_domain.domain_access_key IS NULL OR site_domain.domain_access_key = '')
|
|
AND (site_domain.required_referrer IS NULL OR site_domain.required_referrer = '')
|
|
"""
|
|
|
|
sql = f"""
|
|
SELECT *, site_enable_from AS 'enable_from', site_enable_to AS 'enable_to'
|
|
FROM `v_site_domain` AS site_domain
|
|
WHERE site_domain.fqdn = :fqdn
|
|
{sql_access_key_referrer}
|
|
;
|
|
"""
|
|
log.debug(sql)
|
|
|
|
# NOTE: This need to broken out into a methods function for site and or site_domain
|
|
if site_domain_obj_result := sql_select(data=data, sql=sql):
|
|
if isinstance(site_domain_obj_result, dict):
|
|
log.info('Got a site domain result')
|
|
try:
|
|
site_obj = Site_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
log.debug(site_obj)
|
|
try:
|
|
site_domain_obj = Site_Domain_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
log.debug(site_domain_obj)
|
|
|
|
site_domain_obj['site'] = site_obj
|
|
|
|
return mk_resp(data=site_domain_obj, response=response)
|
|
elif isinstance(site_domain_obj_result, list):
|
|
log.info('Got multiple site domain results')
|
|
log.warning(f'More than one site domain records were returned. This was unexpected and needs to be corrected. FQDN: {fqdn}')
|
|
site_domain_obj_li = site_domain_obj_result
|
|
return mk_resp(data=site_domain_obj_li, response=response)
|
|
else:
|
|
log.error(f'Unexpected results for the site domain records was returned. FQDN: {fqdn}; Access Key {access_key}; Referrer: {referrer}')
|
|
return mk_resp(data=False, status_code=500, status_message=f'Unexpected results for the site domain records was returned. FQDN: {fqdn}; Access Key {access_key}; Referrer: {referrer}', response=response)
|
|
else:
|
|
log.info('No site domain results')
|
|
log.debug(site_domain_obj_result)
|
|
return mk_resp(data=False, status_code=404, response=response)
|
|
|
|
|
|
@router.get('/site/domain/list', response_model=Resp_Body_Base)
|
|
async def get_site_domain_obj_li(
|
|
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
|
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site_domain'
|
|
result = get_obj_li_template(
|
|
obj_type=obj_type,
|
|
for_obj_type=for_obj_type,
|
|
for_obj_id=for_obj_id,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.get('/site/domain/{site_domain_id}', response_model=Resp_Body_Base)
|
|
async def get_site_domain_obj(
|
|
site_domain_id: str = Query(..., min_length=11, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
exclude_none: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if site_domain_id := redis_lookup_id_random(record_id_random=site_domain_id, table_name='site_domain'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=response)
|
|
|
|
data = { 'site_domain_id': site_domain_id }
|
|
|
|
sql = f"""
|
|
SELECT *, site_enable_from AS 'enable_from', site_enable_to AS 'enable_to'
|
|
FROM `v_site_domain` AS site_domain
|
|
WHERE site_domain.id = :site_domain_id
|
|
;
|
|
"""
|
|
log.debug(sql)
|
|
|
|
# NOTE: This need to broken out into a methods function for site and or site_domain
|
|
if site_domain_obj_result := sql_select(data=data, sql=sql):
|
|
try:
|
|
site_obj = Site_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
log.debug(site_obj)
|
|
try:
|
|
site_domain_obj = Site_Domain_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
log.debug(site_domain_obj)
|
|
|
|
site_domain_obj['site'] = site_obj
|
|
log.debug(site_domain_obj)
|
|
|
|
return mk_resp(data=site_domain_obj, response=response)
|
|
else:
|
|
log.debug(site_domain_obj_result)
|
|
return mk_resp(data=False, status_code=404, response=response)
|
|
|
|
|
|
@router.delete('/site/domain/{obj_id}', response_model=Resp_Body_Base)
|
|
async def delete_site_domain_obj(
|
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site_domain'
|
|
result = delete_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
)
|
|
return result
|