Skip to content

Commit

Permalink
feat(sdk): add support for the forceDestroy property on the bucket (#…
Browse files Browse the repository at this point in the history
…7213)

The forceDestroy property allows to delete a bucket that contains
objects

## Checklist

- [X] Title matches [Winglang's style
guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [X] Description explains motivation and solution
- [X] Tests added (always)
- [X] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end
testing

*By submitting this pull request, I confirm that my contribution is made
under the terms of the [Wing Cloud Contribution
License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.

---------

Signed-off-by: monada-bot[bot] <monabot@monada.co>
Co-authored-by: monada-bot[bot] <monabot@monada.co>
  • Loading branch information
ShaiBer and monadabot authored Nov 1, 2024
1 parent 1408ea0 commit ee1ab4b
Show file tree
Hide file tree
Showing 27 changed files with 686 additions and 28 deletions.
14 changes: 14 additions & 0 deletions docs/api/04-standard-library/cloud/bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ let BucketProps = cloud.BucketProps{ ... };
| --- | --- | --- |
| <code><a href="#@winglang/sdk.cloud.BucketProps.property.cors">cors</a></code> | <code>bool</code> | Whether to add default cors configuration. |
| <code><a href="#@winglang/sdk.cloud.BucketProps.property.corsOptions">corsOptions</a></code> | <code><a href="#@winglang/sdk.cloud.BucketCorsOptions">BucketCorsOptions</a></code> | Custom cors configuration for the bucket. |
| <code><a href="#@winglang/sdk.cloud.BucketProps.property.forceDestroy">forceDestroy</a></code> | <code>bool</code> | Whether to allow the bucket to be deleted even if it is not empty. |
| <code><a href="#@winglang/sdk.cloud.BucketProps.property.public">public</a></code> | <code>bool</code> | Whether the bucket's objects should be publicly accessible. |

---
Expand Down Expand Up @@ -1130,6 +1131,19 @@ with the following options:

---

##### `forceDestroy`<sup>Optional</sup> <a name="forceDestroy" id="@winglang/sdk.cloud.BucketProps.property.forceDestroy"></a>

```wing
forceDestroy: bool;
```

- *Type:* bool
- *Default:* false

Whether to allow the bucket to be deleted even if it is not empty.

---

##### `public`<sup>Optional</sup> <a name="public" id="@winglang/sdk.cloud.BucketProps.property.public"></a>

```wing
Expand Down
7 changes: 5 additions & 2 deletions packages/@winglang/platform-awscdk/src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {

private readonly bucket: S3Bucket;
private readonly public: boolean;
private readonly forceDestroy: boolean;
private bucketDeployment?: BucketDeployment;

constructor(scope: Construct, id: string, props: cloud.BucketProps = {}) {
super(scope, id, props);

this.public = props.public ?? false;
this.forceDestroy = props.forceDestroy ?? false;

this.bucket = createEncryptedBucket(this, this.public);
this.bucket = createEncryptedBucket(this, this.public, this.forceDestroy);

if (props.cors ?? true) {
this.addCorsRule(
Expand Down Expand Up @@ -254,6 +256,7 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
export function createEncryptedBucket(
scope: Construct,
isPublic: boolean,
forceDestroy: boolean,
name: string = "Default"
): S3Bucket {
const isTestEnvironment = App.of(scope).isTestEnvironment;
Expand All @@ -270,6 +273,6 @@ export function createEncryptedBucket(
: BlockPublicAccess.BLOCK_ALL,
publicReadAccess: isPublic ? true : false,
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: isTestEnvironment ? true : false,
autoDeleteObjects: isTestEnvironment || forceDestroy ? true : false,
});
}
2 changes: 1 addition & 1 deletion packages/@winglang/platform-awscdk/src/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Website extends cloud.Website implements IAwsWebsite {
constructor(scope: Construct, id: string, props: cloud.WebsiteProps) {
super(scope, id, props);

this.bucket = createEncryptedBucket(this, false, "WebsiteBucket");
this.bucket = createEncryptedBucket(this, false, false, "WebsiteBucket");

new BucketDeployment(this, "BucketWebsiteConfiguration", {
destinationBucket: this.bucket,
Expand Down
Loading

0 comments on commit ee1ab4b

Please sign in to comment.