create read and update customers_meta table (#68)
taylor_socfortress committed
Jul 26, 2023 at 10:02 UTC
6c9daa2b0eaa563acdaba74b84487c8e0d6a29f2
3 files changed
+494
backend/app/routes/customers.py
+112
@@ -6,6 +6,7 @@ from flask import request
6
from loguru import logger
7
8
from app.services.Customers.universal import UniversalCustomers
9
+from app.services.Customers.universal import UniversalCustomersMeta
10
11
bp = Blueprint("customers", __name__)
12
@@ -144,3 +145,114 @@ def update_customer(id: int):
145
)
146
147
return jsonify(updated_customer), 201
148
+
149
+
150
+@bp.route("/customers_meta/create", methods=["POST"])
151
+def create_customer_meta():
152
+ """
153
+ Endpoint to create a new entry in the `CustomersMeta` table.
154
+
155
+ Returns:
156
+ Tuple[jsonify, int]: A Tuple where the first element is a JSON response
157
+ indicating if the customer meta was created successfully and the second element
158
+ is the HTTP status code.
159
+ """
160
+ if not request.is_json:
161
+ return jsonify({"message": "Missing JSON in request", "success": False}), 400
162
+
163
+ customerCode = request.json.get("customerCode", None)
164
+ clientName = request.json.get("clientName", None)
165
+ customerMetaGraylogIndex = request.json.get("customerMetaGraylogIndex", None)
166
+ customerMetaGraylogStream = request.json.get("customerMetaGraylogStream", None)
167
+ customerMetaInfluxOrg = request.json.get("customerMetaInfluxOrg", None)
168
+ customerMetaGrafanaOrg = request.json.get("customerMetaGrafanaOrg", None)
169
+ customerMetaWazuhGroup = request.json.get("customerMetaWazuhGroup", None)
170
+ indexRetention = request.json.get("indexRetention", None)
171
+ wazuhRegistrationPort = request.json.get("wazuhRegistrationPort", None)
172
+ wazuhLogIngestionPort = request.json.get("wazuhLogIngestionPort", None)
173
+
174
+ new_customer_meta = UniversalCustomersMeta.create(
175
+ customerCode,
176
+ clientName,
177
+ customerMetaGraylogIndex,
178
+ customerMetaGraylogStream,
179
+ customerMetaInfluxOrg,
180
+ customerMetaGrafanaOrg,
181
+ customerMetaWazuhGroup,
182
+ indexRetention,
183
+ wazuhRegistrationPort,
184
+ wazuhLogIngestionPort,
185
+ )
186
+
187
+ return jsonify(new_customer_meta), 201
188
+
189
+
190
+@bp.route("/customers_meta/read/all", methods=["GET"])
191
+def read_all_customers_meta():
192
+ """
193
+ Endpoint to list all customers from the `customers` table.
194
+
195
+ Returns:
196
+ jsonify: A JSON response containing the list of all customers.
197
+ """
198
+ logger.info("Received request to get all customers")
199
+ customers = UniversalCustomersMeta.read_all()
200
+ return jsonify(customers)
201
+
202
+
203
+@bp.route("/customers_meta/read/<int:id>", methods=["GET"])
204
+def read_customer_meta_by_id(id: int):
205
+ """
206
+ Endpoint to fetch a customer by their id.
207
+
208
+ Returns:
209
+ Tuple[jsonify, int]: A Tuple where the first element is a JSON response
210
+ containing the customer and the second element is the HTTP status code.
211
+ """
212
+ logger.info(f"Received request to get customer with id {id}")
213
+ customer = UniversalCustomersMeta.read_by_id(id)
214
+ if customer:
215
+ return jsonify(customer), 200
216
+ return jsonify({"message": "Customer not found", "success": False}), 404
217
+
218
+
219
+@bp.route("/customers_meta/update/<int:id>", methods=["PUT"])
220
+def update_customer_meta(id: int):
221
+ """
222
+ Endpoint to update a customersmeta details into the `customers_meta` table.
223
+
224
+ Returns:
225
+ Tuple[jsonify, int]: A Tuple where the first element is a JSON response
226
+ indicating if the customer was updated successfully and the second element
227
+ is the HTTP status code.
228
+ """
229
+ logger.info(f"Received data to update a customer: {request.json}")
230
+ if not request.is_json:
231
+ return jsonify({"message": "Missing JSON in request", "success": False}), 400
232
+
233
+ customerCode = request.json.get("customerCode", None)
234
+ clientName = request.json.get("clientName", None)
235
+ customerMetaGraylogIndex = request.json.get("customerMetaGraylogIndex", None)
236
+ customerMetaGraylogStream = request.json.get("customerMetaGraylogStream", None)
237
+ customerMetaInfluxOrg = request.json.get("customerMetaInfluxOrg", None)
238
+ customerMetaGrafanaOrg = request.json.get("customerMetaGrafanaOrg", None)
239
+ customerMetaWazuhGroup = request.json.get("customerMetaWazuhGroup", None)
240
+ indexRetention = request.json.get("indexRetention", None)
241
+ wazuhRegistrationPort = request.json.get("wazuhRegistrationPort", None)
242
+ wazuhLogIngestionPort = request.json.get("wazuhLogIngestionPort", None)
243
+
244
+ updated_customer_meta = UniversalCustomersMeta.update(
245
+ id,
246
+ customerCode,
247
+ clientName,
248
+ customerMetaGraylogIndex,
249
+ customerMetaGraylogStream,
250
+ customerMetaInfluxOrg,
251
+ customerMetaGrafanaOrg,
252
+ customerMetaWazuhGroup,
253
+ indexRetention,
254
+ wazuhRegistrationPort,
255
+ wazuhLogIngestionPort,
256
+ )
257
+
258
+ return jsonify(updated_customer_meta), 201
backend/app/services/Customers/universal.py
+164
@@ -6,7 +6,10 @@ from typing import Union
6
7
from app import db
8
from app.models.customers import Customers
9
+from app.models.customers import CustomersMeta
10
+from app.models.customers import customer_meta_schema
11
from app.models.customers import customer_schema
12
+from app.models.customers import customers_meta_schema
13
from app.models.customers import customers_schema
14
15
@@ -204,3 +207,164 @@ class UniversalCustomers:
207
"""
208
Customers.query.filter(Customers.id == id).delete()
209
db.session.commit()
210
+
211
+
212
+class UniversalCustomersMeta:
213
+ @staticmethod
214
+ def create(
215
+ customerCode: str,
216
+ clientName: str = None,
217
+ customerMetaGraylogIndex: str = None,
218
+ customerMetaGraylogStream: str = None,
219
+ customerMetaInfluxOrg: str = None,
220
+ customerMetaGrafanaOrg: str = None,
221
+ customerMetaWazuhGroup: str = None,
222
+ indexRetention: int = None,
223
+ wazuhRegistrationPort: int = None,
224
+ wazuhLogIngestionPort: int = None,
225
+ ) -> Dict[str, Union[int, str]]:
226
+ """
227
+ Create a new customer meta and add it to the database.
228
+
229
+ :param customerCode: The code of the customer.
230
+ :param clientName: The name of the client.
231
+ :param customerMetaGraylogIndex: The Graylog index of the customer metadata.
232
+ :param customerMetaGraylogStream: The Graylog stream of the customer metadata.
233
+ :param customerMetaInfluxOrg: The InfluxOrg of the customer metadata.
234
+ :param customerMetaGrafanaOrg: The GrafanaOrg of the customer metadata.
235
+ :param customerMetaWazuhGroup: The WazuhGroup of the customer metadata.
236
+ :param indexRetention: The index retention of the customer metadata.
237
+ :param wazuhRegistrationPort: The Wazuh registration port of the customer's Wazuh Agents.
238
+ :param wazuhLogIngestionPort: The Wazuh log ingestion port of the customer's Wazuh Agents.
239
+ :return: Dictionary of the created customer meta.
240
+ """
241
+ new_customer_meta = CustomersMeta(
242
+ customerCode,
243
+ clientName,
244
+ customerMetaGraylogIndex,
245
+ customerMetaGraylogStream,
246
+ customerMetaInfluxOrg,
247
+ customerMetaGrafanaOrg,
248
+ customerMetaWazuhGroup,
249
+ indexRetention,
250
+ wazuhRegistrationPort,
251
+ wazuhLogIngestionPort,
252
+ )
253
+ db.session.add(new_customer_meta)
254
+ db.session.commit()
255
+ return {
256
+ "message": "Customer Meta created successfully.",
257
+ "success": True,
258
+ "customersMeta": customer_meta_schema.dump(new_customer_meta),
259
+ }
260
+
261
+ @staticmethod
262
+ def read_by_id(id: int) -> Optional[Dict[str, Union[int, str]]]:
263
+ """
264
+ Fetch customer meta by their id.
265
+
266
+ :param id: ID of the customer meta.
267
+ :return: Dictionary of the customer meta or None if not found.
268
+ """
269
+ customer_meta = CustomersMeta.query.get(id)
270
+ return customer_meta_schema.dump(customer_meta) if customer_meta else None
271
+
272
+ @staticmethod
273
+ def read_by_customerCode(customerCode: str) -> Optional[Dict[str, Union[int, str]]]:
274
+ """
275
+ Fetch customer meta by customerCode.
276
+
277
+ :param customerCode: The code of the customer meta.
278
+ :return: Dictionary of the customer meta or None if not found.
279
+ """
280
+ customer_meta = CustomersMeta.query.filter_by(customerCode=customerCode).first()
281
+ return customer_meta_schema.dump(customer_meta) if customer_meta else None
282
+
283
+ @staticmethod
284
+ def read_all() -> List[Dict[str, Union[int, str]]]:
285
+ """
286
+ Fetch all customer metas.
287
+
288
+ :return: List of dictionaries of all customer metas.
289
+ """
290
+ customer_metas = CustomersMeta.query.all()
291
+ return {
292
+ "message": "Customer Metas retrieved successfully.",
293
+ "success": True,
294
+ "customersMetas": customers_meta_schema.dump(customer_metas, many=True),
295
+ }
296
+
297
+ @staticmethod
298
+ def update(
299
+ id: int,
300
+ customerCode: Optional[str] = None,
301
+ clientName: Optional[str] = None,
302
+ customerMetaGraylogIndex: Optional[str] = None,
303
+ customerMetaGraylogStream: Optional[str] = None,
304
+ customerMetaInfluxOrg: Optional[str] = None,
305
+ customerMetaGrafanaOrg: Optional[str] = None,
306
+ customerMetaWazuhGroup: Optional[str] = None,
307
+ indexRetention: Optional[int] = None,
308
+ wazuhRegistrationPort: Optional[int] = None,
309
+ wazuhLogIngestionPort: Optional[int] = None,
310
+ ) -> Optional[Dict[str, Union[int, str]]]:
311
+ """
312
+ Update existing customer meta.
313
+
314
+ :param id: ID of the customer meta.
315
+ :param customerCode: The code of the customer.
316
+ :param clientName: The name of the client.
317
+ :param customerMetaGraylogIndex: The Graylog index of the customer metadata.
318
+ :param customerMetaGraylogStream: The Graylog stream of the customer metadata.
319
+ :param customerMetaInfluxOrg: The InfluxOrg of the customer metadata.
320
+ :param customerMetaGrafanaOrg: The GrafanaOrg of the customer metadata.
321
+ :param customerMetaWazuhGroup: The WazuhGroup of the customer metadata.
322
+ :param indexRetention: The index retention of the customer metadata.
323
+ :param wazuhRegistrationPort: The Wazuh registration port of the customer's Wazuh Agents.
324
+ :param wazuhLogIngestionPort: The Wazuh log ingestion port of the customer's Wazuh Agents.
325
+ :return: Dictionary of the updated customer meta or None if not found.
326
+ """
327
+ customer_meta = CustomersMeta.query.get(id)
328
+ if not customer_meta:
329
+ return None
330
+ if customerCode is not None:
331
+ customer_meta.customerCode = customerCode
332
+ if clientName is not None:
333
+ customer_meta.clientName = clientName
334
+ if customerMetaGraylogIndex is not None:
335
+ customer_meta.customerMetaGraylogIndex = customerMetaGraylogIndex
336
+ if customerMetaGraylogStream is not None:
337
+ customer_meta.customerMetaGraylogStream = customerMetaGraylogStream
338
+ if customerMetaInfluxOrg is not None:
339
+ customer_meta.customerMetaInfluxOrg = customerMetaInfluxOrg
340
+ if customerMetaGrafanaOrg is not None:
341
+ customer_meta.customerMetaGrafanaOrg = customerMetaGrafanaOrg
342
+ if customerMetaWazuhGroup is not None:
343
+ customer_meta.customerMetaWazuhGroup = customerMetaWazuhGroup
344
+ if indexRetention is not None:
345
+ customer_meta.indexRetention = indexRetention
346
+ if wazuhRegistrationPort is not None:
347
+ customer_meta.wazuhRegistrationPort = wazuhRegistrationPort
348
+ if wazuhLogIngestionPort is not None:
349
+ customer_meta.wazuhLogIngestionPort = wazuhLogIngestionPort
350
+
351
+ db.session.commit()
352
+ return customer_meta_schema.dump(customer_meta)
353
+
354
+ @staticmethod
355
+ def delete_all():
356
+ """
357
+ Delete all customer metas from the table.
358
+ """
359
+ CustomersMeta.query.delete()
360
+ db.session.commit()
361
+
362
+ @staticmethod
363
+ def delete_by_id(id: int):
364
+ """
365
+ Delete a customer meta with the given id.
366
+
367
+ :param id: ID of the customer meta to delete.
368
+ """
369
+ CustomersMeta.query.filter(CustomersMeta.id == id).delete()
370
+ db.session.commit()
backend/app/static/swagger.json
+218
@@ -566,6 +566,224 @@
566
}
567
}
568
},
569
+ "/customers_meta/create": {
570
+ "post": {
571
+ "tags": ["Customers Meta"],
572
+ "summary": "Create a new customer meta",
573
+ "description": "Endpoint to store a new customer meta into the `CustomersMeta` table.",
574
+ "requestBody": {
575
+ "content": {
576
+ "application/json": {
577
+ "schema": {
578
+ "type": "object",
579
+ "properties": {
580
+ "customerCode": { "type": "string", "description": "Customer Code" },
581
+ "clientName": { "type": "string", "description": "Client Name" },
582
+ "customerMetaGraylogIndex": { "type": "string", "description": "Customer Meta Graylog Index" },
583
+ "customerMetaGraylogStream": { "type": "string", "description": "Customer Meta Graylog Stream" },
584
+ "customerMetaInfluxOrg": { "type": "string", "description": "Customer Meta Influx Org" },
585
+ "customerMetaGrafanaOrg": { "type": "string", "description": "Customer Meta Grafana Org" },
586
+ "customerMetaWazuhGroup": { "type": "string", "description": "Customer Meta Wazuh Group" },
587
+ "indexRetention": { "type": "integer", "description": "Index Retention" },
588
+ "wazuhRegistrationPort": { "type": "integer", "description": "Wazuh Registration Port" },
589
+ "wazuhLogIngestionPort": { "type": "integer", "description": "Wazuh Log Ingestion Port" }
590
+ },
591
+ "required": ["customerCode"]
592
+ }
593
+ }
594
+ }
595
+ },
596
+ "responses": {
597
+ "201": {
598
+ "description": "Customer Meta created",
599
+ "content": {
600
+ "application/json": {
601
+ "schema": {
602
+ "type": "object",
603
+ "properties": {
604
+ "message": { "type": "string", "example": "Customer Meta created successfully." },
605
+ "success": { "type": "boolean", "example": true },
606
+ "customerMeta": { "$ref": "#/definitions/CustomerMeta" }
607
+ }
608
+ }
609
+ }
610
+ }
611
+ },
612
+ "400": {
613
+ "description": "Invalid input",
614
+ "content": {
615
+ "application/json": {
616
+ "schema": {
617
+ "type": "object",
618
+ "properties": {
619
+ "message": { "type": "string", "example": "Invalid input" },
620
+ "success": { "type": "boolean", "example": false }
621
+ }
622
+ }
623
+ }
624
+ }
625
+ }
626
+ }
627
+ }
628
+ },
629
+ "/customers_meta/read/all": {
630
+ "get": {
631
+ "tags": ["Customers Meta"],
632
+ "summary": "Get all customers meta",
633
+ "description": "Endpoint to retrieve all customers meta from the `CustomersMeta` table.",
634
+ "responses": {
635
+ "200": {
636
+ "description": "Successful operation",
637
+ "content": {
638
+ "application/json": {
639
+ "schema": {
640
+ "type": "object",
641
+ "properties": {
642
+ "message": { "type": "string", "example": "Customers Meta retrieved successfully." },
643
+ "success": { "type": "boolean", "example": true },
644
+ "customersMeta": {
645
+ "type": "array",
646
+ "items": { "$ref": "#/definitions/CustomerMeta" }
647
+ }
648
+ }
649
+ }
650
+ }
651
+ }
652
+ }
653
+ }
654
+ }
655
+ },
656
+ "/customers_meta/read/{id}": {
657
+ "get": {
658
+ "tags": ["Customers Meta"],
659
+ "summary": "Get a customer",
660
+ "description": "Endpoint to retrieve a customer meta details from the `customers_meta` table.",
661
+ "parameters": [
662
+ {
663
+ "name": "id",
664
+ "in": "path",
665
+ "description": "ID of the customer to retrieve",
666
+ "required": true,
667
+ "type": "integer"
668
+ }
669
+ ],
670
+ "responses": {
671
+ "200": {
672
+ "description": "Successful operation",
673
+ "content": {
674
+ "application/json": {
675
+ "schema": {
676
+ "type": "object",
677
+ "properties": {
678
+ "message": { "type": "string", "example": "Customer retrieved successfully." },
679
+ "success": { "type": "boolean", "example": true },
680
+ "customer": { "$ref": "#/definitions/Customer" }
681
+ }
682
+ }
683
+ }
684
+ }
685
+ },
686
+ "404": {
687
+ "description": "Customer not found",
688
+ "content": {
689
+ "application/json": {
690
+ "schema": {
691
+ "type": "object",
692
+ "properties": {
693
+ "message": { "type": "string", "example": "Customer not found." },
694
+ "success": { "type": "boolean", "example": false }
695
+ }
696
+ }
697
+ }
698
+ }
699
+ }
700
+ }
701
+ }
702
+ },
703
+ "/customers_meta/update/{id}": {
704
+ "put": {
705
+ "tags": ["Customers Meta"],
706
+ "summary": "Update an existing customer meta",
707
+ "description": "Endpoint to update an existing customer meta in the `CustomersMeta` table.",
708
+ "parameters": [
709
+ {
710
+ "name": "id",
711
+ "in": "path",
712
+ "description": "ID of customer meta to update",
713
+ "required": true,
714
+ "schema": {
715
+ "type": "integer"
716
+ }
717
+ }
718
+ ],
719
+ "requestBody": {
720
+ "content": {
721
+ "application/json": {
722
+ "schema": {
723
+ "type": "object",
724
+ "properties": {
725
+ "customerCode": { "type": "string", "description": "Customer Code" },
726
+ "clientName": { "type": "string", "description": "Client Name" },
727
+ "customerMetaGraylogIndex": { "type": "string", "description": "Customer Meta Graylog Index" },
728
+ "customerMetaGraylogStream": { "type": "string", "description": "Customer Meta Graylog Stream" },
729
+ "customerMetaInfluxOrg": { "type": "string", "description": "Customer Meta Influx Org" },
730
+ "customerMetaGrafanaOrg": { "type": "string", "description": "Customer Meta Grafana Org" },
731
+ "customerMetaWazuhGroup": { "type": "string", "description": "Customer Meta Wazuh Group" },
732
+ "indexRetention": { "type": "integer", "description": "Index Retention" },
733
+ "wazuhRegistrationPort": { "type": "integer", "description": "Wazuh Registration Port" },
734
+ "wazuhLogIngestionPort": { "type": "integer", "description": "Wazuh Log Ingestion Port" }
735
+ }
736
+ }
737
+ }
738
+ }
739
+ },
740
+ "responses": {
741
+ "200": {
742
+ "description": "Customer Meta updated",
743
+ "content": {
744
+ "application/json": {
745
+ "schema": {
746
+ "type": "object",
747
+ "properties": {
748
+ "message": { "type": "string", "example": "Customer Meta updated successfully." },
749
+ "success": { "type": "boolean", "example": true },
750
+ "customerMeta": { "$ref": "#/definitions/CustomerMeta" }
751
+ }
752
+ }
753
+ }
754
+ }
755
+ },
756
+ "400": {
757
+ "description": "Invalid input",
758
+ "content": {
759
+ "application/json": {
760
+ "schema": {
761
+ "type": "object",
762
+ "properties": {
763
+ "message": { "type": "string", "example": "Invalid input" },
764
+ "success": { "type": "boolean", "example": false }
765
+ }
766
+ }
767
+ }
768
+ }
769
+ },
770
+ "404": {
771
+ "description": "Customer Meta not found",
772
+ "content": {
773
+ "application/json": {
774
+ "schema": {
775
+ "type": "object",
776
+ "properties": {
777
+ "message": { "type": "string", "example": "Customer Meta not found" },
778
+ "success": { "type": "boolean", "example": false }
779
+ }
780
+ }
781
+ }
782
+ }
783
+ }
784
+ }
785
+ }
786
+ },
787
"/agents": {
788
"get": {
789
"tags": ["Agents"],