* Sets the autodelete flag of a disk to given value. * * @param {string} projectId - Project ID or project number of the Cloud project you want to use. * @param {string} zone - Name of the zone in which is the disk you want to modify. * @param {string} instanceName - Name of the instance the dis
(projectId, zone, instanceName, diskName, autoDelete)
| 22 | * @param {boolean} autoDelete - The new value of the autodelete flag. |
| 23 | */ |
| 24 | function main(projectId, zone, instanceName, diskName, autoDelete) { |
| 25 | // [START compute_disk_autodelete_change] |
| 26 | /** |
| 27 | * TODO(developer): Uncomment and replace these variables before running the sample. |
| 28 | */ |
| 29 | // const projectId = 'YOUR_PROJECT_ID'; |
| 30 | // const zone = 'europe-central2-b'; |
| 31 | // const instanceName = 'YOUR_INSTANCE_NAME'; |
| 32 | // const diskName = 'YOUR_DISK_NAME'; |
| 33 | // const autoDelete = true; |
| 34 | |
| 35 | const compute = require('@google-cloud/compute'); |
| 36 | |
| 37 | async function setDiskAutodelete() { |
| 38 | const instancesClient = new compute.InstancesClient(); |
| 39 | |
| 40 | const [instance] = await instancesClient.get({ |
| 41 | project: projectId, |
| 42 | zone, |
| 43 | instance: instanceName, |
| 44 | }); |
| 45 | |
| 46 | if (!instance.disks.some(disk => disk.deviceName === diskName)) { |
| 47 | throw new Error( |
| 48 | `Instance ${instanceName} doesn't have a disk named ${diskName} attached.` |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | const [response] = await instancesClient.setDiskAutoDelete({ |
| 53 | project: projectId, |
| 54 | zone, |
| 55 | instance: instanceName, |
| 56 | deviceName: diskName, |
| 57 | autoDelete, |
| 58 | }); |
| 59 | let operation = response.latestResponse; |
| 60 | const operationsClient = new compute.ZoneOperationsClient(); |
| 61 | |
| 62 | // Wait for the update instance operation to complete. |
| 63 | while (operation.status !== 'DONE') { |
| 64 | [operation] = await operationsClient.wait({ |
| 65 | operation: operation.name, |
| 66 | project: projectId, |
| 67 | zone: operation.zone.split('/').pop(), |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | console.log('Disk autoDelete field updated.'); |
| 72 | } |
| 73 | |
| 74 | setDiskAutodelete(); |
| 75 | // [END compute_disk_autodelete_change] |
| 76 | } |
| 77 | |
| 78 | process.on('unhandledRejection', err => { |
| 79 | console.error(err.message); |
no test coverage detected