Skip to content

Commit ebfb8bd

Browse files
FrodoTheTrueAce Nassri
authored and
Ace Nassri
committed
chore(samples): add creating managing windows instances samples (#704)
1 parent e13e8c6 commit ebfb8bd

File tree

3 files changed

+358
-0
lines changed

3 files changed

+358
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 Windows Server instance that has an external IP address.
17+
*
18+
* @param {string} projectId - ID or number of the 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 new machine.
21+
* @param {string} machineType - Machine type you want to create in following format:
22+
* "zones/{zone}/machineTypes/{type_name}". For example:
23+
* "zones/europe-west3-c/machineTypes/f1-micro"
24+
* You can find the list of available machine types using:
25+
* https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list
26+
* @param {string} sourceImageFamily - Name of the public image family for Windows Server or SQL Server images.
27+
* https://cloud.google.com/compute/docs/images#os-compute-support
28+
*/
29+
function main(
30+
projectId,
31+
zone,
32+
instanceName,
33+
machineType = 'n1-standard-1',
34+
sourceImageFamily = 'windows-2012-r2'
35+
) {
36+
// [START compute_create_windows_instance_external_ip]
37+
/**
38+
* TODO(developer): Uncomment and replace these variables before running the sample.
39+
*/
40+
// const projectId = 'YOUR_PROJECT_ID';
41+
// const zone = 'europe-central2-b';
42+
// const instanceName = 'YOUR_INSTANCE_NAME';
43+
// const machineType = 'n1-standard-1';
44+
// const sourceImageFamily = 'windows-2012-r2';
45+
46+
const compute = require('@google-cloud/compute');
47+
48+
async function createWndowsServerInstanceExpernalIP() {
49+
const instancesClient = new compute.InstancesClient();
50+
51+
const [response] = await instancesClient.insert({
52+
instanceResource: {
53+
name: instanceName,
54+
disks: [
55+
{
56+
// Describe the size and source image of the boot disk to attach to the instance.
57+
initializeParams: {
58+
diskSizeGb: '64',
59+
sourceImage: `projects/windows-cloud/global/images/family/${sourceImageFamily}/`,
60+
},
61+
autoDelete: true,
62+
boot: true,
63+
type: 'PERSISTENT',
64+
},
65+
],
66+
machineType: `zones/${zone}/machineTypes/${machineType}`,
67+
networkInterfaces: [
68+
{
69+
accessConfigs: [
70+
{
71+
type: 'ONE_TO_ONE_NAT',
72+
name: 'External NAT',
73+
},
74+
],
75+
// If you going you use custom VPC network, it must be configured to allow access to kms.windows.googlecloud.com.
76+
// https://cloud.google.com/compute/docs/instances/windows/creating-managing-windows-instances#kms-server.
77+
name: 'global/networks/default',
78+
},
79+
],
80+
// If you chose an image that supports Shielded VM, you can optionally change the instance's Shielded VM settings.
81+
// "shieldedInstanceConfig": {
82+
// "enableSecureBoot": true,
83+
// "enableVtpm": true,
84+
// "enableIntegrityMonitoring": true
85+
// },
86+
},
87+
project: projectId,
88+
zone,
89+
});
90+
let operation = response.latestResponse;
91+
const operationsClient = new compute.ZoneOperationsClient();
92+
93+
// Wait for the create operation to complete.
94+
while (operation.status !== 'DONE') {
95+
[operation] = await operationsClient.wait({
96+
operation: operation.name,
97+
project: projectId,
98+
zone: operation.zone.split('/').pop(),
99+
});
100+
}
101+
102+
console.log('Instance created.');
103+
}
104+
105+
createWndowsServerInstanceExpernalIP();
106+
// [END compute_create_windows_instance_external_ip]
107+
}
108+
109+
process.on('unhandledRejection', err => {
110+
console.error(err.message);
111+
process.exitCode = 1;
112+
});
113+
114+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 Windows Server instance that has only an internal IP address.
17+
*
18+
* @param {string} projectId - ID or number of the 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 new machine.
21+
* @param {string} machineType - Machine type you want to create in following format:
22+
* "zones/{zone}/machineTypes/{type_name}". For example:
23+
* "zones/europe-west3-c/machineTypes/f1-micro"
24+
* You can find the list of available machine types using:
25+
* https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list
26+
* @param {string} sourceImageFamily - Name of the public image family for Windows Server or SQL Server images.
27+
* https://cloud.google.com/compute/docs/images#os-compute-support
28+
* @param {string} networkLink - Name of the network you want the new instance to use.
29+
* For example: "global/networks/default" represents the network
30+
* named "default", which is created automatically for each project.
31+
* @param {string} subnetworkLink - Name of the subnetwork you want the new instance to use.
32+
* This value uses the following format:
33+
* "regions/{region}/subnetworks/{subnetwork_name}"
34+
*/
35+
function main(
36+
projectId,
37+
zone,
38+
instanceName,
39+
machineType = 'n1-standard-1',
40+
sourceImageFamily = 'windows-2012-r2',
41+
networkLink = 'global/networks/default',
42+
subnetworkLink = 'regions/europe-central2/subnetworks/default'
43+
) {
44+
// [START compute_create_windows_instance_internal_ip]
45+
/**
46+
* TODO(developer): Uncomment and replace these variables before running the sample.
47+
*/
48+
// const projectId = 'YOUR_PROJECT_ID';
49+
// const zone = 'europe-central2-b';
50+
// const instanceName = 'YOUR_INSTANCE_NAME';
51+
// const machineType = 'n1-standard-1';
52+
// const sourceImageFamily = 'windows-2012-r2';
53+
// const networkLink = 'global/networks/default';
54+
// const subnetworkLink = 'regions/europe-central2/subnetworks/default';
55+
56+
const compute = require('@google-cloud/compute');
57+
58+
async function createWndowsServerInstanceExpernalIP() {
59+
const instancesClient = new compute.InstancesClient();
60+
61+
const [response] = await instancesClient.insert({
62+
instanceResource: {
63+
name: instanceName,
64+
disks: [
65+
{
66+
// Describe the size and source image of the boot disk to attach to the instance.
67+
initializeParams: {
68+
diskSizeGb: '64',
69+
sourceImage: `projects/windows-cloud/global/images/family/${sourceImageFamily}/`,
70+
},
71+
autoDelete: true,
72+
boot: true,
73+
type: 'PERSISTENT',
74+
},
75+
],
76+
machineType: `zones/${zone}/machineTypes/${machineType}`,
77+
networkInterfaces: [
78+
{
79+
// You must verify or configure routes and firewall rules in your VPC network
80+
// to allow access to kms.windows.googlecloud.com.
81+
// More information about access to kms.windows.googlecloud.com: https://cloud.google.com/compute/docs/instances/windows/creating-managing-windows-instances#kms-server
82+
83+
// Additionally, you must enable Private Google Access for subnets in your VPC network
84+
// that contain Windows instances with only internal IP addresses.
85+
// More information about Private Google Access: https://cloud.google.com/vpc/docs/configure-private-google-access#enabling
86+
name: networkLink,
87+
subnetwork: subnetworkLink,
88+
},
89+
],
90+
// If you chose an image that supports Shielded VM, you can optionally change the instance's Shielded VM settings.
91+
// "shieldedInstanceConfig": {
92+
// "enableSecureBoot": true,
93+
// "enableVtpm": true,
94+
// "enableIntegrityMonitoring": true
95+
// },
96+
},
97+
project: projectId,
98+
zone,
99+
});
100+
let operation = response.latestResponse;
101+
const operationsClient = new compute.ZoneOperationsClient();
102+
103+
// Wait for the create operation to complete.
104+
while (operation.status !== 'DONE') {
105+
[operation] = await operationsClient.wait({
106+
operation: operation.name,
107+
project: projectId,
108+
zone: operation.zone.split('/').pop(),
109+
});
110+
}
111+
112+
console.log('Instance created.');
113+
}
114+
115+
createWndowsServerInstanceExpernalIP();
116+
// [END compute_create_windows_instance_internal_ip]
117+
}
118+
119+
process.on('unhandledRejection', err => {
120+
console.error(err.message);
121+
process.exitCode = 1;
122+
});
123+
124+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
'use strict';
16+
17+
const compute = require('@google-cloud/compute');
18+
19+
const {describe, it} = require('mocha');
20+
const cp = require('child_process');
21+
const {assert} = require('chai');
22+
23+
const {generateTestId, getStaleVMInstances, deleteInstance} = require('./util');
24+
25+
const instancesClient = new compute.InstancesClient();
26+
const firewallsClient = new compute.FirewallsClient();
27+
const routesClient = new compute.RoutesClient();
28+
const globalOperationsClient = new compute.GlobalOperationsClient();
29+
30+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
31+
32+
const deleteFirewallRule = async (projectId, firewallRule) => {
33+
const [response] = await firewallsClient.delete({
34+
project: projectId,
35+
firewall: firewallRule,
36+
});
37+
let operation = response.latestResponse;
38+
39+
// Wait for the delete operation to complete.
40+
while (operation.status !== 'DONE') {
41+
[operation] = await globalOperationsClient.wait({
42+
operation: operation.name,
43+
project: projectId,
44+
});
45+
}
46+
};
47+
48+
const deleteRoute = async (projectId, routeName) => {
49+
const [response] = await routesClient.delete({
50+
project: projectId,
51+
route: routeName,
52+
});
53+
let operation = response.latestResponse;
54+
const operationsClient = new compute.GlobalOperationsClient();
55+
56+
// Wait for the delete operation to complete.
57+
while (operation.status !== 'DONE') {
58+
[operation] = await operationsClient.wait({
59+
operation: operation.name,
60+
project: projectId,
61+
});
62+
}
63+
};
64+
65+
describe('creating managing windows instances tests', () => {
66+
const instanceName = generateTestId();
67+
const firewallRuleName = generateTestId();
68+
const networkName = 'global/networks/default-compute';
69+
const subnetworkName = 'regions/europe-central2/subnetworks/default-compute';
70+
const routeName = generateTestId();
71+
const zone = 'europe-central2-b';
72+
const machineType = 'n1-standard-1';
73+
const sourceImageFamily = 'windows-2012-r2';
74+
75+
before(async () => {
76+
const instances = await getStaleVMInstances();
77+
await Promise.all(
78+
instances.map(instance =>
79+
deleteInstance(instance.zone, instance.instanceName)
80+
)
81+
);
82+
});
83+
84+
it('should create windows server instance with external IP', async () => {
85+
const projectId = await instancesClient.getProjectId();
86+
87+
const output = execSync(
88+
`node instances/windows/creating-managing-windows-instances/createWindowsServerInstanceExternalIP ${projectId} ${zone} ${instanceName}`
89+
);
90+
assert.match(output, /Instance created./);
91+
92+
execSync(`node deleteInstance ${projectId} ${zone} ${instanceName}`);
93+
});
94+
95+
it('should create windows server instance with internal IP and firewall rule', async () => {
96+
const projectId = await instancesClient.getProjectId();
97+
98+
const output = execSync(
99+
`node instances/windows/creating-managing-windows-instances/createWindowsServerInstanceInternalIP ${projectId} ${zone} ${instanceName} ${machineType} ${sourceImageFamily} ${networkName} ${subnetworkName}`
100+
);
101+
102+
assert.match(output, /Instance created./);
103+
104+
const outputFirewall = execSync(
105+
`node instances/windows/creating-managing-windows-instances/createFirewallRuleForWindowsActivationHost ${projectId} ${firewallRuleName} ${networkName}`
106+
);
107+
108+
assert.match(outputFirewall, /Firewall rule created./);
109+
110+
const outputRoute = execSync(
111+
`node instances/windows/creating-managing-windows-instances/createRouteToWindowsActivationHost ${projectId} ${routeName} ${networkName}`
112+
);
113+
114+
assert.match(outputRoute, /Route created./);
115+
116+
await deleteRoute(projectId, routeName);
117+
await deleteFirewallRule(projectId, firewallRuleName);
118+
execSync(`node deleteInstance ${projectId} ${zone} ${instanceName}`);
119+
});
120+
});

0 commit comments

Comments
 (0)