Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fe5ee5bd4d | |||
| 4c649f90bc | |||
| 2c69cab5d5 | |||
| b89ac90bca | |||
| 3109229061 | |||
| cda9df9e13 |
Binary file not shown.
@ -13,12 +13,12 @@ def get_pillar_name_sequence(name: str) -> list[str]:
|
|||||||
|
|
||||||
def decode_pillar_value(pillar: Pillar) -> str | int | float | bool | list | dict:
|
def decode_pillar_value(pillar: Pillar) -> str | int | float | bool | list | dict:
|
||||||
match pillar.parameter_type:
|
match pillar.parameter_type:
|
||||||
case 'string': return str(pillar.value)
|
case 'string': return str(json.loads(pillar.value))
|
||||||
case 'integer': return int(pillar.value)
|
case 'integer': return int(json.loads(pillar.value))
|
||||||
case 'float': return float(pillar.value)
|
case 'float': return float(json.loads(pillar.value))
|
||||||
case 'boolean': return bool(pillar.value)
|
case 'boolean': return bool(json.loads(pillar.value))
|
||||||
case 'array': return json.loads(pillar.value)
|
case 'list': return list(json.loads(pillar.value))
|
||||||
case 'dict': return json.loads(pillar.value)
|
case 'dict': return dict(json.loads(pillar.value))
|
||||||
raise RuntimeError(f"Failed to decode pillar value: Invalid type '{pillar.parameter_type}'")
|
raise RuntimeError(f"Failed to decode pillar value: Invalid type '{pillar.parameter_type}'")
|
||||||
|
|
||||||
|
|
||||||
@ -43,6 +43,6 @@ def get_pillar_for_target(db: Session, target: UUID) -> dict:
|
|||||||
current[label] = {}
|
current[label] = {}
|
||||||
current = current[label]
|
current = current[label]
|
||||||
else:
|
else:
|
||||||
current[label] = json.loads(str(value))
|
current[label] = value
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -140,10 +140,10 @@ def hostgroup_create(req: Request, name: str, params: HostgroupParams):
|
|||||||
|
|
||||||
print("[DEBUG] hostgroup_create: creating hostgroup hierarchy with {} label(s): {}".format(len(labels), labels))
|
print("[DEBUG] hostgroup_create: creating hostgroup hierarchy with {} label(s): {}".format(len(labels), labels))
|
||||||
|
|
||||||
stmt = select(Host).where(and_(Host.name == bindparam('name'), Host.is_hostgroup == True, Host.parent_id == bindparam('last')))
|
|
||||||
last = None
|
last = None
|
||||||
for label in labels:
|
for label in labels:
|
||||||
result = db.execute(stmt, {'name': label, 'last': last}).fetchall()
|
stmt = select(Host).where(and_(Host.name == label, Host.is_hostgroup == True, Host.parent_id == last))
|
||||||
|
result = db.execute(stmt).fetchall()
|
||||||
|
|
||||||
if len(result) == 1:
|
if len(result) == 1:
|
||||||
# simply step down through the hierarchy
|
# simply step down through the hierarchy
|
||||||
|
|||||||
@ -11,7 +11,7 @@ from starlette.requests import Request
|
|||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from starlette.responses import JSONResponse
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
from pillar_tool.db import Host, Pillar
|
from pillar_tool.db import Host, Pillar, TopFile
|
||||||
from pillar_tool.db.models.top_data import State, StateAssignment
|
from pillar_tool.db.models.top_data import State, StateAssignment
|
||||||
from pillar_tool.db.queries.pillar_queries import get_pillar_for_target
|
from pillar_tool.db.queries.pillar_queries import get_pillar_for_target
|
||||||
from pillar_tool.schemas import PillarParams, get_model_from_query
|
from pillar_tool.schemas import PillarParams, get_model_from_query
|
||||||
@ -83,9 +83,20 @@ def pillar_get(req: Request, target: str) -> JSONResponse:
|
|||||||
|
|
||||||
path.reverse()
|
path.reverse()
|
||||||
|
|
||||||
|
# create 'virtual' pillar for states
|
||||||
|
stmt_states = select(State).join(TopFile, TopFile.state_id == State.id).where(TopFile.host_id == host.id)
|
||||||
|
states_list: list[str] = [ x[0].name for x in db.execute(stmt_states).fetchall() ]
|
||||||
|
|
||||||
|
# create path name
|
||||||
|
hostgroup_name = '/'.join(x.name for x in path[:-1])
|
||||||
|
|
||||||
print("[DEBUG] pillar_get: resolved host hierarchy with {} hosts: {}".format(len(path), [h.name for h in path]))
|
print("[DEBUG] pillar_get: resolved host hierarchy with {} hosts: {}".format(len(path), [h.name for h in path]))
|
||||||
|
|
||||||
out = merge([get_pillar_for_target(db, host.id) for host in path]) # type: ignore
|
out = merge([get_pillar_for_target(db, host.id) for host in path]) # type: ignore
|
||||||
|
out.update({
|
||||||
|
"states": states_list,
|
||||||
|
"hostgroup": hostgroup_name
|
||||||
|
})
|
||||||
print("[DEBUG] pillar_get: merged pillar data contains {} top-level key(s)".format(len(out)))
|
print("[DEBUG] pillar_get: merged pillar data contains {} top-level key(s)".format(len(out)))
|
||||||
return JSONResponse(status_code=200, content=out)
|
return JSONResponse(status_code=200, content=out)
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "pillartool"
|
name = "pillartool"
|
||||||
version = "0.1.0"
|
version = "0.6.0"
|
||||||
description = "Add your description here"
|
description = "Add your description here"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user