main
py 209 lines 6.3 KB
Raw
1 from fastapi import APIRouter
2 from fastapi import Depends
3 from fastapi import Security
4 from loguru import logger
5 from sqlalchemy.ext.asyncio import AsyncSession
6
7 from app.auth.utils import AuthHandler
8
9 # from app.connectors.portainer.schema.integrations import ExecuteWorkflowRequest
10 from app.connectors.portainer.schema.nodes import NodesResponse
11 from app.connectors.portainer.schema.stack import DeleteStackResponse
12 from app.connectors.portainer.schema.stack import StackIDResponse
13 from app.connectors.portainer.schema.stack import StackResponse
14 from app.connectors.portainer.schema.stack import StacksResponse
15 from app.connectors.portainer.services.nodes import get_node_details
16 from app.connectors.portainer.services.stack import create_wazuh_customer_stack
17 from app.connectors.portainer.services.stack import delete_wazuh_customer_stack
18 from app.connectors.portainer.services.stack import get_stack_details
19 from app.connectors.portainer.services.stack import get_stacks
20 from app.connectors.portainer.services.stack import start_wazuh_customer_stack
21 from app.connectors.portainer.services.stack import stop_wazuh_customer_stack
22 from app.connectors.portainer.utils.universal import get_customer_portainer_stack_id
23 from app.connectors.portainer.utils.universal import update_customer_portainer_stack_id
24 from app.customer_provisioning.schema.provision import ProvisionNewCustomer
25 from app.db.db_session import get_db
26
27 portainer_integrations_router = APIRouter()
28
29
30 @portainer_integrations_router.get(
31 "/node-details",
32 description="Execute a portainer Integration.",
33 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
34 response_model=NodesResponse,
35 )
36 async def node_details_route():
37 """
38 Get the IP addresses of all nodes in the swarm.
39
40 Returns:
41 list: The list of IP addresses.
42 """
43 logger.info("Getting swarm node details")
44 return await get_node_details()
45
46
47 @portainer_integrations_router.get(
48 "/stacks",
49 description="Get the list of stacks.",
50 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
51 response_model=StacksResponse,
52 )
53 async def stacks_route():
54 """
55 Get the list of stacks.
56
57 Returns:
58 dict: The response object.
59 """
60 return await get_stacks()
61
62
63 @portainer_integrations_router.get(
64 "/stack-details",
65 description="Get the details of a stack.",
66 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
67 response_model=StackResponse,
68 )
69 async def stack_details_route(stack_id: int):
70 """
71 Get the details of a stack.
72
73 Args:
74 stack_id (int): The ID of the stack.
75
76 Returns:
77 dict: The response object.
78 """
79 return await get_stack_details(stack_id)
80
81
82 @portainer_integrations_router.get(
83 "/get-customer-stack-id",
84 description="Get the ID of a customer stack.",
85 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
86 response_model=StackIDResponse,
87 )
88 async def get_customer_stack_id_route(
89 customer_name: str,
90 session: AsyncSession = Depends(get_db),
91 ):
92 """
93 Get the ID of a customer stack by the customer name.
94
95 Args:
96 customer_name (str): The name of the customer.
97
98 Returns:
99 dict: The response object.
100 """
101 return StackIDResponse(
102 stack_id=await get_customer_portainer_stack_id(customer_name=customer_name, session=session),
103 success=True,
104 message="Customer stack ID retrieved successfully.",
105 )
106
107
108 @portainer_integrations_router.post(
109 "/create-wazuh-customer-stack",
110 description="Create a Wazuh stack for a customer.",
111 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
112 response_model=StackResponse,
113 )
114 async def create_wazuh_customer_stack_route(request: ProvisionNewCustomer):
115 """
116 Create a Wazuh stack for a customer.
117
118 Args:
119 request (ProvisionNewCustomer): The request object.
120
121 Returns:
122 dict: The response object.
123 """
124 return await create_wazuh_customer_stack(request)
125
126
127 @portainer_integrations_router.post(
128 "/start-wazuh-customer-stack",
129 description="Start a Wazuh stack for a customer.",
130 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
131 response_model=StackResponse,
132 )
133 async def start_wazuh_customer_stack_route(stack_id: int):
134 """
135 Start a Wazuh stack for a customer.
136
137 Args:
138 request (ProvisionNewCustomer): The request object.
139
140 Returns:
141 dict: The response object.
142 """
143 return await start_wazuh_customer_stack(stack_id)
144
145
146 @portainer_integrations_router.post(
147 "/stop-wazuh-customer-stack",
148 description="Stop a Wazuh stack for a customer.",
149 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
150 response_model=StackResponse,
151 )
152 async def stop_wazuh_customer_stack_route(stack_id: int):
153 """
154 Stop a Wazuh stack for a customer.
155
156 Args:
157 request (ProvisionNewCustomer): The request object.
158
159 Returns:
160 dict: The response object.
161 """
162 return await stop_wazuh_customer_stack(stack_id)
163
164
165 @portainer_integrations_router.post(
166 "/update-customer-stack-id",
167 description="Update the ID of a customer stack.",
168 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
169 response_model=StackIDResponse,
170 )
171 async def update_customer_stack_id_route(
172 customer_name: str,
173 stack_id: int,
174 session: AsyncSession = Depends(get_db),
175 ):
176 """
177 Update the ID of a customer stack by the customer name.
178
179 Args:
180 customer_name (str): The name of the customer.
181 stack_id (int): The ID of the stack.
182
183 Returns:
184 dict: The response object.
185 """
186 return StackIDResponse(
187 stack_id=await update_customer_portainer_stack_id(customer_name=customer_name, stack_id=stack_id, session=session),
188 success=True,
189 message="Customer stack ID updated successfully.",
190 )
191
192
193 @portainer_integrations_router.delete(
194 "/delete-wazuh-customer-stack",
195 description="Delete a Wazuh stack for a customer.",
196 dependencies=[Security(AuthHandler().require_any_scope("admin"))],
197 response_model=DeleteStackResponse,
198 )
199 async def delete_wazuh_customer_stack_route(stack_id: int):
200 """
201 Delete a Wazuh stack for a customer.
202
203 Args:
204 request (ProvisionNewCustomer): The request object.
205
206 Returns:
207 dict: The response object.
208 """
209 return await delete_wazuh_customer_stack(stack_id)