""" AUTHOR: Khushal P Soonderji DATE: Monday, 10th Feb., 2025. OBJECTIVE: To handle all MikroTik configuration from one place. REFERENCES: N/A DOWNLOADS: N/A """ # ***************************************************************************************************************** # ***** **** # *** IMPORT *** # ***** **** # ***************************************************************************************************************** # To make sibling directories accessible for imports: import sys sys.path.append(".") sys.path.append("..") # My async utils: from utils_v2.database.async_mysql_v2 import AsyncMySQL from utils_v2.database.async_mongo_v2 import AsyncMongo from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache # Controllers: from controllers_v2.core.software import CoreSoftwareController # To make very controlled API calls: from utils_v2.rest.controllers.async_base import AsyncREST from utils_v2.rest.models.api_call import ApiResponse # Models: from models.software.mikrotik.auth import ( MikroTikPPPoE1000Auth, MikroTikHotspot1000Auth, MikroTikAuthResponse ) # To work with datatypes: from typing import List, Any # To make HTTP requests: import httpx # to work with MongoDB: from bson.objectid import ObjectId # To make abstract classes: from abc import ABC, abstractmethod # ***************************************************************************************************************** # ***** **** # *** MACROS / ONE-TIME INIT *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** VARIABLES *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** FUNCTIONS *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** CLASSES *** # ***** **** # ***************************************************************************************************************** class MikroTikController(CoreSoftwareController, ABC): # ┏┓┓ ┓┏ # ┃ ┃┏┓┏┏ ┃┃┏┓┏┓┏ # ┗┛┗┗┻┛┛ ┗┛┗┻┛ ┛ SERVICE_TYPE = "mikrotik" # ┏┓ # ┃ ┏┓┏┓┏╋┏┓┓┏┏╋┏┓┏┓ # ┗┛┗┛┛┗┛┗┛ ┗┻┗┗┗┛┛ def __init__( self, cache: AsyncRedisCache = None, http_client: httpx.AsyncClient = None, alert_url: str = None, base_filter: dict = None, debug: bool = True, debug_prefix: str = "MikroTik (C) | ", debug_only_errors: bool = True ): """ This is the foundational controller for all MikroTik services. This is built on top of the core message controller, and, in turn, all individual MikroTik client controllers must be built on top of this. :param cache: The object to use for caching results from database calls. :param http_client: The HTTP client :param base_filter: The basic filter that will be applied to all fetching/updating queries. WARNING: THE BASE FILTER WILL ALWAYS BE APPLIED AUTOMATICALLY. SET THIS UP WISELY. :param debug: Whether, or not, you would like to print debugging messages: :param debug_prefix: The prefix to print with the debugging messages. :param debug_only_errors: Whether you would like to print only error messages or all messages. :return: None. """ # Prepare the combined base filter: mikrotik_filter = {} for k, v in (base_filter or {}).items(): mikrotik_filter[k] = v mikrotik_filter["serviceType"] = self.SERVICE_TYPE # Invoke the parent's constructor: CoreSoftwareController.__init__( self, cache = cache, alert_url = alert_url, http_client = http_client, base_filter = mikrotik_filter, debug = debug, debug_prefix = debug_prefix, debug_only_errors = debug_only_errors ) # Init a variable in a parent: self._service_type = self.SERVICE_TYPE # For controlled REST-ful calls: self._rest = AsyncREST( http_client = http_client, debug = debug, debug_prefix = debug_prefix, debug_only_errors = debug_only_errors ) # ┓┏ ┓ # ┣┫┏┓┃┏┓┏┓┏┓┏ # ┛┗┗ ┗┣┛┗ ┛ ┛ # ┛ @staticmethod def get_mikrotik_url( nas_ip: str, path: str, port_no: int | str = None, use_https: bool = True ) -> str: """ Simply creates the base URL for hitting the MikroTik server. :param nas_ip: The IP address of the MikroTik device. :param path: The path of the REST API to hit. :param port_no: The port no. to hit the MikroTik device on. :param use_https: Whether to use HTTPS, or HTTP. :return: The Base URL string. """ base_url = r"https://" if use_https else r"http://" base_url += nas_ip if port_no is not None: base_url += f":{port_no}" base_url += "/rest" if not path.startswith("/"): path = "/" + path return base_url + path # ┏┓ # ┗┓┓┏┏╋┏┓┏┳┓ # ┗┛┗┫┛┗┗ ┛┗┗ # ┛ async def get_system_resource( self, nas_ip: str, username: str, password: str, port_no: int | str = None, use_https: bool = True ) -> ApiResponse: """ To get a summary of the hardware resources available in the MikroTik device. This also becomes a great way to quickly check if any given device is valid, and up and running. :param nas_ip: The IP address of the MikroTik device. :param username: The username to get access to the MikroTik device. :param password: The password to get access to the MikroTik device. :param port_no: The port no. to hit the MikroTik device on. :param use_https: Whether to use HTTPS, or HTTP. :return: A structured API response. """ # Make the API call and return the response: return await self._rest.get( url = self.get_mikrotik_url( nas_ip = nas_ip, path = r"/system/resource", port_no = port_no, use_https = use_https ), auth = httpx.BasicAuth( username = username, password = password ) ) # ┏┓ ┓ # ┣┫┓┏╋┣┓ # ┛┗┗┻┗┛┗ @abstractmethod async def save_auth( self, sql_conn: AsyncMySQL, mongo_data_conn: AsyncMongo, mikrotik_auth: MikroTikPPPoE1000Auth | MikroTikHotspot1000Auth ) -> MikroTikAuthResponse: """ Checks if a particular set of incoming credentials give access to a valid server and then stores the credentials. :param sql_conn: The database connection to use to perform this task. :param mongo_data_conn: The database connection to use to perform this task. :param mikrotik_auth: The set of credentials as received from the UI/API. :return: A structured response to indicate what happened during authorization. """ pass # ***************************************************************************************************************** # ***** **** # *** MAIN PROGRAM *** # ***** **** # ***************************************************************************************************************** if __name__ == "__main__": pass