Skip to content

Commit 2304236

Browse files
authored
Merge branch 'main' into chore/datastore-cleanup
2 parents 732429d + 5cfc28f commit 2304236

11 files changed

Lines changed: 849 additions & 6 deletions
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on an Anywhere Cache
19+
* instance with the Google Cloud Storage API.
20+
*
21+
* For more information, see the documentation at https://cloud.google.com/storage/docs/anywhere-cache.
22+
*/
23+
24+
function main(bucketName, zoneName) {
25+
// [START storage_control_create_anywhere_cache]
26+
27+
/**
28+
* Creates an Anywhere Cache instance for a Cloud Storage bucket.
29+
* Anywhere Cache is a feature that provides an SSD-backed zonal read cache.
30+
* This can significantly improve read performance for frequently accessed data
31+
* by caching it in the same zone as your compute resources.
32+
*
33+
* @param {string} bucketName The name of the bucket to create the cache for.
34+
* Example: 'your-gcp-bucket-name'
35+
* @param {string} zoneName The zone where the cache will be created.
36+
* Example: 'us-central1-a'
37+
*/
38+
39+
// Imports the Control library
40+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
41+
42+
// Instantiates a client
43+
const controlClient = new StorageControlClient();
44+
45+
async function callCreateAnywhereCache() {
46+
const bucketPath = controlClient.bucketPath('_', bucketName);
47+
48+
// Create the request
49+
const request = {
50+
parent: bucketPath,
51+
anywhereCache: {
52+
zone: zoneName,
53+
ttl: {
54+
seconds: '10000s',
55+
}, // Optional. Default: '86400s'(1 day)
56+
admissionPolicy: 'admit-on-first-miss', // Optional. Default: 'admit-on-first-miss'
57+
},
58+
};
59+
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...`);
64+
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+
}
75+
}
76+
77+
// A custom function to check the operation's progress.
78+
async function checkCreateAnywhereCacheProgress(operationName) {
79+
let operation = {done: false};
80+
console.log('Starting manual polling for operation...');
81+
82+
// Poll the operation until it's done.
83+
while (!operation.done) {
84+
await new Promise(resolve => setTimeout(resolve, 180000)); // Wait for 3 minutes before the next check.
85+
const request = {
86+
name: operationName,
87+
};
88+
try {
89+
const [latestOperation] = await controlClient.getOperation(request);
90+
operation = latestOperation;
91+
} catch (err) {
92+
// Handle potential errors during polling.
93+
console.error('Error while polling:', err.message);
94+
break; // Exit the loop on error.
95+
}
96+
}
97+
98+
// Return the final result of the operation.
99+
if (operation.response) {
100+
// Decode the operation response into a usable Operation object
101+
const decodeOperation = new controlClient._gaxModule.Operation(
102+
operation,
103+
controlClient.descriptors.longrunning.createAnywhereCache,
104+
controlClient._gaxModule.createDefaultBackoffSettings()
105+
);
106+
// Return the decoded operation
107+
return decodeOperation;
108+
} else {
109+
// If there's no response, it indicates an issue, so throw an error
110+
throw new Error('Operation completed without a response.');
111+
}
112+
}
113+
114+
callCreateAnywhereCache();
115+
// [END storage_control_create_anywhere_cache]
116+
}
117+
118+
process.on('unhandledRejection', err => {
119+
console.error(err.message);
120+
process.exitCode = 1;
121+
});
122+
main(...process.argv.slice(2));
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on an Anywhere Cache
19+
* instance with the Google Cloud Storage API.
20+
*
21+
* For more information, see the documentation at https://cloud.google.com/storage/docs/anywhere-cache.
22+
*/
23+
24+
function main(bucketName, cacheName) {
25+
// [START storage_control_disable_anywhere_cache]
26+
/**
27+
* Disables an Anywhere Cache instance.
28+
*
29+
* Disabling a cache is the first step to permanently removing it. Once disabled,
30+
* the cache stops ingesting new data. After a grace period, the cache and its
31+
* contents are deleted. This is useful for decommissioning caches that are no
32+
* longer needed.
33+
*
34+
* @param {string} bucketName The name of the bucket where the cache resides.
35+
* Example: 'your-gcp-bucket-name'
36+
* @param {string} cacheName The unique identifier of the cache instance to disable.
37+
* Example: 'cacheName'
38+
*/
39+
40+
// Imports the Control library
41+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
42+
43+
// Instantiates a client
44+
const controlClient = new StorageControlClient();
45+
46+
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+
51+
const anywhereCachePath = controlClient.anywhereCachePath(
52+
'_',
53+
bucketName,
54+
cacheName
55+
);
56+
57+
// Create the request
58+
const request = {
59+
name: anywhereCachePath,
60+
};
61+
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+
}
91+
}
92+
93+
callDisableAnywhereCache();
94+
// [END storage_control_disable_anywhere_cache]
95+
}
96+
97+
process.on('unhandledRejection', err => {
98+
console.error(err.message);
99+
process.exitCode = 1;
100+
});
101+
main(...process.argv.slice(2));
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on an Anywhere Cache
19+
* instance with the Google Cloud Storage API.
20+
*
21+
* For more information, see the documentation at https://cloud.google.com/storage/docs/anywhere-cache.
22+
*/
23+
24+
function main(bucketName, cacheName) {
25+
// [START storage_control_get_anywhere_cache]
26+
/**
27+
* Retrieves details of a specific Anywhere Cache instance.
28+
*
29+
* This function is useful for checking the current state, configuration (like TTL),
30+
* and other metadata of an existing cache.
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'
36+
*/
37+
38+
// Imports the Control library
39+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
40+
41+
// Instantiates a client
42+
const controlClient = new StorageControlClient();
43+
44+
async function callGetAnywhereCache() {
45+
const anywhereCachePath = controlClient.anywhereCachePath(
46+
'_',
47+
bucketName,
48+
cacheName
49+
);
50+
51+
// Create the request
52+
const request = {
53+
name: anywhereCachePath,
54+
};
55+
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+
}
81+
}
82+
83+
callGetAnywhereCache();
84+
// [END storage_control_get_anywhere_cache]
85+
}
86+
87+
process.on('unhandledRejection', err => {
88+
console.error(err.message);
89+
process.exitCode = 1;
90+
});
91+
main(...process.argv.slice(2));
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
'use strict';
17+
18+
/**
19+
* This application demonstrates how to perform basic operations on an Anywhere Cache
20+
* instance with the Google Cloud Storage API.
21+
*
22+
* For more information, see the documentation at https://cloud.google.com/storage/docs/anywhere-cache.
23+
*/
24+
25+
function main(bucketName) {
26+
// [START storage_control_list_anywhere_caches]
27+
/**
28+
* Lists all Anywhere Cache instances for a Cloud Storage bucket.
29+
* This function helps you discover all active and pending caches associated with
30+
* a specific bucket, which is useful for auditing and management.
31+
*
32+
* @param {string} bucketName The name of the bucket to list caches for.
33+
* Example: 'your-gcp-bucket-name'
34+
*/
35+
36+
// Imports the Control library
37+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
38+
39+
// Instantiates a client
40+
const controlClient = new StorageControlClient();
41+
42+
async function callListAnywhereCaches() {
43+
const bucketPath = controlClient.bucketPath('_', bucketName);
44+
45+
// Create the request
46+
const request = {
47+
parent: bucketPath,
48+
};
49+
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;
71+
}
72+
}
73+
74+
callListAnywhereCaches();
75+
// [END storage_control_list_anywhere_caches]
76+
}
77+
78+
process.on('unhandledRejection', err => {
79+
console.error(err.message);
80+
process.exitCode = 1;
81+
});
82+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)