Skip to content

Commit e984fd1

Browse files
FrodoTheTruedandhlee
authored andcommitted
chore(samples): fix LRO wrapper (#99)
1 parent c08a74a commit e984fd1

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

compute/compute/snippets/quickstart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def create_instance(
121121
Instance object.
122122
"""
123123
instance_client = compute_v1.InstancesClient()
124+
operation_client = compute_v1.ZoneOperationsClient()
124125

125126
# Describe the size and source image of the boot disk to attach to the instance.
126127
disk = compute_v1.AttachedDisk()
@@ -155,8 +156,7 @@ def create_instance(
155156
# Wait for the create operation to complete.
156157
print(f"Creating the {instance_name} instance in {zone}...")
157158
operation = instance_client.insert(request=request)
158-
if operation.status == compute_v1.Operation.Status.RUNNING:
159-
operation_client = compute_v1.ZoneOperationsClient()
159+
while operation.status != compute_v1.Operation.Status.DONE:
160160
operation = operation_client.wait(
161161
operation=operation.name, zone=zone, project=project_id
162162
)
@@ -180,13 +180,13 @@ def delete_instance(project_id: str, zone: str, machine_name: str) -> None:
180180
machine_name: name of the machine you want to delete.
181181
"""
182182
instance_client = compute_v1.InstancesClient()
183+
operation_client = compute_v1.ZoneOperationsClient()
183184

184185
print(f"Deleting {machine_name} from {zone}...")
185186
operation = instance_client.delete(
186187
project=project_id, zone=zone, instance=machine_name
187188
)
188-
if operation.status == compute_v1.Operation.Status.RUNNING:
189-
operation_client = compute_v1.ZoneOperationsClient()
189+
while operation.status != compute_v1.Operation.Status.DONE:
190190
operation = operation_client.wait(
191191
operation=operation.name, zone=zone, project=project_id
192192
)

compute/compute/snippets/sample_default_values.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ def set_usage_export_bucket(project_id: str, bucket_name: str,
5959
project=project_id, usage_export_location_resource=usage_export_location)
6060

6161
op_client = compute_v1.GlobalOperationsClient()
62-
op_client.wait(project=project_id, operation=operation.name)
62+
63+
while operation.status != compute_v1.Operation.Status.DONE:
64+
operation = op_client.wait(
65+
operation=operation.name, project=project_id
66+
)
67+
6368
# [END compute_usage_report_set]
6469

6570

@@ -113,5 +118,9 @@ def disable_usage_export(project_id: str) -> None:
113118
project=project_id, usage_export_location_resource=None)
114119

115120
op_client = compute_v1.GlobalOperationsClient()
116-
op_client.wait(project=project_id, operation=operation.name)
121+
122+
while operation.status != compute_v1.Operation.Status.DONE:
123+
operation = op_client.wait(
124+
operation=operation.name, project=project_id
125+
)
117126
# [END compute_usage_report_disable]

compute/compute/snippets/sample_start_stop.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def start_instance(project_id: str, zone: str, instance_name: str):
3535

3636
op = instance_client.start(project=project_id, zone=zone, instance=instance_name)
3737

38-
op_client.wait(project=project_id, zone=zone, operation=op.name)
38+
while op.status != compute_v1.Operation.Status.DONE:
39+
op = op_client.wait(
40+
operation=op.name, zone=zone, project=project_id
41+
)
3942
return
4043
# [END compute_start_instance]
4144

@@ -71,7 +74,10 @@ def start_instance_with_encryption_key(project_id: str, zone: str, instance_name
7174
op = instance_client.start_with_encryption_key(project=project_id, zone=zone, instance=instance_name,
7275
instances_start_with_encryption_key_request_resource=enc_data)
7376

74-
op_client.wait(project=project_id, zone=zone, operation=op.name)
77+
while op.status != compute_v1.Operation.Status.DONE:
78+
op = op_client.wait(
79+
operation=op.name, zone=zone, project=project_id
80+
)
7581
return
7682
# [END compute_start_enc_instance]
7783

@@ -91,7 +97,10 @@ def stop_instance(project_id: str, zone: str, instance_name: str):
9197

9298
op = instance_client.stop(project=project_id, zone=zone, instance=instance_name)
9399

94-
op_client.wait(project=project_id, zone=zone, operation=op.name)
100+
while op.status != compute_v1.Operation.Status.DONE:
101+
op = op_client.wait(
102+
operation=op.name, zone=zone, project=project_id
103+
)
95104
return
96105
# [END compute_stop_instance]
97106

@@ -111,6 +120,9 @@ def reset_instance(project_id: str, zone: str, instance_name: str):
111120

112121
op = instance_client.reset(project=project_id, zone=zone, instance=instance_name)
113122

114-
op_client.wait(project=project_id, zone=zone, operation=op.name)
123+
while op.status != compute_v1.Operation.Status.DONE:
124+
op = op_client.wait(
125+
operation=op.name, zone=zone, project=project_id
126+
)
115127
return
116128
# [END compute_reset_instance]

compute/compute/snippets/test_sample_start_stop.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def _create_instance(request: compute_v1.InsertInstanceRequest):
7777
operation_client = compute_v1.ZoneOperationsClient()
7878

7979
operation = instance_client.insert(request=request)
80-
operation_client.wait(operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT)
80+
while operation.status != compute_v1.Operation.Status.DONE:
81+
operation = operation_client.wait(
82+
operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT
83+
)
8184

8285
return instance_client.get(project=PROJECT, zone=INSTANCE_ZONE, instance=request.instance_resource.name)
8386

@@ -87,7 +90,10 @@ def _delete_instance(instance: compute_v1.Instance):
8790
operation_client = compute_v1.ZoneOperationsClient()
8891

8992
operation = instance_client.delete(project=PROJECT, zone=INSTANCE_ZONE, instance=instance.name)
90-
operation_client.wait(operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT)
93+
while operation.status != compute_v1.Operation.Status.DONE:
94+
operation = operation_client.wait(
95+
operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT
96+
)
9197

9298

9399
def _get_status(instance: compute_v1.Instance) -> compute_v1.Instance.Status:

0 commit comments

Comments
 (0)