Skip to content

Commit

Permalink
feat: support autoclass v2.1 (#2325)
Browse files Browse the repository at this point in the history
* feat: support autoclass v2.1

* reorder tests

* update autoclass samples per canonical, update samples tests
  • Loading branch information
ddelgrosso1 authored Oct 30, 2023
1 parent 30473e7 commit 6572ce9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
7 changes: 6 additions & 1 deletion samples/getAutoclass.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ function main(bucketName = 'my-bucket') {
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}.`
`Autoclass is ${
metadata.autoclass.enabled ? 'enabled' : 'disabled'
} for ${metadata.name} at ${metadata.autoclass.toggleTime}.
Autoclass terminal storage class is last updated to ${
metadata.autoclass.terminalStorageClass
} at ${metadata.autoclass.terminalStorageClassUpdateTime}.`
);
}

Expand Down
21 changes: 15 additions & 6 deletions samples/setAutoclass.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,41 @@
* limitations under the License.
*/

function main(bucketName = 'my-bucket', toggle = false) {
function main(
bucketName = 'my-bucket',
toggle = true,
terminalStorageClass = 'ARCHIVE'
) {
// [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';

// The terminal storage class to be set on your GCS bucket. Valid values are NEARLINE and ARCHIVE.
// const terminalStorageClass = 'NEARLINE';

// 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.
// Configure the Autoclass setting for a bucket.
// terminalStorageClass field is optional and defaults to NEARLINE if not otherwise specified.
// Valid terminalStorageClass values are NEARLINE and ARCHIVE.
const [metadata] = await storage.bucket(bucketName).setMetadata({
autoclass: {
enabled: toggle,
terminalStorageClass,
},
});

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

setAutoclass().catch(console.error);
Expand Down
12 changes: 10 additions & 2 deletions samples/system-test/buckets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,20 @@ it('should get bucket metadata', async () => {
assert.include(output, bucketName);
});

it('should disable autoclass', async () => {
it('should set autoclass terminal storage class to ARCHIVE', async () => {
await storage.createBucket(bucketNameAutoclass, {
autoclass: {
enabled: true,
terminalStorageClass: 'NEARLINE',
},
});
const output = execSync(
`node setAutoclass.js ${bucketNameAutoclass} ${true} ARCHIVE`
);
assert.include(output, 'ARCHIVE');
});

it('should disable autoclass', async () => {
const output = execSync(
`node setAutoclass.js ${bucketNameAutoclass} ${false}`
);
Expand All @@ -90,7 +98,7 @@ it('should disable autoclass', async () => {

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

it('should set a buckets default KMS key', async () => {
Expand Down
1 change: 0 additions & 1 deletion samples/system-test/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ describe('file', () => {
});

it('should copy file with old versions', async () => {
console.log('bucket: ', bucketNameWithVersioning);
const destFileName = 'file-two.txt';
const [files] = await bucketWithVersioning.getFiles({versions: true});
const generation = files[0].metadata.generation;
Expand Down
2 changes: 2 additions & 0 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ export interface BucketMetadata extends BaseMetadata {
autoclass?: {
enabled?: boolean;
toggleTime?: string;
terminalStorageClass?: string;
terminalStorageClassUpdateTime?: string;
};
billing?: {
requesterPays?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export interface CustomPlacementConfig {

export interface AutoclassConfig {
enabled?: boolean;
terminalStorageClass?: 'NEARLINE' | 'ARCHIVE';
}

export interface CreateBucketRequest {
Expand Down Expand Up @@ -853,6 +854,8 @@ export class Storage extends Service {
* @property {boolean} [archive=false] Specify the storage class as Archive.
* @property {object} [autoclass.enabled=false] Specify whether Autoclass is
* enabled for the bucket.
* @property {object} [autoclass.terminalStorageClass='NEARLINE'] The storage class that objects in an Autoclass bucket eventually transition to if
* they are not read for a certain length of time. Valid values are NEARLINE and ARCHIVE.
* @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
2 changes: 2 additions & 0 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,11 +1061,13 @@ describe('storage', () => {
const [bucket] = await storage.createBucket(generateName(), {
autoclass: {
enabled: true,
terminalStorageClass: 'ARCHIVE',
},
});
let [metadata] = await bucket.getMetadata();
const timestampEnabled = metadata!.autoclass!.toggleTime;
assert.strictEqual(metadata!.autoclass!.enabled, true);
assert.strictEqual(metadata!.autoclass?.terminalStorageClass, 'ARCHIVE');
[metadata] = await bucket.setMetadata({
autoclass: {
enabled: false,
Expand Down

0 comments on commit 6572ce9

Please sign in to comment.