Skip to content

Commit 572f7f1

Browse files
committed
feat(storage-control): Improve Anywhere Cache API samples
Improve Anywhere Cache API samples (Documentation & Error Handling) * Wraps all asynchronous Anywhere Cache API samples (Create, Get, List, Disable, Pause, Resume) in `try...catch` blocks for production readiness. * Adds specific gRPC error code checks (NOT_FOUND, FAILED_PRECONDITION) to provide better diagnostic feedback to users. * Clarifies documentation regarding the optional nature and default values of `ttl` and `admissionPolicy` for cache creation.
1 parent fd57da1 commit 572f7f1

File tree

7 files changed

+194
-50
lines changed

7 files changed

+194
-50
lines changed

storage-control/createAnywhereCache.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,28 @@ function main(bucketName, zoneName) {
5050
parent: bucketPath,
5151
anywhereCache: {
5252
zone: zoneName,
53-
ttl: '70000s',
54-
admissionPolicy: 'admit-on-first-miss',
53+
ttl: {
54+
seconds: '10000s',
55+
}, // Optional. Default: '86400s'(1 day)
56+
admissionPolicy: 'admit-on-first-miss', // Optional. Default: 'admit-on-first-miss'
5557
},
5658
};
5759

58-
// Run the request, which returns an Operation object
59-
const [operation] = await controlClient.createAnywhereCache(request);
60-
console.log(`Waiting for operation ${operation.name} to complete...`);
60+
try {
61+
// Run the request, which returns an Operation object
62+
const [operation] = await controlClient.createAnywhereCache(request);
63+
console.log(`Waiting for operation ${operation.name} to complete...`);
6164

62-
// Wait for the operation to complete and get the final resource
63-
const anywhereCache = await checkCreateAnywhereCacheProgress(
64-
operation.name
65-
);
66-
console.log(`Created anywhere cache: ${anywhereCache.result.name}.`);
65+
// Wait for the operation to complete and get the final resource
66+
const anywhereCache = await checkCreateAnywhereCacheProgress(
67+
operation.name
68+
);
69+
console.log(`Created anywhere cache: ${anywhereCache.result.name}.`);
70+
} catch (error) {
71+
// Handle any error that occurred during the creation or polling process.
72+
console.error('Failed to create Anywhere Cache:', error.message);
73+
throw error;
74+
}
6775
}
6876

6977
// A custom function to check the operation's progress.

