Skip to content

Commit 4f0a8fa

Browse files
docs(samples): Adding samples for delete protection (#208)
* chore(samples): Adding samples for delete protection. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore(samples): Applying review comment. Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 88e8562 commit 4f0a8fa

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
import uuid
15+
16+
import google.auth
17+
import pytest
18+
19+
from quickstart import delete_instance
20+
from sample_delete_protection import create_instance
21+
from sample_delete_protection import get_delete_protection
22+
from sample_delete_protection import set_delete_protection
23+
24+
PROJECT = google.auth.default()[1]
25+
INSTANCE_ZONE = "europe-central2-a"
26+
27+
28+
@pytest.fixture
29+
def autodelete_instance_name():
30+
instance_name = "test-instance-" + uuid.uuid4().hex[:10]
31+
32+
yield instance_name
33+
34+
if get_delete_protection(PROJECT, INSTANCE_ZONE, instance_name):
35+
set_delete_protection(PROJECT, INSTANCE_ZONE, instance_name, False)
36+
37+
delete_instance(PROJECT, INSTANCE_ZONE, instance_name)
38+
39+
40+
def test_delete_protection(autodelete_instance_name):
41+
instance = create_instance(PROJECT, INSTANCE_ZONE, autodelete_instance_name, True)
42+
assert instance.name == autodelete_instance_name
43+
44+
assert (
45+
get_delete_protection(PROJECT, INSTANCE_ZONE, autodelete_instance_name) is True
46+
)
47+
48+
set_delete_protection(PROJECT, INSTANCE_ZONE, autodelete_instance_name, False)
49+
50+
assert (
51+
get_delete_protection(PROJECT, INSTANCE_ZONE, autodelete_instance_name) is False
52+
)

0 commit comments

Comments
 (0)