simple-module-feature-flags 0.0.1__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.
- simple_module_feature_flags-0.0.1/.gitignore +59 -0
- simple_module_feature_flags-0.0.1/LICENSE +21 -0
- simple_module_feature_flags-0.0.1/PKG-INFO +83 -0
- simple_module_feature_flags-0.0.1/README.md +55 -0
- simple_module_feature_flags-0.0.1/feature_flags/__init__.py +1 -0
- simple_module_feature_flags-0.0.1/feature_flags/constants.py +31 -0
- simple_module_feature_flags-0.0.1/feature_flags/contracts/__init__.py +13 -0
- simple_module_feature_flags-0.0.1/feature_flags/contracts/schemas.py +47 -0
- simple_module_feature_flags-0.0.1/feature_flags/deps.py +27 -0
- simple_module_feature_flags-0.0.1/feature_flags/endpoints/__init__.py +0 -0
- simple_module_feature_flags-0.0.1/feature_flags/endpoints/api.py +136 -0
- simple_module_feature_flags-0.0.1/feature_flags/endpoints/views.py +98 -0
- simple_module_feature_flags-0.0.1/feature_flags/locales/en.json +40 -0
- simple_module_feature_flags-0.0.1/feature_flags/models.py +42 -0
- simple_module_feature_flags-0.0.1/feature_flags/module.py +74 -0
- simple_module_feature_flags-0.0.1/feature_flags/pages/Browse.tsx +277 -0
- simple_module_feature_flags-0.0.1/feature_flags/py.typed +0 -0
- simple_module_feature_flags-0.0.1/feature_flags/service.py +189 -0
- simple_module_feature_flags-0.0.1/package.json +16 -0
- simple_module_feature_flags-0.0.1/pyproject.toml +48 -0
- simple_module_feature_flags-0.0.1/tests/test_feature_flags_api.py +142 -0
- simple_module_feature_flags-0.0.1/tests/test_feature_flags_service.py +211 -0
- simple_module_feature_flags-0.0.1/tsconfig.json +11 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.venv/
|
|
8
|
+
*.egg
|
|
9
|
+
|
|
10
|
+
# UV
|
|
11
|
+
uv.lock
|
|
12
|
+
|
|
13
|
+
# Node
|
|
14
|
+
node_modules/
|
|
15
|
+
|
|
16
|
+
# IDE
|
|
17
|
+
.idea/
|
|
18
|
+
.vscode/
|
|
19
|
+
*.swp
|
|
20
|
+
*.swo
|
|
21
|
+
|
|
22
|
+
# Environment
|
|
23
|
+
.env
|
|
24
|
+
|
|
25
|
+
# Database
|
|
26
|
+
*.db
|
|
27
|
+
*.sqlite3
|
|
28
|
+
|
|
29
|
+
# Module-managed runtime state (e.g. uploaded dataset files,
|
|
30
|
+
# default storage_dir for SM_DATASETS_STORAGE_DIR).
|
|
31
|
+
var/
|
|
32
|
+
|
|
33
|
+
# file_storage filesystem backend default root (override via SM_FILE_STORAGE_FS_ROOT_PATH).
|
|
34
|
+
uploads/
|
|
35
|
+
|
|
36
|
+
# Vite
|
|
37
|
+
host/static/dist/
|
|
38
|
+
|
|
39
|
+
# Auto-generated frontend module manifest (regenerated by the host at boot
|
|
40
|
+
# or via `make gen-pages`).
|
|
41
|
+
host/client_app/modules.manifest.json
|
|
42
|
+
host/client_app/modules.generated.ts
|
|
43
|
+
host/client_app/modules.generated.css
|
|
44
|
+
|
|
45
|
+
# Worktrees
|
|
46
|
+
.worktrees/
|
|
47
|
+
|
|
48
|
+
# Performance profiles
|
|
49
|
+
.memray/
|
|
50
|
+
.benchmarks/
|
|
51
|
+
|
|
52
|
+
# OS
|
|
53
|
+
.DS_Store
|
|
54
|
+
Thumbs.db
|
|
55
|
+
|
|
56
|
+
.playwright-cli/*
|
|
57
|
+
.playwright-mcp/*
|
|
58
|
+
host/client_app/.playwright-cli/*
|
|
59
|
+
.superpowers/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anto Subash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple_module_feature_flags
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Simple feature-flag module with per-tenant overrides and a consumer API for simple_module
|
|
5
|
+
Project-URL: Homepage, https://github.com/antosubash/simple_module_python
|
|
6
|
+
Project-URL: Repository, https://github.com/antosubash/simple_module_python
|
|
7
|
+
Project-URL: Issues, https://github.com/antosubash/simple_module_python/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/antosubash/simple_module_python/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Anto Subash <antosubash@live.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: feature-flags,multi-tenant,simple-module,toggles
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Framework :: FastAPI
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.12
|
|
24
|
+
Requires-Dist: simple-module-core==0.0.1
|
|
25
|
+
Requires-Dist: simple-module-db==0.0.1
|
|
26
|
+
Requires-Dist: simple-module-hosting==0.0.1
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# simple_module_feature_flags
|
|
30
|
+
|
|
31
|
+
Feature flags for [simple_module](https://github.com/antosubash/simple_module_python) apps. Global flags with per-tenant overrides, a tiny consumer API, and no external service to run.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install simple_module_feature_flags
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## What it provides
|
|
40
|
+
|
|
41
|
+
- `Flag` and `TenantFlagOverride` SQLModel tables.
|
|
42
|
+
- `is_enabled("flag.name", tenant_id=...)` consumer API.
|
|
43
|
+
- Admin UI at `/feature-flags/admin` — toggle flags, add tenant overrides.
|
|
44
|
+
- Cache layer so checking a flag on every request is cheap.
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
Gate a route:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from feature_flags import is_enabled # type: ignore[import-not-found]
|
|
52
|
+
from fastapi import APIRouter, Depends, HTTPException
|
|
53
|
+
|
|
54
|
+
router = APIRouter()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@router.get("/new-feature")
|
|
58
|
+
async def new_feature(tenant_id: int = Depends(current_tenant_id)):
|
|
59
|
+
if not await is_enabled("orders.new_pricing_engine", tenant_id=tenant_id):
|
|
60
|
+
raise HTTPException(404)
|
|
61
|
+
return {"rolled_out": True}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Seed a flag in a migration or admin UI:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
# via migration
|
|
68
|
+
Flag(name="orders.new_pricing_engine", enabled=False)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Tenant override:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
TenantFlagOverride(tenant_id=7, flag_name="orders.new_pricing_engine", enabled=True)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Depends on
|
|
78
|
+
|
|
79
|
+
- `simple_module_core`, `simple_module_db`, `simple_module_hosting`
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
MIT — see [LICENSE](https://github.com/antosubash/simple_module_python/blob/main/LICENSE).
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# simple_module_feature_flags
|
|
2
|
+
|
|
3
|
+
Feature flags for [simple_module](https://github.com/antosubash/simple_module_python) apps. Global flags with per-tenant overrides, a tiny consumer API, and no external service to run.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install simple_module_feature_flags
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## What it provides
|
|
12
|
+
|
|
13
|
+
- `Flag` and `TenantFlagOverride` SQLModel tables.
|
|
14
|
+
- `is_enabled("flag.name", tenant_id=...)` consumer API.
|
|
15
|
+
- Admin UI at `/feature-flags/admin` — toggle flags, add tenant overrides.
|
|
16
|
+
- Cache layer so checking a flag on every request is cheap.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Gate a route:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from feature_flags import is_enabled # type: ignore[import-not-found]
|
|
24
|
+
from fastapi import APIRouter, Depends, HTTPException
|
|
25
|
+
|
|
26
|
+
router = APIRouter()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@router.get("/new-feature")
|
|
30
|
+
async def new_feature(tenant_id: int = Depends(current_tenant_id)):
|
|
31
|
+
if not await is_enabled("orders.new_pricing_engine", tenant_id=tenant_id):
|
|
32
|
+
raise HTTPException(404)
|
|
33
|
+
return {"rolled_out": True}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Seed a flag in a migration or admin UI:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
# via migration
|
|
40
|
+
Flag(name="orders.new_pricing_engine", enabled=False)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Tenant override:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
TenantFlagOverride(tenant_id=7, flag_name="orders.new_pricing_engine", enabled=True)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Depends on
|
|
50
|
+
|
|
51
|
+
- `simple_module_core`, `simple_module_db`, `simple_module_hosting`
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT — see [LICENSE](https://github.com/antosubash/simple_module_python/blob/main/LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""FeatureFlags module."""
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Stable identifiers for the feature_flags module."""
|
|
2
|
+
|
|
3
|
+
PERM_FEATURE_FLAGS_VIEW = "feature_flags.view"
|
|
4
|
+
PERM_FEATURE_FLAGS_MANAGE = "feature_flags.manage"
|
|
5
|
+
|
|
6
|
+
# Prefix is load-bearing on SQLite, which has no per-module schemas.
|
|
7
|
+
TABLE_OVERRIDE = "feature_flags_override"
|
|
8
|
+
UQ_OVERRIDE_SCOPE_NAME = "uq_feature_flags_override_scope_scope_id_name"
|
|
9
|
+
|
|
10
|
+
SCOPE_SYSTEM = "system"
|
|
11
|
+
SCOPE_TENANT = "tenant"
|
|
12
|
+
ALL_SCOPES = (SCOPE_SYSTEM, SCOPE_TENANT)
|
|
13
|
+
# Empty string, not NULL: PG treats NULLs as distinct in unique indexes,
|
|
14
|
+
# which would let two "system" rows for the same flag coexist.
|
|
15
|
+
SYSTEM_SCOPE_ID = ""
|
|
16
|
+
|
|
17
|
+
SCOPE_MAX_LENGTH = 10
|
|
18
|
+
SCOPE_ID_MAX_LENGTH = 64
|
|
19
|
+
|
|
20
|
+
LOCALE_NAMESPACE = "feature_flags"
|
|
21
|
+
|
|
22
|
+
MENU_LABEL = "Feature Flags"
|
|
23
|
+
MENU_URL = "/feature_flags"
|
|
24
|
+
MENU_ICON = "flag"
|
|
25
|
+
MENU_ORDER = 45
|
|
26
|
+
|
|
27
|
+
PAGE_BROWSE = "FeatureFlags/Browse"
|
|
28
|
+
|
|
29
|
+
API_PREFIX = "/api/feature_flags"
|
|
30
|
+
VIEW_PREFIX = "/feature_flags"
|
|
31
|
+
QP_TENANT_ID = "tenant_id"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""feature_flags contracts — public interface for other modules."""
|
|
2
|
+
|
|
3
|
+
from feature_flags.contracts.schemas import (
|
|
4
|
+
FeatureFlagOverrideOut,
|
|
5
|
+
FeatureFlagView,
|
|
6
|
+
ToggleRequest,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"FeatureFlagOverrideOut",
|
|
11
|
+
"FeatureFlagView",
|
|
12
|
+
"ToggleRequest",
|
|
13
|
+
]
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""SQLModel DTOs for the feature_flags module."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
|
|
7
|
+
from pydantic import ConfigDict
|
|
8
|
+
from sqlmodel import Field, SQLModel
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FeatureFlagOverrideOut(SQLModel):
|
|
12
|
+
"""Stored override row as returned by the service/API."""
|
|
13
|
+
|
|
14
|
+
model_config = ConfigDict(from_attributes=True)
|
|
15
|
+
|
|
16
|
+
id: int
|
|
17
|
+
scope: str
|
|
18
|
+
scope_id: str
|
|
19
|
+
name: str
|
|
20
|
+
enabled: bool
|
|
21
|
+
created_at: datetime | None = None
|
|
22
|
+
updated_at: datetime | None = None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class FeatureFlagView(SQLModel):
|
|
26
|
+
"""A flag as shown in the admin UI for a given scope.
|
|
27
|
+
|
|
28
|
+
``enabled`` is the value an ``is_enabled(name, tenant_id=...)`` call
|
|
29
|
+
would return for this scope. ``overridden`` reports whether the row
|
|
30
|
+
that produced ``enabled`` lives at *this* scope (vs. inherited from
|
|
31
|
+
system or default). ``system_enabled`` exposes the underlying system
|
|
32
|
+
value when viewing a tenant scope, so the UI can show "inheriting:
|
|
33
|
+
on/off" next to the toggle.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
name: str
|
|
37
|
+
description: str = ""
|
|
38
|
+
default_enabled: bool
|
|
39
|
+
enabled: bool
|
|
40
|
+
overridden: bool
|
|
41
|
+
system_enabled: bool | None = None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ToggleRequest(SQLModel):
|
|
45
|
+
"""Body for PUT /api/feature_flags/[tenant/{tenant_id}/]{name} — sets an override."""
|
|
46
|
+
|
|
47
|
+
enabled: bool = Field(...)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""FastAPI dependencies for the feature_flags module."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Annotated
|
|
6
|
+
|
|
7
|
+
from fastapi import Depends, Request
|
|
8
|
+
from simple_module_core.feature_flags import FeatureFlagRegistry
|
|
9
|
+
from simple_module_db.deps import get_db
|
|
10
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
11
|
+
|
|
12
|
+
from feature_flags.service import FeatureFlagService
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async def get_feature_flag_service(
|
|
16
|
+
db: AsyncSession = Depends(get_db),
|
|
17
|
+
) -> FeatureFlagService:
|
|
18
|
+
return FeatureFlagService(db)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def get_feature_flag_registry(request: Request) -> FeatureFlagRegistry:
|
|
22
|
+
"""Return the process-wide FeatureFlagRegistry owned by the framework."""
|
|
23
|
+
return request.app.state.sm.feature_flags
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
FeatureFlagServiceDep = Annotated[FeatureFlagService, Depends(get_feature_flag_service)]
|
|
27
|
+
FeatureFlagRegistryDep = Annotated[FeatureFlagRegistry, Depends(get_feature_flag_registry)]
|
|
File without changes
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""REST API endpoints for feature_flags management."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from fastapi import APIRouter, Depends, HTTPException
|
|
6
|
+
from simple_module_core.feature_flags import FeatureFlagRegistry
|
|
7
|
+
from simple_module_hosting.permissions import RequiresPermission
|
|
8
|
+
|
|
9
|
+
from feature_flags.constants import (
|
|
10
|
+
PERM_FEATURE_FLAGS_MANAGE,
|
|
11
|
+
PERM_FEATURE_FLAGS_VIEW,
|
|
12
|
+
SCOPE_TENANT,
|
|
13
|
+
)
|
|
14
|
+
from feature_flags.contracts.schemas import FeatureFlagView, ToggleRequest
|
|
15
|
+
from feature_flags.deps import FeatureFlagRegistryDep, FeatureFlagServiceDep
|
|
16
|
+
|
|
17
|
+
router = APIRouter()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _ensure_registered(name: str, registry: FeatureFlagRegistry) -> None:
|
|
21
|
+
# A typo here would silently persist a dead row that ``list_flags`` filters out.
|
|
22
|
+
if name not in {f.name for f in registry.all_flags}:
|
|
23
|
+
raise HTTPException(status_code=404, detail="Feature flag not registered")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@router.get(
|
|
27
|
+
"/",
|
|
28
|
+
response_model=list[FeatureFlagView],
|
|
29
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_VIEW))],
|
|
30
|
+
)
|
|
31
|
+
async def list_flags(
|
|
32
|
+
service: FeatureFlagServiceDep,
|
|
33
|
+
registry: FeatureFlagRegistryDep,
|
|
34
|
+
) -> list[FeatureFlagView]:
|
|
35
|
+
return await service.list_flags(registry)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@router.get(
|
|
39
|
+
"/{name}",
|
|
40
|
+
response_model=FeatureFlagView,
|
|
41
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_VIEW))],
|
|
42
|
+
)
|
|
43
|
+
async def get_flag(
|
|
44
|
+
name: str,
|
|
45
|
+
service: FeatureFlagServiceDep,
|
|
46
|
+
registry: FeatureFlagRegistryDep,
|
|
47
|
+
) -> FeatureFlagView:
|
|
48
|
+
view = service.build_view(registry, name)
|
|
49
|
+
if view is None:
|
|
50
|
+
raise HTTPException(status_code=404, detail="Feature flag not registered")
|
|
51
|
+
return view
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@router.put(
|
|
55
|
+
"/{name}",
|
|
56
|
+
response_model=FeatureFlagView,
|
|
57
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_MANAGE))],
|
|
58
|
+
)
|
|
59
|
+
async def set_override(
|
|
60
|
+
name: str,
|
|
61
|
+
body: ToggleRequest,
|
|
62
|
+
service: FeatureFlagServiceDep,
|
|
63
|
+
registry: FeatureFlagRegistryDep,
|
|
64
|
+
) -> FeatureFlagView:
|
|
65
|
+
_ensure_registered(name, registry)
|
|
66
|
+
await service.set_override(name, body.enabled, registry=registry)
|
|
67
|
+
view = service.build_view(registry, name)
|
|
68
|
+
assert view is not None # _ensure_registered already proved it exists
|
|
69
|
+
return view
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@router.delete(
|
|
73
|
+
"/{name}",
|
|
74
|
+
status_code=204,
|
|
75
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_MANAGE))],
|
|
76
|
+
)
|
|
77
|
+
async def clear_override(
|
|
78
|
+
name: str,
|
|
79
|
+
service: FeatureFlagServiceDep,
|
|
80
|
+
registry: FeatureFlagRegistryDep,
|
|
81
|
+
) -> None:
|
|
82
|
+
cleared = await service.clear_override(name, registry=registry)
|
|
83
|
+
if not cleared:
|
|
84
|
+
raise HTTPException(status_code=404, detail="No override set for this flag")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@router.get(
|
|
88
|
+
"/tenant/{tenant_id}",
|
|
89
|
+
response_model=list[FeatureFlagView],
|
|
90
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_VIEW))],
|
|
91
|
+
)
|
|
92
|
+
async def list_flags_for_tenant(
|
|
93
|
+
tenant_id: str,
|
|
94
|
+
service: FeatureFlagServiceDep,
|
|
95
|
+
registry: FeatureFlagRegistryDep,
|
|
96
|
+
) -> list[FeatureFlagView]:
|
|
97
|
+
return await service.list_flags(registry, tenant_id=tenant_id)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@router.put(
|
|
101
|
+
"/tenant/{tenant_id}/{name}",
|
|
102
|
+
response_model=FeatureFlagView,
|
|
103
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_MANAGE))],
|
|
104
|
+
)
|
|
105
|
+
async def set_tenant_override(
|
|
106
|
+
tenant_id: str,
|
|
107
|
+
name: str,
|
|
108
|
+
body: ToggleRequest,
|
|
109
|
+
service: FeatureFlagServiceDep,
|
|
110
|
+
registry: FeatureFlagRegistryDep,
|
|
111
|
+
) -> FeatureFlagView:
|
|
112
|
+
_ensure_registered(name, registry)
|
|
113
|
+
await service.set_override(
|
|
114
|
+
name, body.enabled, registry=registry, scope=SCOPE_TENANT, scope_id=tenant_id
|
|
115
|
+
)
|
|
116
|
+
view = service.build_view(registry, name, tenant_id=tenant_id)
|
|
117
|
+
assert view is not None # _ensure_registered already proved it exists
|
|
118
|
+
return view
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@router.delete(
|
|
122
|
+
"/tenant/{tenant_id}/{name}",
|
|
123
|
+
status_code=204,
|
|
124
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_MANAGE))],
|
|
125
|
+
)
|
|
126
|
+
async def clear_tenant_override(
|
|
127
|
+
tenant_id: str,
|
|
128
|
+
name: str,
|
|
129
|
+
service: FeatureFlagServiceDep,
|
|
130
|
+
registry: FeatureFlagRegistryDep,
|
|
131
|
+
) -> None:
|
|
132
|
+
cleared = await service.clear_override(
|
|
133
|
+
name, registry=registry, scope=SCOPE_TENANT, scope_id=tenant_id
|
|
134
|
+
)
|
|
135
|
+
if not cleared:
|
|
136
|
+
raise HTTPException(status_code=404, detail="No override set for this flag")
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Inertia view endpoints for feature_flags admin UI.
|
|
2
|
+
|
|
3
|
+
The browse page renders either system-scope or a tenant-scope view of the
|
|
4
|
+
flags. Tenant scope is selected via a ``tenant_id`` query string. The form
|
|
5
|
+
actions for toggling/clearing accept the same ``tenant_id`` query so the
|
|
6
|
+
frontend doesn't need separate routes per scope.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from fastapi import APIRouter, Depends, Request
|
|
12
|
+
from inertia import InertiaResponse
|
|
13
|
+
from simple_module_hosting.inertia_deps import InertiaDep
|
|
14
|
+
from simple_module_hosting.permissions import RequiresPermission
|
|
15
|
+
from starlette.responses import RedirectResponse
|
|
16
|
+
|
|
17
|
+
from feature_flags.constants import (
|
|
18
|
+
MENU_URL,
|
|
19
|
+
PAGE_BROWSE,
|
|
20
|
+
PERM_FEATURE_FLAGS_MANAGE,
|
|
21
|
+
PERM_FEATURE_FLAGS_VIEW,
|
|
22
|
+
QP_TENANT_ID,
|
|
23
|
+
SCOPE_SYSTEM,
|
|
24
|
+
SCOPE_TENANT,
|
|
25
|
+
SYSTEM_SCOPE_ID,
|
|
26
|
+
)
|
|
27
|
+
from feature_flags.deps import FeatureFlagRegistryDep, FeatureFlagServiceDep
|
|
28
|
+
|
|
29
|
+
router = APIRouter()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _redirect_for_tenant(tenant_id: str | None) -> RedirectResponse:
|
|
33
|
+
target = MENU_URL if not tenant_id else f"{MENU_URL}?{QP_TENANT_ID}={tenant_id}"
|
|
34
|
+
return RedirectResponse(target, status_code=303)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _scope_args(tenant_id: str | None) -> dict[str, str]:
|
|
38
|
+
if tenant_id:
|
|
39
|
+
return {"scope": SCOPE_TENANT, "scope_id": tenant_id}
|
|
40
|
+
return {"scope": SCOPE_SYSTEM, "scope_id": SYSTEM_SCOPE_ID}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@router.get(
|
|
44
|
+
"/",
|
|
45
|
+
response_model=None,
|
|
46
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_VIEW))],
|
|
47
|
+
)
|
|
48
|
+
async def browse(
|
|
49
|
+
inertia: InertiaDep,
|
|
50
|
+
service: FeatureFlagServiceDep,
|
|
51
|
+
registry: FeatureFlagRegistryDep,
|
|
52
|
+
tenant_id: str | None = None,
|
|
53
|
+
) -> InertiaResponse:
|
|
54
|
+
flags = await service.list_flags(registry, tenant_id=tenant_id)
|
|
55
|
+
tenants = await service.list_tenants_with_overrides()
|
|
56
|
+
return await inertia.render(
|
|
57
|
+
PAGE_BROWSE,
|
|
58
|
+
{
|
|
59
|
+
"flags": [f.model_dump(mode="json") for f in flags],
|
|
60
|
+
"tenant_id": tenant_id,
|
|
61
|
+
"tenants": tenants,
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@router.post(
|
|
67
|
+
"/{name}/toggle",
|
|
68
|
+
response_model=None,
|
|
69
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_MANAGE))],
|
|
70
|
+
)
|
|
71
|
+
async def toggle_action(
|
|
72
|
+
name: str,
|
|
73
|
+
request: Request,
|
|
74
|
+
service: FeatureFlagServiceDep,
|
|
75
|
+
registry: FeatureFlagRegistryDep,
|
|
76
|
+
tenant_id: str | None = None,
|
|
77
|
+
) -> RedirectResponse:
|
|
78
|
+
if name not in {f.name for f in registry.all_flags}:
|
|
79
|
+
return _redirect_for_tenant(tenant_id)
|
|
80
|
+
body = await request.json()
|
|
81
|
+
enabled = bool(body.get("enabled", False))
|
|
82
|
+
await service.set_override(name, enabled, registry=registry, **_scope_args(tenant_id))
|
|
83
|
+
return _redirect_for_tenant(tenant_id)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@router.post(
|
|
87
|
+
"/{name}/clear",
|
|
88
|
+
response_model=None,
|
|
89
|
+
dependencies=[Depends(RequiresPermission(PERM_FEATURE_FLAGS_MANAGE))],
|
|
90
|
+
)
|
|
91
|
+
async def clear_action(
|
|
92
|
+
name: str,
|
|
93
|
+
service: FeatureFlagServiceDep,
|
|
94
|
+
registry: FeatureFlagRegistryDep,
|
|
95
|
+
tenant_id: str | None = None,
|
|
96
|
+
) -> RedirectResponse:
|
|
97
|
+
await service.clear_override(name, registry=registry, **_scope_args(tenant_id))
|
|
98
|
+
return _redirect_for_tenant(tenant_id)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"browse": {
|
|
3
|
+
"title": "Feature Flags",
|
|
4
|
+
"description": "Toggle features exposed by installed modules. Overrides persist across restarts.",
|
|
5
|
+
"empty_title": "No feature flags registered",
|
|
6
|
+
"empty_description": "Modules declare flags via register_feature_flags(); install one to see it here.",
|
|
7
|
+
"count_one": "{count} flag",
|
|
8
|
+
"count_other": "{count} flags",
|
|
9
|
+
"viewing_system": "Viewing system-level flags. Switch scope to override per tenant.",
|
|
10
|
+
"viewing_tenant": "Viewing overrides for tenant \"{tenant_id}\". Tenant overrides beat the system value.",
|
|
11
|
+
"tenant_id_label": "Tenant ID",
|
|
12
|
+
"tenant_id_placeholder": "e.g. acme",
|
|
13
|
+
"scope_label": "Scope",
|
|
14
|
+
"scope_system": "System (all tenants)",
|
|
15
|
+
"scope_tenant": "Specific tenant",
|
|
16
|
+
"go": "View",
|
|
17
|
+
"back_to_system": "Back to system view",
|
|
18
|
+
"tenants_with_overrides": "Tenants with overrides"
|
|
19
|
+
},
|
|
20
|
+
"table": {
|
|
21
|
+
"name": "Flag",
|
|
22
|
+
"description": "Description",
|
|
23
|
+
"default": "Default",
|
|
24
|
+
"status": "Status",
|
|
25
|
+
"actions": "Actions",
|
|
26
|
+
"enabled": "Enabled",
|
|
27
|
+
"disabled": "Disabled",
|
|
28
|
+
"overridden": "Override active",
|
|
29
|
+
"following_default": "Following default",
|
|
30
|
+
"following_system": "Following system",
|
|
31
|
+
"system_value": "System: {value}",
|
|
32
|
+
"clear_override": "Clear override"
|
|
33
|
+
},
|
|
34
|
+
"toasts": {
|
|
35
|
+
"enabled": "\"{name}\" enabled",
|
|
36
|
+
"disabled": "\"{name}\" disabled",
|
|
37
|
+
"cleared": "Override cleared for \"{name}\"",
|
|
38
|
+
"toggle_failed": "Failed to update flag"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""SQLModel tables for the feature_flags module.
|
|
2
|
+
|
|
3
|
+
Stores persisted overrides for flags that modules declare via
|
|
4
|
+
``register_feature_flags``. The definition of a flag (name, description,
|
|
5
|
+
default) lives in code; this table only records admin-applied overrides so
|
|
6
|
+
they survive across app restarts.
|
|
7
|
+
|
|
8
|
+
A row's ``scope`` selects whether the override applies globally
|
|
9
|
+
(``system``, ``scope_id=""``) or to a single tenant (``tenant``,
|
|
10
|
+
``scope_id=<tenant_id>``). Resolution at runtime: tenant > system > default.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from simple_module_db.base import create_module_base
|
|
16
|
+
from simple_module_db.mixins import AuditMixin
|
|
17
|
+
from sqlalchemy import UniqueConstraint
|
|
18
|
+
from sqlmodel import Field
|
|
19
|
+
|
|
20
|
+
from feature_flags.constants import (
|
|
21
|
+
SCOPE_ID_MAX_LENGTH,
|
|
22
|
+
SCOPE_MAX_LENGTH,
|
|
23
|
+
SCOPE_SYSTEM,
|
|
24
|
+
SYSTEM_SCOPE_ID,
|
|
25
|
+
TABLE_OVERRIDE,
|
|
26
|
+
UQ_OVERRIDE_SCOPE_NAME,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
Base = create_module_base("feature_flags")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class FeatureFlagOverride(Base, AuditMixin, table=True): # ty: ignore[unsupported-base]
|
|
33
|
+
"""Persisted override for a single feature flag at a given scope."""
|
|
34
|
+
|
|
35
|
+
__tablename__ = TABLE_OVERRIDE
|
|
36
|
+
__table_args__ = (UniqueConstraint("scope", "scope_id", "name", name=UQ_OVERRIDE_SCOPE_NAME),)
|
|
37
|
+
|
|
38
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
39
|
+
scope: str = Field(default=SCOPE_SYSTEM, max_length=SCOPE_MAX_LENGTH, index=True)
|
|
40
|
+
scope_id: str = Field(default=SYSTEM_SCOPE_ID, max_length=SCOPE_ID_MAX_LENGTH, index=True)
|
|
41
|
+
name: str = Field(max_length=200, index=True)
|
|
42
|
+
enabled: bool = Field(default=False)
|