MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

compute/disks/createDiskFromSnapshot.js:29–74  ·  view source on GitHub ↗

* Creates a new disk in a project in given zone. * * @param {string} projectId - Project ID or project number of the Cloud project you want to use. * @param {string} zone - Name of the zone to create the instance in. For example: "us-west3-b". * @param {string} diskName - Name of the disk you wa

(projectId, zone, diskName, diskType, diskSizeGb, snapshotLink)

Source from the content-addressed store, hash-verified

27 */
28
29function main(projectId, zone, diskName, diskType, diskSizeGb, snapshotLink) {
30 // [START compute_disk_create_from_snapshot]
31 /**
32 * TODO(developer): Uncomment and replace these variables before running the sample.
33 */
34 // const projectId = 'YOUR_PROJECT_ID';
35 // const zone = 'europe-central2-b';
36 // const diskName = 'YOUR_DISK_NAME';
37 // const diskType = 'zones/us-west3-b/diskTypes/pd-ssd';
38 // const diskSizeGb = 10;
39 // const snapshotLink = 'projects/project_name/global/snapshots/snapshot_name';
40
41 const compute = require('@google-cloud/compute');
42
43 async function createDiskFromSnapshot() {
44 const disksClient = new compute.DisksClient();
45
46 const [response] = await disksClient.insert({
47 project: projectId,
48 zone,
49 diskResource: {
50 sizeGb: diskSizeGb,
51 name: diskName,
52 zone,
53 type: diskType,
54 sourceSnapshot: snapshotLink,
55 },
56 });
57 let operation = response.latestResponse;
58 const operationsClient = new compute.ZoneOperationsClient();
59
60 // Wait for the create disk operation to complete.
61 while (operation.status !== 'DONE') {
62 [operation] = await operationsClient.wait({
63 operation: operation.name,
64 project: projectId,
65 zone: operation.zone.split('/').pop(),
66 });
67 }
68
69 console.log('Disk created.');
70 }
71
72 createDiskFromSnapshot();
73 // [END compute_disk_create_from_snapshot]
74}
75
76process.on('unhandledRejection', err => {
77 console.error(err.message);

Callers 1

Calls 1

createDiskFromSnapshotFunction · 0.85

Tested by

no test coverage detected