From 91471a167f0653ddd0f10a59a5f94258d0a72c66 Mon Sep 17 00:00:00 2001 From: Karthik Satheesh Kumar Date: Sat, 2 Sep 2023 04:23:29 -0700 Subject: [PATCH 01/16] Linted & formatted audit_logs file --- pycentral/audit_logs.py | 113 +++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 47 deletions(-) diff --git a/pycentral/audit_logs.py b/pycentral/audit_logs.py index 264141e..661f746 100644 --- a/pycentral/audit_logs.py +++ b/pycentral/audit_logs.py @@ -9,8 +9,8 @@ # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -22,52 +22,60 @@ from pycentral.url_utils import AuditUrl, urlJoin from pycentral.base_utils import console_logger + urls = AuditUrl() -class Audit(): - """Get the audit logs and event logs with the functions in this class. - """ - def get_traillogs(self, conn, limit=100, offset=0, username=None, start_time=None, - end_time=None, description=None, target=None, classification=None, - customer_name=None, ip_address=None, app_id=None): - """Get audit logs, sort by time in in reverse chronological order. This API returns the first 10,000 - results only. Please use filter in the API for more relevant results. MSP Customer Would see logs of MSP's - and tenants as well. - :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an API call. +class Audit: + """Get the audit logs and event logs with the functions in this class.""" + + def get_traillogs(self, conn, limit=100, offset=0, username=None, + start_time=None, end_time=None, description=None, + target=None, classification=None, customer_name=None, + ip_address=None, app_id=None): + """Get audit logs, sort by time in in reverse chronological order. + This API returns the first 10,000 results only. Please use filter + in the API for more relevant results. MSP Customer Would see logs + of MSP's and tenants as well. + + :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an\ + API call. :type conn: class:`pycentral.ArubaCentralBase` - :param limit: Maximum number of audit events to be returned, defaults to 100 + :param limit: Maximum number of audit events to be returned, defaults\ + to 100 :type limit: int, optional - :param offset: Number of items to be skipped before returning the data, useful for pagination, defaults to 0 + :param offset: Number of items to be skipped before returning the \ + data, useful for pagination, defaults to 0 :type offset: int, optional :param username: Filter audit logs by User Name, defaults to None :type username: str, optional - :param start_time: Filter audit logs by Time Range. Start time of the audit logs should be provided in epoch - seconds, defaults to None + :param start_time: Filter audit logs by Time Range. Start time of the\ + audit logs should be provided in epoch seconds, defaults to None :type start_time: int, optional - :param end_time: Filter audit logs by Time Range. End time of the audit logs should be provided in epoch - seconds, defaults to None + :param end_time: Filter audit logs by Time Range. End time of the \ + audit logs should be provided in epoch seconds, defaults to None :type end_time: int, optional - :param description: Filter audit logs by Description, defaults to None + :param description: Filter audit logs by Description, defaults to \ + None :type description: str, optional :param target: Filter audit logs by target, defaults to None :type target: str, optional - :param classification: Filter audit logs by Classification, defaults to None + :param classification: Filter audit logs by Classification, defaults \ + to None :type classification: str, optional - :param customer_name: Filter audit logs by Customer NameFilter audit logs by Customer Name, defaults to None + :param customer_name: Filter audit logs by Customer NameFilter audit \ + logs by Customer Name, defaults to None :type customer_name: str, optional :param ip_address: Filter audit logs by IP Address, defaults to None :type ip_address: str, optional :param app_id: Filter audit logs by app_id, defaults to None :type app_id: str, optional - :return: HTTP Response as provided by 'command' function in class:`pycentral.ArubaCentralBase` + :return: HTTP Response as provided by 'command' function in \ + class:`pycentral.ArubaCentralBase` :rtype: dict """ path = urls.TRAIL_LOG["GET_ALL"] - params = { - "limit": limit, - "offset": offset - } + params = {"limit": limit, "offset": offset} if username: params["username"] = username if start_time: @@ -92,49 +100,58 @@ def get_traillogs(self, conn, limit=100, offset=0, username=None, start_time=Non def get_traillogs_detail(self, conn, id): """Get details of an audit log - :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an API call. + :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an\ + API call. :type conn: class:`pycentral.ArubaCentralBase` :param id: ID of audit event :type id: str - :return: HTTP Response as provided by 'command' function in class:`pycentral.ArubaCentralBase` + :return: HTTP Response as provided by 'command' function in \ + class:`pycentral.ArubaCentralBase` :rtype: dict """ path = urlJoin(urls.TRAIL_LOG["GET"], id) resp = conn.command(apiMethod="GET", apiPath=path) return resp - def get_eventlogs(self, conn, limit=100, offset=0, group_name=None, device_id=None, - classification=None, start_time=None, end_time=None): - """Get audit events for all groups, sort by time in in reverse chronological order.This API returns the - first 10,000 results only. Please use filter in the API for more relevant results. + def get_eventlogs(self, conn, limit=100, offset=0, group_name=None, + device_id=None, classification=None, start_time=None, + end_time=None): + """Get audit events for all groups, sort by time in in reverse\ + chronological order.This API returns the first 10,000 results\ + only. Please use filter in the API for more relevant results. - :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an API call. + :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an\ + API call. :type conn: class:`pycentral.ArubaCentralBase` - :param limit: Maximum number of audit events to be returned, defaults to 100 + :param limit: Maximum number of audit events to be returned, defaults\ + to 100 :type limit: int, optional - :param offset: Number of items to be skipped before returning the data, useful for pagination, defaults to 0 + :param offset: Number of items to be skipped before returning the\ + data, useful for pagination, defaults to 0 :type offset: int, optional :param group_name: Filter audit events by Group Name, defaults to None :type group_name: str, optional - :param device_id: Filter audit events by Target / Device ID. Device ID for AP is VC Name and Serial Number + :param device_id: Filter audit events by Target / Device ID. Device ID\ + for AP is VC Name and Serial Number for Switches, defaults to None :type device_id: str, optional - :param classification: Filter audit events by classification, defaults to None + :param classification: Filter audit events by classification, defaults\ + to None :type classification: str, optional - :param start_time: Filter audit logs by Time Range. Start time of the audit logs should be provided + :param start_time: Filter audit logs by Time Range. Start time of the\ + audit logs should be provided in epoch seconds, defaults to None :type start_time: int, optional - :param end_time: Filter audit logs by Time Range. End time of the audit logs should be provided in epoch + :param end_time: Filter audit logs by Time Range. End time of the\ + audit logs should be provided in epoch seconds, defaults to None :type end_time: int, optional - :return: HTTP Response as provided by 'command' function in class:`pycentral.ArubaCentralBase` + :return: HTTP Response as provided by 'command' function in\ + class:`pycentral.ArubaCentralBase` :rtype: dict """ path = urls.EVENT_LOG["GET_ALL"] - params = { - "limit": limit, - "offset": offset - } + params = {"limit": limit, "offset": offset} if group_name: params["group_name"] = group_name if device_id: @@ -151,13 +168,15 @@ def get_eventlogs(self, conn, limit=100, offset=0, group_name=None, device_id=No def get_eventlogs_detail(self, conn, id): """Get details of an audit event/log - :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an API call. + :param conn: Instance of class:`pycentral.ArubaCentralBase` to make an\ + API call. :type conn: class:`pycentral.ArubaCentralBase` :param id: ID of audit event :type id: str - :return: HTTP Response as provided by 'command' function in class:`pycentral.ArubaCentralBase` + :return: HTTP Response as provided by 'command' function in\ + class:`pycentral.ArubaCentralBase` :rtype: dict """ path = urlJoin(urls.EVENT_LOG["GET"], id) resp = conn.command(apiMethod="GET", apiPath=path) - return resp \ No newline at end of file + return resp From 8bfc9d5758fcf8da9b4f4ba6deba8aebd55f64b0 Mon Sep 17 00:00:00 2001 From: Karthik Satheesh Kumar Date: Sat, 2 Sep 2023 04:23:40 -0700 Subject: [PATCH 02/16] Linted & formatted base_utils file --- pycentral/base_utils.py | 82 +++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/pycentral/base_utils.py b/pycentral/base_utils.py index 86adb09..2663312 100755 --- a/pycentral/base_utils.py +++ b/pycentral/base_utils.py @@ -9,8 +9,8 @@ # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -20,7 +20,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -import logging, os +import logging +import os from urllib.parse import urlencode, urlparse, urlunparse try: import colorlog # type: ignore @@ -29,12 +30,12 @@ COLOR = False C_LOG_LEVEL = { - "CRITICAL": 50, - "ERROR": 40, - "WARNING": 30, - "INFO": 20, - "DEBUG": 10, - "NOTSET": 0 + "CRITICAL": 50, + "ERROR": 40, + "WARNING": 30, + "INFO": 20, + "DEBUG": 10, + "NOTSET": 0 } C_DEFAULT_ARGS = { @@ -47,13 +48,17 @@ "token": None } + def parseInputArgs(central_info): - """his method parses user input, checks for the availability of mandatory arguments. Optional missing parameters - in central_info variable is initialized as defined in C_DEFAULT_ARGS. + """This method parses user input, checks for the availability of mandatory\ + arguments. Optional missing parameters in central_info variable is\ + initialized as defined in C_DEFAULT_ARGS. - :param central_info: central_info dictionary as read from user's input file. + :param central_info: central_info dictionary as read from user's input\ + file. :type central_info: dict - :return: parsed central_info dict with missing optional params set to default values. + :return: parsed central_info dict with missing optional params set to\ + default values. :rtype: dict """ if not central_info: @@ -70,16 +75,22 @@ def parseInputArgs(central_info): return default_dict + def tokenLocalStoreUtil(token_store, customer_id="customer", client_id="client"): - """Utility function for storeToken and loadToken default access token storage/cache method. - This function generates unique file name for a customer and API gateway client to store and load access - token in the local machine for reuse. The format of the file name is tok__.json. - If customer_id or client_id is not provided, default values mentioned in args will be used. - - :param token_store: Placeholder to support different token storage mechanism. \n - * keyword type: Place holder for different token storage mechanism. Defaults to local storage. \n - * keyword path: path where temp folder is created to store token JSON file. \n + """Utility function for storeToken and loadToken default access token\ + storage/cache method. This function generates unique file name for a\ + customer and API gateway client to store and load access token in the\ + local machine for reuse. The format of the file name is\ + tok__.json. If customer_id or client_id is not\ + provided, default values mentioned in args will be used. + + :param token_store: Placeholder to support different token storage\ + mechanism. \n + * keyword type: Place holder for different token storage mechanism.\ + Defaults to local storage. \n + * keyword path: path where temp folder is created to store token JSON\ + file. \n :type token_store: dict :param customer_id: Aruba Central customer id, defaults to "customer" :type customer_id: str, optional @@ -96,6 +107,7 @@ def tokenLocalStoreUtil(token_store, customer_id="customer", fullName = os.path.join(filePath, fileName) return fullName + def get_url(base_url, path='', params='', query={}, fragment=''): """This method constructs complete URL based on multiple parts of URL. @@ -119,15 +131,16 @@ def get_url(base_url, path='', params='', query={}, fragment=''): url = urlunparse((scheme, netloc, path, params, query, fragment)) return url + def console_logger(name, level="DEBUG"): - """This method create an instance of python logging and sets the following format for log messages. -