105 lines
3.5 KiB
TypeScript
105 lines
3.5 KiB
TypeScript
import { mockLogEvents, mockLogRelays, mockLogWebhooks } from './mock'
|
|
import { mockOn, mocked, request } from './request'
|
|
import type { LogEventPage, LogRelayView, LogWebhookInfo, LogWebhookState } from '@/types/api'
|
|
|
|
export interface LogEventParams {
|
|
page: number
|
|
pageSize: number
|
|
/** 按租户过滤,缺省为全部 */
|
|
cfgId?: number
|
|
}
|
|
|
|
export function listLogEvents(params: LogEventParams): Promise<LogEventPage> {
|
|
if (mockOn) {
|
|
const filtered = params.cfgId
|
|
? mockLogEvents.filter((e) => e.ociConfigId === params.cfgId)
|
|
: mockLogEvents
|
|
const start = (params.page - 1) * params.pageSize
|
|
return mocked({
|
|
items: filtered.slice(start, start + params.pageSize),
|
|
total: filtered.length,
|
|
})
|
|
}
|
|
return request('/log-events', { query: { ...params } })
|
|
}
|
|
|
|
export function getLogWebhook(cfgId: number): Promise<LogWebhookState> {
|
|
if (mockOn) {
|
|
const info = mockLogWebhooks.get(cfgId)
|
|
return mocked(info ? { exists: true, webhook: { ...info } } : { exists: false })
|
|
}
|
|
return request(`/oci-configs/${cfgId}/log-webhook`)
|
|
}
|
|
|
|
/** 生成(或幂等返回)回传回调地址 */
|
|
export function ensureLogWebhook(cfgId: number): Promise<LogWebhookInfo> {
|
|
if (mockOn) {
|
|
let info = mockLogWebhooks.get(cfgId)
|
|
if (!info) {
|
|
const secret = `mock${cfgId}`.padEnd(64, '0')
|
|
info = {
|
|
path: `/api/v1/webhooks/oci-logs/${secret}`,
|
|
secret,
|
|
createdAt: new Date().toISOString(),
|
|
}
|
|
mockLogWebhooks.set(cfgId, info)
|
|
}
|
|
return mocked({ ...info }, 400)
|
|
}
|
|
return request(`/oci-configs/${cfgId}/log-webhook`, { method: 'POST' })
|
|
}
|
|
|
|
/** 撤销回调地址,旧 URL 随即 404 */
|
|
export function revokeLogWebhook(cfgId: number): Promise<void> {
|
|
if (mockOn) {
|
|
mockLogWebhooks.delete(cfgId)
|
|
return mocked(undefined, 300)
|
|
}
|
|
return request(`/oci-configs/${cfgId}/log-webhook`, { method: 'DELETE' })
|
|
}
|
|
|
|
/** 查询 OCI 侧链路状态与关键事件清单 */
|
|
export function getLogRelay(cfgId: number): Promise<LogRelayView> {
|
|
if (mockOn) return mocked(structuredCloneRelay(cfgId))
|
|
return request(`/oci-configs/${cfgId}/log-relay`)
|
|
}
|
|
|
|
/** 一键建立回传链路(P1):同步执行,含订阅确认与 Connector 就绪等待,可达 2 分钟 */
|
|
export function setupLogRelay(cfgId: number): Promise<LogRelayView> {
|
|
if (mockOn) {
|
|
mockLogRelays.set(cfgId, true)
|
|
return mocked(structuredCloneRelay(cfgId), 1500)
|
|
}
|
|
return request(`/oci-configs/${cfgId}/log-relay`, { method: 'POST' })
|
|
}
|
|
|
|
/** 销毁 OCI 侧链路并撤销回调地址 */
|
|
export function teardownLogRelay(cfgId: number): Promise<void> {
|
|
if (mockOn) {
|
|
mockLogRelays.delete(cfgId)
|
|
mockLogWebhooks.delete(cfgId)
|
|
return mocked(undefined, 800)
|
|
}
|
|
return request(`/oci-configs/${cfgId}/log-relay`, { method: 'DELETE' })
|
|
}
|
|
|
|
/** mock 模式下按已建状态拼一份链路视图 */
|
|
function structuredCloneRelay(cfgId: number): LogRelayView {
|
|
const built = mockLogRelays.get(cfgId) === true
|
|
const res = (suffix: string) => (built ? { id: `ocid1.mock.${suffix}`, state: 'ACTIVE' } : { id: '', state: '' })
|
|
return {
|
|
endpoint: built ? `https://demo.example.com/api/v1/webhooks/oci-logs/mock${cfgId}` : undefined,
|
|
topic: res('topic'),
|
|
subscription: res('sub'),
|
|
connector: res('conn'),
|
|
policy: res('policy'),
|
|
ready: built,
|
|
events: [
|
|
'LaunchInstance', 'TerminateInstance', 'InstanceAction',
|
|
'CreateUser', 'DeleteUser', 'UpdateUser',
|
|
'CreateApiKey', 'DeleteApiKey', 'UpdateUserCapabilities',
|
|
'CreateRegionSubscription', 'CreatePolicy', 'UpdatePolicy', 'DeletePolicy',
|
|
],
|
|
}
|
|
}
|