Skip to content

Commit cdc3147

Browse files
Bugfix
1 parent 69084f5 commit cdc3147

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

src/V3_CreateConfig.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ def createconfig(declaration: ConfigDeclaration, apiversion: str, runfromautosyn
5858

5959
try:
6060
# Pydantic JSON validation
61-
ConfigDeclaration(**declaration.dict())
61+
ConfigDeclaration(**declaration.model_dump())
6262
except ValidationError as e:
6363
print(f'Invalid declaration {e}')
6464

65-
d = declaration.dict()
65+
d = declaration.model_dump()
6666
decltype = d['output']['type']
6767

6868
if 'http' in d['declaration']:
@@ -172,7 +172,7 @@ def createconfig(declaration: ConfigDeclaration, apiversion: str, runfromautosyn
172172
httpConf = j2_env.get_template(NcgConfig.config['templates']['httpconf']).render(
173173
declaration=d['declaration']['http'], ncgconfig=NcgConfig.config) if 'http' in d['declaration'] else ''
174174
streamConf = j2_env.get_template(NcgConfig.config['templates']['streamconf']).render(
175-
declaration=d['declaration']['layer4'], ncgconfig=NcgConfig.config) if 'layer' in d['declaration'] else ''
175+
declaration=d['declaration']['layer4'], ncgconfig=NcgConfig.config) if 'layer4' in d['declaration'] else ''
176176

177177
b64HttpConf = str(base64.urlsafe_b64encode(httpConf.encode("utf-8")), "utf-8")
178178
b64StreamConf = str(base64.urlsafe_b64encode(streamConf.encode("utf-8")), "utf-8")
@@ -557,7 +557,7 @@ def patch_config(declaration: ConfigDeclaration, configUid: str, apiversion: str
557557
)
558558

559559
# The declaration sections to be patched
560-
declarationToPatch = declaration.dict()
560+
declarationToPatch = declaration.model_dump()
561561

562562
# The currently applied declaration
563563
status_code, currentDeclaration = get_declaration(configUid=configUid)
@@ -582,40 +582,40 @@ def patch_config(declaration: ConfigDeclaration, configUid: str, apiversion: str
582582
if 'declaration' in declarationToPatch:
583583
# HTTP
584584
d_upstreams = Contrib.MiscUtils.getDictKey(declarationToPatch, 'declaration.http.upstreams')
585-
if d_upstreams is not None:
585+
if d_upstreams:
586586
# HTTP upstream patch
587587
for u in d_upstreams:
588-
# print(f"Patching HTTP upstream [{u['name']}]")
588+
#print(f"Patching HTTP upstream [{u['name']}]")
589589
currentDeclaration = Contrib.DeclarationPatcher.patchHttpUpstream(
590590
sourceDeclaration=currentDeclaration, patchedHttpUpstream=u)
591591

592592
d_servers = Contrib.MiscUtils.getDictKey(declarationToPatch, 'declaration.http.servers')
593-
if d_servers is not None:
593+
if d_servers:
594594
# HTTP servers patch
595595
for s in d_servers:
596-
# print(f"Patching HTTP server [{s['name']}]")
596+
#print(f"Patching HTTP server [{s['name']}]")
597597
currentDeclaration = Contrib.DeclarationPatcher.patchHttpServer(
598598
sourceDeclaration=currentDeclaration, patchedHttpServer=s)
599599

600600
# Stream / Layer4
601601
d_upstreams = Contrib.MiscUtils.getDictKey(declarationToPatch, 'declaration.layer4.upstreams')
602-
if d_upstreams is not None:
602+
if d_upstreams:
603603
# Stream upstream patch
604604
for u in d_upstreams:
605-
# print(f"Patching Stream upstream [{u['name']}]")
605+
#print(f"Patching Stream upstream [{u['name']}]")
606606
currentDeclaration = Contrib.DeclarationPatcher.patchStreamUpstream(
607607
sourceDeclaration=currentDeclaration, patchedStreamUpstream=u)
608608

609609
d_servers = Contrib.MiscUtils.getDictKey(declarationToPatch, 'declaration.layer4.servers')
610-
if d_servers is not None:
610+
if d_servers:
611611
# Stream servers patch
612612
for s in d_servers:
613-
# print(f"Patching Stream server [{s['name']}]")
613+
#print(f"Patching Stream server [{s['name']}]")
614614
currentDeclaration = Contrib.DeclarationPatcher.patchStreamServer(
615615
sourceDeclaration=currentDeclaration, patchedStreamServer=s)
616616

617617
# Apply the updated declaration
618-
configDeclaration = ConfigDeclaration.parse_raw(json.dumps(currentDeclaration))
618+
configDeclaration = ConfigDeclaration.model_validate_json(json.dumps(currentDeclaration))
619619

620620
r = createconfig(declaration=configDeclaration, apiversion=apiversion,
621621
runfromautosync=True, configUid=configUid)

src/V3_NginxConfigDeclaration.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ class Declaration(BaseModel, extra=Extra.forbid):
389389
http: Optional[Http] = {}
390390

391391

392+
class APIGateway(BaseModel, extra=Extra.forbid):
393+
openapi_schema: Optional[str] = ""
394+
395+
392396
class ConfigDeclaration(BaseModel, extra=Extra.forbid):
393397
output: Output
394-
declaration: Optional[Declaration] = {}
398+
declaration: Optional[Declaration] = {}
399+
apigateway: Optional[APIGateway] = {}

src/main.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@
1010
import schedule
1111
import uvicorn
1212
from fastapi import FastAPI
13-
from fastapi.requests import Request
1413
from fastapi.responses import PlainTextResponse, Response, JSONResponse
14+
from openapi_parser import parse as openAPIparse
15+
from pydantic import BaseModel, BaseConfig, create_model
1516

1617
# NGINX Declarative API modules
1718
import NcgConfig
1819
import NcgRedis
1920
import V3_CreateConfig
2021
import V3_NginxConfigDeclaration
2122

22-
# pydantic models
23-
2423
cfg = NcgConfig.NcgConfig(configFile="../etc/config.toml")
2524
redis = NcgRedis.NcgRedis(host=cfg.config['redis']['host'], port=cfg.config['redis']['port'])
25+
BaseConfig.arbitrary_types_allowed = True
2626

2727
app = FastAPI(
2828
title=cfg.config['main']['banner'],
2929
version=cfg.config['main']['version'],
3030
contact={"name": "GitHub", "url": cfg.config['main']['url']}
3131
)
3232

33-
33+
# GitOps autosync scheduler
3434
def runScheduler():
3535
while True:
3636
schedule.run_pending()
@@ -59,7 +59,7 @@ def post_config_v3(d: V3_NginxConfigDeclaration.ConfigDeclaration, response: Res
5959
return JSONResponse(content=response, status_code=output['status_code'], headers=headers)
6060

6161

62-
# Modify eclaration using v3 API
62+
# Modify declaration using v3 API
6363
@app.patch("/v3/config/{configuid}", status_code=200, response_class=PlainTextResponse)
6464
def patch_config_v3(d: V3_NginxConfigDeclaration.ConfigDeclaration, response: Response, configuid: str):
6565
return V3_CreateConfig.patch_config(declaration=d, configUid=configuid, apiversion='v3')
@@ -136,13 +136,6 @@ def delete_config(configuid: str = ""):
136136
)
137137

138138

139-
# Import OpenAPI schema - beta
140-
@app.post("/v3/openapi", status_code=200, response_class=JSONResponse)
141-
def post_openapi_v3(request: Request):
142-
openAPISchema = request.json()
143-
print(openAPISchema)
144-
145-
146139
if __name__ == '__main__':
147140
print(f"{cfg.config['main']['banner']} {cfg.config['main']['version']}")
148141

0 commit comments

Comments
 (0)