|
| 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)); |
0 commit comments