Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support autoclass #2078

Merged
merged 7 commits into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -147,6 +147,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
| Generate V4 Read Signed Url | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/generateV4ReadSignedUrl.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/generateV4ReadSignedUrl.js,samples/README.md) |
| Generate V4 Signed Policy | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/generateV4SignedPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/generateV4SignedPolicy.js,samples/README.md) |
| Generate V4 Upload Signed Url | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/generateV4UploadSignedUrl.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/generateV4UploadSignedUrl.js,samples/README.md) |
| Get Autoclass | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getAutoclass.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/getAutoclass.js,samples/README.md) |
| Get Default Event Based Hold | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getDefaultEventBasedHold.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/getDefaultEventBasedHold.js,samples/README.md) |
| Get Metadata | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getMetadata.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/getMetadata.js,samples/README.md) |
| Get Metadata Notifications | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getMetadataNotifications.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/getMetadataNotifications.js,samples/README.md) |
Expand Down Expand Up @@ -190,6 +191,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
| Remove Retention Policy | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/removeRetentionPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/removeRetentionPolicy.js,samples/README.md) |
| Rename File | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/renameFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/renameFile.js,samples/README.md) |
| Rotate Encryption Key | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/rotateEncryptionKey.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/rotateEncryptionKey.js,samples/README.md) |
| Set Autoclass | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setAutoclass.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/setAutoclass.js,samples/README.md) |
| Set Client Endpoint | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setClientEndpoint.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/setClientEndpoint.js,samples/README.md) |
| Set Event Based Hold | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setEventBasedHold.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/setEventBasedHold.js,samples/README.md) |
| Set Public Access Prevention Enforced | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setPublicAccessPreventionEnforced.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/setPublicAccessPreventionEnforced.js,samples/README.md) |
Expand Down
36 changes: 36 additions & 0 deletions samples/README.md
Expand Up @@ -66,6 +66,7 @@ objects to users via direct download.
* [Generate V4 Read Signed Url](#generate-v4-read-signed-url)
* [Generate V4 Signed Policy](#generate-v4-signed-policy)
* [Generate V4 Upload Signed Url](#generate-v4-upload-signed-url)
* [Get Autoclass](#get-autoclass)
* [Get Default Event Based Hold](#get-default-event-based-hold)
* [Get Metadata](#get-metadata)
* [Get Metadata Notifications](#get-metadata-notifications)
Expand Down Expand Up @@ -109,6 +110,7 @@ objects to users via direct download.
* [Remove Retention Policy](#remove-retention-policy)
* [Rename File](#rename-file)
* [Rotate Encryption Key](#rotate-encryption-key)
* [Set Autoclass](#set-autoclass)
* [Set Client Endpoint](#set-client-endpoint)
* [Set Event Based Hold](#set-event-based-hold)
* [Set Public Access Prevention Enforced](#set-public-access-prevention-enforced)
Expand Down Expand Up @@ -991,6 +993,23 @@ __Usage:__



### Get Autoclass

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getAutoclass.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/getAutoclass.js,samples/README.md)

__Usage:__


`node samples/getAutoclass.js`


-----




### Get Default Event Based Hold

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getDefaultEventBasedHold.js).
Expand Down Expand Up @@ -1746,6 +1765,23 @@ __Usage:__



### Set Autoclass

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setAutoclass.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/setAutoclass.js,samples/README.md)

__Usage:__


`node samples/setAutoclass.js`


-----




### Set Client Endpoint

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setClientEndpoint.js).
Expand Down
47 changes: 47 additions & 0 deletions samples/getAutoclass.js
@@ -0,0 +1,47 @@
// Copyright 2022 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.

/**
* This application demonstrates how to perform basic operations on files with
* the Google Cloud Storage API.
*
* For more information, see the README.md under /storage and the documentation
* at https://cloud.google.com/storage/docs.
*/

function main(bucketName = 'my-bucket') {
// [START storage_get_autoclass]
/**
* 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 getAutoclass() {
const [metadata] = await storage.bucket(bucketName).getMetadata();
console.log(
`Autoclass enabled is set to ${metadata.autoclass.enabled} for ${metadata.name} at ${metadata.autoclass.toggleTime}.`
);
}

getAutoclass().catch(console.error);
// [END storage_get_autoclass]
}
main(...process.argv.slice(2));
49 changes: 49 additions & 0 deletions samples/setAutoclass.js
@@ -0,0 +1,49 @@
/**
* Copyright 2022 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.
*/

function main(bucketName = 'my-bucket', toggle = false) {
// [START storage_set_autoclass]
/**
* 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 setAutoclass() {
// Disables Autoclass for a bucket.
// Note: Only patch requests that disable autoclass are currently supported.
// To enable autoclass, you must set it at bucket creation time.
const [metadata] = await storage.bucket(bucketName).setMetadata({
autoclass: {
enabled: toggle,
},
});

console.log(`Autoclass enabled is set to ${metadata.autoclass.enabled} for
${metadata.name} at ${metadata.autoclass.toggleTime}.`);
}

setAutoclass().catch(console.error);
// [END storage_set_autoclass]
}

main(...process.argv.slice(2));
18 changes: 18 additions & 0 deletions samples/system-test/buckets.test.js
Expand Up @@ -28,6 +28,7 @@ const bucketName = `${samplesTestBucketPrefix}-a`;
const bucketNameDualRegion = `${samplesTestBucketPrefix}-b`;
const bucketNameDualRegionTurbo = `${samplesTestBucketPrefix}-c`;
const bucketNameWithClassAndLocation = `${samplesTestBucketPrefix}-d`;
const bucketNameAutoclass = `${samplesTestBucketPrefix}-e`;
const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA;
const bucket = storage.bucket(bucketName);
const bucketWithClassAndLocation = storage.bucket(
Expand Down Expand Up @@ -75,6 +76,23 @@ it('should get bucket metadata', async () => {
assert.include(output, bucketName);
});

it('should disable autoclass', async () => {
await storage.createBucket(bucketNameAutoclass, {
autoclass: {
enabled: true,
},
});
const output = execSync(
`node setAutoclass.js ${bucketNameAutoclass} ${false}`
);
assert.include(output, 'Autoclass');
});

it('should get autoclass', async () => {
const output = execSync(`node getAutoclass.js ${bucketNameAutoclass}`);
assert.include(output, 'Autoclass enabled is set to false');
});

it('should set a buckets default KMS key', async () => {
const output = execSync(
`node enableDefaultKMSKey.js ${bucketName} ${defaultKmsKeyName}`
Expand Down
7 changes: 7 additions & 0 deletions src/storage.ts
Expand Up @@ -107,8 +107,13 @@ export interface CustomPlacementConfig {
dataLocations?: string[];
}

export interface AutoclassConfig {
enabled?: boolean;
}

export interface CreateBucketRequest {
archive?: boolean;
autoclass?: AutoclassConfig;
coldline?: boolean;
cors?: Cors[];
customPlacementConfig?: CustomPlacementConfig;
Expand Down Expand Up @@ -810,6 +815,8 @@ export class Storage extends Service {
*
* @typedef {object} CreateBucketRequest
* @property {boolean} [archive=false] Specify the storage class as Archive.
* @property {object} [autoclass.enabled=false] Specify whether Autoclass is
* enabled for the bucket.
* @property {boolean} [coldline=false] Specify the storage class as Coldline.
* @property {Cors[]} [cors=[]] Specify the CORS configuration to use.
* @property {CustomPlacementConfig} [customPlacementConfig={}] Specify the bucket's regions for dual-region buckets.
Expand Down
19 changes: 19 additions & 0 deletions system-test/storage.ts
Expand Up @@ -1054,6 +1054,25 @@ describe('storage', () => {
assert.strictEqual(metadata.storageClass, 'COLDLINE');
});

it('should allow enabling & disabling autoclass', async () => {
const [bucket] = await storage.createBucket(generateName(), {
autoclass: {
enabled: true,
},
});
let [metadata] = await bucket.getMetadata();
const timestampEnabled = metadata.autoclass.toggleTime;
assert.strictEqual(metadata.autoclass.enabled, true);
[metadata] = await bucket.setMetadata({
autoclass: {
enabled: false,
},
});
const timestampDisabled = metadata.autoclass.toggleTime;
assert.strictEqual(metadata.autoclass.enabled, false);
assert.strictEqual(timestampDisabled > timestampEnabled, true);
});

describe('locationType', () => {
const types = ['multi-region', 'region', 'dual-region'];

Expand Down