|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | + |
| 4 | +# This sample will create the VCN where the NAT Gateway will be created, and create the NAT Gateway that |
| 5 | +# will be moved to a new compartment. Cleanup of NAT gateway and VCN is performed after completion of |
| 6 | +# the change compartment operation. |
| 7 | +# |
| 8 | +# This script takes the following arguments |
| 9 | +# |
| 10 | +# * The OCID of the compartment where the NAT gateway and related resources will be created |
| 11 | +# * TThe OCID of the compartment where the NAT gateway will be moved to |
| 12 | +# |
| 13 | +# Usage : |
| 14 | +# change_nat_gateway_compartment_example.py --src-compartment-id=<src compartment OCID> |
| 15 | +# --dest-compartment-id=<dest compartment OCID> |
| 16 | +# |
| 17 | +# |
| 18 | + |
| 19 | +import argparse |
| 20 | +import oci |
| 21 | +from oci.core import VirtualNetworkClient |
| 22 | +from oci.core import VirtualNetworkClientCompositeOperations |
| 23 | +from oci.core.models import ChangeNatGatewayCompartmentDetails |
| 24 | +from oci.core.models import CreateNatGatewayDetails |
| 25 | +from oci.core.models import CreateVcnDetails |
| 26 | +from oci.core.models import Vcn |
| 27 | +from oci.core.models import NatGateway |
| 28 | + |
| 29 | +nat_gateway_id = None |
| 30 | +src_compartment_id = None |
| 31 | +dest_compartment_id = None |
| 32 | +virtual_network_client = None |
| 33 | +virtual_network_client_composite_ops = None |
| 34 | +vcn_id = None |
| 35 | + |
| 36 | + |
| 37 | +# Sets up the service client used to perform the operations |
| 38 | +def setup_client(): |
| 39 | + global virtual_network_client |
| 40 | + global virtual_network_client_composite_ops |
| 41 | + |
| 42 | + # read oci config |
| 43 | + config = oci.config.from_file() |
| 44 | + |
| 45 | + # Create Virtual Network Client with configuration |
| 46 | + virtual_network_client = VirtualNetworkClient(config) |
| 47 | + |
| 48 | + # Create Virtual Network Client with configuration for composite operations |
| 49 | + virtual_network_client_composite_ops = VirtualNetworkClientCompositeOperations(virtual_network_client) |
| 50 | + |
| 51 | + |
| 52 | +# Creates VCN and waits for it to become available |
| 53 | +def create_vcn(): |
| 54 | + global virtual_network_client_composite_ops |
| 55 | + global vcn_id |
| 56 | + |
| 57 | + # setup create vcn details |
| 58 | + create_vcn_details = CreateVcnDetails() |
| 59 | + create_vcn_details.cidr_block = '10.0.0.0/16' |
| 60 | + create_vcn_details.compartment_id = src_compartment_id |
| 61 | + create_vcn_details.display_name = 'test_change_nat_gateway' |
| 62 | + |
| 63 | + # create vcn |
| 64 | + response = virtual_network_client_composite_ops.create_vcn_and_wait_for_state(create_vcn_details, wait_for_states=[Vcn.LIFECYCLE_STATE_AVAILABLE]) |
| 65 | + vcn_id = response.data.id |
| 66 | + print("Created Vcn: " + vcn_id) |
| 67 | + print("") |
| 68 | + |
| 69 | + |
| 70 | +# Creates NAT Gateway and waits for it to become available |
| 71 | +def create_nat_gateway(): |
| 72 | + global virtual_network_client_composite_ops |
| 73 | + global nat_gateway_id |
| 74 | + global vcn_id |
| 75 | + |
| 76 | + print("Creating NAT Gateway") |
| 77 | + print("=======================================") |
| 78 | + # setup create nat gateway details |
| 79 | + create_ngw_details = CreateNatGatewayDetails() |
| 80 | + create_ngw_details.vcn_id = vcn_id |
| 81 | + create_ngw_details.compartment_id = src_compartment_id |
| 82 | + create_ngw_details.display_name = 'test_change_nat_gateway' |
| 83 | + |
| 84 | + # create ngw |
| 85 | + response = virtual_network_client_composite_ops.create_nat_gateway_and_wait_for_state(create_ngw_details, wait_for_states=[NatGateway.LIFECYCLE_STATE_AVAILABLE]) |
| 86 | + nat_gateway_id = response.data.id |
| 87 | + |
| 88 | + print("Created NAT Gateway and waited for it to become available %s" % response.data) |
| 89 | + print("") |
| 90 | + print("") |
| 91 | + |
| 92 | + |
| 93 | +# Changes the compartment of the NAT Gateway |
| 94 | +def change_nat_gateway_compartment(): |
| 95 | + global virtual_network_client |
| 96 | + global dest_compartment_id |
| 97 | + global nat_gateway_id |
| 98 | + print("Changing NAT Gateway's compartment") |
| 99 | + print("=======================================") |
| 100 | + # setup change compartment details |
| 101 | + change_compartment_details = ChangeNatGatewayCompartmentDetails() |
| 102 | + change_compartment_details.compartment_id = dest_compartment_id |
| 103 | + |
| 104 | + virtual_network_client.change_nat_gateway_compartment(nat_gateway_id, change_compartment_details) |
| 105 | + changedNatGateway = get_nat_gateway() |
| 106 | + print("NAT Gateway's compartment has been changed: %s" % changedNatGateway.data) |
| 107 | + print("") |
| 108 | + print("") |
| 109 | + |
| 110 | + |
| 111 | +# Gets the NAT Gateway |
| 112 | +def get_nat_gateway(): |
| 113 | + global virtual_network_client |
| 114 | + global nat_gateway_id |
| 115 | + |
| 116 | + response = virtual_network_client.get_nat_gateway(nat_gateway_id) |
| 117 | + return response |
| 118 | + |
| 119 | + |
| 120 | +# Deletes the NAT Gateway and waits for it to become terminated |
| 121 | +def delete_nat_gateway(): |
| 122 | + global virtual_network_client_composite_ops |
| 123 | + global nat_gateway_id |
| 124 | + |
| 125 | + virtual_network_client_composite_ops.delete_nat_gateway_and_wait_for_state(nat_gateway_id, wait_for_states=[NatGateway.LIFECYCLE_STATE_TERMINATED]) |
| 126 | + |
| 127 | + |
| 128 | +# Deletes the VCN and waits for it to become terminated |
| 129 | +def delete_vcn(): |
| 130 | + global virtual_network_client_composite_ops |
| 131 | + global vcn_id |
| 132 | + |
| 133 | + virtual_network_client_composite_ops.delete_vcn_and_wait_for_state(vcn_id, wait_for_states=[Vcn.LIFECYCLE_STATE_TERMINATED]) |
| 134 | + |
| 135 | + |
| 136 | +# Parses the command line |
| 137 | +def parse_command_line(): |
| 138 | + global src_compartment_id |
| 139 | + global dest_compartment_id |
| 140 | + |
| 141 | + # parse arguments |
| 142 | + parser = argparse.ArgumentParser() |
| 143 | + parser.add_argument('--src-compartment-id', |
| 144 | + help='source compartment ID in which to create and change nat gateway from', |
| 145 | + required=True |
| 146 | + ) |
| 147 | + parser.add_argument('--dest-compartment-id', |
| 148 | + help='destination compartment ID where the nat gateway should be moved', |
| 149 | + required=True |
| 150 | + ) |
| 151 | + args = parser.parse_args() |
| 152 | + src_compartment_id = args.src_compartment_id |
| 153 | + dest_compartment_id = args.dest_compartment_id |
| 154 | + |
| 155 | + |
| 156 | +def perform_change_compartment(): |
| 157 | + global nat_gateway_id |
| 158 | + global vcn_id |
| 159 | + |
| 160 | + try: |
| 161 | + print("") |
| 162 | + print("Performing operations to change NAT Gateway compartment from %s to %s" % (src_compartment_id, dest_compartment_id)) |
| 163 | + print("") |
| 164 | + setup_client() |
| 165 | + # A VCN is required to create a NAT Gateway |
| 166 | + create_vcn() |
| 167 | + # Here we demonstrate: |
| 168 | + # |
| 169 | + # - Creating a NAT gateway |
| 170 | + # - Changing the NAT gateway's compartment |
| 171 | + # |
| 172 | + # And we'll delete these resources when we're done |
| 173 | + # |
| 174 | + create_nat_gateway() |
| 175 | + change_nat_gateway_compartment() |
| 176 | + |
| 177 | + except Exception as e: |
| 178 | + print("[ERROR]: Error during change nat gateway example execution : %s" % e) |
| 179 | + finally: |
| 180 | + print("Clean Up") |
| 181 | + print("===========") |
| 182 | + if nat_gateway_id: |
| 183 | + delete_nat_gateway() |
| 184 | + print("Deleted NAT Gateway") |
| 185 | + if vcn_id: |
| 186 | + delete_vcn() |
| 187 | + print("Deleted VCN") |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == '__main__': |
| 191 | + parse_command_line() |
| 192 | + perform_change_compartment() |
0 commit comments