-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: clean up delete-buckets script
- Loading branch information
1 parent
1468b07
commit e154a93
Showing
1 changed file
with
15 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,34 +1,25 @@ | ||
import { OrganizationsClient, UpdateOrganizationalUnitCommand } from "@aws-sdk/client-organizations"; | ||
import { DeleteBucketCommand, DeleteObjectCommand, ListBucketsCommand, ListObjectsCommand, S3Client } from "@aws-sdk/client-s3"; | ||
import { fromIni } from "@aws-sdk/credential-providers"; | ||
import { DeleteBucketCommand, DeleteObjectCommand, ListBucketsCommand, ListObjectsCommand, S3Client } from '@aws-sdk/client-s3'; | ||
import { fromIni } from '@aws-sdk/credential-providers'; | ||
|
||
const bucketsToDelete = /(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}/; | ||
deleteBuckets().then(x=>console.log('done')); | ||
//stressOrganization().then(x=>console.log('done')); | ||
|
||
async function deleteBuckets(): Promise<void> { | ||
const s3 = new S3Client({credentials: fromIni({ profile: 'org-formation-test-v2' })}); | ||
const deleteBuckets = async (): Promise<void> => { | ||
const s3 = new S3Client({ credentials: fromIni({ profile: 'org-formation-test-v2' }) }); | ||
const bucketList = await s3.send(new ListBucketsCommand({})); | ||
for(const bucket of bucketList.Buckets) { | ||
for (const bucket of bucketList.Buckets) { | ||
if (bucketsToDelete.test(bucket.Name)) { | ||
console.log(`deleting ${bucket.Name}`); | ||
const objectList = await s3.send(new ListObjectsCommand({Bucket: bucket.Name})); | ||
for(const obj of objectList.Contents) { | ||
await s3.send(new DeleteObjectCommand({Bucket: bucket.Name, Key: obj.Key})); | ||
try { | ||
const objectList = await s3.send(new ListObjectsCommand({ Bucket: bucket.Name })); | ||
for (const obj of objectList.Contents) { | ||
await s3.send(new DeleteObjectCommand({ Bucket: bucket.Name, Key: obj.Key })); | ||
} | ||
await s3.send(new DeleteBucketCommand({ Bucket: bucket.Name })); | ||
} catch (err) { | ||
console.log(`failed deleting bucket ${bucket.Name}`, err); | ||
} | ||
await s3.send(new DeleteBucketCommand({Bucket: bucket.Name})); | ||
} | ||
} | ||
} | ||
|
||
async function stressOrganization(): Promise<void> { | ||
const org = new OrganizationsClient({credentials: fromIni({ profile: 'org-formation-test-v2' }), region: 'us-east-1'}); | ||
let i =0 | ||
while (i < 5) { | ||
org.send(new UpdateOrganizationalUnitCommand( { OrganizationalUnitId: 'ou-kvte-6olfshzg', Name: 'test' + i })).catch(err => { | ||
console.log(err); | ||
}) | ||
i++; | ||
} | ||
} | ||
}; | ||
|
||
deleteBuckets().then(x => console.log('done')); |