* This application demonstrates how to perform basic operations on an Anywhere Cache * instance with the Google Cloud Storage API. * * For more information, see the documentation at https://cloud.google.com/storage/docs/anywhere-cache.
(bucketName, cacheName)
| 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 | ); |
no test coverage detected