pglift-api 3.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pglift_api-3.3.0/.gitignore +23 -0
- pglift_api-3.3.0/PKG-INFO +49 -0
- pglift_api-3.3.0/README.md +12 -0
- pglift_api-3.3.0/hatch.toml +7 -0
- pglift_api-3.3.0/pyproject.toml +60 -0
- pglift_api-3.3.0/pytest.ini +10 -0
- pglift_api-3.3.0/src/pglift_api/__init__.py +14 -0
- pglift_api-3.3.0/src/pglift_api/_compat.py +15 -0
- pglift_api-3.3.0/src/pglift_api/_site.py +140 -0
- pglift_api-3.3.0/src/pglift_api/databases.py +105 -0
- pglift_api-3.3.0/src/pglift_api/deps.py +43 -0
- pglift_api-3.3.0/src/pglift_api/instances.py +106 -0
- pglift_api-3.3.0/src/pglift_api/pm.py +14 -0
- pglift_api-3.3.0/src/pglift_api/py.typed +0 -0
- pglift_api-3.3.0/src/pglift_api/tasks.py +48 -0
- pglift_api-3.3.0/src/pglift_api/tasks_manager.py +199 -0
- pglift_api-3.3.0/tests/expect/test-http-api.t +325 -0
- pglift_api-3.3.0/tests/unit/conftest.py +21 -0
- pglift_api-3.3.0/tests/unit/test_site.py +96 -0
- pglift_api-3.3.0/tests/unit/test_tasksmanager.py +83 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2021 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
docs/_build
|
|
6
|
+
build/
|
|
7
|
+
*/dist/
|
|
8
|
+
.eggs/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
*.egg
|
|
11
|
+
*.py[cod]
|
|
12
|
+
__pycache__/
|
|
13
|
+
*.so
|
|
14
|
+
*~
|
|
15
|
+
|
|
16
|
+
.cache
|
|
17
|
+
.coverage
|
|
18
|
+
.pybuild
|
|
19
|
+
.pytest_cache
|
|
20
|
+
.mypy_cache
|
|
21
|
+
.tox
|
|
22
|
+
|
|
23
|
+
htmlcov
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pglift_api
|
|
3
|
+
Version: 3.3.0
|
|
4
|
+
Summary: Life-cycle management of production-ready PostgreSQL instances
|
|
5
|
+
Project-URL: Documentation, https://pglift.readthedocs.io/
|
|
6
|
+
Project-URL: Source, https://gitlab.com/dalibo/pglift/
|
|
7
|
+
Project-URL: Tracker, https://gitlab.com/dalibo/pglift/-/issues/
|
|
8
|
+
Author-email: Dalibo SCOP <contact@dalibo.com>
|
|
9
|
+
License: GPLv3
|
|
10
|
+
Keywords: administration,deployment,postgresql
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: System Administrators
|
|
14
|
+
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Database
|
|
22
|
+
Classifier: Topic :: System :: Systems Administration
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: <4,>=3.10
|
|
25
|
+
Requires-Dist: fastapi
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pglift[test,typing]; extra == 'dev'
|
|
28
|
+
Requires-Dist: uvicorn; extra == 'dev'
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: pytest-cov; extra == 'test'
|
|
31
|
+
Requires-Dist: pytest>=8; extra == 'test'
|
|
32
|
+
Provides-Extra: typing
|
|
33
|
+
Requires-Dist: mypy>=1.10.0; extra == 'typing'
|
|
34
|
+
Requires-Dist: types-psutil; extra == 'typing'
|
|
35
|
+
Requires-Dist: types-python-dateutil; extra == 'typing'
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
<!--
|
|
39
|
+
SPDX-FileCopyrightText: 2026 Dalibo
|
|
40
|
+
|
|
41
|
+
SPDX-License-Identifier: GPL-3.0-or-later
|
|
42
|
+
-->
|
|
43
|
+
|
|
44
|
+
This package contains the HTTP API for
|
|
45
|
+
[pglift](https://pglift.readthedocs.io/). It is a dependency package and
|
|
46
|
+
should not be installed directly but rather through the `api` *optional
|
|
47
|
+
dependency* (aka "extra") of pglift package, e.g.:
|
|
48
|
+
|
|
49
|
+
$ pip install "pglift[api]"
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Dalibo
|
|
3
|
+
|
|
4
|
+
SPDX-License-Identifier: GPL-3.0-or-later
|
|
5
|
+
-->
|
|
6
|
+
|
|
7
|
+
This package contains the HTTP API for
|
|
8
|
+
[pglift](https://pglift.readthedocs.io/). It is a dependency package and
|
|
9
|
+
should not be installed directly but rather through the `api` *optional
|
|
10
|
+
dependency* (aka "extra") of pglift package, e.g.:
|
|
11
|
+
|
|
12
|
+
$ pip install "pglift[api]"
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2025 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
[build-system]
|
|
6
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
7
|
+
build-backend = "hatchling.build"
|
|
8
|
+
|
|
9
|
+
[project]
|
|
10
|
+
name = "pglift_api"
|
|
11
|
+
description = "Life-cycle management of production-ready PostgreSQL instances"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.10, <4"
|
|
14
|
+
license = { text = "GPLv3" }
|
|
15
|
+
authors = [{ name = "Dalibo SCOP", email = "contact@dalibo.com" }]
|
|
16
|
+
keywords = [
|
|
17
|
+
"postgresql",
|
|
18
|
+
"deployment",
|
|
19
|
+
"administration",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Development Status :: 5 - Production/Stable",
|
|
23
|
+
"Intended Audience :: Developers",
|
|
24
|
+
"Intended Audience :: System Administrators",
|
|
25
|
+
"Topic :: System :: Systems Administration",
|
|
26
|
+
"Topic :: Database",
|
|
27
|
+
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3.10",
|
|
30
|
+
"Programming Language :: Python :: 3.11",
|
|
31
|
+
"Programming Language :: Python :: 3.12",
|
|
32
|
+
"Programming Language :: Python :: 3.13",
|
|
33
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
34
|
+
"Typing :: Typed",
|
|
35
|
+
]
|
|
36
|
+
dynamic = ["version"]
|
|
37
|
+
|
|
38
|
+
dependencies = [
|
|
39
|
+
"fastapi",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.optional-dependencies]
|
|
43
|
+
typing = [
|
|
44
|
+
"mypy >= 1.10.0",
|
|
45
|
+
"types-psutil",
|
|
46
|
+
"types-python-dateutil",
|
|
47
|
+
]
|
|
48
|
+
test = [
|
|
49
|
+
"pytest >= 8",
|
|
50
|
+
"pytest-cov",
|
|
51
|
+
]
|
|
52
|
+
dev = [
|
|
53
|
+
"pglift[test,typing]",
|
|
54
|
+
"uvicorn",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[project.urls]
|
|
58
|
+
Documentation = "https://pglift.readthedocs.io/"
|
|
59
|
+
Source = "https://gitlab.com/dalibo/pglift/"
|
|
60
|
+
Tracker = "https://gitlab.com/dalibo/pglift/-/issues/"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
[pytest]
|
|
6
|
+
addopts = --strict-config --strict-markers
|
|
7
|
+
filterwarnings =
|
|
8
|
+
error
|
|
9
|
+
ignore:cannot guess 'postgresql.versions' setting as 'bindir' is unset:RuntimeWarning
|
|
10
|
+
testpaths = tests/unit
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
from fastapi import FastAPI
|
|
6
|
+
|
|
7
|
+
from . import databases, instances, tasks
|
|
8
|
+
|
|
9
|
+
app = FastAPI()
|
|
10
|
+
|
|
11
|
+
instances.prefixed_router.include_router(instances.router)
|
|
12
|
+
instances.prefixed_router.include_router(databases.router)
|
|
13
|
+
app.include_router(instances.prefixed_router)
|
|
14
|
+
app.include_router(tasks.router)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
# we cannot simply import TypedDict from typing:
|
|
8
|
+
# https://pydantic.dev/docs/validation/latest/errors/usage_errors/#typed-dict-version
|
|
9
|
+
|
|
10
|
+
if sys.version_info[:2] < (3, 12):
|
|
11
|
+
from typing_extensions import TypedDict
|
|
12
|
+
else:
|
|
13
|
+
from typing import TypedDict
|
|
14
|
+
|
|
15
|
+
__all__ = ["TypedDict"]
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
"""Site globals for the API."""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from copy import deepcopy
|
|
10
|
+
from functools import cache
|
|
11
|
+
from typing import TYPE_CHECKING, Annotated, Any, Union, get_args, get_origin
|
|
12
|
+
|
|
13
|
+
from fastapi import HTTPException, status
|
|
14
|
+
from pydantic import BaseModel, ValidationError, create_model
|
|
15
|
+
from pydantic.fields import FieldInfo
|
|
16
|
+
|
|
17
|
+
from pglift import exceptions, plugin_manager
|
|
18
|
+
from pglift.annotations import CREATE_ONLY, api
|
|
19
|
+
from pglift.models import interface
|
|
20
|
+
from pglift.settings import Settings, SiteSettings
|
|
21
|
+
|
|
22
|
+
__all__ = ["SiteSettings"]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@cache
|
|
26
|
+
def _build_model(model: type[BaseModel], createMode: bool = False) -> type[BaseModel]:
|
|
27
|
+
"""
|
|
28
|
+
Dynamically create a Pydantic model derived from a pglift interface
|
|
29
|
+
model while adjusting readOnly JSON Schema extra metadata.
|
|
30
|
+
|
|
31
|
+
This walks the original model and:
|
|
32
|
+
|
|
33
|
+
- recursively processes nested models and supported generic types
|
|
34
|
+
- makes api.HIDDEN fields as readOnly
|
|
35
|
+
- enforces readOnly on CREATE_ONLY fields when not in create mode (e.g. for
|
|
36
|
+
update operation)
|
|
37
|
+
- keeps all other field definitions unchanged
|
|
38
|
+
|
|
39
|
+
The resulting model reuses the interface model while allowing most fields
|
|
40
|
+
to be used in API input schemas, except those explicitly marked as hidden
|
|
41
|
+
or restricted by CREATE_ONLY.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def _is_readonly(metadata: list[Any]) -> bool:
|
|
45
|
+
return api.HIDDEN in metadata or (CREATE_ONLY in metadata and not createMode)
|
|
46
|
+
|
|
47
|
+
def _enforce_readonly(finfo: FieldInfo) -> FieldInfo:
|
|
48
|
+
extra = getattr(finfo, "json_schema_extra", None)
|
|
49
|
+
if not isinstance(extra, dict):
|
|
50
|
+
extra = {}
|
|
51
|
+
|
|
52
|
+
extra["readOnly"] = True
|
|
53
|
+
finfo.json_schema_extra = extra
|
|
54
|
+
return finfo
|
|
55
|
+
|
|
56
|
+
def _transform_annotation(annotation: Any) -> Any:
|
|
57
|
+
origin = get_origin(annotation)
|
|
58
|
+
args = get_args(annotation)
|
|
59
|
+
|
|
60
|
+
if origin is Annotated:
|
|
61
|
+
inner_type, *metadata = args
|
|
62
|
+
|
|
63
|
+
if _is_readonly(metadata):
|
|
64
|
+
metadata = [
|
|
65
|
+
_enforce_readonly(m) if isinstance(m, FieldInfo) else m
|
|
66
|
+
for m in metadata
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
return Annotated[_transform_annotation(inner_type), tuple(metadata)]
|
|
70
|
+
|
|
71
|
+
if origin in (list, dict, tuple, set, Union):
|
|
72
|
+
return origin[tuple(_transform_annotation(a) for a in args)]
|
|
73
|
+
|
|
74
|
+
if isinstance(annotation, type) and issubclass(annotation, BaseModel):
|
|
75
|
+
return _build_model(annotation, createMode)
|
|
76
|
+
|
|
77
|
+
return annotation
|
|
78
|
+
|
|
79
|
+
fields = {}
|
|
80
|
+
for fname, finfo in deepcopy(model.model_fields).items():
|
|
81
|
+
new_type = _transform_annotation(finfo.annotation)
|
|
82
|
+
|
|
83
|
+
metadata = getattr(finfo, "metadata", [])
|
|
84
|
+
|
|
85
|
+
if _is_readonly(metadata):
|
|
86
|
+
finfo = _enforce_readonly(finfo)
|
|
87
|
+
|
|
88
|
+
fields[fname] = (new_type, finfo)
|
|
89
|
+
|
|
90
|
+
kwargs: dict[str, Any] = {}
|
|
91
|
+
if (doc := model.__doc__) is not None:
|
|
92
|
+
kwargs["__doc__"] = doc
|
|
93
|
+
|
|
94
|
+
kwargs.update(fields)
|
|
95
|
+
modelType = "Create" if createMode else "Update"
|
|
96
|
+
return create_model(
|
|
97
|
+
f"{model.__name__}{modelType}",
|
|
98
|
+
__base__=model,
|
|
99
|
+
__module__=model.__module__,
|
|
100
|
+
__cls_kwargs__={"extra": "forbid"},
|
|
101
|
+
**kwargs,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@cache
|
|
106
|
+
def _settings() -> Settings:
|
|
107
|
+
try:
|
|
108
|
+
return SiteSettings()
|
|
109
|
+
except (exceptions.SettingsError, ValidationError) as e:
|
|
110
|
+
raise HTTPException(
|
|
111
|
+
status.HTTP_500_INTERNAL_SERVER_ERROR, f"invalid site settings\n{e}"
|
|
112
|
+
) from e
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
_default_settings = cache(Settings)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _instance_update_model() -> type[BaseModel]:
|
|
119
|
+
pm = plugin_manager(_settings())
|
|
120
|
+
return _build_model(interface.Instance.composite(pm))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _instance_create_model() -> type[BaseModel]:
|
|
124
|
+
pm = plugin_manager(_settings())
|
|
125
|
+
return _build_model(interface.Instance.composite(pm), True)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@cache
|
|
129
|
+
def _role_model() -> type[interface.Role]:
|
|
130
|
+
pm = plugin_manager(_settings())
|
|
131
|
+
return interface.Role.composite(pm)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
SETTINGS = _settings()
|
|
135
|
+
if TYPE_CHECKING:
|
|
136
|
+
InstanceModel = interface.Instance
|
|
137
|
+
InstanceCreateModel = interface.Instance
|
|
138
|
+
else:
|
|
139
|
+
InstanceModel: interface.Instance = _instance_update_model()
|
|
140
|
+
InstanceCreateModel: interface.Instance = _instance_create_model()
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
import uuid
|
|
5
|
+
from typing import Annotated
|
|
6
|
+
|
|
7
|
+
from fastapi import APIRouter, Depends, HTTPException, status
|
|
8
|
+
|
|
9
|
+
from pglift import databases, postgresql
|
|
10
|
+
from pglift.models import interface, system
|
|
11
|
+
from pglift.settings import Settings
|
|
12
|
+
|
|
13
|
+
from . import deps, tasks_manager
|
|
14
|
+
|
|
15
|
+
router = APIRouter(prefix="/{version}/{instance_name}/databases", tags=["databases"])
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@router.get("")
|
|
19
|
+
async def get_databases(
|
|
20
|
+
instance: Annotated[system.Instance, Depends(deps.instance)],
|
|
21
|
+
) -> list[interface.DatabaseListItem]:
|
|
22
|
+
return await databases.ls(instance.postgresql)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@router.get("/{dbname}")
|
|
26
|
+
async def get_database(
|
|
27
|
+
instance: Annotated[system.Instance, Depends(deps.instance)], dbname: str
|
|
28
|
+
) -> interface.Database:
|
|
29
|
+
if not await databases.exists(instance.postgresql, dbname):
|
|
30
|
+
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Database not found")
|
|
31
|
+
return await databases.get(instance.postgresql, dbname)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
async def _apply_database(
|
|
35
|
+
manifest: interface.Database, pg_instance: system.PostgreSQLInstance
|
|
36
|
+
) -> None:
|
|
37
|
+
async with postgresql.running(pg_instance):
|
|
38
|
+
await databases.apply(pg_instance, manifest)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@router.put("/{dbname}")
|
|
42
|
+
async def update_database(
|
|
43
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
44
|
+
instance: Annotated[system.Instance, Depends(deps.instance)],
|
|
45
|
+
manifest: interface.Database,
|
|
46
|
+
dbname: str,
|
|
47
|
+
background_tasks: Annotated[
|
|
48
|
+
tasks_manager.CustomBackgroundTasks, Depends(deps.get_custom_tasks)
|
|
49
|
+
],
|
|
50
|
+
) -> uuid.UUID:
|
|
51
|
+
if manifest.name != dbname:
|
|
52
|
+
raise HTTPException(
|
|
53
|
+
status_code=400,
|
|
54
|
+
detail="Manifest does not match URI database qualifier",
|
|
55
|
+
)
|
|
56
|
+
return background_tasks.add_task(
|
|
57
|
+
settings,
|
|
58
|
+
f"update database {dbname}",
|
|
59
|
+
_apply_database,
|
|
60
|
+
manifest,
|
|
61
|
+
instance.postgresql,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@router.post("")
|
|
66
|
+
async def create_database(
|
|
67
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
68
|
+
instance: Annotated[system.Instance, Depends(deps.instance)],
|
|
69
|
+
manifest: interface.Database,
|
|
70
|
+
background_tasks: Annotated[
|
|
71
|
+
tasks_manager.CustomBackgroundTasks, Depends(deps.get_custom_tasks)
|
|
72
|
+
],
|
|
73
|
+
) -> uuid.UUID:
|
|
74
|
+
return background_tasks.add_task(
|
|
75
|
+
settings,
|
|
76
|
+
f"create database {manifest.name}",
|
|
77
|
+
_apply_database,
|
|
78
|
+
manifest,
|
|
79
|
+
instance.postgresql,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
async def _delete_database(
|
|
84
|
+
instance: system.Instance, database: interface.Database
|
|
85
|
+
) -> None:
|
|
86
|
+
async with postgresql.running(instance.postgresql):
|
|
87
|
+
await databases.drop(instance.postgresql, database)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@router.delete("/{dbname}")
|
|
91
|
+
async def delete_database(
|
|
92
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
93
|
+
instance: Annotated[system.Instance, Depends(deps.instance)],
|
|
94
|
+
database: Annotated[interface.Database, Depends(deps.database)],
|
|
95
|
+
background_tasks: Annotated[
|
|
96
|
+
tasks_manager.CustomBackgroundTasks, Depends(deps.get_custom_tasks)
|
|
97
|
+
],
|
|
98
|
+
) -> uuid.UUID:
|
|
99
|
+
return background_tasks.add_task(
|
|
100
|
+
settings,
|
|
101
|
+
f"delete database {database.name}",
|
|
102
|
+
_delete_database,
|
|
103
|
+
instance,
|
|
104
|
+
database,
|
|
105
|
+
)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
from typing import Annotated
|
|
6
|
+
|
|
7
|
+
from fastapi import BackgroundTasks, Depends, HTTPException, status
|
|
8
|
+
|
|
9
|
+
from pglift import databases, exceptions
|
|
10
|
+
from pglift.models import interface, system
|
|
11
|
+
from pglift.settings import Settings, _postgresql
|
|
12
|
+
|
|
13
|
+
from . import _site
|
|
14
|
+
from .tasks_manager import CustomBackgroundTasks
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_custom_tasks(background_tasks: BackgroundTasks) -> CustomBackgroundTasks:
|
|
18
|
+
return CustomBackgroundTasks(background_tasks)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def settings() -> Settings:
|
|
22
|
+
return _site.SETTINGS
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def instance(
|
|
26
|
+
version: _postgresql.Version,
|
|
27
|
+
instance_name: str,
|
|
28
|
+
settings: Annotated[Settings, Depends(_site._settings)],
|
|
29
|
+
) -> system.Instance:
|
|
30
|
+
try:
|
|
31
|
+
return system.Instance.system_lookup(
|
|
32
|
+
instance_name, version=version, settings=settings
|
|
33
|
+
)
|
|
34
|
+
except exceptions.InstanceNotFound as e:
|
|
35
|
+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) from e
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
async def database(
|
|
39
|
+
instance: Annotated[system.Instance, Depends(instance)], dbname: str
|
|
40
|
+
) -> interface.Database:
|
|
41
|
+
if not (await databases.exists(instance.postgresql, dbname)):
|
|
42
|
+
raise HTTPException(status.HTTP_404_NOT_FOUND, "Database not found")
|
|
43
|
+
return await databases.get(instance.postgresql, dbname)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
import uuid
|
|
5
|
+
from typing import Annotated, Any
|
|
6
|
+
|
|
7
|
+
from fastapi import APIRouter, Depends, HTTPException, status
|
|
8
|
+
|
|
9
|
+
from pglift import instances, manager
|
|
10
|
+
from pglift.models import interface, system
|
|
11
|
+
from pglift.settings import PostgreSQLVersion, Settings
|
|
12
|
+
|
|
13
|
+
from . import _site, deps, tasks_manager
|
|
14
|
+
|
|
15
|
+
prefixed_router = APIRouter(prefix="/instances")
|
|
16
|
+
router = APIRouter(tags=["instances"])
|
|
17
|
+
|
|
18
|
+
task_registry: dict[str, dict[str, Any]] = {}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@router.get("/")
|
|
22
|
+
async def get_instances(
|
|
23
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
24
|
+
) -> list[interface.InstanceListItem]:
|
|
25
|
+
return [item async for item in instances.ls(settings)]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@router.get("/{version}")
|
|
29
|
+
async def get_instances_by_version(
|
|
30
|
+
version: PostgreSQLVersion,
|
|
31
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
32
|
+
) -> list[interface.InstanceListItem]:
|
|
33
|
+
return [item async for item in instances.ls(settings, version=version)]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
async def _apply_instance(manifest: interface.Instance, settings: Settings) -> None:
|
|
37
|
+
with manager.from_manifest(manifest, settings):
|
|
38
|
+
await instances.apply(settings, manifest)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@router.put("/{version}/{instance_name}", status_code=status.HTTP_202_ACCEPTED)
|
|
42
|
+
async def update_instance(
|
|
43
|
+
version: PostgreSQLVersion,
|
|
44
|
+
instance_name: str,
|
|
45
|
+
manifest: _site.InstanceModel,
|
|
46
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
47
|
+
background_tasks: Annotated[
|
|
48
|
+
tasks_manager.CustomBackgroundTasks, Depends(deps.get_custom_tasks)
|
|
49
|
+
],
|
|
50
|
+
) -> uuid.UUID:
|
|
51
|
+
if manifest.version != version or manifest.name != instance_name:
|
|
52
|
+
raise HTTPException(
|
|
53
|
+
status_code=400,
|
|
54
|
+
detail="Manifest does not match URI instance qualifier",
|
|
55
|
+
)
|
|
56
|
+
return background_tasks.add_task(
|
|
57
|
+
settings,
|
|
58
|
+
f"update instance {manifest.qualname}",
|
|
59
|
+
_apply_instance,
|
|
60
|
+
manifest,
|
|
61
|
+
settings,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@router.post("/", status_code=status.HTTP_202_ACCEPTED)
|
|
66
|
+
async def create_instance(
|
|
67
|
+
manifest: _site.InstanceCreateModel,
|
|
68
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
69
|
+
background_tasks: Annotated[
|
|
70
|
+
tasks_manager.CustomBackgroundTasks, Depends(deps.get_custom_tasks)
|
|
71
|
+
],
|
|
72
|
+
) -> uuid.UUID:
|
|
73
|
+
if instances.exists(manifest.name, manifest.version, settings):
|
|
74
|
+
raise HTTPException(status_code=409, detail="Instance already exists")
|
|
75
|
+
return background_tasks.add_task(
|
|
76
|
+
settings,
|
|
77
|
+
f"create instance {manifest.qualname}",
|
|
78
|
+
_apply_instance,
|
|
79
|
+
manifest,
|
|
80
|
+
settings,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@router.get("/{version}/{instance_name}")
|
|
85
|
+
async def get_instance(
|
|
86
|
+
instance: Annotated[system.Instance, Depends(deps.instance)],
|
|
87
|
+
) -> interface.Instance:
|
|
88
|
+
return await instances.get(instance)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
async def _delete_instance(instance: system.Instance) -> None:
|
|
92
|
+
with manager.from_instance(instance.postgresql):
|
|
93
|
+
await instances.drop(instance)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@router.delete("/{version}/{instance_name}")
|
|
97
|
+
async def delete_instance(
|
|
98
|
+
instance: Annotated[system.Instance, Depends(deps.instance)],
|
|
99
|
+
settings: Annotated[Settings, Depends(deps.settings)],
|
|
100
|
+
background_tasks: Annotated[
|
|
101
|
+
tasks_manager.CustomBackgroundTasks, Depends(deps.get_custom_tasks)
|
|
102
|
+
],
|
|
103
|
+
) -> uuid.UUID:
|
|
104
|
+
return background_tasks.add_task(
|
|
105
|
+
settings, f"delete instance {instance.qualname}", _delete_instance, instance
|
|
106
|
+
)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from pglift.pm import PluginManager as BasePluginManager
|
|
8
|
+
|
|
9
|
+
from . import __name__ as pkgname
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PluginManager(BasePluginManager):
|
|
13
|
+
ns = pkgname
|
|
14
|
+
modules = ("patroni", "pgbackrest", "pgbackrest.repo_path", "prometheus")
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Dalibo
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import uuid
|
|
7
|
+
from typing import Annotated
|
|
8
|
+
|
|
9
|
+
from fastapi import APIRouter, Depends, HTTPException
|
|
10
|
+
|
|
11
|
+
from pglift.settings import Settings
|
|
12
|
+
|
|
13
|
+
from . import deps
|
|
14
|
+
from ._compat import TypedDict
|
|
15
|
+
from .tasks_manager import TaskResponse, task_registry
|
|
16
|
+
|
|
17
|
+
router = APIRouter(prefix="/tasks", tags=["tasks"])
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class TaskListItem(TypedDict):
|
|
21
|
+
id: uuid.UUID
|
|
22
|
+
state: str
|
|
23
|
+
description: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@router.get("/")
|
|
27
|
+
def list_tasks() -> list[TaskListItem]:
|
|
28
|
+
"""
|
|
29
|
+
Return all registered background tasks and their current state.
|
|
30
|
+
"""
|
|
31
|
+
return [
|
|
32
|
+
TaskListItem(id=u, state=v.state, description=v.description)
|
|
33
|
+
for u, v in task_registry.tasks.items()
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@router.get("/{task_id}")
|
|
38
|
+
def get_task(
|
|
39
|
+
task_id: uuid.UUID, settings: Annotated[Settings, Depends(deps.settings)]
|
|
40
|
+
) -> TaskResponse:
|
|
41
|
+
"""
|
|
42
|
+
Return logs of a specific background task.
|
|
43
|
+
"""
|
|
44
|
+
task = task_registry.tasks.get(task_id)
|
|
45
|
+
if not task:
|
|
46
|
+
raise HTTPException(status_code=404, detail="Task not found")
|
|
47
|
+
formatter = logging.Formatter(fmt=settings.api.log_format)
|
|
48
|
+
return TaskResponse.from_request(task, formatter)
|