storage-control/disableAnywhereCache.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ function main(bucketName, cacheName) {
4444
const controlClient = new StorageControlClient();
4545

4646
async function callDisableAnywhereCache() {
47+
// You have a one-hour grace period after disabling a cache to resume it and prevent its deletion.
48+
// If you don't resume the cache within that hour, it will be deleted, its data will be evicted,
49+
// and the cache will be permanently removed from the bucket.
50+
4751
const anywhereCachePath = controlClient.anywhereCachePath(
4852
'_',
4953
bucketName,
@@ -55,6 +59,35 @@ function main(bucketName, cacheName) {
5559
name: anywhereCachePath,
5660
};
5761

62+
try {
63+
// Run request. This initiates the disablement process.
64+
const [response] = await controlClient.disableAnywhereCache(request);
65+
66+
console.log(
67+
`Successfully initiated disablement for Anywhere Cache '${cacheName}'.`
68+
);
69+
console.log(` Current State: ${response.state}`);
70+
console.log(` Resource Name: ${response.name}`);
71+
} catch (error) {
72+
// Catch and handle potential API errors.
73+
console.error(
74+
`Error disabling Anywhere Cache '${cacheName}': ${error.message}`
75+
);
76+
77+
if (error.code === 5) {
78+
// NOT_FOUND (gRPC code 5) error can occur if the bucket or cache does not exist.
79+
console.error(
80+
`Please ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
81+
);
82+
} else if (error.code === 9) {
83+
// FAILED_PRECONDITION (gRPC code 9) can occur if the cache is already being disabled
84+
// or is not in a RUNNING state that allows the disable operation.
85+
console.error(
86+
`Cache '${cacheName}' may not be in a state that allows disabling (e.g., must be RUNNING).`
87+
);
88+
}
89+
throw error;
90+
}
5891
// Run request
5992
const [response] = await controlClient.disableAnywhereCache(request);
6093
console.log(`Disabled anywhere cache: ${response.name}.`);

storage-control/getAnywhereCache.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,31 @@ function main(bucketName, cacheName) {
5353
name: anywhereCachePath,
5454
};
5555

56-
// Run request
57-
const [response] = await controlClient.getAnywhereCache(request);
58-
console.log(`Got anywhere cache: ${response.name}.`);
59-
console.log(`Anywhere Cache details for '${cacheName}':`);
60-
console.log(` ID: ${response.id}`);
61-
console.log(` Zone: ${response.zone}`);
62-
console.log(` State: ${response.state}`);
63-
console.log(` TTL: ${response.ttl.seconds}s`);
64-
console.log(` Admission Policy: ${response.admissionPolicy}`);
65-
console.log(
66-
` Create Time: ${new Date(response.createTime.seconds * 1000).toISOString()}`
67-
);
56+
try {
57+
// Run request
58+
const [response] = await controlClient.getAnywhereCache(request);
59+
console.log(`Anywhere Cache details for '${cacheName}':`);
60+
console.log(` Name: ${response.name}`);
61+
console.log(` Zone: ${response.zone}`);
62+
console.log(` State: ${response.state}`);
63+
console.log(` TTL: ${response.ttl.seconds}s`);
64+
console.log(` Admission Policy: ${response.admissionPolicy}`);
65+
console.log(
66+
` Create Time: ${new Date(response.createTime.seconds * 1000).toISOString()}`
67+
);
68+
} catch (error) {
69+
// Handle errors (e.g., cache not found, permission denied).
70+
console.error(
71+
`Error retrieving Anywhere Cache '${cacheName}': ${error.message}`
72+
);
73+
74+
if (error.code === 5) {
75+
console.error(
76+
`Ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
77+
);
78+
}
79+
throw error;
80+
}
6881
}
6982

7083
callGetAnywhereCache();

storage-control/listAnywhereCaches.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,27 @@ function main(bucketName) {
4747
parent: bucketPath,
4848
};
4949

50-
// Run request
51-
const [response] = await controlClient.listAnywhereCaches(request);
52-
for (const anywhereCache of response) {
53-
console.log(anywhereCache.name);
50+
try {
51+
// Run request. The response is an array where the first element is the list of caches.
52+
const [response] = await controlClient.listAnywhereCaches(request);
53+
54+
if (response && response.length > 0) {
55+
console.log(
56+
`Found ${response.length} Anywhere Caches for bucket: ${bucketName}`
57+
);
58+
for (const anywhereCache of response) {
59+
console.log(anywhereCache.name);
60+
}
61+
} else {
62+
// Case: Successful but empty list (No Anywhere Caches found)
63+
console.log(`No Anywhere Caches found for bucket: ${bucketName}.`);
64+
}
65+
} catch (error) {
66+
console.error(
67+
`Error listing Anywhere Caches for bucket ${bucketName}:`,
68+
error.message
69+
);
70+
throw error;
5471
}
5572
}
5673

storage-control/pauseAnywhereCache.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424
function main(bucketName, cacheName) {
2525
// [START storage_control_pause_anywhere_cache]
2626
/**
27-
* TODO(developer): Uncomment these variables before running the sample.
27+
* Pauses an Anywhere Cache instance.
28+
*
29+
* This synchronous function stops the ingestion of new data for a cache that's in a RUNNING state.
30+
* While PAUSED, you can still read existing data (which resets the TTL), but no new data is ingested.
31+
* The cache can be returned to the RUNNING state by calling the resume function.
32+
*
33+
* @param {string} bucketName The name of the bucket where the cache resides.
34+
* Example: 'your-gcp-bucket-name'
35+
* @param {string} cacheName The unique identifier of the cache instance.
36+
* Example: 'my-anywhere-cache-id'
2837
*/
2938

30-
// The name of your GCS bucket
31-
// const bucketName = 'bucketName';
32-
33-
// The name of the cache to be paused
34-
// const cacheName = 'cacheName';
35-
3639
// Imports the Control library
3740
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
3841

@@ -51,9 +54,31 @@ function main(bucketName, cacheName) {
5154
name: anywhereCachePath,
5255
};
5356

54-
// Run request
55-
const [response] = await controlClient.pauseAnywhereCache(request);
56-
console.log(`Paused anywhere cache: ${response.name}.`);
57+
try {
58+
// Run request
59+
const [response] = await controlClient.pauseAnywhereCache(request);
60+
61+
console.log(`Successfully paused anywhere cache: ${response.name}.`);
62+
console.log(` Current State: ${response.state}`);
63+
} catch (error) {
64+
// Catch and handle potential API errors.
65+
console.error(
66+
`Error pausing Anywhere Cache '${cacheName}': ${error.message}`
67+
);
68+
69+
if (error.code === 5) {
70+
// NOT_FOUND (gRPC code 5)
71+
console.error(
72+
`Please ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
73+
);
74+
} else if (error.code === 9) {
75+
// FAILED_PRECONDITION (gRPC code 9)
76+
console.error(
77+
`Cache '${cacheName}' may not be in a state that allows pausing (e.g., must be RUNNING).`
78+
);
79+
}
80+
throw error;
81+
}
5782
}
5883

5984
callPauseAnywhereCache();

storage-control/resumeAnywhereCache.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424
function main(bucketName, cacheName) {
2525
// [START storage_control_resume_anywhere_cache]
2626
/**
27-
* TODO(developer): Uncomment these variables before running the sample.
27+
* Resumes a disabled Anywhere Cache instance.
28+
*
29+
* This action reverts a cache from a PAUSED state or a DISABLED state back to RUNNING,
30+
* provided it is done within the 1-hour grace period before the cache is permanently deleted.
31+
*
32+
* @param {string} bucketName The name of the bucket where the cache resides.
33+
* Example: 'your-gcp-bucket-name'
34+
* @param {string} cacheName The unique identifier of the cache instance.
35+
* Example: 'my-anywhere-cache-id'
2836
*/
2937

30-
// The name of your GCS bucket
31-
// const bucketName = 'bucketName';
32-
33-
// The name of the cache to be resumed
34-
// const cacheName = 'cacheName';
35-
3638
// Imports the Control library
3739
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
3840

@@ -51,9 +53,31 @@ function main(bucketName, cacheName) {
5153
name: anywhereCachePath,
5254
};
5355

54-
// Run request
55-
const [response] = await controlClient.resumeAnywhereCache(request);
56-
console.log(`Resumed anywhere cache: ${response.name}.`);
56+
try {
57+
// Run request
58+
const [response] = await controlClient.resumeAnywhereCache(request);
59+
60+
console.log(`Successfully resumed anywhere cache: ${response.name}.`);
61+
console.log(` Current State: ${response.state}`);
62+
} catch (error) {
63+
// Catch and handle potential API errors.
64+
console.error(
65+
`Error resuming Anywhere Cache '${cacheName}': ${error.message}`
66+
);
67+
68+
if (error.code === 5) {
69+
// NOT_FOUND (gRPC code 5)
70+
console.error(
71+
`Please ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
72+
);
73+
} else if (error.code === 9) {
74+
// FAILED_PRECONDITION (gRPC code 9)
75+
console.error(
76+
`Cache '${cacheName}' may not be in a state that allows resuming (e.g., already RUNNING or past the 1-hour deletion grace period).`
77+
);
78+
}
79+
throw error;
80+
}
5781
}
5882

5983
callResumeAnywhereCache();

storage-control/updateAnywhereCache.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,35 @@ function main(bucketName, cacheName, admissionPolicy) {
5858
},
5959
};
6060

61-
// Run request
62-
const [operation] = await controlClient.updateAnywhereCache(request);
63-
const [response] = await operation.promise();
61+
try {
62+
// Run request
63+
const [operation] = await controlClient.updateAnywhereCache(request);
64+
console.log(
65+
`Waiting for update operation ${operation.name} to complete...`
66+
);
6467

65-
console.log(`Updated anywhere cache: ${response.name}.`);
68+
const [response] = await operation.promise();
69+
70+
console.log(`Updated anywhere cache: ${response.name}.`);
71+
} catch (error) {
72+
// Handle errors during the initial request or during the LRO polling.
73+
console.error(
74+
`Error updating Anywhere Cache '${cacheName}': ${error.message}`
75+
);
76+
77+
if (error.code === 5) {
78+
// NOT_FOUND (gRPC code 5)
79+
console.error(
80+
`Ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
81+
);
82+
} else if (error.code === 3) {
83+
// INVALID_ARGUMENT (gRPC code 3)
84+
console.error(
85+
`Ensure '${admissionPolicy}' is a valid Admission Policy.`
86+
);
87+
}
88+
throw error;
89+
}
6690
}
6791

6892
callUpdateAnywhereCache();

0 commit comments

Comments
 (0)