-
-
Notifications
You must be signed in to change notification settings - Fork 33
Feat/aws dynamodb explorer #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AncientGear
wants to merge
2
commits into
floci-io:main
Choose a base branch
from
AncientGear:feat/aws-dynamodb-explorer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,3 +67,5 @@ temp/ | |
|
|
||
| data | ||
| local | ||
| .atl | ||
| openspec | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/api/src/adapter-aws/AwsDynamoDbAdapter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import {describe, expect, test} from 'bun:test' | ||
| import {AwsDynamoDbAdapter} from './AwsDynamoDbAdapter' | ||
| import type {DynamoDbTable} from '../services/dynamodb' | ||
|
|
||
| const baseTable: DynamoDbTable = { | ||
| tableName: 'users', | ||
| arn: 'arn:aws:dynamodb:us-east-1:123456789012:table/users', | ||
| status: 'ACTIVE', | ||
| billingMode: 'PAY_PER_REQUEST', | ||
| itemCount: 12, | ||
| sizeBytes: 256, | ||
| keySchema: [{attributeName: 'pk', keyType: 'HASH'}], | ||
| region: 'us-east-1', | ||
| createdAt: '2025-01-01T00:00:00.000Z', | ||
| } | ||
|
|
||
| function fakeService(overrides: Partial<{ | ||
| listTables: () => Promise<DynamoDbTable[]> | ||
| describeTable: (tableName: string) => Promise<DynamoDbTable> | ||
| }> = {}) { | ||
| return { | ||
| listTables: async () => [baseTable], | ||
| describeTable: async (_tableName: string) => baseTable, | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| describe('AwsDynamoDbAdapter', () => { | ||
| test('list returns mapped CloudResource array', async () => { | ||
| const adapter = new AwsDynamoDbAdapter(fakeService()) | ||
| const result = await adapter.list() | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(result[0]).toMatchObject({ | ||
| id: 'users', | ||
| name: 'users', | ||
| cloud: 'aws', | ||
| service: 'dynamodb', | ||
| type: 'dynamodb-table', | ||
| status: 'ACTIVE', | ||
| region: 'us-east-1', | ||
| }) | ||
| expect(result[0].metadata).toEqual({ | ||
| arn: 'arn:aws:dynamodb:us-east-1:123456789012:table/users', | ||
| billingMode: 'PAY_PER_REQUEST', | ||
| itemCount: 12, | ||
| sizeBytes: 256, | ||
| keySchema: [{attributeName: 'pk', keyType: 'HASH'}], | ||
| }) | ||
| }) | ||
|
|
||
| test('list filters results by search term', async () => { | ||
| const adapter = new AwsDynamoDbAdapter(fakeService({ | ||
| listTables: async () => [ | ||
| baseTable, | ||
| {...baseTable, tableName: 'orders'}, | ||
| ], | ||
| })) | ||
|
|
||
| const result = await adapter.list({search: 'user'}) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(result[0].name).toBe('users') | ||
| }) | ||
|
|
||
| test('list returns an empty array when no tables exist', async () => { | ||
| const adapter = new AwsDynamoDbAdapter(fakeService({ | ||
| listTables: async () => [], | ||
| })) | ||
|
|
||
| await expect(adapter.list()).resolves.toEqual([]) | ||
| }) | ||
|
|
||
| test('get returns null when the table is not found', async () => { | ||
| const notFound = Object.assign(new Error('Requested resource not found'), { | ||
| name: 'ResourceNotFoundException', | ||
| $metadata: {httpStatusCode: 404}, | ||
| }) | ||
| const adapter = new AwsDynamoDbAdapter(fakeService({ | ||
| describeTable: async () => { | ||
| throw notFound | ||
| }, | ||
| })) | ||
|
|
||
| await expect(adapter.get('missing')).resolves.toBeNull() | ||
| }) | ||
|
|
||
| test('get rethrows non-404 errors', async () => { | ||
| const adapter = new AwsDynamoDbAdapter(fakeService({ | ||
| describeTable: async () => { | ||
| throw new Error('boom') | ||
| }, | ||
| })) | ||
|
|
||
| await expect(adapter.get('users')).rejects.toThrow('boom') | ||
| }) | ||
|
|
||
| test('schema returns the aws dynamodb schema', () => { | ||
| const adapter = new AwsDynamoDbAdapter(fakeService()) | ||
| const schema = adapter.schema() | ||
|
|
||
| expect(schema.cloud).toBe('aws') | ||
| expect(schema.service).toBe('dynamodb') | ||
| expect(schema.displayName).toBe('DynamoDB') | ||
| expect(schema.actions).toContain('list') | ||
| expect(schema.actions).toContain('inspect') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import {awsDynamoDbSchema} from '../cloud-spi/dynamodbSchema' | ||
| import type { | ||
| CloudResource, | ||
| CloudServiceAdapter, | ||
| CreateResourceInput, | ||
| ResourceQuery, | ||
| ServiceSchema, | ||
| } from '../cloud-spi/types' | ||
| import {dynamoDbService, type DynamoDbTable} from '../services/dynamodb' | ||
|
|
||
| type DynamoDbServiceShape = { | ||
| listTables(): Promise<DynamoDbTable[]> | ||
| describeTable(tableName: string): Promise<DynamoDbTable> | ||
| } | ||
|
|
||
| export class AwsDynamoDbAdapter implements CloudServiceAdapter { | ||
| readonly cloud = 'aws' as const | ||
| readonly service = 'dynamodb' as const | ||
|
|
||
| constructor(private readonly service_: DynamoDbServiceShape = dynamoDbService) {} | ||
|
|
||
| schema(): ServiceSchema { | ||
| return awsDynamoDbSchema() | ||
| } | ||
|
|
||
| async list(query: ResourceQuery = {}): Promise<CloudResource[]> { | ||
| const resources = (await this.service_.listTables()).map(tableToResource) | ||
| return filterBySearch(resources, query.search) | ||
| } | ||
|
|
||
| async get(id: string): Promise<CloudResource | null> { | ||
| try { | ||
| return tableToResource(await this.service_.describeTable(id)) | ||
| } catch (error) { | ||
| if (hasNotFoundStatus(error)) return null | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| async create(_input: CreateResourceInput): Promise<CloudResource> { | ||
| throw new Error('DynamoDB table creation is not supported from the dynamic Cloud Explorer.') | ||
| } | ||
|
|
||
| async delete(_id: string): Promise<void> { | ||
| throw new Error('DynamoDB table deletion is not supported from the dynamic Cloud Explorer.') | ||
| } | ||
| } | ||
|
|
||
| function tableToResource(table: DynamoDbTable): CloudResource { | ||
| return { | ||
| id: table.tableName, | ||
| name: table.tableName, | ||
| cloud: 'aws', | ||
| service: 'dynamodb', | ||
| type: 'dynamodb-table', | ||
| region: table.region, | ||
| createdAt: table.createdAt ?? null, | ||
| status: table.status ?? null, | ||
| metadata: { | ||
| arn: table.arn, | ||
| billingMode: table.billingMode, | ||
| itemCount: table.itemCount, | ||
| sizeBytes: table.sizeBytes, | ||
| keySchema: table.keySchema, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| function filterBySearch(resources: CloudResource[], search?: string): CloudResource[] { | ||
| const normalized = search?.trim().toLowerCase() | ||
| if (!normalized) return resources | ||
| return resources.filter((resource) => resource.name.toLowerCase().includes(normalized)) | ||
| } | ||
|
|
||
| function hasNotFoundStatus(error: unknown): boolean { | ||
| if (typeof error !== 'object' || error === null) return false | ||
| const metadata = (error as {$metadata?: {httpStatusCode?: number}}).$metadata | ||
| const name = 'name' in error ? error.name : undefined | ||
| return metadata?.httpStatusCode === 404 || name === 'ResourceNotFoundException' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import {describe, expect, test} from 'bun:test' | ||
| import {awsClients, awsRegion} from './aws' | ||
|
|
||
| describe('aws client registry', () => { | ||
| test('exposes a dynamodb client', () => { | ||
| expect(typeof awsClients.dynamodb.send).toBe('function') | ||
| }) | ||
|
|
||
| test('exports the resolved aws region', () => { | ||
| expect(awsRegion).toBe(process.env.AWS_REGION ?? 'us-east-1') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import {describe, expect, test} from 'bun:test' | ||
| import {awsDynamoDbSchema, dynamodbSchemaFor} from './dynamodbSchema' | ||
|
|
||
| describe('dynamodb schema', () => { | ||
| test('returns the aws dynamodb schema', () => { | ||
| const schema = awsDynamoDbSchema() | ||
|
|
||
| expect(schema.cloud).toBe('aws') | ||
| expect(schema.service).toBe('dynamodb') | ||
| expect(schema.displayName).toBe('DynamoDB') | ||
| expect(schema.actions).toEqual(['list', 'inspect']) | ||
| expect(schema.columns.map((column) => column.name)).toEqual([ | ||
| 'name', | ||
| 'status', | ||
| 'billingMode', | ||
| 'itemCount', | ||
| 'sizeBytes', | ||
| ]) | ||
| }) | ||
|
|
||
| test('returns a provider fallback only for aws', () => { | ||
| expect(dynamodbSchemaFor('aws')?.displayName).toBe('DynamoDB') | ||
| expect(dynamodbSchemaFor('azure')).toBeNull() | ||
| expect(dynamodbSchemaFor('gcp')).toBeNull() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type {CloudProvider, FieldSchema, ServiceSchema, TableColumnSchema} from './types' | ||
|
|
||
| const dynamodbColumns: TableColumnSchema[] = [ | ||
| {name: 'name', label: 'Name'}, | ||
| {name: 'status', label: 'Status'}, | ||
| {name: 'billingMode', label: 'Billing Mode'}, | ||
| {name: 'itemCount', label: 'Item Count'}, | ||
| {name: 'sizeBytes', label: 'Size (Bytes)'}, | ||
| ] | ||
|
|
||
| const dynamodbFilters: FieldSchema[] = [ | ||
| {name: 'search', label: 'Search', type: 'text', required: false}, | ||
| ] | ||
|
|
||
| export function awsDynamoDbSchema(): ServiceSchema { | ||
| return { | ||
| cloud: 'aws', | ||
| service: 'dynamodb', | ||
| displayName: 'DynamoDB', | ||
| fields: [], | ||
| actions: ['list', 'inspect'], | ||
| filters: dynamodbFilters, | ||
| columns: dynamodbColumns, | ||
| } | ||
| } | ||
|
|
||
| export function dynamodbSchemaFor(cloud: CloudProvider): ServiceSchema | null { | ||
| if (cloud === 'aws') return awsDynamoDbSchema() | ||
| return null | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The three service-specific columns —
billingMode,itemCount, andsizeBytes— point to field names that don't exist at the top level ofCloudResource.ResourceTableresolves columns viaresource[column.name as keyof CloudResource], so any column name that isn't a known top-level property returnsundefined, whichformatValuerenders as'-'. Compare: the EKS schema uses aversioncolumn becauseversionis a first-class field onCloudResource, and the Database schema usesengine/instanceClassfor the same reason.The fix is either to promote
billingMode,itemCount, andsizeBytesto top-level fields onCloudResource(both backendtypes.tsand frontendtypes/resource.ts) and expose them directly fromtableToResource, or alternatively to teachResourceTableto fall back toresource.metadata[column.name]when a column name isn't a recognized top-level key.