Skip to content

Commit

Permalink
[FEATURE]: Config Vars
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Jul 4, 2023
1 parent 726554c commit ac21851
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 127 deletions.
95 changes: 27 additions & 68 deletions jaseci_core/jaseci/extens/api/config_api.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,46 @@
"""
Admin config api functions as a mixin
"""

from typing import Optional
from json import dumps, loads
from jaseci.utils.utils import manipulate_data
from jaseci.extens.api.interface import Interface


class ConfigApi:
"""
Admin config APIs
Abstracted since there are no valid configs in core atm, see jaseci_serv
to see how used.
"""

def __init__(self, *args, **kwargs):
self._valid_configs = [
"CONFIG_EXAMPLE",
"ACTION_SETS",
"REDIS_CONFIG",
"TASK_CONFIG",
"MAIL_CONFIG",
"PROME_CONFIG",
"ELASTIC_CONFIG",
"STRIPE_CONFIG",
"KUBE_CONFIG",
"JSORC_CONFIG",
]

@Interface.admin_api(cli_args=["name"])
def config_get(self, name: str, do_check: bool = True):
def config_get(self, name: str):
"""
Get a config
"""
if do_check and not self.name_check(name):
return self.name_error(name)
return self._h.get_glob(name)
conf = self._h.get_conf(name)
if isinstance(conf, (str, bytes, bytearray)):
try:
return loads(conf)
except Exception:
pass

return conf

@Interface.admin_api(cli_args=["name"])
def config_set(self, name: str, value: str or dict, do_check: bool = True):
def config_set(self, name: str, value: str or dict or list or tuple):
"""
Set a config
"""
if do_check and not self.name_check(name):
return self.name_error(name)

if not (value is None) and type(value) is dict:
if not (value is None) and isinstance(value, (dict, list, tuple)):
value = dumps(value)

self._h.save_glob(name, value)
return [f"Config of '{name}' to '{value}' set!"]
self._h.save_conf(name, value)
return [f"Config '{name}' to '{value}' set!"]

