|
| 1 | +# Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This script provides an example on how to use waiters in the Python SDK to block/wait until a resource (e.g. an instance, a VCN) |
| 4 | +# reaches a certain state. |
| 5 | + |
| 6 | +import oci |
| 7 | + |
| 8 | +# Default config file and profile |
| 9 | +config = oci.config.from_file() |
| 10 | +compartment_id = '<Your compartment OCID here>' |
| 11 | +availability_domain = '<An availability domain, e.g. crmS:IAD-AD-1, here>' |
| 12 | +second_availability_domain = '<An availability domain, e.g. crmS:IAD-AD-2, here. This should be different to availability_domain>' |
| 13 | + |
| 14 | +virtual_network_client = oci.core.VirtualNetworkClient(config) |
| 15 | +load_balancer_client = oci.load_balancer.LoadBalancerClient(config) |
| 16 | + |
| 17 | +# This creates a VCN and then waits until the VCN's lifecycle state is AVAILABLE |
| 18 | +print('Creating VCN') |
| 19 | +result = virtual_network_client.create_vcn(oci.core.models.CreateVcnDetails(cidr_block='10.0.0.0/16', display_name='WaitForResourceExampleVcn', compartment_id=compartment_id)) |
| 20 | +vcn_ocid = result.data.id |
| 21 | +get_vcn_response = oci.wait_until(virtual_network_client, virtual_network_client.get_vcn(vcn_ocid), 'lifecycle_state', 'AVAILABLE') |
| 22 | +print(get_vcn_response.data) |
| 23 | + |
| 24 | +# This creates a subnet in the VCN and waits until the subnet's lifecycle state is AVAILABLE |
| 25 | +print('Creating Subnet 1') |
| 26 | +result = virtual_network_client.create_subnet( |
| 27 | + oci.core.models.CreateSubnetDetails( |
| 28 | + compartment_id=compartment_id, |
| 29 | + availability_domain=availability_domain, |
| 30 | + display_name='WaitForResourceExampleSubnet', |
| 31 | + vcn_id=vcn_ocid, |
| 32 | + cidr_block='10.0.0.0/24' |
| 33 | + ) |
| 34 | +) |
| 35 | +subnet_ocid = result.data.id |
| 36 | +get_subnet_response = oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_ocid), 'lifecycle_state', 'AVAILABLE') |
| 37 | +print(get_subnet_response.data) |
| 38 | + |
| 39 | +print('Creating Subnet 2') |
| 40 | +result = virtual_network_client.create_subnet( |
| 41 | + oci.core.models.CreateSubnetDetails( |
| 42 | + compartment_id=compartment_id, |
| 43 | + availability_domain=second_availability_domain, |
| 44 | + display_name='WaitForResourceExampleSubnet2', |
| 45 | + vcn_id=vcn_ocid, |
| 46 | + cidr_block='10.0.1.0/24' |
| 47 | + ) |
| 48 | +) |
| 49 | +subnet_two_ocid = result.data.id |
| 50 | +get_subnet_response = oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_two_ocid), 'lifecycle_state', 'AVAILABLE') |
| 51 | +print(get_subnet_response.data) |
| 52 | + |
| 53 | +# 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 |
| 54 | +# returns a work request and it is the work request whose state we should wait on (we wait until it has succeeded) |
| 55 | +print('Creating Load Balancer') |
| 56 | +create_load_balancer_details = oci.load_balancer.models.CreateLoadBalancerDetails( |
| 57 | + compartment_id=compartment_id, |
| 58 | + display_name='WaitForResourceExampleLB', |
| 59 | + shape_name='100Mbps', |
| 60 | + subnet_ids=[subnet_ocid, subnet_two_ocid], |
| 61 | + backend_sets={ |
| 62 | + 'WaitExampleBackSet': oci.load_balancer.models.BackendSetDetails( |
| 63 | + policy='ROUND_ROBIN', |
| 64 | + health_checker=oci.load_balancer.models.HealthCheckerDetails( |
| 65 | + protocol='HTTP', |
| 66 | + url_path='/', |
| 67 | + port=80, |
| 68 | + retries=1, |
| 69 | + timeout_in_millis=100, |
| 70 | + interval_in_millis=1000, |
| 71 | + ), |
| 72 | + session_persistence_configuration=oci.load_balancer.models.SessionPersistenceConfigurationDetails(cookie_name='*', disable_fallback=False) |
| 73 | + ) |
| 74 | + } |
| 75 | +) |
| 76 | +result = load_balancer_client.create_load_balancer(create_load_balancer_details) |
| 77 | +work_request_id = result.headers['opc-work-request-id'] |
| 78 | +get_work_request_response = oci.wait_until(load_balancer_client, load_balancer_client.get_work_request(work_request_id), 'lifecycle_state', 'SUCCEEDED') |
| 79 | +print(get_work_request_response.data) |
| 80 | +load_balancer_ocid = get_work_request_response.data.load_balancer_id |
| 81 | + |
| 82 | +# 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 |
| 83 | +# 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 |
| 84 | +# only be useful for delete/terminate scenarios and its normal default is False. |
| 85 | +print('Deleting Load Balancer') |
| 86 | +load_balancer_client.delete_backend_set(load_balancer_ocid, 'WaitExampleBackSet') |
| 87 | +load_balancer_client.delete_load_balancer(load_balancer_ocid) |
| 88 | +oci.wait_until(load_balancer_client, load_balancer_client.get_load_balancer(load_balancer_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True) |
| 89 | + |
| 90 | +print('Deleting Subnet 1') |
| 91 | +virtual_network_client.delete_subnet(subnet_ocid) |
| 92 | +oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True) |
| 93 | + |
| 94 | +print('Deleting Subnet 2') |
| 95 | +virtual_network_client.delete_subnet(subnet_two_ocid) |
| 96 | +oci.wait_until(virtual_network_client, virtual_network_client.get_subnet(subnet_two_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True) |
| 97 | + |
| 98 | +print('Deleting VCN') |
| 99 | +virtual_network_client.delete_vcn(vcn_ocid) |
| 100 | +oci.wait_until(virtual_network_client, virtual_network_client.get_vcn(vcn_ocid), 'lifecycle_state', 'TERMINATED', succeed_on_not_found=True) |
0 commit comments