Skip to content

Commit 1a14129

Browse files
author
ace-n
committed
Merge remote-tracking branch 'migration/main' into nodejs-compute-migration
2 parents e7c19e7 + e294626 commit 1a14129

File tree

73 files changed

+7136
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+7136
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 instance template with the provided name and a specific instance configuration.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you use.
19+
* @param {string} templateName - Name of the new template to create.
20+
*/
21+
function main(projectId, templateName) {
22+
// [START compute_template_create]
23+
/**
24+
* TODO(developer): Uncomment and replace these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const templateName = 'your_template_name';
28+
29+
const compute = require('@google-cloud/compute');
30+
31+
// Create a new instance template with the provided name and a specific instance configuration.
32+
async function createTemplate() {
33+
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
34+
35+
const [response] = await instanceTemplatesClient.insert({
36+
project: projectId,
37+
instanceTemplateResource: {
38+
name: templateName,
39+
properties: {
40+
disks: [
41+
{
42+
// The template describes the size and source image of the boot disk
43+
// to attach to the instance.
44+
initializeParams: {
45+
diskSizeGb: '250',
46+
sourceImage:
47+
'projects/debian-cloud/global/images/family/debian-11',
48+
},
49+
autoDelete: true,
50+
boot: true,
51+
},
52+
],
53+
machineType: 'e2-standard-4',
54+
// The template connects the instance to the `default` network,
55+
// without specifying a subnetwork.
56+
networkInterfaces: [
57+
{
58+
// Use the network interface provided in the networkName argument.
59+
name: 'global/networks/default',
60+
// The template lets the instance use an external IP address.
61+
accessConfigs: [
62+
{
63+
name: 'External NAT',
64+
type: 'ONE_TO_ONE_NAT',
65+
networkTier: 'PREMIUM',
66+
},
67+
],
68+
},
69+
],
70+
},
71+
},
72+
});
73+
let operation = response.latestResponse;
74+
const operationsClient = new compute.GlobalOperationsClient();
75+
76+
// Wait for the create operation to complete.
77+
while (operation.status !== 'DONE') {
78+
[operation] = await operationsClient.wait({
79+
operation: operation.name,
80+
project: projectId,
81+
});
82+
}
83+
84+
console.log('Instance template created.');
85+
}
86+
87+
createTemplate();
88+
// [END compute_template_create]
89+
}
90+
91+
process.on('unhandledRejection', err => {
92+
console.error(err.message);
93+
process.exitCode = 1;
94+
});
95+
96+
main(...process.argv.slice(2));
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 instance template based on an existing instance.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you use.
19+
* @param {string} instance - The instance to base the new template on. This value uses
20+
* the following format: "projects/{project}/zones/{zone}/instances/{instance_name}"
21+
* @param {string} templateName - Name of the new template to create.
22+
*/
23+
function main(projectId, instance, templateName) {
24+
// [START compute_template_create_from_instance]
25+
/**
26+
* TODO(developer): Uncomment and replace these variables before running the sample.
27+
*/
28+
// const projectId = 'YOUR_PROJECT_ID';
29+
// const instance = 'projects/project/zones/zone/instances/instance';
30+
// const templateName = 'your_template_name';
31+
32+
const compute = require('@google-cloud/compute');
33+
34+
// Create a new instance template based on an existing instance.
35+
// This new template specifies a different boot disk.
36+
async function createTemplateFromInstance() {
37+
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
38+
39+
const [response] = await instanceTemplatesClient.insert({
40+
project: projectId,
41+
instanceTemplateResource: {
42+
name: templateName,
43+
sourceInstance: instance,
44+
sourceInstanceParams: {
45+
diskConfigs: [
46+
{
47+
// Device name must match the name of a disk attached to the instance
48+
// your template is based on.
49+
deviceName: 'disk-1',
50+
// Replace the original boot disk image used in your instance with a Rocky Linux image.
51+
instantiateFrom: 'CUSTOM_IMAGE',
52+
customImage:
53+
'projects/rocky-linux-cloud/global/images/family/rocky-linux-8',
54+
// Override the auto_delete setting.
55+
autoDelete: true,
56+
},
57+
],
58+
},
59+
},
60+
});
61+
let operation = response.latestResponse;
62+
const operationsClient = new compute.GlobalOperationsClient();
63+
64+
// Wait for the create operation to complete.
65+
while (operation.status !== 'DONE') {
66+
[operation] = await operationsClient.wait({
67+
operation: operation.name,
68+
project: projectId,
69+
});
70+
}
71+
72+
console.log('Instance template created.');
73+
}
74+
75+
createTemplateFromInstance();
76+
// [END compute_template_create_from_instance]
77+
}
78+
79+
process.on('unhandledRejection', err => {
80+
console.error(err.message);
81+
process.exitCode = 1;
82+
});
83+
84+
main(...process.argv.slice(2));
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 an instance template that uses a provided subnet.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you use.
19+
* @param {string} network - The network to be used in the new template. This value uses
20+
* the following format: "projects/{project}/global/networks/{network}"
21+
* @param {string} subnetwork - The subnetwork to be used in the new template. This value
22+
* uses the following format: "projects/{project}/regions/{region}/subnetworks/{subnetwork}"
23+
* @param {string} templateName - Name of the new template to create.
24+
*/
25+
function main(projectId, network, subnetwork, templateName) {
26+
// [START compute_template_create_with_subnet]
27+
/**
28+
* TODO(developer): Uncomment and replace these variables before running the sample.
29+
*/
30+
// const projectId = 'YOUR_PROJECT_ID';
31+
// const network = 'projects/project/global/networks/network';
32+
// const subnetwork = 'projects/project/regions/region/subnetworks/subnetwork';
33+
// const templateName = 'your_template_name';
34+
35+
const compute = require('@google-cloud/compute');
36+
37+
// Create an instance template that uses a provided subnet.
38+
async function createTemplateWithSubnet() {
39+
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
40+
41+
const [response] = await instanceTemplatesClient.insert({
42+
project: projectId,
43+
instanceTemplateResource: {
44+
name: templateName,
45+
properties: {
46+
// The template describes the size and source image of the boot disk
47+
// to attach to the instance.
48+
disks: [
49+
{
50+
// The template describes the size and source image of the boot disk
51+
// to attach to the instance.
52+
initializeParams: {
53+
diskSizeGb: '250',
54+
sourceImage:
55+
'projects/debian-cloud/global/images/family/debian-11',
56+
},
57+
autoDelete: true,
58+
boot: true,
59+
},
60+
],
61+
machineType: 'e2-standard-4',
62+
// The template connects the instance to the specified network and subnetwork.
63+
networkInterfaces: [
64+
{
65+
network,
66+
subnetwork,
67+
},
68+
],
69+
},
70+
},
71+
});
72+
let operation = response.latestResponse;
73+
const operationsClient = new compute.GlobalOperationsClient();
74+
75+
// Wait for the create operation to complete.
76+
while (operation.status !== 'DONE') {
77+
[operation] = await operationsClient.wait({
78+
operation: operation.name,
79+
project: projectId,
80+
});
81+
}
82+
83+
console.log('Instance template created.');
84+
}
85+
86+
createTemplateWithSubnet();
87+
// [END compute_template_create_with_subnet]
88+
}
89+
90+
process.on('unhandledRejection', err => {
91+
console.error(err.message);
92+
process.exitCode = 1;
93+
});
94+
95+
main(...process.argv.slice(2));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 an instance template.
17+
*
18+
* @param {string} projectId - Project ID or project number of the Cloud project you use.
19+
* @param {string} templateName - Name of the template to delete.
20+
*/
21+
function main(projectId, templateName) {
22+
// [START compute_template_delete]
23+
/**
24+
* TODO(developer): Uncomment and replace these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const templateName = 'your_template_name';
28+
29+
const compute = require('@google-cloud/compute');
30+
31+
// Delete an instance template.
32+
async function deleteInstanceTemplate() {
33+
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
34+
35+
const [response] = await instanceTemplatesClient.delete({
36+
project: projectId,
37+
instanceTemplate: templateName,
38+
});
39+
let operation = response.latestResponse;
40+
const operationsClient = new compute.GlobalOperationsClient();
41+
42+
// Wait for the create operation to complete.
43+
while (operation.status !== 'DONE') {
44+
[operation] = await operationsClient.wait({
45+
operation: operation.name,
46+
project: projectId,
47+
});
48+
}
49+
50+
console.log('Instance template deleted.');
51+
}
52+
53+
deleteInstanceTemplate();
54+
// [END compute_template_delete]
55+
}
56+
57+
process.on('unhandledRejection', err => {
58+
console.error(err.message);
59+
process.exitCode = 1;
60+
});
61+
62+
main(...process.argv.slice(2));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
* Retrieves an instance template, which you can use to create virtual machine
17+
* (VM) instances and managed instance groups (MIGs).
18+
*
19+
* @param {string} projectId - Project ID or project number of the Cloud project you use.
20+
* @param {string} templateName - Name of the template to retrieve.
21+
*/
22+
function main(projectId, templateName) {
23+
// [START compute_template_get]
24+
/**
25+
* TODO(developer): Uncomment and replace these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const templateName = 'your_template_name';
29+
30+
const compute = require('@google-cloud/compute');
31+
32+
// Retrieve an instance template, which you can use to create
33+
// virtual machine (VM) instances and managed instance groups (MIGs).
34+
async function getInstanceTemplate() {
35+
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
36+
37+
const [instance] = await instanceTemplatesClient.get({
38+
project: projectId,
39+
instanceTemplate: templateName,
40+
});
41+
42+
console.log('Instance template:', instance);
43+
}
44+
45+
getInstanceTemplate();
46+
// [END compute_template_get]
47+
}
48+
49+
process.on('unhandledRejection', err => {
50+
console.error(err.message);
51+
process.exitCode = 1;
52+
});
53+
54+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)