* Creates a snapshot of a disk. * * You need to pass `zone` or `region` parameter relevant to the disk you want to * snapshot, but not both. Pass `zone` parameter for zonal disks and `region` for * regional disks. * * @param {string} projectId - Project ID or project number of the Cloud projec
( projectId, diskName, snapshotName, zone, region, location, diskProjectId )
| 34 | */ |
| 35 | |
| 36 | function main( |
| 37 | projectId, |
| 38 | diskName, |
| 39 | snapshotName, |
| 40 | zone, |
| 41 | region, |
| 42 | location, |
| 43 | diskProjectId |
| 44 | ) { |
| 45 | // [START compute_snapshot_create] |
| 46 | /** |
| 47 | * TODO(developer): Uncomment and replace these variables before running the sample. |
| 48 | */ |
| 49 | // const projectId = 'YOUR_PROJECT_ID'; |
| 50 | // const diskName = 'YOUR_DISK_NAME'; |
| 51 | // const snapshotName = 'YOUR_SNAPSHOT_NAME'; |
| 52 | // const zone = 'europe-central2-b'; |
| 53 | // const region = ''; |
| 54 | // const location = 'europe-central2'; |
| 55 | // let diskProjectId = 'YOUR_DISK_PROJECT_ID'; |
| 56 | |
| 57 | const compute = require('@google-cloud/compute'); |
| 58 | |
| 59 | async function createSnapshot() { |
| 60 | const snapshotsClient = new compute.SnapshotsClient(); |
| 61 | |
| 62 | let disk; |
| 63 | |
| 64 | if (!zone && !region) { |
| 65 | throw new Error( |
| 66 | 'You need to specify `zone` or `region` for this function to work.' |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | if (zone && region) { |
| 71 | throw new Error("You can't set both `zone` and `region` parameters"); |
| 72 | } |
| 73 | |
| 74 | if (!diskProjectId) { |
| 75 | diskProjectId = projectId; |
| 76 | } |
| 77 | |
| 78 | if (zone) { |
| 79 | const disksClient = new compute.DisksClient(); |
| 80 | [disk] = await disksClient.get({ |
| 81 | project: diskProjectId, |
| 82 | zone, |
| 83 | disk: diskName, |
| 84 | }); |
| 85 | } else { |
| 86 | const regionDisksClient = new compute.RegionDisksClient(); |
| 87 | [disk] = await regionDisksClient.get({ |
| 88 | project: diskProjectId, |
| 89 | region, |
| 90 | disk: diskName, |
| 91 | }); |
| 92 | } |
| 93 |
no test coverage detected