mirror of https://github.com/Aidaho12/haproxy-wi
v8.2: Add return statements and enhance response handling
Add return statements to `insert_*` methods for consistency. Introduce a `delete_section` method and implement it in the view. Enhance response handling with new `IdDataStrResponse` and update OpenAPI spec to better reflect data structures.pull/399/head
parent
3a9c18392b
commit
7173ed8357
|
@ -95,24 +95,24 @@ def select_saved_servers(**kwargs):
|
||||||
|
|
||||||
def insert_new_section(server_id: int, section_type: str, section_name: str, body: HaproxyConfigRequest):
|
def insert_new_section(server_id: int, section_type: str, section_name: str, body: HaproxyConfigRequest):
|
||||||
try:
|
try:
|
||||||
HaproxySection.insert(
|
return (HaproxySection.insert(
|
||||||
server_id=server_id,
|
server_id=server_id,
|
||||||
type=section_type,
|
type=section_type,
|
||||||
name=section_name,
|
name=section_name,
|
||||||
config=body.model_dump(mode='json')
|
config=body.model_dump(mode='json')
|
||||||
).execute()
|
).execute())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
out_error(e)
|
out_error(e)
|
||||||
|
|
||||||
|
|
||||||
def insert_or_update_new_section(server_id: int, section_type: str, section_name: str, body: Union[HaproxyGlobalRequest, HaproxyDefaultsRequest]):
|
def insert_or_update_new_section(server_id: int, section_type: str, section_name: str, body: Union[HaproxyGlobalRequest, HaproxyDefaultsRequest]):
|
||||||
try:
|
try:
|
||||||
HaproxySection.insert(
|
return (HaproxySection.insert(
|
||||||
server_id=server_id,
|
server_id=server_id,
|
||||||
type=section_type,
|
type=section_type,
|
||||||
name=section_name,
|
name=section_name,
|
||||||
config=body.model_dump(mode='json')
|
config=body.model_dump(mode='json')
|
||||||
).on_conflict('replace').execute()
|
).on_conflict('replace').execute())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
out_error(e)
|
out_error(e)
|
||||||
|
|
||||||
|
@ -141,3 +141,16 @@ def get_section(server_id: int, section_type: str, section_name: str) -> Haproxy
|
||||||
raise RoxywiResourceNotFound
|
raise RoxywiResourceNotFound
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
out_error(e)
|
out_error(e)
|
||||||
|
|
||||||
|
|
||||||
|
def delete_section(server_id: int, section_type: str, section_name: str):
|
||||||
|
try:
|
||||||
|
HaproxySection.delete().where(
|
||||||
|
(HaproxySection.server_id == server_id)
|
||||||
|
& (HaproxySection.type == section_type)
|
||||||
|
& (HaproxySection.name == section_name)
|
||||||
|
).execute()
|
||||||
|
except HaproxySection.DoesNotExist:
|
||||||
|
raise RoxywiResourceNotFound
|
||||||
|
except Exception as e:
|
||||||
|
out_error(e)
|
||||||
|
|
|
@ -54,6 +54,10 @@ class IdDataResponse(IdResponse):
|
||||||
data: str
|
data: str
|
||||||
|
|
||||||
|
|
||||||
|
class IdDataStrResponse(IdStrResponse):
|
||||||
|
data: str
|
||||||
|
|
||||||
|
|
||||||
class DataResponse(BaseModel):
|
class DataResponse(BaseModel):
|
||||||
data: Union[list, dict]
|
data: Union[list, dict]
|
||||||
|
|
||||||
|
@ -388,6 +392,7 @@ class HaproxyConfigRequest(BaseModel):
|
||||||
antibot: Optional[bool] = 0
|
antibot: Optional[bool] = 0
|
||||||
backends: Optional[str] = None
|
backends: Optional[str] = None
|
||||||
circuit_breaking: Optional[HaproxyCircuitBreaking] = None
|
circuit_breaking: Optional[HaproxyCircuitBreaking] = None
|
||||||
|
action: Optional[Literal['save', 'test', 'reload', 'restart']] = "save"
|
||||||
|
|
||||||
|
|
||||||
class HaproxyUserListUser(BaseModel):
|
class HaproxyUserListUser(BaseModel):
|
||||||
|
@ -401,6 +406,7 @@ class HaproxyUserListRequest(BaseModel):
|
||||||
type: Literal['userlist']
|
type: Literal['userlist']
|
||||||
userlist_users: Optional[List[HaproxyUserListUser]] = ''
|
userlist_users: Optional[List[HaproxyUserListUser]] = ''
|
||||||
userlist_groups: Optional[List[str]] = ''
|
userlist_groups: Optional[List[str]] = ''
|
||||||
|
action: Optional[Literal['save', 'test', 'reload', 'restart']] = "save"
|
||||||
|
|
||||||
|
|
||||||
class HaproxyPeers(BaseModel):
|
class HaproxyPeers(BaseModel):
|
||||||
|
@ -413,6 +419,7 @@ class HaproxyPeersRequest(BaseModel):
|
||||||
name: EscapedString
|
name: EscapedString
|
||||||
type: Literal['peers']
|
type: Literal['peers']
|
||||||
peers: List[HaproxyPeers]
|
peers: List[HaproxyPeers]
|
||||||
|
action: Optional[Literal['save', 'test', 'reload', 'restart']] = "save"
|
||||||
|
|
||||||
|
|
||||||
class GenerateConfigRequest(BaseModel):
|
class GenerateConfigRequest(BaseModel):
|
||||||
|
@ -430,6 +437,7 @@ class HaproxyGlobalRequest(BaseModel):
|
||||||
socket: Optional[List[str]] = ['*:1999 level admin', '/var/run/haproxy.sock mode 600 level admin', '/var/lib/haproxy/stats']
|
socket: Optional[List[str]] = ['*:1999 level admin', '/var/run/haproxy.sock mode 600 level admin', '/var/lib/haproxy/stats']
|
||||||
type: Optional[Literal['global']] = 'global'
|
type: Optional[Literal['global']] = 'global'
|
||||||
option: Optional[str] = ''
|
option: Optional[str] = ''
|
||||||
|
action: Optional[Literal['save', 'test', 'reload', 'restart']] = "save"
|
||||||
|
|
||||||
|
|
||||||
class HaproxyDefaultsTimeout(BaseModel):
|
class HaproxyDefaultsTimeout(BaseModel):
|
||||||
|
@ -449,3 +457,4 @@ class HaproxyDefaultsRequest(BaseModel):
|
||||||
option: Optional[str] = ''
|
option: Optional[str] = ''
|
||||||
maxconn: Optional[int] = 5000
|
maxconn: Optional[int] = 5000
|
||||||
type: Optional[Literal['defaults']] = 'defaults'
|
type: Optional[Literal['defaults']] = 'defaults'
|
||||||
|
action: Optional[Literal['save', 'test', 'reload', 'restart']] = "save"
|
||||||
|
|
|
@ -16,7 +16,7 @@ import app.modules.roxywi.common as roxywi_common
|
||||||
from app.middleware import get_user_params, page_for_admin, check_group, check_services
|
from app.middleware import get_user_params, page_for_admin, check_group, check_services
|
||||||
from app.modules.db.db_model import Server
|
from app.modules.db.db_model import Server
|
||||||
from app.modules.roxywi.class_models import BaseResponse, DataStrResponse, HaproxyConfigRequest, GenerateConfigRequest, \
|
from app.modules.roxywi.class_models import BaseResponse, DataStrResponse, HaproxyConfigRequest, GenerateConfigRequest, \
|
||||||
HaproxyUserListRequest, HaproxyPeersRequest, HaproxyGlobalRequest, HaproxyDefaultsRequest
|
HaproxyUserListRequest, HaproxyPeersRequest, HaproxyGlobalRequest, HaproxyDefaultsRequest, IdDataStrResponse
|
||||||
from app.modules.common.common_classes import SupportClass
|
from app.modules.common.common_classes import SupportClass
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,7 +37,10 @@ class HaproxySectionView(MethodView):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
section = add_sql.get_section(server_id, section_type, section_name)
|
section = add_sql.get_section(server_id, section_type, section_name)
|
||||||
return jsonify(model_to_dict(section, recurse=False))
|
output = {'server_id': section.server_id.server_id, **model_to_dict(section, recurse=False)}
|
||||||
|
output['id'] = f'{server_id}-{section_name}'
|
||||||
|
output.update(section.config)
|
||||||
|
return jsonify(output)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot get HAProxy section')
|
return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot get HAProxy section')
|
||||||
|
|
||||||
|
@ -86,7 +89,9 @@ class HaproxySectionView(MethodView):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot add HAProxy section')
|
return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot add HAProxy section')
|
||||||
|
|
||||||
return DataStrResponse(data=output).model_dump(mode='json'), 201
|
res = IdDataStrResponse(data=output, id=f'{server_id}-{body.name}').model_dump(mode='json')
|
||||||
|
print(res)
|
||||||
|
return res, 201
|
||||||
|
|
||||||
def put(self,
|
def put(self,
|
||||||
service: Literal['haproxy'],
|
service: Literal['haproxy'],
|
||||||
|
@ -136,8 +141,9 @@ class HaproxySectionView(MethodView):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._edit_config(service, server, '', 'delete', section_type=section_type, section_name=section_name)
|
self._edit_config(service, server, '', 'delete', section_type=section_type, section_name=section_name)
|
||||||
|
add_sql.delete_section(server_id, section_type, section_name)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot create HAProxy section')
|
return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot delete HAProxy section')
|
||||||
|
|
||||||
return BaseResponse().model_dump(mode='json'), 204
|
return BaseResponse().model_dump(mode='json'), 204
|
||||||
|
|
||||||
|
@ -162,7 +168,7 @@ class HaproxySectionView(MethodView):
|
||||||
if len(output['failures']) > 0 or len(output['dark']) > 0:
|
if len(output['failures']) > 0 or len(output['dark']) > 0:
|
||||||
raise Exception('Cannot create HAProxy section. Check Apache error log')
|
raise Exception('Cannot create HAProxy section. Check Apache error log')
|
||||||
|
|
||||||
output = config_mod.master_slave_upload_and_restart(server.ip, cfg, 'save', 'haproxy')
|
output = config_mod.master_slave_upload_and_restart(server.ip, cfg, str(body.action), 'haproxy')
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
@ -840,21 +846,29 @@ class UserListSectionView(HaproxySectionView):
|
||||||
properties:
|
properties:
|
||||||
type:
|
type:
|
||||||
type: string
|
type: string
|
||||||
server:
|
example: "userlist"
|
||||||
type: string
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
userlist_users:
|
userlist_users:
|
||||||
type: object
|
type: array
|
||||||
properties:
|
items:
|
||||||
user:
|
type: object
|
||||||
type: string
|
properties:
|
||||||
password:
|
user:
|
||||||
type: string
|
type: string
|
||||||
group:
|
example: "admin"
|
||||||
type: string
|
password:
|
||||||
|
type: string
|
||||||
|
example: "secretpassword"
|
||||||
|
group:
|
||||||
|
type: string
|
||||||
|
example: ""
|
||||||
userlist_groups:
|
userlist_groups:
|
||||||
type: array
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
example: "ops"
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: "ops_users"
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Haproxy section successfully created.
|
description: Haproxy section successfully created.
|
||||||
|
@ -899,21 +913,29 @@ class UserListSectionView(HaproxySectionView):
|
||||||
properties:
|
properties:
|
||||||
type:
|
type:
|
||||||
type: string
|
type: string
|
||||||
server:
|
example: "userlist"
|
||||||
type: string
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
userlist_users:
|
userlist_users:
|
||||||
type: object
|
type: array
|
||||||
properties:
|
items:
|
||||||
user:
|
type: object
|
||||||
type: string
|
properties:
|
||||||
password:
|
user:
|
||||||
type: string
|
type: string
|
||||||
group:
|
example: "admin"
|
||||||
type: string
|
password:
|
||||||
|
type: string
|
||||||
|
example: "secretpassword"
|
||||||
|
group:
|
||||||
|
type: string
|
||||||
|
example: ""
|
||||||
userlist_groups:
|
userlist_groups:
|
||||||
type: array
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
example: "ops"
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: "ops_users"
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Haproxy section successfully created.
|
description: Haproxy section successfully created.
|
||||||
|
@ -1055,21 +1077,24 @@ class PeersSectionView(HaproxySectionView):
|
||||||
properties:
|
properties:
|
||||||
type:
|
type:
|
||||||
type: string
|
type: string
|
||||||
server:
|
description: The type of the section.
|
||||||
type: string
|
peers:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: The name of the peer.
|
||||||
|
ip:
|
||||||
|
type: string
|
||||||
|
description: The IP address of the peer.
|
||||||
|
port:
|
||||||
|
type: string
|
||||||
|
description: The port of the peer.
|
||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
userlist_users:
|
description: The name of the section.
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
user:
|
|
||||||
type: string
|
|
||||||
password:
|
|
||||||
type: string
|
|
||||||
group:
|
|
||||||
type: string
|
|
||||||
userlist_groups:
|
|
||||||
type: array
|
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Haproxy section successfully created.
|
description: Haproxy section successfully created.
|
||||||
|
@ -1114,21 +1139,24 @@ class PeersSectionView(HaproxySectionView):
|
||||||
properties:
|
properties:
|
||||||
type:
|
type:
|
||||||
type: string
|
type: string
|
||||||
server:
|
description: The type of the section.
|
||||||
type: string
|
peers:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: The name of the peer.
|
||||||
|
ip:
|
||||||
|
type: string
|
||||||
|
description: The IP address of the peer.
|
||||||
|
port:
|
||||||
|
type: string
|
||||||
|
description: The port of the peer.
|
||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
userlist_users:
|
description: The name of the section.
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
user:
|
|
||||||
type: string
|
|
||||||
password:
|
|
||||||
type: string
|
|
||||||
group:
|
|
||||||
type: string
|
|
||||||
userlist_groups:
|
|
||||||
type: array
|
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Haproxy section successfully created.
|
description: Haproxy section successfully created.
|
||||||
|
@ -1287,40 +1315,34 @@ class GlobalSectionView(HaproxySectionView):
|
||||||
schema:
|
schema:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
config:
|
chroot:
|
||||||
type: object
|
type: string
|
||||||
properties:
|
default: "/var/lib/haproxy"
|
||||||
chroot:
|
daemon:
|
||||||
type: string
|
type: integer
|
||||||
default: "/var/lib/haproxy"
|
default: 1
|
||||||
daemon:
|
group:
|
||||||
type: integer
|
type: string
|
||||||
default: 1
|
default: "haproxy"
|
||||||
group:
|
log:
|
||||||
type: string
|
type: array
|
||||||
default: "haproxy"
|
items:
|
||||||
log:
|
type: string
|
||||||
type: array
|
default: ["127.0.0.1 local", "127.0.0.1 local1 notice"]
|
||||||
items:
|
maxconn:
|
||||||
type: string
|
type: integer
|
||||||
default: ["127.0.0.1 local", "127.0.0.1 local1 notice"]
|
default: 5000
|
||||||
maxconn:
|
pidfile:
|
||||||
type: integer
|
type: string
|
||||||
default: 5000
|
default: "/var/run/haproxy.pid"
|
||||||
pidfile:
|
socket:
|
||||||
type: string
|
type: array
|
||||||
default: "/var/run/haproxy.pid"
|
items:
|
||||||
socket:
|
type: string
|
||||||
type: array
|
default: ["*:1999 level admin", "/var/run/haproxy.sock mode 600 level admin", "/var/lib/haproxy/stats"]
|
||||||
items:
|
user:
|
||||||
type: string
|
type: string
|
||||||
default: ["*:1999 level admin", "/var/run/haproxy.sock mode 600 level admin", "/var/lib/haproxy/stats"]
|
default: "haproxy"
|
||||||
type:
|
|
||||||
type: string
|
|
||||||
default: "global"
|
|
||||||
user:
|
|
||||||
type: string
|
|
||||||
default: "haproxy"
|
|
||||||
id:
|
id:
|
||||||
type: integer
|
type: integer
|
||||||
default: 20
|
default: 20
|
||||||
|
|
Loading…
Reference in New Issue