|
| 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 | +# [START compute_delete_protection_create] |
| 15 | +import sys |
| 16 | + |
| 17 | +# [END compute_delete_protection_create] |
| 18 | + |
| 19 | +# [START compute_delete_protection_get] |
| 20 | +# [START compute_delete_protection_set] |
| 21 | +# [START compute_delete_protection_create] |
| 22 | +from google.cloud import compute_v1 |
| 23 | + |
| 24 | +# [END compute_delete_protection_create] |
| 25 | +# [END compute_delete_protection_set] |
| 26 | +# [END compute_delete_protection_get] |
| 27 | + |
| 28 | + |
| 29 | +# [START compute_delete_protection_create] |
| 30 | +def create_instance( |
| 31 | + project_id: str, zone: str, instance_name: str, delete_protection: bool, |
| 32 | +) -> compute_v1.Instance: |
| 33 | + """ |
| 34 | + Send an instance creation request to the Compute Engine API and wait for it to complete. |
| 35 | +
|
| 36 | + Args: |
| 37 | + project_id: project ID or project number of the Cloud project you want to use. |
| 38 | + zone: name of the zone you want to use. For example: “us-west3-b” |
| 39 | + instance_name: name of the new virtual machine. |
| 40 | + delete_protection: boolean value indicating if the new virtual machine should be |
| 41 | + protected against deletion or not. |
| 42 | + Returns: |
| 43 | + Instance object. |
| 44 | + """ |
| 45 | + instance_client = compute_v1.InstancesClient() |
| 46 | + operation_client = compute_v1.ZoneOperationsClient() |
| 47 | + |
| 48 | + # Describe the size and source image of the boot disk to attach to the instance. |
| 49 | + disk = compute_v1.AttachedDisk() |
| 50 | + initialize_params = compute_v1.AttachedDiskInitializeParams() |
| 51 | + initialize_params.source_image = ( |
| 52 | + "projects/debian-cloud/global/images/family/debian-10" |
| 53 | + ) |
| 54 | + initialize_params.disk_size_gb = 10 |
| 55 | + disk.initialize_params = initialize_params |
| 56 | + disk.auto_delete = True |
| 57 | + disk.boot = True |
| 58 | + disk.type_ = "PERSISTENT" |
| 59 | + |
| 60 | + # Use the default VPC network. |
| 61 | + network_interface = compute_v1.NetworkInterface() |
| 62 | + network_interface.name = "default" |
| 63 | + |
| 64 | + # Collect information into the Instance object. |
| 65 | + instance = compute_v1.Instance() |
| 66 | + instance.name = instance_name |
| 67 | + instance.disks = [disk] |
| 68 | + instance.machine_type = f"zones/{zone}/machineTypes/e2-small" |
| 69 | + instance.network_interfaces = [network_interface] |
| 70 | + |
| 71 | + # Set the delete protection bit |
| 72 | + instance.deletion_protection = delete_protection |
| 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_delete_protection_create] |
| 96 | + |
| 97 | + |
| 98 | +# [START compute_delete_protection_set] |
| 99 | +def set_delete_protection( |
| 100 | + project_id: str, zone: str, instance_name: str, delete_protection: bool |
| 101 | +): |
| 102 | + """ |
| 103 | + Updates the delete protection setting of given instance. |
| 104 | +
|
| 105 | + Args: |
| 106 | + project_id: project ID or project number of the Cloud project you want to use. |
| 107 | + zone: name of the zone you want to use. For example: “us-west3-b” |
| 108 | + instance_name: name of the virtual machine to update. |
| 109 | + delete_protection: boolean value indicating if the virtual machine should be |
| 110 | + protected against deletion or not. |
| 111 | + """ |
| 112 | + instance_client = compute_v1.InstancesClient() |
| 113 | + operation_client = compute_v1.ZoneOperationsClient() |
| 114 | + |
| 115 | + request = compute_v1.SetDeletionProtectionInstanceRequest() |
| 116 | + request.project = project_id |
| 117 | + request.zone = zone |
| 118 | + request.resource = instance_name |
| 119 | + request.deletion_protection = delete_protection |
| 120 | + |
| 121 | + operation = instance_client.set_deletion_protection_unary(request) |
| 122 | + operation_client.wait(project=project_id, zone=zone, operation=operation.name) |
| 123 | + |
| 124 | + |
| 125 | +# [END compute_delete_protection_set] |
| 126 | + |
| 127 | + |
| 128 | +# [START compute_delete_protection_get] |
| 129 | +def get_delete_protection(project_id: str, zone: str, instance_name: str) -> bool: |
| 130 | + """ |
| 131 | + Returns the state of delete protection flag of given instance. |
| 132 | +
|
| 133 | + Args: |
| 134 | + project_id: project ID or project number of the Cloud project you want to use. |
| 135 | + zone: name of the zone you want to use. For example: “us-west3-b” |
| 136 | + instance_name: name of the virtual machine to check. |
| 137 | + Returns: |
| 138 | + The state of the delete protection setting. |
| 139 | + """ |
| 140 | + instance_client = compute_v1.InstancesClient() |
| 141 | + instance = instance_client.get( |
| 142 | + project=project_id, zone=zone, instance=instance_name |
| 143 | + ) |
| 144 | + return instance.deletion_protection |
| 145 | + |
| 146 | + |
| 147 | +# [END compute_delete_protection_get] |
0 commit comments