Merge pull request #10589 from Budibase/budi-6932/verify_dynamodb

Implement dynamodb connection verification
This commit is contained in:
Michael Drury 2023-05-15 17:10:10 +01:00 committed by GitHub
commit d77107f540
3 changed files with 90 additions and 6 deletions

View File

@ -140,7 +140,7 @@ export function init(endpoint: string) {
docClient = new AWS.DynamoDB.DocumentClient(docClientParams)
}
if (!env.isProd()) {
if (!env.isProd() && !env.isJest()) {
env._set("AWS_ACCESS_KEY_ID", "KEY_ID")
env._set("AWS_SECRET_ACCESS_KEY", "SECRET_KEY")
init("http://localhost:8333")

View File

@ -8,6 +8,7 @@ import {
import AWS from "aws-sdk"
import { AWS_REGION } from "../db/dynamoClient"
import { DocumentClient } from "aws-sdk/clients/dynamodb"
interface DynamoDBConfig {
region: string
@ -130,7 +131,7 @@ const SCHEMA: Integration = {
class DynamoDBIntegration implements IntegrationBase {
private config: DynamoDBConfig
private client: any
private client
constructor(config: DynamoDBConfig) {
this.config = config
@ -150,7 +151,19 @@ class DynamoDBIntegration implements IntegrationBase {
this.client = new AWS.DynamoDB.DocumentClient(this.config)
}
async create(query: { table: string; json: object }) {
async testConnection() {
try {
const scanRes = await new AWS.DynamoDB(this.config).listTables().promise()
return !!scanRes.$response
} catch (e: any) {
return { error: e.message as string }
}
}
async create(query: {
table: string
json: Omit<DocumentClient.PutItemInput, "TableName">
}) {
const params = {
TableName: query.table,
...query.json,
@ -191,7 +204,10 @@ class DynamoDBIntegration implements IntegrationBase {
return new AWS.DynamoDB(this.config).describeTable(params).promise()
}
async get(query: { table: string; json: object }) {
async get(query: {
table: string
json: Omit<DocumentClient.GetItemInput, "TableName">
}) {
const params = {
TableName: query.table,
...query.json,
@ -199,7 +215,10 @@ class DynamoDBIntegration implements IntegrationBase {
return this.client.get(params).promise()
}
async update(query: { table: string; json: object }) {
async update(query: {
table: string
json: Omit<DocumentClient.UpdateItemInput, "TableName">
}) {
const params = {
TableName: query.table,
...query.json,
@ -207,7 +226,10 @@ class DynamoDBIntegration implements IntegrationBase {
return this.client.update(params).promise()
}
async delete(query: { table: string; json: object }) {
async delete(query: {
table: string
json: Omit<DocumentClient.DeleteItemInput, "TableName">
}) {
const params = {
TableName: query.table,
...query.json,

View File

@ -0,0 +1,62 @@
import { GenericContainer } from "testcontainers"
import { env } from "@budibase/backend-core"
import dynamodb from "../../../../packages/server/src/integrations/dynamodb"
import { generator } from "../../shared"
jest.unmock("aws-sdk")
describe("datasource validators", () => {
describe("dynamodb", () => {
let connectionSettings: {
user: string
password: string
url: string
}
beforeAll(async () => {
const user = "root"
const password = generator.hash()
const container = await new GenericContainer("amazon/dynamodb-local")
.withExposedPorts(8000)
.start()
connectionSettings = {
user,
password,
url: `http://${container.getContainerIpAddress()}:${container.getMappedPort(
8000
)}`,
}
env._set("AWS_ACCESS_KEY_ID", "mocked_key")
env._set("AWS_SECRET_ACCESS_KEY", "mocked_secret")
})
it("test valid connection string", async () => {
const integration = new dynamodb.integration({
endpoint: connectionSettings.url,
region: "",
accessKeyId: "",
secretAccessKey: "",
})
const result = await integration.testConnection()
expect(result).toBe(true)
})
it("test wrong endpoint", async () => {
const integration = new dynamodb.integration({
endpoint: "http://wrong.url:2880",
region: "",
accessKeyId: "",
secretAccessKey: "",
})
const result = await integration.testConnection()
expect(result).toEqual({
error:
"Inaccessible host: `wrong.url' at port `undefined'. This service may not be available in the `eu-west-1' region.",
})
})
})
})