(20250218) Will MikroTik quick rollback URL for easy testing
This commit is contained in:
@@ -0,0 +1,167 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
AUTHOR:
|
||||||
|
|
||||||
|
Khushal P Soonderji
|
||||||
|
|
||||||
|
DATE:
|
||||||
|
|
||||||
|
Tuesday, 18th Feb., 2025.
|
||||||
|
|
||||||
|
OBJECTIVE:
|
||||||
|
|
||||||
|
For simple MikroTik Rollbacks for testing.
|
||||||
|
NOT TO BE USED FOR LONG.
|
||||||
|
|
||||||
|
REFERENCES:
|
||||||
|
|
||||||
|
N/A
|
||||||
|
|
||||||
|
DOWNLOADS:
|
||||||
|
|
||||||
|
N/A
|
||||||
|
|
||||||
|
NOTES:
|
||||||
|
|
||||||
|
N/A
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** IMPORT ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# To make sibling directories accessible for imports:
|
||||||
|
import sys
|
||||||
|
sys.path.append(".")
|
||||||
|
sys.path.append("..")
|
||||||
|
|
||||||
|
# For using Quart:
|
||||||
|
from quart import Blueprint, current_app, request, render_template
|
||||||
|
|
||||||
|
# My utils:
|
||||||
|
from utils_v2.string import json
|
||||||
|
from utils_v2.mikrotik.controllers.async_mikrotik import AsyncMikroTik
|
||||||
|
from utils_v2.api.codes import StatusCodes, HttpCodes
|
||||||
|
from utils_v2.api.response import ResponseModel
|
||||||
|
from utils_v2.api.async_quart import (
|
||||||
|
set_api_version,
|
||||||
|
read_input,
|
||||||
|
get_session_info,
|
||||||
|
log_request_to_mongo,
|
||||||
|
log_chain_to_mongo,
|
||||||
|
should_not_be_under_maintenance,
|
||||||
|
only_whitelisted_ips,
|
||||||
|
limit_rate,
|
||||||
|
validate_input,
|
||||||
|
handle_cancelled_request
|
||||||
|
)
|
||||||
|
|
||||||
|
# Common:
|
||||||
|
from shared import constants
|
||||||
|
|
||||||
|
# For asynchronous activities:
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
# For random choices:
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** MACROS / ONE-TIME INIT ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# Related to Quart:
|
||||||
|
test_mikrotik_rollback_bp = Blueprint("test_mt_rb", __name__)
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** VARIABLES ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# --- Nothing Yet
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** FUNCTIONS ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
@test_mikrotik_rollback_bp.record_once
|
||||||
|
def init(blueprint_setup_state):
|
||||||
|
|
||||||
|
# This gets called when the blueprint is registered.
|
||||||
|
# Consider this to be a one-time setup for the whole blueprint:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@test_mikrotik_rollback_bp.route("/mikrotik/rollback", methods = ["GET", "POST", "PUT", "PATCH", "DELETE"])
|
||||||
|
@set_api_version(api_version = "1.0.0")
|
||||||
|
@read_input(sanitize_headers = False, sanitize_data = False)
|
||||||
|
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
|
||||||
|
@log_request_to_mongo(
|
||||||
|
attr_name = "logs_mongo",
|
||||||
|
project = constants.PROJECT_NAME,
|
||||||
|
log_type = constants.MODULE_NAME,
|
||||||
|
operation = "testMikroTikRlBckApi",
|
||||||
|
log_input = True,
|
||||||
|
log_output = True,
|
||||||
|
sensitive_keys = ["sessionToken", "X-Session-Token"]
|
||||||
|
)
|
||||||
|
@log_chain_to_mongo(attr_name = "logs_mongo")
|
||||||
|
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
||||||
|
@handle_cancelled_request()
|
||||||
|
async def callback_test(
|
||||||
|
inbound_headers: dict = None,
|
||||||
|
inbound_data: dict = None,
|
||||||
|
inbound_files: dict = None,
|
||||||
|
**kwargs
|
||||||
|
):
|
||||||
|
|
||||||
|
response = await current_app.mikrotik_hotspot_1000_controller.quick_roll_back(
|
||||||
|
mikrotik_client = AsyncMikroTik(
|
||||||
|
config_by = "easyfi-hs-1000",
|
||||||
|
mikrotik_ip = inbound_data.get("ip", "102.210.173.150"),
|
||||||
|
username = inbound_data.get("u", "easyfi"),
|
||||||
|
password = inbound_data.get("p", "easyfi"),
|
||||||
|
port = None,
|
||||||
|
use_https = False,
|
||||||
|
http_client = None,
|
||||||
|
action_log_conn = current_app.data_mongo
|
||||||
|
),
|
||||||
|
use_https = False
|
||||||
|
)
|
||||||
|
|
||||||
|
# Done here:
|
||||||
|
return ResponseModel(
|
||||||
|
status_code = StatusCodes.OK if response.success else StatusCodes.FAILED,
|
||||||
|
http_code = HttpCodes.SUCCESS if response.success else HttpCodes.INTERNAL_SERVER_ERROR,
|
||||||
|
message = response.message
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** MAIN PROGRAM ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
pass
|
||||||
@@ -155,6 +155,7 @@ from api.blueprints.common.disable import auth_token_disable_bp
|
|||||||
# Tech and Testing Blueprints:
|
# Tech and Testing Blueprints:
|
||||||
from api.blueprints.tech.chat_alerts import tech_chat_alert_bp
|
from api.blueprints.tech.chat_alerts import tech_chat_alert_bp
|
||||||
from api.blueprints.test.callback import test_callback_bp
|
from api.blueprints.test.callback import test_callback_bp
|
||||||
|
from api.blueprints.test.mikrotik_roll_back import test_mikrotik_rollback_bp
|
||||||
|
|
||||||
# All the helpers:
|
# All the helpers:
|
||||||
from api.helpers.user import session
|
from api.helpers.user import session
|
||||||
@@ -230,6 +231,7 @@ app.register_blueprint(llm_invoke_bp, url_prefix = f"/{MODULE_BASE}/ai")
|
|||||||
# Tech and Testing Blueprints:
|
# Tech and Testing Blueprints:
|
||||||
app.register_blueprint(tech_chat_alert_bp, url_prefix = f"/{MODULE_BASE}/tech/alert")
|
app.register_blueprint(tech_chat_alert_bp, url_prefix = f"/{MODULE_BASE}/tech/alert")
|
||||||
app.register_blueprint(test_callback_bp, url_prefix = f"/{MODULE_BASE}/test")
|
app.register_blueprint(test_callback_bp, url_prefix = f"/{MODULE_BASE}/test")
|
||||||
|
app.register_blueprint(test_mikrotik_rollback_bp, url_prefix = f"/{MODULE_BASE}/test")
|
||||||
|
|
||||||
|
|
||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
|
|||||||
@@ -520,9 +520,7 @@ class MikroTikHotspot1000Controller(MikroTikController):
|
|||||||
resources = api_response.data or []
|
resources = api_response.data or []
|
||||||
total_count, success_count = 0, 0
|
total_count, success_count = 0, 0
|
||||||
for resource in resources:
|
for resource in resources:
|
||||||
print("NAT RES:", resource)
|
|
||||||
if mikrotik_client.is_my_config(resource, name_substring = self.NAME_PREFIX):
|
if mikrotik_client.is_my_config(resource, name_substring = self.NAME_PREFIX):
|
||||||
print("IS MINE!")
|
|
||||||
total_count += 1
|
total_count += 1
|
||||||
api_response = await mikrotik_client.remove_firewall_nat(resource[".id"], use_https = use_https)
|
api_response = await mikrotik_client.remove_firewall_nat(resource[".id"], use_https = use_https)
|
||||||
if api_response.success: success_count += 1
|
if api_response.success: success_count += 1
|
||||||
|
|||||||
@@ -105,7 +105,6 @@ def parse_vlans(vlan_range_str) -> List[int]:
|
|||||||
|
|
||||||
# Done here:
|
# Done here:
|
||||||
result = sorted(result)
|
result = sorted(result)
|
||||||
print(result)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user