|
19 | 19 | print('Creating VCN') |
20 | 20 | result = virtual_network_client.create_vcn(oci.core.models.CreateVcnDetails(cidr_block='10.0.0.0/16', display_name='WaitForResourceExampleVcn', compartment_id=compartment_id)) |
21 | 21 | 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) |
24 | 25 |
|
25 | 26 | # This creates a subnet in the VCN and waits until the subnet's lifecycle state is AVAILABLE |
26 | 27 | print('Creating Subnet 1') |
|
34 | 35 | ) |
35 | 36 | ) |
36 | 37 | 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) |
39 | 41 |
|
40 | 42 | # 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 |
41 | 43 | # 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 | 56 | ) |
55 | 57 | ) |
56 | 58 | 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) |
59 | 62 |
|
60 | 63 | # 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 |
61 | 64 | # returns a work request and it is the work request whose state we should wait on (we wait until it has succeeded) |
|
82 | 85 | ) |
83 | 86 | result = load_balancer_client.create_load_balancer(create_load_balancer_details) |
84 | 87 | 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) |
87 | 91 | load_balancer_ocid = get_work_request_response.data.load_balancer_id |
88 | 92 |
|
89 | 93 | # 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 |
90 | 94 | # 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 |
91 | 95 | # only be useful for delete/terminate scenarios and its normal default is False. |
92 | 96 | print('Deleting Load Balancer') |
| 97 | +get_load_balancer_response = load_balancer_client.get_load_balancer(load_balancer_ocid) |
93 | 98 | load_balancer_client.delete_backend_set(load_balancer_ocid, 'WaitExampleBackSet') |
94 | 99 | 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) |
96 | 101 |
|
97 | 102 | print('Deleting Subnet 1') |
| 103 | +get_subnet_response = virtual_network_client.get_subnet(subnet_ocid) |
98 | 104 | 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) |
100 | 106 |
|
101 | 107 | print('Deleting Subnet 2') |
| 108 | +get_subnet_two_response = virtual_network_client.get_subnet(subnet_two_ocid) |
102 | 109 | 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) |
104 | 111 |
|
105 | 112 | print('Deleting VCN') |
| 113 | +get_vcn_response = virtual_network_client.get_vcn(vcn_ocid) |
106 | 114 | 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