From 18e9f0151cb1967186666033144c36a97127af7d Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Tue, 30 Aug 2022 19:17:10 +0100 Subject: [PATCH] Create Bucket --- packages/server/__mocks__/aws-sdk.ts | 7 +++ packages/server/src/integrations/s3.ts | 54 ++++++++----------- .../server/src/integrations/tests/s3.spec.js | 29 +++++++--- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/packages/server/__mocks__/aws-sdk.ts b/packages/server/__mocks__/aws-sdk.ts index 75353db7e6..24873ac174 100644 --- a/packages/server/__mocks__/aws-sdk.ts +++ b/packages/server/__mocks__/aws-sdk.ts @@ -37,6 +37,13 @@ module AwsMock { Contents: {}, }) ) + + // @ts-ignore + this.createBucket = jest.fn( + response({ + Contents: {}, + }) + ) } aws.DynamoDB = { DocumentClient } diff --git a/packages/server/src/integrations/s3.ts b/packages/server/src/integrations/s3.ts index 67568436c3..b717ece222 100644 --- a/packages/server/src/integrations/s3.ts +++ b/packages/server/src/integrations/s3.ts @@ -131,22 +131,10 @@ module S3Module { "public-read", "public-read-write", "authenticated-read", - ] - } - }, - objectOwnership: { - required: false, - displayName: "Object ownership", - type: DatasourceFieldType.LIST, - data: { - create: [ - "BucketOwnerPreferred", - "ObjectWriter", - "BucketOwnerEnforced", ], }, }, - } + }, } class S3Integration implements IntegrationBase { @@ -165,33 +153,33 @@ module S3Module { } async create(query: { - bucket: string, - location: string, - grantFullControl: string, - grantRead: string, - grantReadAcp: string, - grantWrite: string, - grantWriteAcp: string, + bucket: string + location: string + grantFullControl: string + grantRead: string + grantReadAcp: string + grantWrite: string + grantWriteAcp: string extra: { - acl: string, - objectOwnership: string, - }}) { - const response = await this.client.createBucket({ + acl: string + } + }) { + let params: any = { Bucket: query.bucket, - // ACL: query.extra?.acl, - CreateBucketConfiguration: { - LocationConstraint: query.location - }, + ACL: query.extra?.acl, GrantFullControl: query.grantFullControl, GrantRead: query.grantRead, GrantReadACP: query.grantReadAcp, GrantWrite: query.grantWrite, GrantWriteACP: query.grantWriteAcp, - }, (err: any) => { - console.log("ERR ", err) - }) - .promise() - return response.Contents + } + if (query.location) { + params["CreateBucketConfiguration"] = { + LocationConstraint: query.location, + } + } + const response = await this.client.createBucket(params).promise() + return response } async read(query: { diff --git a/packages/server/src/integrations/tests/s3.spec.js b/packages/server/src/integrations/tests/s3.spec.js index 48e7221ef8..26a87fa99f 100644 --- a/packages/server/src/integrations/tests/s3.spec.js +++ b/packages/server/src/integrations/tests/s3.spec.js @@ -41,10 +41,10 @@ describe("S3 Integration", () => { grantReadAcp: "her", grantWrite: "she", grantWriteAcp: "he", - objectLockEnabledForBucket: true - }, { - acl: "private", - objectOwnership: "BucketOwnerPreferred" + objectLockEnabledForBucket: true, + extra: { + acl: "private" + } }) expect(config.integration.client.createBucket).toHaveBeenCalledWith({ Bucket: "test", @@ -53,12 +53,25 @@ describe("S3 Integration", () => { }, GrantFullControl: "me", GrantRead: "him", - GrantReadAcp: "her", + GrantReadACP: "her", GrantWrite: "she", - GrantWriteAcp: "he", - ObjectLockEnabledForBucket: true, + GrantWriteACP: "he", ACL: "private", - ObjectOwnership: "BucketOwnerPreferred" + }) + }) + + it("does not add undefined location constraint when calling the create method", async () => { + await config.integration.create({ + bucket: "test" + }) + expect(config.integration.client.createBucket).toHaveBeenCalledWith({ + Bucket: "test", + GrantFullControl: undefined, + GrantRead: undefined, + GrantReadACP: undefined, + GrantWrite: undefined, + GrantWriteACP: undefined, + ACL: undefined, }) }) }) \ No newline at end of file