first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import {describe, expect, test} from 'bun:test'
|
||||
import {awsStorageSchema} from '../cloud-spi/storageSchema'
|
||||
import type {CloudResource, CloudServiceAdapter, CreateResourceInput} from '../cloud-spi/types'
|
||||
import {CloudAdapterRegistry} from './CloudAdapterRegistry'
|
||||
|
||||
const adapter: CloudServiceAdapter = {
|
||||
cloud: 'aws',
|
||||
service: 'storage',
|
||||
schema: awsStorageSchema,
|
||||
list: async () => [],
|
||||
get: async () => null,
|
||||
create: async (_input: CreateResourceInput): Promise<CloudResource> => ({
|
||||
id: 'demo',
|
||||
name: 'demo',
|
||||
cloud: 'aws',
|
||||
service: 'storage',
|
||||
type: 'bucket',
|
||||
region: null,
|
||||
createdAt: null,
|
||||
metadata: {},
|
||||
}),
|
||||
delete: async () => {},
|
||||
}
|
||||
|
||||
describe('CloudAdapterRegistry', () => {
|
||||
test('returns registered adapters by cloud and service', () => {
|
||||
const registry = new CloudAdapterRegistry([adapter])
|
||||
|
||||
expect(registry.get('aws', 'storage')).toBe(adapter)
|
||||
expect(registry.get('azure', 'storage')).toBeUndefined()
|
||||
})
|
||||
|
||||
test('lists services registered for a cloud', () => {
|
||||
const registry = new CloudAdapterRegistry([adapter])
|
||||
|
||||
expect(registry.servicesFor('aws')).toEqual(['storage'])
|
||||
expect(registry.servicesFor('azure')).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,29 @@
|
||||
import type {CloudProvider, CloudServiceAdapter, CloudServiceType} from '../cloud-spi/types'
|
||||
|
||||
export class CloudAdapterRegistry {
|
||||
private readonly adapters = new Map<string, CloudServiceAdapter>()
|
||||
|
||||
constructor(adapters: CloudServiceAdapter[] = []) {
|
||||
for (const adapter of adapters) {
|
||||
this.register(adapter)
|
||||
}
|
||||
}
|
||||
|
||||
register(adapter: CloudServiceAdapter): void {
|
||||
this.adapters.set(this.key(adapter.cloud, adapter.service), adapter)
|
||||
}
|
||||
|
||||
get(cloud: CloudProvider, service: CloudServiceType): CloudServiceAdapter | undefined {
|
||||
return this.adapters.get(this.key(cloud, service))
|
||||
}
|
||||
|
||||
servicesFor(cloud: CloudProvider): CloudServiceType[] {
|
||||
return [...this.adapters.values()]
|
||||
.filter((adapter) => adapter.cloud === cloud)
|
||||
.map((adapter) => adapter.service)
|
||||
}
|
||||
|
||||
private key(cloud: CloudProvider, service: CloudServiceType): string {
|
||||
return `${cloud}:${service}`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user