Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add key/value store with namespace emulation #559

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from beanie import init_beanie
from fastapi_pagination.ext.motor import paginate
from motor import motor_asyncio
from kernelci.api.models import Hierarchy, Node, parse_node_obj, EventHistory
from redis import asyncio as aioredis
from kernelci.api.models import EventHistory, Hierarchy, Node, parse_node_obj

Check failure on line 14 in api/db.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models'
from .models import User, UserGroup


Expand Down Expand Up @@ -45,6 +46,8 @@

def __init__(self, service='mongodb://db:27017', db_name='kernelci'):
self._motor = motor_asyncio.AsyncIOMotorClient(service)
# TBD: Make redis host configurable
self._redis = aioredis.from_url('redis://redis:6379')
self._db = self._motor[db_name]

async def initialize_beanie(self):
Expand All @@ -60,6 +63,38 @@
col = self.COLLECTIONS[model]
return self._db[col]

async def get_kv(self, namespace, key):
"""
Get value from redis key-value store
Create a keyname by concatenating namespace and key
"""
keyname = f"{namespace}:{key}"
return await self._redis.get(keyname)

async def set_kv(self, namespace, key, value):
"""
Set value in redis key-value store
Create a keyname by concatenating namespace and key
"""
keyname = f"{namespace}:{key}"
return await self._redis.set(keyname, value)

async def del_kv(self, namespace, key):
"""
Delete key from redis key-value store
Create a keyname by concatenating namespace and key
"""
keyname = f"{namespace}:{key}"
return await self._redis.delete(keyname)

async def exists_kv(self, namespace, key):
"""
Check if key exists in redis key-value store
Create a keyname by concatenating namespace and key
"""
keyname = f"{namespace}:{key}"
return await self._redis.exists(keyname)

async def create_indexes(self):
"""Create indexes for models"""
for model in self.COLLECTIONS:
Expand Down
44 changes: 43 additions & 1 deletion api/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

Check warning on line 1 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Too many lines in module (1042/1000)
#
# Copyright (C) 2021-2023 Collabora Limited
# Author: Guillaume Tucker <[email protected]>
Expand All @@ -21,7 +21,8 @@
Request,
Form,
Header,
Query
Query,
Body,
)
from fastapi.responses import JSONResponse, PlainTextResponse, FileResponse
from fastapi.security import OAuth2PasswordRequestForm
Expand All @@ -31,7 +32,7 @@
from pymongo.errors import DuplicateKeyError
from fastapi_users import FastAPIUsers
from beanie import PydanticObjectId
from kernelci.api.models import (

Check failure on line 35 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models'
Node,
Hierarchy,
PublishEvent,
Expand Down Expand Up @@ -780,6 +781,47 @@
return obj_list


# -----------------------------------------------------------------------------
# Key/Value namespace enabled store
@app.get('/kv/{namespace}/{key}', response_model=Union[str, None])
async def get_kv(namespace: str, key: str,
user: User = Depends(get_current_user)):

"""Get a key value pair from the store"""
metrics.add('http_requests_total', 1)
return await db.get_kv(namespace, key)


@app.post('/kv/{namespace}/{key}', response_model=Optional[str])
async def post_kv(namespace: str, key: str,
value: Optional[str] = Body(default=None),
user: User = Depends(get_current_user)):
"""Set a key-value pair in the store
namespace and key are part of the URL
value is part of the request body.
If value is not provided, we need to call delete_kv to remove the key.
"""
metrics.add('http_requests_total', 1)
if not value:
await db.del_kv(namespace, key)
return "OK"
ret = await db.set_kv(namespace, key, value)
if ret:
return "OK"
raise HTTPException(status_code=500, detail="Failed to set key-value pair")


# Delete a key-value pair from the store
@app.delete('/kv/{namespace}/{key}', response_model=Optional[str])
async def delete_kv(namespace: str, key: str,
user: User = Depends(get_current_user)):
"""Delete a key-value pair from the store"""
metrics.add('http_requests_total', 1)
await db.del_kv(namespace, key)
response = "Key-value pair deleted successfully"
return response


# -----------------------------------------------------------------------------
# Pub/Sub

Expand Down
Loading