67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
import { mockIpv6Report, mockSecurityLists, mockSubnets, mockVcns } from './mock'
|
|
import { mockOn, mocked, request } from './request'
|
|
import type { CreateVcnRequest, Ipv6Report, SecurityList, Subnet, Vcn } from '@/types/api'
|
|
|
|
export function listVcns(cfgId: number, region?: string, compartmentId?: string): Promise<Vcn[]> {
|
|
if (mockOn) return mocked(compartmentId ? [] : (mockVcns[cfgId] ?? []))
|
|
return request(`/oci-configs/${cfgId}/vcns`, { query: { region, compartmentId } })
|
|
}
|
|
|
|
export function getVcn(cfgId: number, vcnId: string, region?: string): Promise<Vcn> {
|
|
if (mockOn) {
|
|
const found = Object.values(mockVcns)
|
|
.flat()
|
|
.find((v) => v.id === vcnId)
|
|
return found ? mocked(found) : Promise.reject(new Error('VCN 不存在'))
|
|
}
|
|
return request(`/oci-configs/${cfgId}/vcns/${encodeURIComponent(vcnId)}`, { query: { region } })
|
|
}
|
|
|
|
export function createVcn(cfgId: number, body: CreateVcnRequest): Promise<Vcn> {
|
|
if (mockOn) return mocked({ ...mockVcns[1][0], id: 'ocid1.vcn..new', displayName: body.displayName }, 900)
|
|
return request(`/oci-configs/${cfgId}/vcns`, { method: 'POST', body })
|
|
}
|
|
|
|
export function renameVcn(
|
|
cfgId: number,
|
|
vcnId: string,
|
|
displayName: string,
|
|
region?: string,
|
|
): Promise<Vcn> {
|
|
if (mockOn) return mocked({ ...mockVcns[1][0], displayName })
|
|
return request(`/oci-configs/${cfgId}/vcns/${encodeURIComponent(vcnId)}`, {
|
|
method: 'PUT',
|
|
body: { region: region ?? '', displayName },
|
|
})
|
|
}
|
|
|
|
export function deleteVcn(cfgId: number, vcnId: string, region?: string): Promise<void> {
|
|
if (mockOn) return mocked(undefined)
|
|
return request(`/oci-configs/${cfgId}/vcns/${encodeURIComponent(vcnId)}`, {
|
|
method: 'DELETE',
|
|
query: { region },
|
|
})
|
|
}
|
|
|
|
export function enableVcnIpv6(cfgId: number, vcnId: string, region?: string): Promise<Ipv6Report> {
|
|
if (mockOn) return mocked(mockIpv6Report, 1500)
|
|
return request(`/oci-configs/${cfgId}/vcns/${encodeURIComponent(vcnId)}/enable-ipv6`, {
|
|
method: 'POST',
|
|
body: { region: region ?? '' },
|
|
})
|
|
}
|
|
|
|
export function listSubnets(cfgId: number, vcnId: string, region?: string): Promise<Subnet[]> {
|
|
if (mockOn) return mocked(mockSubnets.filter((s) => s.vcnId === vcnId || vcnId === ''))
|
|
return request(`/oci-configs/${cfgId}/subnets`, { query: { vcnId, region } })
|
|
}
|
|
|
|
export function listSecurityLists(
|
|
cfgId: number,
|
|
vcnId: string,
|
|
region?: string,
|
|
): Promise<SecurityList[]> {
|
|
if (mockOn) return mocked(mockSecurityLists.filter((s) => s.vcnId === vcnId || vcnId === ''))
|
|
return request(`/oci-configs/${cfgId}/security-lists`, { query: { vcnId, region } })
|
|
}
|