Skip to content

Commit

Permalink
tests for kubeapi
Browse files Browse the repository at this point in the history
  • Loading branch information
ikethecoder committed May 8, 2024
1 parent 2556ffb commit 773bf4f
Show file tree
Hide file tree
Showing 18 changed files with 937 additions and 338 deletions.
1 change: 1 addition & 0 deletions microservices/kubeApi/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ typings/
__pycache__/
*.py[cod]
*$py.class
.pytest_cache

# C extensions
*.so
Expand Down
17 changes: 17 additions & 0 deletions microservices/kubeApi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ docker run -ti --rm \
kubeapi
```

### Development

Locally running:

```sh
ACCESS_USER=kubeuser ACCESS_SECRET=s3cret \
uvicorn main:app --host 0.0.0.0 --port 8080 --reload
```

Testing:

```
ACCESS_USER=kubeuser ACCESS_SECRET=s3cret \
coverage run --branch --source=auth,clients,routers -m pytest -s
coverage xml html
```
36 changes: 36 additions & 0 deletions microservices/kubeApi/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from fastapi import FastAPI, Request, status, Request
from fastapi.exceptions import RequestValidationError
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from starlette.responses import HTMLResponse
from routers.routes import router

def create_app():

app = FastAPI(title="GWA Kubernetes API",
description="Description: API to create resources in Openshift using Kubectl",
version="1.0.0")

app.include_router(router)

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
)

@app.get("/health")
async def get_health():
return {"status": "up"}

@app.get("/")
def main():
title = """
<h1>
GWA KUBE API
</h1> """
return HTMLResponse(title)

return app
10 changes: 6 additions & 4 deletions microservices/kubeApi/clients/ocp_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,16 @@ def prepare_route_last_version(ns, select_tag):

def prepare_apply_routes(ns, select_tag, hosts, rootPath, data_plane, ns_template_version, overrides):
out_filename = "%s/routes-current.yaml" % rootPath
ts = int(time.time())
fmt_time = datetime.now().strftime("%Y.%m-%b.%d")
ts = time_secs()
fmt_time = datetime.fromtimestamp(ts).strftime("%Y.%m-%b.%d")

resource_versions = prepare_route_last_version(ns, select_tag)


with open(out_filename, 'w') as out_file:
index = 1
for host in hosts:
templ_version = ns_template_version
if 'aps.route.session.cookie.enabled' in overrides and host in overrides['aps.route.session.cookie.enabled']:
if overrides and 'aps.route.session.cookie.enabled' in overrides and host in overrides['aps.route.session.cookie.enabled']:
templ_version = 'v1'

route_template = ROUTES[templ_version]["ROUTE"]
Expand Down Expand Up @@ -196,3 +195,6 @@ def get_gwa_ocp_routes(extralabels=""):
raise Exception("Failed to get existing routes")

return json.loads(out)['items']

def time_secs():
return int(time.time())
1 change: 0 additions & 1 deletion microservices/kubeApi/clients/ocp_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ def get_gwa_ocp_services(extralabels=""):
return json.loads(out)['items']

def get_gwa_ocp_service_secrets(extralabels=""):

secret_names = []
result_data = []
services = get_gwa_ocp_services(extralabels=extralabels)
Expand Down
2 changes: 1 addition & 1 deletion microservices/kubeApi/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"enabled": config('HOST_TRANSFORM_ENABLED', cast=bool, default=False)
}

log_level = config('LOG_LEVEL')
log_level = config('LOG_LEVEL', default='DEBUG' )

access_credentials = {
"accessUser": config('ACCESS_USER'),
Expand Down
35 changes: 2 additions & 33 deletions microservices/kubeApi/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
from fastapi import FastAPI, Request, status, Request
from fastapi.exceptions import RequestValidationError
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from starlette.responses import HTMLResponse
from routers import routes
from config import settings
import logging
import logging.config
from app import create_app

logging.config.dictConfig({
'version': 1,
Expand All @@ -34,31 +28,6 @@
}
})

app = FastAPI(title="GWA Kubernetes API",
description="Description: API to create resources in Openshift using Kubectl",
version="1.0.0")
app.include_router(routes.router)

logger = logging.getLogger(__name__)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
)


@app.get("/health")
async def get_health():
return {"status": "up"}


@app.get("/")
def main():
title = """
<h1>
GWA KUBE API
</h1> """
return HTMLResponse(title)
app = create_app()
Loading

0 comments on commit 773bf4f

Please sign in to comment.