|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | +# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. |
| 4 | + |
| 5 | +# This example creates a new loadbalancer with SSL cipher suites. After that it creates a new listener with the SSL configuration and updates the backend set with it. Finally it updates the loadbalancer cipher suites and deletes it in the end. |
| 6 | + |
| 7 | +import oci |
| 8 | +import argparse |
| 9 | + |
| 10 | +# ---------- parse arguments |
| 11 | +parser = argparse.ArgumentParser() |
| 12 | +parser.add_argument('--compartment-id', |
| 13 | + help='compartment ID in which to perform the operation', |
| 14 | + required=True |
| 15 | + ) |
| 16 | +parser.add_argument('--subnet-id', |
| 17 | + help='subnet ID in which the load balancer will be created', |
| 18 | + required=True |
| 19 | + ) |
| 20 | +parser.add_argument('--display-name', |
| 21 | + help='display name for the load balancer to be created', |
| 22 | + required=False, |
| 23 | + default='python-sdk-create-lb-example' |
| 24 | + ) |
| 25 | +parser.add_argument('--shape-name', |
| 26 | + help='shape name of the load balancer to be created', |
| 27 | + required=False, |
| 28 | + default='100Mbps' |
| 29 | + ) |
| 30 | +args = parser.parse_args() |
| 31 | + |
| 32 | +# ---------- certificate keys |
| 33 | +cert_private_key = """-----BEGIN RSA PRIVATE KEY-----### the content of your private key ###-----END RSA PRIVATE KEY-----""" |
| 34 | +cert_public_key = """-----BEGIN CERTIFICATE-----### the content of your certificate ###-----END CERTIFICATE-----""" |
| 35 | +cert_password = "password of your certificate" |
| 36 | + |
| 37 | +# ---------- assign provided arguments |
| 38 | +compartment_id = args.compartment_id |
| 39 | +subnet_id = args.subnet_id |
| 40 | +display_name = args.display_name |
| 41 | +shape_name = args.shape_name |
| 42 | + |
| 43 | +# ---------- read config from file |
| 44 | +config = oci.config.from_file() |
| 45 | +load_balancer_client = oci.load_balancer.LoadBalancerClient(config) |
| 46 | +load_balancer_client_composite_ops = oci.load_balancer.LoadBalancerClientCompositeOperations(load_balancer_client) |
| 47 | + |
| 48 | +print('Create new Load Balancer') |
| 49 | +print('\n================================\n') |
| 50 | +# ---------- create load balancer |
| 51 | +load_balancer = load_balancer_client_composite_ops.create_load_balancer_and_wait_for_state( |
| 52 | + oci.load_balancer.models.CreateLoadBalancerDetails( |
| 53 | + compartment_id=compartment_id, |
| 54 | + display_name=display_name, |
| 55 | + shape_name=shape_name, |
| 56 | + subnet_ids=[subnet_id], |
| 57 | + backend_sets={ |
| 58 | + 'backend1': oci.load_balancer.models.BackendSetDetails( |
| 59 | + policy='ROUND_ROBIN', |
| 60 | + health_checker=oci.load_balancer.models.HealthCheckerDetails( |
| 61 | + protocol='HTTP', |
| 62 | + url_path='/', |
| 63 | + port=80, |
| 64 | + retries=1, |
| 65 | + timeout_in_millis=100, |
| 66 | + interval_in_millis=1000 |
| 67 | + ), |
| 68 | + session_persistence_configuration=oci.load_balancer.models.SessionPersistenceConfigurationDetails( |
| 69 | + cookie_name='*', |
| 70 | + disable_fallback=False |
| 71 | + ) |
| 72 | + ), |
| 73 | + 'backend2': oci.load_balancer.models.BackendSetDetails( |
| 74 | + policy='ROUND_ROBIN', |
| 75 | + health_checker=oci.load_balancer.models.HealthCheckerDetails( |
| 76 | + protocol='HTTP', |
| 77 | + url_path='/testurl2', |
| 78 | + port=80, |
| 79 | + retries=1, |
| 80 | + timeout_in_millis=100, |
| 81 | + interval_in_millis=1000 |
| 82 | + ) |
| 83 | + ) |
| 84 | + }, |
| 85 | + ssl_cipher_suites={ |
| 86 | + 'test-suite': oci.load_balancer.models.SSLCipherSuiteDetails( |
| 87 | + name='test-suite', |
| 88 | + ciphers=["ECDHE-RSA-AES256-GCM-SHA384", "ECDHE-ECDSA-AES256-GCM-SHA384", "ECDHE-RSA-AES128-GCM-SHA256"] |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + ), |
| 93 | + [oci.load_balancer.models.WorkRequest.LIFECYCLE_STATE_SUCCEEDED] |
| 94 | +) |
| 95 | + |
| 96 | +load_balancer_ocid = load_balancer.data.id |
| 97 | + |
| 98 | +print('Created Load Balancer %s' % (load_balancer_ocid)) |
| 99 | +print('\n================================\n') |
| 100 | + |
| 101 | +print('Create new certificate') |
| 102 | +print('\n================================\n') |
| 103 | +load_balancer_client_composite_ops.create_certificate_and_wait_for_state( |
| 104 | + oci.load_balancer.models.CreateCertificateDetails( |
| 105 | + certificate_name='example-certificate', |
| 106 | + public_certificate=cert_public_key, |
| 107 | + passphrase=cert_password, |
| 108 | + private_key=cert_private_key, |
| 109 | + ca_certificate=cert_public_key |
| 110 | + ), |
| 111 | + load_balancer_ocid, |
| 112 | + wait_for_states=[oci.load_balancer.models.WorkRequest.LIFECYCLE_STATE_SUCCEEDED] |
| 113 | +) |
| 114 | + |
| 115 | +print('Create new listener with ssl configuration') |
| 116 | +print('\n================================\n') |
| 117 | +load_balancer_client_composite_ops.create_listener_and_wait_for_state( |
| 118 | + oci.load_balancer.models.CreateListenerDetails( |
| 119 | + name='listener1', |
| 120 | + default_backend_set_name='backend1', |
| 121 | + port=8080, |
| 122 | + protocol='HTTP', |
| 123 | + ssl_configuration=oci.load_balancer.models.SSLConfigurationDetails( |
| 124 | + certificate_name='example-certificate', |
| 125 | + cipher_suite_name='test-suite', |
| 126 | + protocols=["TLSv1.1", "TLSv1.2"], |
| 127 | + server_order_preference="ENABLED", |
| 128 | + verify_peer_certificate=True |
| 129 | + ) |
| 130 | + ), |
| 131 | + load_balancer_ocid, |
| 132 | + wait_for_states=[oci.load_balancer.models.WorkRequest.LIFECYCLE_STATE_SUCCEEDED] |
| 133 | +) |
| 134 | + |
| 135 | +print('Update backend sets with sslconfig') |
| 136 | +print('\n================================\n') |
| 137 | +load_balancer_client_composite_ops.update_backend_set_and_wait_for_state( |
| 138 | + oci.load_balancer.models.UpdateBackendSetDetails( |
| 139 | + policy='ROUND_ROBIN', |
| 140 | + health_checker=oci.load_balancer.models.HealthCheckerDetails( |
| 141 | + protocol='HTTP', |
| 142 | + url_path='/testurl2', |
| 143 | + port=80, |
| 144 | + retries=1, |
| 145 | + timeout_in_millis=100, |
| 146 | + interval_in_millis=1000 |
| 147 | + ), |
| 148 | + ssl_configuration=oci.load_balancer.models.SSLConfigurationDetails( |
| 149 | + certificate_name='example-certificate', |
| 150 | + cipher_suite_name='test-suite', |
| 151 | + protocols=["TLSv1.1"], |
| 152 | + verify_peer_certificate=False |
| 153 | + ), |
| 154 | + backends=[] |
| 155 | + ), |
| 156 | + load_balancer_ocid, |
| 157 | + 'backend1', |
| 158 | + wait_for_states=[oci.load_balancer.models.WorkRequest.LIFECYCLE_STATE_SUCCEEDED] |
| 159 | +) |
| 160 | + |
| 161 | +print('Update ssl cipher suite') |
| 162 | +print('\n================================\n') |
| 163 | +load_balancer_client_composite_ops.update_ssl_cipher_suite_and_wait_for_state( |
| 164 | + oci.load_balancer.models.UpdateSSLCipherSuiteDetails( |
| 165 | + ciphers=["DHE-DSS-AES256-SHA256", "CAMELLIA256-SHA"] |
| 166 | + ), |
| 167 | + load_balancer_ocid, |
| 168 | + 'test-suite', |
| 169 | + wait_for_states=[oci.load_balancer.models.WorkRequest.LIFECYCLE_STATE_SUCCEEDED] |
| 170 | +) |
| 171 | + |
| 172 | +print("Attempting to delete load balancer {}".format(load_balancer_ocid)) |
| 173 | +print('\n================================\n') |
| 174 | +load_balancer_client_composite_ops.delete_load_balancer_and_wait_for_state( |
| 175 | + load_balancer_ocid, |
| 176 | + wait_for_states=[oci.load_balancer.models.WorkRequest.LIFECYCLE_STATE_SUCCEEDED]) |
| 177 | +print('Deleted Load Balancer') |
0 commit comments