Skip to content

Commit 7d17fc0

Browse files
committed
fix the wait_until examples to use an existing reponse object instead of calling clients
Signed-off-by: Ken Caruso <[email protected]>
1 parent 9f99c23 commit 7d17fc0

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

examples/wait_for_resource_in_state.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
print('Creating VCN')
2020
result = virtual_network_client.create_vcn(oci.core.models.CreateVcnDetails(cidr_block='10.0.0.0/16', display_name='WaitForResourceExampleVcn', compartment_id=compartment_id))
2121
vcn_ocid = result.data.id
22-
get_vcn_response = oci.wait_until(virtual_network_client, virtual_network_client.get_vcn(vcn_ocid), 'lifecycle_state', 'AVAILABLE')
23-
print(get_vcn_response.data)
22+
get_vcn_response = virtual_network_client.get_vcn(vcn_ocid)
23+
wait_until_vcn_available_response = oci.wait_until(virtual_network_client, get_vcn_response, 'lifecycle_state', 'AVAILABLE')
24+
print(wait_until_vcn_available_response.data)
2425

2526
# This creates a subnet in the VCN and waits until the subnet's lifecycle state is AVAILABLE
2627
print('Creating Subnet 1')
@@ -34,8 +35,9 @@
3435
)
3536
)
3637
subnet_ocid = result.data.id
37-
get_subnet_response = oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_ocid), 'lifecycle_state', 'AVAILABLE')
38-
print(get_subnet_response.data)
38+
get_subnet_response = virtual_network_client.get_subnet(subnet_ocid)
39+
wait_until_subnet_available_response = oci.wait_until(virtual_network_client, get_subnet_response, 'lifecycle_state', 'AVAILABLE')
40+
print(wait_until_subnet_available_response.data)
3941

4042
# Here we use a variation of the wait_until function where instead of specifying the property and state we can pass in a function reference (either
4143
# a reference to a defined function or a lambda) that returns a truthy value if the waiter should stop waiting and a falsey value if the waiter
@@ -54,8 +56,9 @@
5456
)
5557
)
5658
subnet_two_ocid = result.data.id
57-
get_subnet_response = oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_two_ocid), evaluate_response=lambda r: r.data.lifecycle_state == 'AVAILABLE')
58-
print(get_subnet_response.data)
59+
get_subnet_two_response = virtual_network_client.get_subnet(subnet_two_ocid)
60+
wait_until_subnet_two_available_response = oci.wait_until(virtual_network_client, get_subnet_two_response, evaluate_response=lambda r: r.data.lifecycle_state == 'AVAILABLE')
61+
print(wait_until_subnet_two_available_response.data)
5962

6063
# Now we create a load balancer and wait until it has been created. Load balancers work slightly differently in that the create_load_balancer call
6164
# returns a work request and it is the work request whose state we should wait on (we wait until it has succeeded)
@@ -82,26 +85,31 @@
8285
)
8386
result = load_balancer_client.create_load_balancer(create_load_balancer_details)
8487
work_request_id = result.headers['opc-work-request-id']
85-
get_work_request_response = oci.wait_until(load_balancer_client, load_balancer_client.get_work_request(work_request_id), 'lifecycle_state', 'SUCCEEDED')
86-
print(get_work_request_response.data)
88+
get_work_request_response = load_balancer_client.get_work_request(work_request_id)
89+
wait_until_succeeded_response = oci.wait_until(load_balancer_client, get_work_request_response, 'lifecycle_state', 'SUCCEEDED')
90+
print(wait_until_succeeded_response.data)
8791
load_balancer_ocid = get_work_request_response.data.load_balancer_id
8892

8993
# Here we delete the load balancer. Note that on the waiter we use the optional succeed_on_not_found and set it to True. This meants that if we get a
9094
# 404 back from the service when checking the load balancer's state, instead of throwing an exception we will return successfully. This flag will typically
9195
# only be useful for delete/terminate scenarios and its normal default is False.
9296
print('Deleting Load Balancer')
97+
get_load_balancer_response = load_balancer_client.get_load_balancer(load_balancer_ocid)
9398
load_balancer_client.delete_backend_set(load_balancer_ocid, 'WaitExampleBackSet')
9499
load_balancer_client.delete_load_balancer(load_balancer_ocid)
95-
oci.wait_until(load_balancer_client, load_balancer_client.get_load_balancer(load_balancer_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)
100+
oci.wait_until(load_balancer_client, get_load_balancer_response, 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)
96101

97102
print('Deleting Subnet 1')
103+
get_subnet_response = virtual_network_client.get_subnet(subnet_ocid)
98104
virtual_network_client.delete_subnet(subnet_ocid)
99-
oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)
105+
oci.wait_until(virtual_network_client, get_subnet_response, 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)
100106

101107
print('Deleting Subnet 2')
108+
get_subnet_two_response = virtual_network_client.get_subnet(subnet_two_ocid)
102109
virtual_network_client.delete_subnet(subnet_two_ocid)
103-
oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_two_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)
110+
oci.wait_until(virtual_network_client, get_subnet_two_response, 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)
104111

105112
print('Deleting VCN')
113+
get_vcn_response = virtual_network_client.get_vcn(vcn_ocid)
106114
virtual_network_client.delete_vcn(vcn_ocid)
107-
oci.wait_until(virtual_network_client, virtual_network_client.get_vcn(vcn_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)
115+
oci.wait_until(virtual_network_client, get_vcn_response, 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True)

0 commit comments

Comments
 (0)