@Interface.admin_api(cli_args=["name"])
def config_update(
self,
name: str,
field_key: str,
field_key: str or int or list or tuple,
field_value: str
or int
or float
Expand All @@ -64,66 +49,40 @@ def config_update(
or bool
or tuple
or None, # json serializable types
do_check: bool = True,
):
"""
Update a key-value of a config
"""
if do_check and not self.name_check(name):
return self.name_error(name)

conf = self._h.get_glob(name)
conf = self._h.get_conf(name)
try:
conf = loads(conf)
except Exception:
conf = manipulate_data(conf, field_key, field_value)
self._h.save_conf(name, conf)
return [
f"Config {name} is not a dictionary. Uses config_set to set the value for this config."
f"Config '{name}' is updated with {field_key}:{field_value}. Current config value: {conf}"
]
except Exception as e:
return [
f"Config {name} is not a dictionary or list. Uses config_set to set the value for this config."
]
if field_key not in conf:
return [f"Field {field_key} does not exist in config {name}"]

conf[field_key] = field_value
conf = dumps(conf)
self._h.save_glob(name, conf)
return [
f"Config of '{name}' is updated with {field_key}:{field_value}. Current config value: {conf}"
]

@Interface.admin_api()
def config_list(self):
"""
Check a config is present
"""
return [v for v in self._h.list_glob() if v in self._valid_configs]

@Interface.admin_api()
def config_index(self):
"""
List all valid configs
"""
return self._valid_configs
return self._h.list_conf()

@Interface.admin_api(cli_args=["name"])
def config_exists(self, name: str):
"""
Check a config is present
"""
return self._h.has_glob(name)
return self._h.has_conf(name)

@Interface.admin_api(cli_args=["name"])
def config_delete(self, name: str, do_check: bool = True):
def config_delete(self, name: str):
"""
Delete a config
"""
if do_check and not self.name_check(name):
return self.name_error(name)
self._h.destroy_glob(name)
self._h.destroy_conf(name)
return [f"{name} Deleted."]

def name_error(self, name):
"""Much used error output"""
return [f"Config {name} not recognized, must be in {self._valid_configs}!"]

def name_check(self, name):
"""Much used name check"""
return name in self._valid_configs
93 changes: 75 additions & 18 deletions jaseci_core/jaseci/extens/api/global_api.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,100 @@
"""
Admin Global api functions as a mixin
"""
from jaseci.utils.utils import manipulate_data
from jaseci.extens.api.interface import Interface
from jaseci.prim.sentinel import Sentinel
import uuid
from json import dumps, loads


class GlobalApi:
"""
Admin global APIs
"""

@Interface.private_api(cli_args=["name"])
def global_get(self, name: str):
"""
Get a global
"""
glob = self._h.get_glob(name)

if isinstance(glob, (str, bytes, bytearray)):
try:
return loads(glob)
except Exception:
pass

return glob

@Interface.admin_api(cli_args=["name"])
def global_set(self, name: str, value: str):
def global_set(self, name: str, value: str or dict or list or tuple):
"""
Set a global
"""
ret = {"success": True}
if name == "GLOB_SENTINEL" or name in self._valid_configs:
ret["response"] = f"{name} is sacred!"
ret["success"] = False
else:
self._h.save_glob(name, value)
ret["response"] = f"Global variable '{name}' to '{value}' set!"
return ret
if name == "GLOB_SENTINEL":
return f"{name} is sacred!"

if not (value is None) and isinstance(value, (dict, list, tuple)):
value = dumps(value)

self._h.save_glob(name, value)
return [f"Global '{name}' to '{value}' set!"]

@Interface.admin_api(cli_args=["name"])
def global_update(
self,
name: str,
field_key: str or int or list or tuple,
field_value: str
or int
or float
or list
or dict
or bool
or tuple
or None, # json serializable types
):
"""
Update a key-value of a global
"""

glob = self._h.get_glob(name)
try:
glob = manipulate_data(glob, field_key, field_value)
self._h.save_glob(name, glob)
return [
f"Global '{name}' is updated with {field_key}:{field_value}. Current global value: {glob}"
]
except Exception:
return [
f"Global {name} is not a dictionary or list. Uses global_set to set the value."
]

@Interface.admin_api()
def global_list(self):
"""
Check a global is present
"""
return self._h.list_glob()

@Interface.admin_api(cli_args=["name"])
def global_exists(self, name: str):
"""
Check a global is present
"""
return self._h.has_glob(name)

@Interface.admin_api(cli_args=["name"])
def global_delete(self, name: str):
"""
Delete a global
"""
ret = {"success": True}
if name == "GLOB_SENTINEL" or name in self._valid_configs:
ret["response"] = f"{name} is sacred!"
ret["success"] = False
else:
self._h.destroy_glob(name)
ret["response"] = f"Global {name} deleted."
return ret
if name == "GLOB_SENTINEL":
return f"{name} is sacred!"

self._h.destroy_glob(name)
return [f"{name} Deleted."]

@Interface.admin_api()
def global_sentinel_set(self, snt: Sentinel = None):
Expand Down
10 changes: 5 additions & 5 deletions jaseci_core/jaseci/jsorc/jsorc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def configure(cls):

if cls.db_check():
hook = cls.hook()
config = hook.get_or_create_glob("JSORC_CONFIG", config)
config = hook.resolve_conf("JSORC_CONFIG", config)

cls._config = config
cls._backoff_interval = max(5, config.get("backoff_interval", 10))
Expand Down Expand Up @@ -228,10 +228,10 @@ def _svc(cls, service: str) -> cs:
if cls.db_check():
hook = cls.hook(use_proxy=instance["proxy"])

config = hook.get_or_create_glob(instance["config"], config)
config = hook.resolve_conf(instance["config"], config)

manifest = (
hook.get_or_create_glob(instance["manifest"], manifest)
hook.resolve_conf(instance["manifest"], manifest)
if instance["manifest"]
else {}
)
Expand Down Expand Up @@ -484,15 +484,15 @@ def regenerate_service(cls):
if service.manifest and kube.is_running():
try:
manifest = kube.resolve_manifest(
hook.get_or_create_glob(
hook.resolve_conf(
service.source["manifest"], service.manifest
),
*cls.overrided_namespace(
regeneration_queue, service.manifest_type
),
)

rmhists: dict = hook.get_or_create_glob(
rmhists: dict = hook.resolve_conf(
"RESOLVED_MANIFEST_HISTORY", {}
)

Expand Down
Loading

0 comments on commit ac21851

Please sign in to comment.