import httpClient from '../lib/httpClient'; import { config } from '../config'; import { ensureAuthenticated } from './authService'; import logger from '../lib/logger'; import { OmadaApiResponse, OmadaVlan, OmadaVlanCreate } from '../types/omada'; const BASE_URL = `${config.omada.northboundBase}/openapi/v1/omada/${config.omada.id}`; /** * Lists all VLANs for a site */ export async function listVlans(siteId: string): Promise { await ensureAuthenticated(); try { const url = `${BASE_URL}/sites/${siteId}/networks`; logger.debug('Fetching VLANs', { siteId }); const response = await httpClient.get>(url); const data = response.data; if (data.errorCode !== 0) { throw new Error(`Failed to fetch VLANs: ${data.msg || 'Unknown error'}`); } const vlans = data.result || data.data || []; logger.info(`Fetched ${vlans.length} VLANs for site ${siteId}`); return vlans; } catch (error) { logger.error('Error fetching VLANs', { siteId, error }); throw error; } } /** * Gets a specific VLAN by ID */ export async function getVlan(siteId: string, vlanId: string): Promise { await ensureAuthenticated(); try { const url = `${BASE_URL}/sites/${siteId}/networks/${vlanId}`; logger.debug('Fetching VLAN', { siteId, vlanId }); const response = await httpClient.get>(url); const data = response.data; if (data.errorCode !== 0) { throw new Error(`Failed to fetch VLAN: ${data.msg || 'Unknown error'}`); } const vlan = data.result || data.data; if (!vlan) { throw new Error('No VLAN returned from fetch operation'); } logger.info('VLAN fetched', { siteId, vlanId }); return vlan; } catch (error) { logger.error('Error fetching VLAN', { siteId, vlanId, error }); throw error; } } /** * Creates a new VLAN */ export async function createVlan(siteId: string, vlanConfig: OmadaVlanCreate): Promise { await ensureAuthenticated(); try { const url = `${BASE_URL}/sites/${siteId}/networks`; logger.info('Creating VLAN', { siteId, vlanConfig }); const response = await httpClient.post>(url, vlanConfig); const data = response.data; if (data.errorCode !== 0) { throw new Error(`Failed to create VLAN: ${data.msg || 'Unknown error'}`); } const vlan = data.result || data.data; if (!vlan) { throw new Error('No VLAN returned from create operation'); } logger.info('VLAN created', { siteId, vlanId: vlan.id, vlanName: vlan.name }); return vlan; } catch (error) { logger.error('Error creating VLAN', { siteId, error }); throw error; } } /** * Updates an existing VLAN */ export async function updateVlan( siteId: string, vlanId: string, vlanConfig: Partial ): Promise { await ensureAuthenticated(); try { const url = `${BASE_URL}/sites/${siteId}/networks/${vlanId}`; logger.info('Updating VLAN', { siteId, vlanId, vlanConfig }); const response = await httpClient.put>(url, vlanConfig); const data = response.data; if (data.errorCode !== 0) { throw new Error(`Failed to update VLAN: ${data.msg || 'Unknown error'}`); } const vlan = data.result || data.data; if (!vlan) { throw new Error('No VLAN returned from update operation'); } logger.info('VLAN updated', { siteId, vlanId }); return vlan; } catch (error) { logger.error('Error updating VLAN', { siteId, vlanId, error }); throw error; } } /** * Deletes a VLAN */ export async function deleteVlan(siteId: string, vlanId: string): Promise { await ensureAuthenticated(); try { const url = `${BASE_URL}/sites/${siteId}/networks/${vlanId}`; logger.info('Deleting VLAN', { siteId, vlanId }); const response = await httpClient.delete(url); const data = response.data; if (data.errorCode !== 0) { throw new Error(`Failed to delete VLAN: ${data.msg || 'Unknown error'}`); } logger.info('VLAN deleted', { siteId, vlanId }); } catch (error) { logger.error('Error deleting VLAN', { siteId, vlanId, error }); throw error; } }