Skip to content

Commit 91769bf

Browse files
authored
chore(samples): add moving instances between zones samples (#747)
1 parent c944ecc commit 91769bf

11 files changed

+1017
-0
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Creates a new disk in a project in given zone.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you want to use.
19+
* @param {string} zone - Name of the zone to create the instance in. For example: "us-west3-b".
20+
* @param {string} diskName - Name of the disk you want to create.
21+
* @param {string} diskType - The type of disk you want to create. This value uses the following format:
22+
* "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
23+
* For example: "zones/us-west3-b/diskTypes/pd-ssd".
24+
* @param {int} diskSizeGb - Size of the new disk in gigabytes.
25+
* @param {string} snapshotLink - A link to the snapshot you want to use as a source for the new disk.
26+
* This value uses the following format: "projects/{project_name}/global/snapshots/{snapshot_name}"
27+
*/
28+
29+
function 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+
76+
process.on('unhandledRejection', err => {
77+
console.error(err.message);
78+
process.exitCode = 1;
79+
});
80+
81+
const args = process.argv.slice(2);
82+
args[4] = parseInt(args[4]);
83+
main(...args);

compute/disks/deleteDisk.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Deletes a disk from a project.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you want to use.
19+
* @param {string} zone - Name of the zone in which is the disk you want to delete.
20+
* @param {string} diskName - Name of the disk you want to delete.
21+
*/
22+
function main(projectId, zone, diskName) {
23+
// [START compute_disk_delete]
24+
/**
25+
* TODO(developer): Uncomment and replace these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const zone = 'europe-central2-b';
29+
// const diskName = 'YOUR_DISK_NAME';
30+
31+
const compute = require('@google-cloud/compute');
32+
33+
async function deleteDisk() {
34+
const disksClient = new compute.DisksClient();
35+
36+
const [response] = await disksClient.delete({
37+
project: projectId,
38+
zone,
39+
disk: diskName,
40+
});
41+
let operation = response.latestResponse;
42+
const operationsClient = new compute.ZoneOperationsClient();
43+
44+
// Wait for the create disk operation to complete.
45+
while (operation.status !== 'DONE') {
46+
[operation] = await operationsClient.wait({
47+
operation: operation.name,
48+
project: projectId,
49+
zone: operation.zone.split('/').pop(),
50+
});
51+
}
52+
53+
console.log('Disk deleted.');
54+
}
55+
56+
deleteDisk();
57+
// [END compute_disk_delete]
58+
}
59+
60+
process.on('unhandledRejection', err => {
61+
console.error(err.message);
62+
process.exitCode = 1;
63+
});
64+
65+
main(...process.argv.slice(2));

compute/disks/setDiskAutodelete.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Sets the autodelete flag of a disk to given value.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you want to use.
19+
* @param {string} zone - Name of the zone in which is the disk you want to modify.
20+
* @param {string} instanceName - Name of the instance the disk is attached to.
21+
* @param {string} diskName - The name of the disk which flag you want to modify.
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);
80+
process.exitCode = 1;
81+
});
82+
83+
const args = process.argv.slice(2);
84+
args[4] = args[4] === 'true';
85+
main(...args);

compute/getInstance.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Prints information about a VM instance in the given zone in the specified project.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you want to use.
19+
* @param {string} zone - Name of the zone you want to use. For example: 'us-west3-b'.
20+
* @param {string} instanceName - Name of the VM instance you want to query.
21+
*/
22+
function main(projectId, zone, instanceName) {
23+
// [START compute_instances_get]
24+
/**
25+
* TODO(developer): Uncomment and replace these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const zone = 'europe-central2-b'
29+
// const instanceName = 'YOUR_INSTANCE_NAME'
30+
31+
const compute = require('@google-cloud/compute');
32+
33+
async function getInstance() {
34+
const instancesClient = new compute.InstancesClient();
35+
36+
const [instance] = await instancesClient.get({
37+
project: projectId,
38+
zone,
39+
instance: instanceName,
40+
});
41+
42+
console.log(
43+
`Instance ${instanceName} data:\n${JSON.stringify(instance, null, 4)}`
44+
);
45+
}
46+
getInstance();
47+
// [END compute_instances_get]
48+
}
49+
50+
process.on('unhandledRejection', err => {
51+
console.error(err.message);
52+
process.exitCode = 1;
53+
});
54+
55+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Create a new VM instance using selected disks. The first disk in diskNames will be used as boot disk.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you want to use.
19+
* @param {string} zone - Name of the zone to create the instance in. For example: "us-west3-b"
20+
* @param {string} instanceName - Name of the new virtual machine (VM) instance.
21+
* @param {Array<string>} diskNames - Array of disk names to be attached to the new virtual machine.
22+
* First disk in this list will be used as the boot device.
23+
*/
24+
function main(projectId, zone, instanceName, diskNames) {
25+
// [START compute_instances_create_with_existing_disks]
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 diskNames = ['boot_disk', 'disk1', 'disk2'];
33+
34+
const compute = require('@google-cloud/compute');
35+
36+
async function createWithExistingDisks() {
37+
const instancesClient = new compute.InstancesClient();
38+
const disksClient = new compute.DisksClient();
39+
40+
if (diskNames.length < 1) {
41+
throw new Error('At least one disk should be provided');
42+
}
43+
44+
const disks = [];
45+
for (const diskName of diskNames) {
46+
const [disk] = await disksClient.get({
47+
project: projectId,
48+
zone,
49+
disk: diskName,
50+
});
51+
disks.push(disk);
52+
}
53+
54+
const attachedDisks = [];
55+
56+
for (const disk of disks) {
57+
attachedDisks.push({
58+
source: disk.selfLink,
59+
});
60+
}
61+
62+
attachedDisks[0].boot = true;
63+
64+
const [response] = await instancesClient.insert({
65+
project: projectId,
66+
zone,
67+
instanceResource: {
68+
name: instanceName,
69+
disks: attachedDisks,
70+
machineType: `zones/${zone}/machineTypes/n1-standard-1`,
71+
networkInterfaces: [
72+
{
73+
name: 'global/networks/default',
74+
},
75+
],
76+
},
77+
});
78+
let operation = response.latestResponse;
79+
const operationsClient = new compute.ZoneOperationsClient();
80+
81+
// Wait for the create operation to complete.
82+
while (operation.status !== 'DONE') {
83+
[operation] = await operationsClient.wait({
84+
operation: operation.name,
85+
project: projectId,
86+
zone: operation.zone.split('/').pop(),
87+
});
88+
}
89+
90+
console.log('Instance created.');
91+
}
92+
93+
createWithExistingDisks();
94+
// [END compute_instances_create_with_existing_disks]
95+
}
96+
97+
process.on('unhandledRejection', err => {
98+
console.error(err.message);
99+
process.exitCode = 1;
100+
});
101+
102+
const args = process.argv.slice(2);
103+
args[3] = args[3].split(',');
104+
main(...args);

0 commit comments

Comments
 (0)