Skip to content

Commit 6572ce9

Browse files
authored
feat: support autoclass v2.1 (#2325)
* feat: support autoclass v2.1 * reorder tests * update autoclass samples per canonical, update samples tests
1 parent 30473e7 commit 6572ce9

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

samples/getAutoclass.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ function main(bucketName = 'my-bucket') {
3737
async function getAutoclass() {
3838
const [metadata] = await storage.bucket(bucketName).getMetadata();
3939
console.log(
40-
`Autoclass enabled is set to ${metadata.autoclass.enabled} for ${metadata.name} at ${metadata.autoclass.toggleTime}.`
40+
`Autoclass is ${
41+
metadata.autoclass.enabled ? 'enabled' : 'disabled'
42+
} for ${metadata.name} at ${metadata.autoclass.toggleTime}.
43+
Autoclass terminal storage class is last updated to ${
44+
metadata.autoclass.terminalStorageClass
45+
} at ${metadata.autoclass.terminalStorageClassUpdateTime}.`
4146
);
4247
}
4348

samples/setAutoclass.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,41 @@
1414
* limitations under the License.
1515
*/
1616

17-
function main(bucketName = 'my-bucket', toggle = false) {
17+
function main(
18+
bucketName = 'my-bucket',
19+
toggle = true,
20+
terminalStorageClass = 'ARCHIVE'
21+
) {
1822
// [START storage_set_autoclass]
1923
/**
2024
* TODO(developer): Uncomment the following lines before running the sample.
2125
*/
2226
// The ID of your GCS bucket
2327
// const bucketName = 'your-unique-bucket-name';
2428

29+
// The terminal storage class to be set on your GCS bucket. Valid values are NEARLINE and ARCHIVE.
30+
// const terminalStorageClass = 'NEARLINE';
31+
2532
// Imports the Google Cloud client library
2633
const {Storage} = require('@google-cloud/storage');
2734

2835
// Creates a client
2936
const storage = new Storage();
3037

3138
async function setAutoclass() {
32-
// Disables Autoclass for a bucket.
33-
// Note: Only patch requests that disable autoclass are currently supported.
34-
// To enable autoclass, you must set it at bucket creation time.
39+
// Configure the Autoclass setting for a bucket.
40+
// terminalStorageClass field is optional and defaults to NEARLINE if not otherwise specified.
41+
// Valid terminalStorageClass values are NEARLINE and ARCHIVE.
3542
const [metadata] = await storage.bucket(bucketName).setMetadata({
3643
autoclass: {
3744
enabled: toggle,
45+
terminalStorageClass,
3846
},
3947
});
4048

41-
console.log(`Autoclass enabled is set to ${metadata.autoclass.enabled} for
42-
${metadata.name} at ${metadata.autoclass.toggleTime}.`);
49+
console.log(
50+
`Autoclass terminal storage class is ${metadata.autoclass.terminalStorageClass}.`
51+
);
4352
}
4453

4554
setAutoclass().catch(console.error);

samples/system-test/buckets.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,20 @@ it('should get bucket metadata', async () => {
7676
assert.include(output, bucketName);
7777
});
7878

79-
it('should disable autoclass', async () => {
79+
it('should set autoclass terminal storage class to ARCHIVE', async () => {
8080
await storage.createBucket(bucketNameAutoclass, {
8181
autoclass: {
8282
enabled: true,
83+
terminalStorageClass: 'NEARLINE',
8384
},
8485
});
86+
const output = execSync(
87+
`node setAutoclass.js ${bucketNameAutoclass} ${true} ARCHIVE`
88+
);
89+
assert.include(output, 'ARCHIVE');
90+
});
91+
92+
it('should disable autoclass', async () => {
8593
const output = execSync(
8694
`node setAutoclass.js ${bucketNameAutoclass} ${false}`
8795
);
@@ -90,7 +98,7 @@ it('should disable autoclass', async () => {
9098

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

96104
it('should set a buckets default KMS key', async () => {

samples/system-test/files.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ describe('file', () => {
530530
});
531531

532532
it('should copy file with old versions', async () => {
533-
console.log('bucket: ', bucketNameWithVersioning);
534533
const destFileName = 'file-two.txt';
535534
const [files] = await bucketWithVersioning.getFiles({versions: true});
536535
const generation = files[0].metadata.generation;

src/bucket.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ export interface BucketMetadata extends BaseMetadata {
291291
autoclass?: {
292292
enabled?: boolean;
293293
toggleTime?: string;
294+
terminalStorageClass?: string;
295+
terminalStorageClassUpdateTime?: string;
294296
};
295297
billing?: {
296298
requesterPays?: boolean;

src/storage.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export interface CustomPlacementConfig {
111111

112112
export interface AutoclassConfig {
113113
enabled?: boolean;
114+
terminalStorageClass?: 'NEARLINE' | 'ARCHIVE';
114115
}
115116

116117
export interface CreateBucketRequest {
@@ -853,6 +854,8 @@ export class Storage extends Service {
853854
* @property {boolean} [archive=false] Specify the storage class as Archive.
854855
* @property {object} [autoclass.enabled=false] Specify whether Autoclass is
855856
* enabled for the bucket.
857+
* @property {object} [autoclass.terminalStorageClass='NEARLINE'] The storage class that objects in an Autoclass bucket eventually transition to if
858+
* they are not read for a certain length of time. Valid values are NEARLINE and ARCHIVE.
856859
* @property {boolean} [coldline=false] Specify the storage class as Coldline.
857860
* @property {Cors[]} [cors=[]] Specify the CORS configuration to use.
858861
* @property {CustomPlacementConfig} [customPlacementConfig={}] Specify the bucket's regions for dual-region buckets.

system-test/storage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,11 +1061,13 @@ describe('storage', () => {
10611061
const [bucket] = await storage.createBucket(generateName(), {
10621062
autoclass: {
10631063
enabled: true,
1064+
terminalStorageClass: 'ARCHIVE',
10641065
},
10651066
});
10661067
let [metadata] = await bucket.getMetadata();
10671068
const timestampEnabled = metadata!.autoclass!.toggleTime;
10681069
assert.strictEqual(metadata!.autoclass!.enabled, true);
1070+
assert.strictEqual(metadata!.autoclass?.terminalStorageClass, 'ARCHIVE');
10691071
[metadata] = await bucket.setMetadata({
10701072
autoclass: {
10711073
enabled: false,

0 commit comments

Comments
 (0)