* 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_pause_anywhere_cache] |
| 26 | /** |
| 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' |
| 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 callPauseAnywhereCache() { |
| 46 | const anywhereCachePath = controlClient.anywhereCachePath( |
| 47 | '_', |
| 48 | bucketName, |
| 49 | cacheName |
| 50 | ); |
| 51 | |
| 52 | // Create the request |
| 53 | const request = { |
| 54 | name: anywhereCachePath, |
| 55 | }; |
| 56 | |
| 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 | } |
no test coverage detected