zboxapi 0.0.3__tar.gz → 0.0.4__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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.4"
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import contextlib
|
|
2
2
|
import fcntl
|
|
3
|
+
import os
|
|
3
4
|
import re
|
|
4
5
|
import socket
|
|
5
6
|
|
|
@@ -12,6 +13,7 @@ from typing import IO, Annotated
|
|
|
12
13
|
|
|
13
14
|
import uvicorn
|
|
14
15
|
from fastapi import APIRouter, Depends, FastAPI, HTTPException, Security, status
|
|
16
|
+
from fastapi.routing import APIRoute
|
|
15
17
|
from fastapi.security.api_key import APIKey, APIKeyHeader
|
|
16
18
|
from pydantic import AfterValidator, BaseModel
|
|
17
19
|
from pydantic_core import PydanticCustomError
|
|
@@ -107,6 +109,17 @@ def sort_hosts_lines(item: dict):
|
|
|
107
109
|
)
|
|
108
110
|
|
|
109
111
|
|
|
112
|
+
def simplify_operation_ids(api: FastAPI) -> None:
|
|
113
|
+
"""
|
|
114
|
+
Update operation IDs so that generated API clients have simpler function
|
|
115
|
+
names.
|
|
116
|
+
"""
|
|
117
|
+
for route in api.routes:
|
|
118
|
+
if isinstance(route, APIRoute) and not route.operation_id:
|
|
119
|
+
tag = route.tags[0] if route.tags else "default"
|
|
120
|
+
route.operation_id = f"{tag}_{route.name}"
|
|
121
|
+
|
|
122
|
+
|
|
110
123
|
def make_list(obj):
|
|
111
124
|
return obj if isinstance(obj, list) else [obj]
|
|
112
125
|
|
|
@@ -163,24 +176,24 @@ def validate_fqdn(value: str):
|
|
|
163
176
|
FQDN = Annotated[str, AfterValidator(validate_fqdn)]
|
|
164
177
|
|
|
165
178
|
|
|
166
|
-
class
|
|
179
|
+
class HostsCreate(BaseModel):
|
|
167
180
|
ip: IPv4Address
|
|
168
181
|
fqdn: FQDN
|
|
169
182
|
|
|
170
183
|
|
|
171
|
-
class
|
|
184
|
+
class HostsDelete(BaseModel):
|
|
172
185
|
ip: IPv4Address
|
|
173
186
|
fqdn: FQDN
|
|
174
187
|
|
|
175
188
|
|
|
176
|
-
class
|
|
189
|
+
class HostsUpdate(BaseModel):
|
|
177
190
|
ip: IPv4Address
|
|
178
191
|
fqdn: FQDN
|
|
179
192
|
new_ip: IPv4Address
|
|
180
193
|
new_fqdn: FQDN
|
|
181
194
|
|
|
182
195
|
|
|
183
|
-
class
|
|
196
|
+
class HostsView(BaseModel):
|
|
184
197
|
ip: IPv4Address
|
|
185
198
|
fqdn: str
|
|
186
199
|
|
|
@@ -192,7 +205,7 @@ hosts_router = APIRouter(prefix="/hosts", tags=["hosts"])
|
|
|
192
205
|
def get_hosts(
|
|
193
206
|
ip: IPv4Address | None = None,
|
|
194
207
|
fqdn: str | None = None,
|
|
195
|
-
) -> list[
|
|
208
|
+
) -> list[HostsView]:
|
|
196
209
|
with get_hosts_file_object() as hosts_fo:
|
|
197
210
|
hosts_lines = get_hosts_lines(hosts_fo)
|
|
198
211
|
hosts_lines = filter_hosts_file(hosts_lines, ip, fqdn)
|
|
@@ -200,7 +213,7 @@ def get_hosts(
|
|
|
200
213
|
|
|
201
214
|
|
|
202
215
|
@hosts_router.post("")
|
|
203
|
-
def add_hosts(lines_in: list[
|
|
216
|
+
def add_hosts(lines_in: list[HostsCreate] | HostsCreate) -> list[HostsView]:
|
|
204
217
|
with get_hosts_file_object() as hosts_fo:
|
|
205
218
|
hosts_lines = get_hosts_lines(hosts_fo)
|
|
206
219
|
for line in make_list(lines_in):
|
|
@@ -213,7 +226,7 @@ def add_hosts(lines_in: list[DnsCreate] | DnsCreate) -> list[DnsView]:
|
|
|
213
226
|
|
|
214
227
|
|
|
215
228
|
@hosts_router.put("")
|
|
216
|
-
def update_hosts(lines_in: list[
|
|
229
|
+
def update_hosts(lines_in: list[HostsUpdate] | HostsUpdate) -> list[HostsView]:
|
|
217
230
|
with get_hosts_file_object() as hosts_fo:
|
|
218
231
|
hosts_lines = get_hosts_lines(hosts_fo)
|
|
219
232
|
for line in make_list(lines_in):
|
|
@@ -228,7 +241,7 @@ def update_hosts(lines_in: list[DnsUpdate] | DnsUpdate) -> list[DnsView]:
|
|
|
228
241
|
|
|
229
242
|
|
|
230
243
|
@hosts_router.delete("")
|
|
231
|
-
def delete_hosts(lines_in: list[
|
|
244
|
+
def delete_hosts(lines_in: list[HostsDelete] | HostsDelete) -> list[HostsView]:
|
|
232
245
|
with get_hosts_file_object() as hosts_fo:
|
|
233
246
|
hosts_lines = get_hosts_lines(hosts_fo)
|
|
234
247
|
for line in make_list(lines_in):
|
|
@@ -241,15 +254,23 @@ def delete_hosts(lines_in: list[DnsDelete] | DnsDelete) -> list[DnsView]:
|
|
|
241
254
|
return hosts_lines
|
|
242
255
|
|
|
243
256
|
|
|
244
|
-
|
|
257
|
+
zboxapi_root_path = os.getenv("ZBOXAPI_ROOT_PATH", None)
|
|
258
|
+
|
|
259
|
+
app = FastAPI(
|
|
260
|
+
title="zBox API",
|
|
261
|
+
root_path=zboxapi_root_path,
|
|
262
|
+
dependencies=[Depends(validate_api_key)],
|
|
263
|
+
)
|
|
245
264
|
app.include_router(hosts_router)
|
|
265
|
+
simplify_operation_ids(app)
|
|
266
|
+
|
|
246
267
|
ZPOD_PASSWORD = get_zpod_password()
|
|
247
268
|
|
|
248
269
|
|
|
249
270
|
def launch():
|
|
250
271
|
uvicorn.run(
|
|
251
272
|
app,
|
|
252
|
-
host="
|
|
273
|
+
host="127.0.0.1",
|
|
253
274
|
port=8000,
|
|
254
275
|
)
|
|
255
276
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.0.3"
|
|
File without changes
|