|
| 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 | +# [START compute_instances_create_custom_hostname] |
| 16 | +import sys |
| 17 | + |
| 18 | + |
| 19 | +# [START compute_instances_get_hostname] |
| 20 | +from google.cloud import compute_v1 |
| 21 | + |
| 22 | +# [END compute_instances_get_hostname] |
| 23 | +# [END compute_instances_create_custom_hostname] |
| 24 | + |
| 25 | + |
| 26 | +# [START compute_instances_create_custom_hostname] |
| 27 | +def create_instance( |
| 28 | + project_id: str, zone: str, instance_name: str, hostname: str, |
| 29 | +) -> compute_v1.Instance: |
| 30 | + """ |
| 31 | + Send an instance creation request to the Compute Engine API and wait for it to complete. |
| 32 | +
|
| 33 | + Args: |
| 34 | + project_id: project ID or project number of the Cloud project you want to use. |
| 35 | + zone: name of the zone you want to use. For example: “us-west3-b” |
| 36 | + instance_name: name of the new virtual machine. |
| 37 | + hostname: Custom hostname of the new VM instance. |
| 38 | + Custom hostnames must conform to RFC 1035 requirements for valid hostnames. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + Instance object. |
| 42 | + """ |
| 43 | + instance_client = compute_v1.InstancesClient() |
| 44 | + operation_client = compute_v1.ZoneOperationsClient() |
| 45 | + |
| 46 | + # Describe the size and source image of the boot disk to attach to the instance. |
| 47 | + disk = compute_v1.AttachedDisk() |
| 48 | + initialize_params = compute_v1.AttachedDiskInitializeParams() |
| 49 | + initialize_params.source_image = ( |
| 50 | + "projects/debian-cloud/global/images/family/debian-10" |
| 51 | + ) |
| 52 | + initialize_params.disk_size_gb = 10 |
| 53 | + disk.initialize_params = initialize_params |
| 54 | + disk.auto_delete = True |
| 55 | + disk.boot = True |
| 56 | + disk.type_ = "PERSISTENT" |
| 57 | + |
| 58 | + # Use the default VPC network. |
| 59 | + network_interface = compute_v1.NetworkInterface() |
| 60 | + network_interface.name = "default" |
| 61 | + |
| 62 | + # Collect information into the Instance object. |
| 63 | + instance = compute_v1.Instance() |
| 64 | + instance.name = instance_name |
| 65 | + instance.disks = [disk] |
| 66 | + instance.machine_type = f"zones/{zone}/machineTypes/e2-small" |
| 67 | + instance.network_interfaces = [network_interface] |
| 68 | + |
| 69 | + # Custom hostnames are not resolved by the automatically created records |
| 70 | + # provided by Compute Engine internal DNS. |
| 71 | + # You must manually configure the DNS record for your custom hostname. |
| 72 | + instance.hostname = hostname |
| 73 | + |
| 74 | + # Prepare the request to insert an instance. |
| 75 | + request = compute_v1.InsertInstanceRequest() |
| 76 | + request.zone = zone |
| 77 | + request.project = project_id |
| 78 | + request.instance_resource = instance |
| 79 | + |
| 80 | + # Wait for the create operation to complete. |
| 81 | + print(f"Creating the {instance_name} instance in {zone}...") |
| 82 | + operation = instance_client.insert_unary(request=request) |
| 83 | + while operation.status != compute_v1.Operation.Status.DONE: |
| 84 | + operation = operation_client.wait( |
| 85 | + operation=operation.name, zone=zone, project=project_id |
| 86 | + ) |
| 87 | + if operation.error: |
| 88 | + print("Error during creation:", operation.error, file=sys.stderr) |
| 89 | + if operation.warnings: |
| 90 | + print("Warning during creation:", operation.warnings, file=sys.stderr) |
| 91 | + print(f"Instance {instance_name} created.") |
| 92 | + return instance |
| 93 | + |
| 94 | + |
| 95 | +# [END compute_instances_create_custom_hostname] |
| 96 | + |
| 97 | + |
| 98 | +# [START compute_instances_get_hostname] |
| 99 | +def get_instance_hostname(project_id: str, zone: str, instance_name: str) -> str: |
| 100 | + """ |
| 101 | + Get the hostname set for given instance. |
| 102 | +
|
| 103 | + Args: |
| 104 | + project_id: project ID or project number of the Cloud project you want to use. |
| 105 | + zone: name of the zone you want to use. For example: “us-west3-b” |
| 106 | + instance_name: name of the virtual machine you want to check. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + The hostname of given instance. |
| 110 | + """ |
| 111 | + instance_client = compute_v1.InstancesClient() |
| 112 | + instance = instance_client.get( |
| 113 | + project=project_id, zone=zone, instance=instance_name |
| 114 | + ) |
| 115 | + return instance.hostname |
| 116 | + |
| 117 | + |
| 118 | +# [END compute_instances_get_hostname] |
0 commit comments