|
| 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 | +function main(bucketName, cacheName, zoneName) { |
| 18 | + // [START storage_control_create_anywhere_cache] |
| 19 | + /** |
| 20 | + * TODO(developer): Uncomment these variables before running the sample. |
| 21 | + */ |
| 22 | + |
| 23 | + // The name of your GCS bucket |
| 24 | + // const bucketName = 'bucketName'; |
| 25 | + |
| 26 | + // The name of the cache to be created |
| 27 | + // const cacheName = 'cacheName'; |
| 28 | + |
| 29 | + // The zone that the cache instance will run in. |
| 30 | + // const zoneName = 'zoneName'; |
| 31 | + |
| 32 | + // Imports the Control library |
| 33 | + const {StorageControlClient} = require('@google-cloud/storage-control').v2; |
| 34 | + |
| 35 | + // Instantiates a client |
| 36 | + const controlClient = new StorageControlClient(); |
| 37 | + |
| 38 | + async function callCreateAnywhereCache() { |
| 39 | + const bucketPath = controlClient.bucketPath('_', bucketName); |
| 40 | + |
| 41 | + // Create the request |
| 42 | + const request = { |
| 43 | + parent: bucketPath, |
| 44 | + anywhereCache: { |
| 45 | + name: cacheName, |
| 46 | + zone: zoneName, |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + // Run the request, which returns an Operation object |
| 51 | + const [operation] = await controlClient.createAnywhereCache(request); |
| 52 | + console.log(`Waiting for operation ${operation.name} to complete...`); |
| 53 | + |
| 54 | + // Wait for the operation to complete and get the final resource |
| 55 | + const anywhereCache = await checkCreateAnywhereCacheProgress( |
| 56 | + operation.name |
| 57 | + ); |
| 58 | + console.log(`Created anywhere cache: ${anywhereCache.result.name}.`); |
| 59 | + } |
| 60 | + |
| 61 | + // A custom function to check the operation's progress. |
| 62 | + async function checkCreateAnywhereCacheProgress(operationName) { |
| 63 | + let operation = {done: false}; |
| 64 | + console.log('Starting manual polling for operation...'); |
| 65 | + |
| 66 | + // Poll the operation until it's done. |
| 67 | + while (!operation.done) { |
| 68 | + await new Promise(resolve => setTimeout(resolve, 180000)); // Wait for 3 minutes before the next check. |
| 69 | + const request = { |
| 70 | + name: operationName, |
| 71 | + }; |
| 72 | + try { |
| 73 | + const [latestOperation] = await controlClient.getOperation(request); |
| 74 | + operation = latestOperation; |
| 75 | + } catch (err) { |
| 76 | + // Handle potential errors during polling. |
| 77 | + console.error('Error while polling:', err.message); |
| 78 | + break; // Exit the loop on error. |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Return the final result of the operation. |
| 83 | + if (operation.response) { |
| 84 | + // Decode the operation response into a usable Operation object |
| 85 | + const decodeOperation = new controlClient._gaxModule.Operation( |
| 86 | + operation, |
| 87 | + controlClient.descriptors.longrunning.createAnywhereCache, |
| 88 | + controlClient._gaxModule.createDefaultBackoffSettings() |
| 89 | + ); |
| 90 | + // Return the decoded operation |
| 91 | + return decodeOperation; |
| 92 | + } else { |
| 93 | + // If there's no response, it indicates an issue, so throw an error |
| 94 | + throw new Error('Operation completed without a response.'); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + callCreateAnywhereCache(); |
| 99 | + // [END storage_control_create_anywhere_cache] |
| 100 | +} |
| 101 | + |
| 102 | +process.on('unhandledRejection', err => { |
| 103 | + console.error(err.message); |
| 104 | + process.exitCode = 1; |
| 105 | +}); |
| 106 | +main(...process.argv.slice(2)); |
0 commit comments