Implements testConnection and listResources against the NetBird Management API (/api/peers), mapping connected/disconnected peers to resource health. Defaults to the NetBird Cloud API but respects an optional baseUrl override for self-hosted management servers. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BbJV5nm8KPVH1oNJYKpnoF
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import type { IntegrationAdapter, Resource } from './types.js'
|
|
|
|
interface NetbirdPeer {
|
|
name: string
|
|
hostname?: string
|
|
connected: boolean
|
|
ip?: string
|
|
os?: string
|
|
}
|
|
|
|
function baseUrlFor(config: Record<string, string>) {
|
|
return (config.baseUrl?.replace(/\/$/, '')) || 'https://api.netbird.io'
|
|
}
|
|
|
|
export const netbird: IntegrationAdapter = {
|
|
async testConnection(config, secrets) {
|
|
const apiKey = secrets.apiKey
|
|
if (!apiKey) return { ok: false, message: 'Missing API key' }
|
|
try {
|
|
const res = await fetch(`${baseUrlFor(config)}/api/peers`, {
|
|
headers: { Authorization: `Token ${apiKey}` },
|
|
})
|
|
if (!res.ok) return { ok: false, message: `HTTP ${res.status}` }
|
|
return { ok: true, message: 'Connected' }
|
|
} catch (err) {
|
|
return { ok: false, message: err instanceof Error ? err.message : 'Connection failed' }
|
|
}
|
|
},
|
|
|
|
async listResources(config, secrets): Promise<Resource[]> {
|
|
const apiKey = secrets.apiKey
|
|
if (!apiKey) return []
|
|
const res = await fetch(`${baseUrlFor(config)}/api/peers`, {
|
|
headers: { Authorization: `Token ${apiKey}` },
|
|
})
|
|
if (!res.ok) return []
|
|
const peers = (await res.json()) as NetbirdPeer[]
|
|
return peers.map((p) => ({
|
|
name: p.name || p.hostname || p.ip || 'unknown peer',
|
|
status: p.connected ? 'healthy' : 'critical',
|
|
detail: p.connected ? `Online — ${p.ip ?? ''}`.trim() : 'Offline',
|
|
}))
|
|
},
|
|
}
|