UNPKG

n8n-nodes-arubacentralnextgen

Version:

n8n community node for Aruba Central NextGen API integration with modern monitoring and management capabilities

134 lines (133 loc) 5.61 kB
"use strict"; // helpers/apiRequest.ts var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.apiRequest = apiRequest; const logger = __importStar(require("./logger")); async function apiRequest(method, endpoint, body = {}, query = {}, requestOptions = {}) { const credentials = await this.getCredentials('arubaCentralNextGenOAuth2Api'); const { clientId, clientSecret, region } = credentials; // Check for debug mode const debugMode = logger.isDebugMode(this); // Map region to base URL const regionUrlMap = { eu: 'ge1.api.central.arubanetworks.com', eucentral2: 'ge2.api.central.arubanetworks.com', eucentral3: 'ge3.api.central.arubanetworks.com', prod: 'us1.api.central.arubanetworks.com', 'central-prod2': 'us2.api.central.arubanetworks.com', uswest4: 'us4.api.central.arubanetworks.com', uswest5: 'us5.api.central.arubanetworks.com', 'us-east-1': 'us6.api.central.arubanetworks.com', starman: 'cn1.api.central.arubanetworks.com', apac: 'in.api.central.arubanetworks.com', apaceast: 'jp1.api.central.arubanetworks.com', apacsouth: 'au1.api.central.arubanetworks.com', internal: 'internal.api.central.arubanetworks.com', }; const baseUrl = regionUrlMap[region]; if (!baseUrl) { logger.error(`Invalid region: ${region}`); throw new Error(`Invalid region: ${region}`); } // Get access token let accessToken; try { if (debugMode) { logger.debug('Obtaining OAuth2 token', { region, clientIdLength: (clientId === null || clientId === void 0 ? void 0 : clientId.length) || 0 }, this); } const tokenResponse = await this.helpers.httpRequest({ method: 'POST', url: 'https://sso.common.cloud.hpe.com/as/token.oauth2', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}`, returnFullResponse: true, }); accessToken = tokenResponse.body.access_token; if (!accessToken) { throw new Error('Failed to obtain access token'); } if (debugMode) { logger.debug('Successfully obtained access token', { tokenLength: accessToken.length, expiresIn: tokenResponse.body.expires_in, }, this); } } catch (error) { logger.error(`Authentication error`, error); throw new Error(`Authentication error: ${error.message}`); } // Make API request const url = `https://${baseUrl}${endpoint}`; const httpRequestOptions = { method, url, headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }, body: Object.keys(body).length ? JSON.stringify(body) : undefined, qs: Object.keys(query).length ? query : undefined, returnFullResponse: false, }; try { // Skip logging request details in non-debug mode or if explicitly skipped if (debugMode && !requestOptions.skipDebug) { logger.debug(`Making ${method} request to ${url}`, { queryParams: httpRequestOptions.qs, bodyParams: httpRequestOptions.body, }, this); } const response = await this.helpers.httpRequest(httpRequestOptions); // Skip logging response in non-debug mode or if explicitly skipped if (debugMode && !requestOptions.skipDebug) { const responseSize = JSON.stringify(response).length; logger.debug(`Received response from ${url}`, { status: 200, responseSize: responseSize, hasPagination: !!response.next, itemCount: Array.isArray(response.devices || response.items) ? (response.devices || response.items).length : 'N/A', }, this); } return response; } catch (error) { logger.error(`Request failed: ${url}`, error); throw new Error(`Request failed: ${error.message}`); } }