diff --git a/storage/disableDefaultEventBasedHold.js b/storage/disableDefaultEventBasedHold.js new file mode 100644 index 0000000000..53237fe56a --- /dev/null +++ b/storage/disableDefaultEventBasedHold.js @@ -0,0 +1,57 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main(bucketName = 'my-bucket') { + // [START storage_disable_default_event_based_hold] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function disableDefaultEventBasedHold() { + try { + // Disables a default event-based hold for a bucket. + await storage.bucket(bucketName).setMetadata({ + defaultEventBasedHold: false, + }); + console.log(`Default event-based hold was disabled for ${bucketName}.`); + } catch (error) { + console.error( + 'Error executing disable default event-based hold:', + error.message || error + ); + } + } + + disableDefaultEventBasedHold(); + // [END storage_disable_default_event_based_hold] +} +main(...process.argv.slice(2)); diff --git a/storage/enableDefaultEventBasedHold.js b/storage/enableDefaultEventBasedHold.js new file mode 100644 index 0000000000..16efcfcf94 --- /dev/null +++ b/storage/enableDefaultEventBasedHold.js @@ -0,0 +1,59 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main(bucketName = 'my-bucket') { + // [START storage_enable_default_event_based_hold] + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function enableDefaultEventBasedHold() { + try { + // Enables a default event-based hold for the bucket. + await storage.bucket(bucketName).setMetadata({ + defaultEventBasedHold: true, + }); + + console.log(`Default event-based hold was enabled for ${bucketName}.`); + } catch (error) { + console.error( + 'Error executing enable default event-based hold:', + error.message || error + ); + } + } + + enableDefaultEventBasedHold(); + // [END storage_enable_default_event_based_hold] +} +main(...process.argv.slice(2)); diff --git a/storage/getDefaultEventBasedHold.js b/storage/getDefaultEventBasedHold.js new file mode 100644 index 0000000000..b9b3837cb5 --- /dev/null +++ b/storage/getDefaultEventBasedHold.js @@ -0,0 +1,56 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main(bucketName = 'my-bucket') { + // [START storage_get_default_event_based_hold] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function getDefaultEventBasedHold() { + try { + const [metadata] = await storage.bucket(bucketName).getMetadata(); + console.log( + `Default event-based hold: ${metadata.defaultEventBasedHold}.` + ); + } catch (error) { + console.error( + 'Error executing get default event-based hold:', + error.message || error + ); + } + } + + getDefaultEventBasedHold(); + // [END storage_get_default_event_based_hold] +} +main(...process.argv.slice(2)); diff --git a/storage/getRetentionPolicy.js b/storage/getRetentionPolicy.js new file mode 100644 index 0000000000..6df1fdec3f --- /dev/null +++ b/storage/getRetentionPolicy.js @@ -0,0 +1,64 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main(bucketName = 'my-bucket') { + // [START storage_get_retention_policy] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function getRetentionPolicy() { + try { + const [metadata] = await storage.bucket(bucketName).getMetadata(); + if (metadata.retentionPolicy) { + const retentionPolicy = metadata.retentionPolicy; + console.log('A retention policy exists!'); + console.log(`Period: ${retentionPolicy.retentionPeriod}`); + console.log(`Effective time: ${retentionPolicy.effectiveTime}`); + if (retentionPolicy.isLocked) { + console.log('Policy is locked'); + } else { + console.log('Policy is unlocked'); + } + } + } catch (error) { + console.error( + 'Error executing get bucket retention policy:', + error.message || error + ); + } + } + + getRetentionPolicy(); + // [END storage_get_retention_policy] +} +main(...process.argv.slice(2)); diff --git a/storage/lockRetentionPolicy.js b/storage/lockRetentionPolicy.js new file mode 100644 index 0000000000..7323803579 --- /dev/null +++ b/storage/lockRetentionPolicy.js @@ -0,0 +1,65 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main(bucketName = 'my-bucket') { + // [START storage_lock_retention_policy] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function lockRetentionPolicy() { + try { + // Gets the current metageneration value for the bucket, required by + // lock_retention_policy + const [unlockedMetadata] = await storage.bucket(bucketName).getMetadata(); + + // Warning: Once a retention policy is locked, it cannot be unlocked. The + // retention period can only be increased + const [lockedMetadata] = await storage + .bucket(bucketName) + .lock(unlockedMetadata.metageneration); + console.log(`Retention policy for ${bucketName} is now locked`); + console.log( + `Retention policy effective as of ${lockedMetadata.retentionPolicy.effectiveTime}` + ); + } catch (error) { + console.error( + 'Error executing lock bucket retention policy:', + error.message || error + ); + } + } + + lockRetentionPolicy(); + // [END storage_lock_retention_policy] +} +main(...process.argv.slice(2)); diff --git a/storage/releaseEventBasedHold.js b/storage/releaseEventBasedHold.js new file mode 100644 index 0000000000..fcc7831008 --- /dev/null +++ b/storage/releaseEventBasedHold.js @@ -0,0 +1,73 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main( + bucketName = 'my-bucket', + fileName = 'test.txt', + metagenerationMatchPrecondition = null +) { + // [START storage_release_event_based_hold] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // The ID of your GCS file + // const fileName = 'your-file-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function releaseEventBasedHold() { + try { + // Optional: set a meta-generation-match precondition to avoid potential race + // conditions and data corruptions. The request to set metadata is aborted if the + // object's metageneration number does not match your precondition. + const options = { + ifMetagenerationMatch: metagenerationMatchPrecondition, + }; + + await storage.bucket(bucketName).file(fileName).setMetadata( + { + eventBasedHold: false, + }, + options + ); + console.log(`Event-based hold was released for ${fileName}.`); + } catch (error) { + console.error( + 'Error executing release event-based hold:', + error.message || error + ); + } + } + + releaseEventBasedHold(); + // [END storage_release_event_based_hold] +} +main(...process.argv.slice(2)); diff --git a/storage/releaseTemporaryHold.js b/storage/releaseTemporaryHold.js new file mode 100644 index 0000000000..d191a93034 --- /dev/null +++ b/storage/releaseTemporaryHold.js @@ -0,0 +1,73 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main( + bucketName = 'my-bucket', + fileName = 'test.txt', + metagenerationMatchPrecondition = null +) { + // [START storage_release_temporary_hold] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // The ID of your GCS file + // const fileName = 'your-file-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function releaseTemporaryHold() { + try { + // Optional: set a meta-generation-match precondition to avoid potential race + // conditions and data corruptions. The request to set metadata is aborted if the + // object's metageneration number does not match your precondition. + const options = { + ifMetagenerationMatch: metagenerationMatchPrecondition, + }; + + await storage.bucket(bucketName).file(fileName).setMetadata( + { + temporaryHold: false, + }, + options + ); + console.log(`Temporary hold was released for ${fileName}.`); + } catch (error) { + console.error( + 'Error executing release temporary hold:', + error.message || error + ); + } + } + + releaseTemporaryHold(); + // [END storage_release_temporary_hold] +} +main(...process.argv.slice(2)); diff --git a/storage/removeRetentionPolicy.js b/storage/removeRetentionPolicy.js new file mode 100644 index 0000000000..05938e18a9 --- /dev/null +++ b/storage/removeRetentionPolicy.js @@ -0,0 +1,61 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main(bucketName = 'my-bucket') { + // [START storage_remove_retention_policy] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function removeRetentionPolicy() { + try { + const [metadata] = await storage.bucket(bucketName).getMetadata(); + if (metadata.retentionPolicy && metadata.retentionPolicy.isLocked) { + console.log( + 'Unable to remove retention period as retention policy is locked.' + ); + } else { + await storage.bucket(bucketName).removeRetentionPeriod(); + console.log(`Removed bucket ${bucketName} retention policy.`); + } + } catch (error) { + console.error( + 'Error executing remove bucket retention policy:', + error.message || error + ); + } + } + + removeRetentionPolicy(); + // [END storage_remove_retention_policy] +} +main(...process.argv.slice(2)); diff --git a/storage/setEventBasedHold.js b/storage/setEventBasedHold.js new file mode 100644 index 0000000000..a46309ce1d --- /dev/null +++ b/storage/setEventBasedHold.js @@ -0,0 +1,74 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main( + bucketName = 'my-bucket', + fileName = 'test.txt', + metagenerationMatchPrecondition = null +) { + // [START storage_set_event_based_hold] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // The ID of your GCS file + // const fileName = 'your-file-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function setEventBasedHold() { + try { + // Optional: set a meta-generation-match precondition to avoid potential race + // conditions and data corruptions. The request to set metadata is aborted if the + // object's metageneration number does not match your precondition. + const options = { + ifMetagenerationMatch: metagenerationMatchPrecondition, + }; + + // Set event-based hold + await storage.bucket(bucketName).file(fileName).setMetadata( + { + eventBasedHold: true, + }, + options + ); + console.log(`Event-based hold was set for ${fileName}.`); + } catch (error) { + console.error( + 'Error executing set event-based hold:', + error.message || error + ); + } + } + + setEventBasedHold(); + // [END storage_set_event_based_hold] +} +main(...process.argv.slice(2)); diff --git a/storage/setRetentionPolicy.js b/storage/setRetentionPolicy.js new file mode 100644 index 0000000000..cb175a8f25 --- /dev/null +++ b/storage/setRetentionPolicy.js @@ -0,0 +1,61 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main(bucketName = 'my-bucket', retentionPeriod = 5) { + // [START storage_set_retention_policy] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // The retention period for objects in bucket + // const retentionPeriod = 3600; // 1 hour in seconds + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function setRetentionPolicy() { + try { + const [metadata] = await storage + .bucket(bucketName) + .setRetentionPeriod(retentionPeriod); + console.log( + `Bucket ${bucketName} retention period set for ${metadata.retentionPolicy.retentionPeriod} seconds.` + ); + } catch (error) { + console.error( + 'Error executing set bucket retention policy:', + error.message || error + ); + } + } + + setRetentionPolicy(); + // [END storage_set_retention_policy] +} +main(...process.argv.slice(2)); diff --git a/storage/setTemporaryHold.js b/storage/setTemporaryHold.js new file mode 100644 index 0000000000..a17d56b7a8 --- /dev/null +++ b/storage/setTemporaryHold.js @@ -0,0 +1,73 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * This application demonstrates how to use Bucket Lock operations on buckets + * and objects using the Google Cloud Storage API. + * + * For more information read the documentation + * at https://cloud.google.com/storage/docs/bucket-lock + */ + +function main( + bucketName = 'my-bucket', + fileName = 'test.txt', + metagenerationMatchPrecondition = null +) { + // [START storage_set_temporary_hold] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // The ID of your GCS file + // const fileName = 'your-file-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function setTemporaryHold() { + try { + // Optional: set a meta-generation-match precondition to avoid potential race + // conditions and data corruptions. The request to set metadata is aborted if the + // object's metageneration number does not match your precondition. + const options = { + ifMetagenerationMatch: metagenerationMatchPrecondition, + }; + + await storage.bucket(bucketName).file(fileName).setMetadata( + { + temporaryHold: true, + }, + options + ); + console.log(`Temporary hold was set for ${fileName}.`); + } catch (error) { + console.error( + 'Error executing set temporary hold:', + error.message || error + ); + } + } + + setTemporaryHold(); + // [END storage_set_temporary_hold] +} +main(...process.argv.slice(2)); diff --git a/storage/system-test/bucketLock.test.js b/storage/system-test/bucketLock.test.js new file mode 100644 index 0000000000..f5a1964fe0 --- /dev/null +++ b/storage/system-test/bucketLock.test.js @@ -0,0 +1,150 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const path = require('path'); +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const {before, after, it} = require('mocha'); +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const storage = new Storage(); +const cwd = path.join(__dirname, '..'); +const bucketName = `nodejs-storage-samples-${uuid.v4()}`; +const bucket = storage.bucket(bucketName); +const fileName = 'test.txt'; + +const uploadFilePath = path.join(cwd, 'resources', fileName); + +before(async () => { + await bucket.create(); + await bucket.upload(uploadFilePath); +}); + +after(async () => { + // The last test locks a retention policy. Wait for the retention + // period (5s) to pass before attempting to delete objects and the bucket. + // A small buffer is added to account for timing inaccuracies. + await new Promise(resolve => setTimeout(resolve, 6000)); + + try { + await bucket.deleteFiles({force: true}); + } catch (err) { + console.error('Failed to delete files during cleanup:', err); + } + try { + await bucket.delete(); + } catch (err) { + console.error('Failed to delete bucket during cleanup:', err); + } +}); + +it('should set a retention policy on a bucket', () => { + const retentionPeriod = 5; + const output = execSync( + `node setRetentionPolicy.js ${bucketName} ${retentionPeriod}` + ); + assert.match( + output, + new RegExp( + `Bucket ${bucketName} retention period set for ${retentionPeriod} seconds` + ) + ); +}); + +it('should get a retention policy on a bucket', () => { + const output = execSync(`node getRetentionPolicy.js ${bucketName}`); + assert.match(output, /A retention policy exists!/); +}); + +it('should enable default event-based hold on a bucket', () => { + const output = execSync(`node enableDefaultEventBasedHold.js ${bucketName}`); + assert.match( + output, + new RegExp(`Default event-based hold was enabled for ${bucketName}.`) + ); +}); + +it('should get default event-based hold on a bucket', () => { + const output = execSync(`node getDefaultEventBasedHold.js ${bucketName}`); + assert.match(output, /Default event-based hold: true./); +}); + +it('should disable default event-based hold on a bucket', () => { + const output = execSync(`node disableDefaultEventBasedHold.js ${bucketName}`); + assert.match( + output, + new RegExp(`Default event-based hold was disabled for ${bucketName}`) + ); +}); + +it('should set an event-based hold on a file', async () => { + const [metadata] = await bucket.file(fileName).getMetadata(); + const output = execSync( + `node setEventBasedHold.js ${bucketName} ${fileName} ${metadata.metageneration}` + ); + assert.match(output, new RegExp(`Event-based hold was set for ${fileName}`)); +}); + +it('should release an event-based hold on a file', async () => { + const [metadata] = await bucket.file(fileName).getMetadata(); + const output = execSync( + `node releaseEventBasedHold.js ${bucketName} ${fileName} ${metadata.metageneration}` + ); + assert.match( + output, + new RegExp(`Event-based hold was released for ${fileName}.`) + ); +}); + +it('should remove a retention policy on a bucket', () => { + const output = execSync(`node removeRetentionPolicy.js ${bucketName}`); + assert.match( + output, + new RegExp(`Removed bucket ${bucketName} retention policy.`) + ); +}); + +it('should set an temporary hold on a file', async () => { + const [metadata] = await bucket.file(fileName).getMetadata(); + const output = execSync( + `node setTemporaryHold.js ${bucketName} ${fileName} ${metadata.metageneration}` + ); + assert.match(output, new RegExp(`Temporary hold was set for ${fileName}.`)); +}); + +it('should release an temporary hold on a file', async () => { + const [metadata] = await bucket.file(fileName).getMetadata(); + const output = execSync( + `node releaseTemporaryHold.js ${bucketName} ${fileName} ${metadata.metageneration}` + ); + assert.match( + output, + new RegExp(`Temporary hold was released for ${fileName}.`) + ); +}); + +it('should lock a bucket with a retention policy', () => { + const retentionPeriod = 5; + execSync(`node setRetentionPolicy.js ${bucketName} ${retentionPeriod}`); + const output = execSync(`node lockRetentionPolicy.js ${bucketName}`); + assert.match( + output, + new RegExp(`Retention policy for ${bucketName} is now locked`) + ); +});