feat(api, web): enhance EvoBGP community handling and UI integration
- Introduced a new function to resolve EvoBGP community IDs, allowing for better handling of legacy labels and UUIDs. - Updated `fetchEvobgpCommunity` to return both resolved community IDs and CIDRs, improving data retrieval. - Enhanced the ListsPage component to manage community IDs separately from user input, ensuring accurate configuration during list creation. - Updated documentation to reflect changes in community ID resolution and integration with the UI. This update improves the user experience by ensuring that community IDs are correctly resolved and stored, facilitating smoother interactions with the EvoBGP API.
This commit is contained in:
@@ -51,13 +51,52 @@ async function fetchJsonUrl(url: string): Promise<string[]> {
|
||||
return uniq(out)
|
||||
}
|
||||
|
||||
const UUID_RE =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
|
||||
async function resolveEvobgpCommunityId(
|
||||
base: string,
|
||||
token: string,
|
||||
communityIdOrLabel: string,
|
||||
): Promise<string> {
|
||||
const key = communityIdOrLabel.trim()
|
||||
if (!key) throw new Error('community_id required')
|
||||
if (UUID_RE.test(key)) return key
|
||||
|
||||
// Heal lists created when Autocomplete stored label instead of UUID.
|
||||
const res = await fetch(`${base}/v1/communities?limit=200`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: 'application/json',
|
||||
},
|
||||
signal: AbortSignal.timeout(20_000),
|
||||
})
|
||||
if (!res.ok) {
|
||||
throw new Error(`EvoBGP communities HTTP ${res.status}`)
|
||||
}
|
||||
const data = (await res.json()) as {
|
||||
items?: { id?: string; community?: string; title?: string | null }[]
|
||||
}
|
||||
const hit = (data.items ?? []).find((c) => {
|
||||
if (!c.id || !c.community) return false
|
||||
if (c.id === key || c.community === key) return true
|
||||
if (c.title && `${c.community} · ${c.title}` === key) return true
|
||||
return false
|
||||
})
|
||||
if (!hit?.id) {
|
||||
throw new Error(`EvoBGP community not found: ${key}`)
|
||||
}
|
||||
return hit.id
|
||||
}
|
||||
|
||||
async function fetchEvobgpCommunity(
|
||||
apiUrl: string,
|
||||
token: string,
|
||||
communityId: string,
|
||||
): Promise<string[]> {
|
||||
): Promise<{ cidrs: string[]; resolvedId: string }> {
|
||||
const base = apiUrl.replace(/\/$/, '')
|
||||
const url = `${base}/v1/communities/${encodeURIComponent(communityId)}/prefixes?limit=5000`
|
||||
const resolvedId = await resolveEvobgpCommunityId(base, token, communityId)
|
||||
const url = `${base}/v1/communities/${encodeURIComponent(resolvedId)}/prefixes?limit=5000`
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
@@ -72,13 +111,13 @@ async function fetchEvobgpCommunity(
|
||||
items?: { prefix?: string }[]
|
||||
prefixes?: string[]
|
||||
}
|
||||
let cidrs: string[] = []
|
||||
if (Array.isArray(data.prefixes) && data.prefixes.length > 0) {
|
||||
return uniq(data.prefixes)
|
||||
cidrs = uniq(data.prefixes)
|
||||
} else if (Array.isArray(data.items)) {
|
||||
cidrs = uniq(data.items.map((i) => i.prefix ?? '').filter(Boolean))
|
||||
}
|
||||
if (Array.isArray(data.items)) {
|
||||
return uniq(data.items.map((i) => i.prefix ?? '').filter(Boolean))
|
||||
}
|
||||
return []
|
||||
return { cidrs, resolvedId }
|
||||
}
|
||||
|
||||
export async function refreshIpList(db: Db, listId: string): Promise<void> {
|
||||
@@ -111,8 +150,18 @@ export async function refreshIpList(db: Db, listId: string): Promise<void> {
|
||||
if (!apiUrl || !token || !communityId) {
|
||||
throw new Error('evobgp_api_url, token and community_id required')
|
||||
}
|
||||
cidrs = await fetchEvobgpCommunity(apiUrl, token, communityId)
|
||||
const fetched = await fetchEvobgpCommunity(apiUrl, token, communityId)
|
||||
cidrs = fetched.cidrs
|
||||
repos.replaceIpListEntries(db, listId, cidrs)
|
||||
// Persist resolved UUID if list was saved with autocomplete label.
|
||||
if (fetched.resolvedId !== communityId) {
|
||||
repos.updateIpList(db, listId, {
|
||||
configJson: JSON.stringify({
|
||||
...config,
|
||||
community_id: fetched.resolvedId,
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const contentHash = hashCidrs(cidrs)
|
||||
|
||||
Reference in New Issue
Block a user