n8n-nodes-arubacentral
Version:
n8n community node for Aruba Central API integration with comprehensive monitoring, configuration, and management capabilities
88 lines (76 loc) • 2.36 kB
text/typescript
import { IExecuteFunctions, IDataObject } from 'n8n-workflow';
import { apiRequest } from './apiRequest';
import { logger } from './logger';
/**
* Find site ID from site name
* Based on the pycentral.ArubaCentralBase.find_site_id implementation
*
* @param this The n8n execution context
* @param siteName Name of the site to find
* @returns Site ID if found, null otherwise
*/
export async function findSiteId(
this: IExecuteFunctions,
siteName: string,
): Promise<number | null> {
try {
const maxLimitSize = 1000;
let totalCount = 0;
let paginationCheck = true;
let offset = 0;
logger.debug('siteHelpers:findSiteId', `Looking for site: ${siteName}`);
while (paginationCheck) {
// Get sites with pagination
const queryParams: IDataObject = {
offset,
limit: maxLimitSize,
};
const response = await apiRequest.call(this, 'GET', '/central/v2/sites', {}, queryParams);
if (response && response.sites && Array.isArray(response.sites)) {
const count = response.sites.length;
totalCount += count;
offset += count;
// Check if we've reached the end
if (response.total && totalCount >= response.total) {
paginationCheck = false;
}
// Search for matching site name
for (const site of response.sites) {
if (site.site_name === siteName) {
logger.debug(
'siteHelpers:findSiteId',
`Found site ID: ${site.site_id} for site: ${siteName}`,
);
return site.site_id;
}
}
// If no more sites to check, break
if (count === 0) {
paginationCheck = false;
}
} else {
// No sites found or invalid response
paginationCheck = false;
}
}
logger.debug('siteHelpers:findSiteId', `Site not found: ${siteName}`);
return null;
} catch (error) {
logger.error('siteHelpers:findSiteId:error', { siteName, message: error.message });
throw error;
}
}
/**
* Filter sites by name from a list of sites
* Used for client-side filtering when API doesn't support server-side filtering
*
* @param sites Array of site objects
* @param siteName Name to filter by
* @returns Filtered array of sites
*/
export function filterSitesByName(sites: IDataObject[], siteName: string): IDataObject[] {
if (!siteName || !Array.isArray(sites)) {
return sites;
}
return sites.filter((site) => site.site_name === siteName);
}