UNPKG

n8n-nodes-arubacentral

Version:

n8n community node for Aruba Central API integration with comprehensive monitoring, configuration, and management capabilities

137 lines (136 loc) 6.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEvents = getEvents; const apiRequest_1 = require("../../../../helpers/apiRequest"); const logger_1 = require("../../../../helpers/logger"); const errorHandler_1 = require("../../../../helpers/errorHandler"); /** * Get Events * * GET /monitoring/v2/events * * @param this The n8n execution context * @returns Formatted API response */ async function getEvents() { try { const returnAll = this.getNodeParameter('returnAll', 0, false); const limit = returnAll ? 0 : this.getNodeParameter('limit', 0, 50); // Get additional fields const additionalFields = this.getNodeParameter('additionalFields', 0, {}); // Construct request options with query parameters const qs = {}; // Set pagination parameters if (!returnAll && limit > 0) { qs.limit = limit; } // Handle timestamp conversion from dateTime to epoch seconds if (additionalFields.from_timestamp !== undefined && additionalFields.from_timestamp !== '') { const fromDate = new Date(additionalFields.from_timestamp); qs.from_timestamp = Math.floor(fromDate.getTime() / 1000); logger_1.logger.debug('parameter:from_timestamp', `Setting from_timestamp to ${qs.from_timestamp}`); } if (additionalFields.to_timestamp !== undefined && additionalFields.to_timestamp !== '') { const toDate = new Date(additionalFields.to_timestamp); qs.to_timestamp = Math.floor(toDate.getTime() / 1000); logger_1.logger.debug('parameter:to_timestamp', `Setting to_timestamp to ${qs.to_timestamp}`); } // Add all other additional fields to query parameters const paramMappings = { group: 'group', swarm_id: 'swarm_id', label: 'label', macaddr: 'macaddr', bssid: 'bssid', device_mac: 'device_mac', hostname: 'hostname', device_type: 'device_type', sort: 'sort', site: 'site', serial: 'serial', level: 'level', event_description: 'event_description', event_type: 'event_type', event_number: 'event_number', event_category: 'event_category', fields: 'fields', calculate_total: 'calculate_total', }; // Process all additional fields and map to query parameters for (const [key, apiParam] of Object.entries(paramMappings)) { if (additionalFields[key] !== undefined && additionalFields[key] !== '') { qs[apiParam] = additionalFields[key]; logger_1.logger.debug(`parameter:${key}`, `Setting ${apiParam} to ${additionalFields[key]}`); } } // Construct API endpoint const endpoint = '/monitoring/v2/events'; logger_1.logger.debug('api:request:prepared', `Endpoint: ${endpoint}, Query params: ${JSON.stringify(qs)}`); // Handle pagination if (returnAll) { const responseData = await apiRequestAllItems.call(this, 'GET', endpoint, {}, qs); return [{ json: responseData }]; } // Make API request const responseData = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, qs); // Format and return response return [{ json: responseData }]; } catch (error) { logger_1.logger.error('monitoring:events:getEvents:error', { message: error.message }); return errorHandler_1.handleApiError.call(this, error, 'Failed to execute get events'); } } /** * Helper for pagination to fetch all items from a paginated API endpoint * * @param this The n8n execution context * @param method HTTP method to use * @param endpoint API endpoint to call * @param body Request body (if applicable) * @param qs Query parameters * @returns Combined array of all items across pages */ async function apiRequestAllItems(method, endpoint, body = {}, qs = {}) { const returnData = []; let responseData; const requestLimit = 100; // API limit per request let hasMore = true; let offset = 0; // Initialize or use the existing limit const queryLimit = qs.limit ? Math.min(qs.limit, requestLimit) : requestLimit; qs.limit = queryLimit; logger_1.logger.debug('pagination:start', `Starting pagination with limit ${queryLimit}`); do { qs.offset = offset; logger_1.logger.debug('pagination:request', `Making request with offset=${offset}, limit=${queryLimit}`); responseData = await apiRequest_1.apiRequest.call(this, method, endpoint, body, qs); logger_1.logger.debug('pagination:response', 'Response received'); // This needs to be adjusted based on actual API response format const items = responseData.items || responseData.data || responseData; if (items && Array.isArray(items)) { logger_1.logger.debug('pagination:items', `Received ${items.length} items`); returnData.push(...items); // If we received fewer items than requested, assume no more items if (items.length < queryLimit) { logger_1.logger.debug('pagination:complete', 'Received fewer items than requested, assuming no more items'); hasMore = false; } else { // Prepare for next batch offset += items.length; logger_1.logger.debug('pagination:next', `Moving to next page, new offset: ${offset}`); } } else { // No items in response or not an array logger_1.logger.debug('pagination:complete', 'No items found in response or response is not an array'); if (items) { returnData.push(items); } hasMore = false; } } while (hasMore); logger_1.logger.debug('pagination:complete', `Pagination complete. Total items retrieved: ${returnData.length}`); return returnData; }