/
reserved_public_ip_example.py
260 lines (226 loc) · 9.65 KB
/
reserved_public_ip_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# coding: utf-8
# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
# This script provides a basic example on how to work with reserved and ephemeral public IPs in the Python SDK by:
#
# - Creating a reserved public IP, assigning it to a private IP and unassigning it from a private IP
# - Creating an ephemeral private IP
#
# This script accepts three arguments:
# - The first argument is the compartment where we'll create the public IPs and required resources for this script
# - The second argument is the availability domain where we'll create a subnet and instance
# - The third argument is the image ID which we'll use to launch the instance
#
# The following supporting resources will be created by this script:
# - A VCN and subnet
# - An instance (so we can assign public IPs to VNICs on the instance)
# These supporting resources will be deleted at the end of the script
import oci
import sys
def create_vcn_and_subnet(virtual_network, compartment_id, availability_domain):
# Create a VCN
vcn_name = 'python_sdk_test_vcn'
cidr_block = "10.0.0.0/16"
result = virtual_network.create_vcn(
oci.core.models.CreateVcnDetails(
cidr_block=cidr_block,
display_name=vcn_name,
compartment_id=compartment_id
)
)
vcn = oci.wait_until(
virtual_network,
virtual_network.get_vcn(result.data.id),
'lifecycle_state',
'AVAILABLE',
max_wait_seconds=300
).data
print('Created VCN')
# Create a subnet
subnet_name = 'python_sdk_test_subnet1'
subnet_cidr_block1 = "10.0.0.0/24"
result = virtual_network.create_subnet(
oci.core.models.CreateSubnetDetails(
compartment_id=compartment_id,
availability_domain=availability_domain,
display_name=subnet_name,
vcn_id=vcn.id,
cidr_block=subnet_cidr_block1
)
)
subnet = oci.wait_until(
virtual_network,
virtual_network.get_subnet(result.data.id),
'lifecycle_state',
'AVAILABLE',
max_wait_seconds=300
).data
print('Created subnet')
return {'vcn': vcn, 'subnet': subnet}
def delete_vcn_and_subnet(virtual_network, vcn_and_subnet):
composite_virtual_network = oci.core.VirtualNetworkClientCompositeOperations(virtual_network)
vcn = vcn_and_subnet['vcn']
subnet = vcn_and_subnet['subnet']
composite_virtual_network.delete_subnet_and_wait_for_state(
subnet.id,
[oci.core.models.Subnet.LIFECYCLE_STATE_TERMINATED]
)
composite_virtual_network.delete_vcn_and_wait_for_state(
vcn.id,
[oci.core.models.Vcn.LIFECYCLE_STATE_TERMINATED]
)
def launch_instance(compute, compartment_id, subnet, image_id):
create_instance_details = oci.core.models.LaunchInstanceDetails()
create_instance_details.compartment_id = compartment_id
create_instance_details.availability_domain = subnet.availability_domain
create_instance_details.display_name = 'pub_ip_test_instance'
create_instance_details.create_vnic_details = oci.core.models.CreateVnicDetails(
subnet_id=subnet.id,
# We don't assign a public IP here so that we can demonstrate public IP functionality later on
# in the script
assign_public_ip=False
)
create_instance_details.image_id = image_id
create_instance_details.shape = 'VM.Standard1.1'
result = compute.launch_instance(create_instance_details)
instance_ocid = result.data.id
get_instance_response = oci.wait_until(
compute,
compute.get_instance(instance_ocid),
'lifecycle_state',
'RUNNING',
max_wait_seconds=600
)
print('Launched instance')
return get_instance_response.data
def terminate_instance(compute, instance):
compute.terminate_instance(instance.id)
oci.wait_until(
compute,
compute.get_instance(instance.id),
'lifecycle_state',
'TERMINATED',
max_wait_seconds=600,
succeed_on_not_found=True
)
if len(sys.argv) != 4:
raise RuntimeError('This script needs to be provided a compartment ID, an availability domain and an image OCID')
compartment_id = sys.argv[1]
availability_domain = sys.argv[2]
image_id = sys.argv[3]
# Default config file and profile
config = oci.config.from_file()
virtual_network = oci.core.VirtualNetworkClient(config)
compute = oci.core.ComputeClient(config)
vcn_and_subnet = create_vcn_and_subnet(virtual_network, compartment_id, availability_domain)
instance = launch_instance(compute, compartment_id, vcn_and_subnet['subnet'], image_id)
# First we'll create a reserved public IP. This does not have to be assigned to a private IP at create time, although
# it can be by specifying a private_ip_id in CreatePublicIpDetails.
create_public_ip_response = virtual_network.create_public_ip(
oci.core.models.CreatePublicIpDetails(
compartment_id=compartment_id,
display_name='py_sdk_example_res_ip',
lifetime='RESERVED'
)
)
print('Created Public IP: {}'.format(create_public_ip_response.data))
print('\n================================\n')
# A Public IP has a lifecycle state, so we can wait until it is available or assigned to a private IP. If we
# need to wait on multiple states, we can use the evaluate_response parameter on the waiter.
get_public_ip_response = oci.wait_until(
virtual_network,
virtual_network.get_public_ip(create_public_ip_response.data.id),
evaluate_response=lambda r: r.data.lifecycle_state in ['AVAILABLE', 'ASSIGNED']
)
public_ip = get_public_ip_response.data
print('Public IP after waiting: {}'.format(public_ip))
print('\n================================\n')
# We can list public IPs. When doing this, we need to provide a scope - for reserved public IPs the scope is
# REGION. Since listing public IPs is a paginated operation, we can use the functionality in oci.pagination
# to get all results
all_region_scoped_public_ips = oci.pagination.list_call_get_all_results(
virtual_network.list_public_ips,
'REGION',
compartment_id
)
print('All reserved public IPs: {}'.format(all_region_scoped_public_ips.data))
print('\n================================\n')
# We'll now assign the public IP to the private IP from the instance we launched earlier. First we'll need to
# get the ID of the private IP
vnic_attachments = compute.list_vnic_attachments(compartment_id, instance_id=instance.id).data
vnic_id = vnic_attachments[0].vnic_id
private_ip_id = virtual_network.list_private_ips(vnic_id=vnic_id).data[0].id
# Now we can update the public IP to link it to the private IP. Once the update has been requested, we can
# use a waiter to wait until the public IP enters the appropriate state
virtual_network.update_public_ip(
public_ip.id,
oci.core.models.UpdatePublicIpDetails(
private_ip_id=private_ip_id
)
)
updated_public_ip = oci.wait_until(
virtual_network,
virtual_network.get_public_ip(public_ip.id),
evaluate_response=lambda r: r.data.lifecycle_state in ['AVAILABLE', 'ASSIGNED']
)
print('Updated public IP: {}'.format(updated_public_ip.data))
print('\n================================\n')
# We can also unassign the public IP from any private IP by passing an empty string as the private_ip_id
# when doing the update. Note that the states we wait on after the update has been requested
virtual_network.update_public_ip(
public_ip.id,
oci.core.models.UpdatePublicIpDetails(
private_ip_id=''
)
)
updated_public_ip = oci.wait_until(
virtual_network,
virtual_network.get_public_ip(public_ip.id),
evaluate_response=lambda r: r.data.lifecycle_state in ['AVAILABLE', 'UNASSIGNED']
)
# If we are done with the public IP, we can delete it
virtual_network.delete_public_ip(public_ip.id)
print('Deleted reserved public IP')
# We can also create an ephemeral public IP. For this type of public IP we need to assign it to a private IP
# at creation time
create_public_ip_response = virtual_network.create_public_ip(
oci.core.models.CreatePublicIpDetails(
compartment_id=compartment_id,
display_name='py_sdk_example_eph_ip',
lifetime='EPHEMERAL',
private_ip_id=private_ip_id
)
)
print('Created Public IP: {}'.format(create_public_ip_response.data))
print('\n================================\n')
get_public_ip_response = oci.wait_until(
virtual_network,
virtual_network.get_public_ip(create_public_ip_response.data.id),
evaluate_response=lambda r: r.data.lifecycle_state in ['AVAILABLE', 'ASSIGNED']
)
public_ip = get_public_ip_response.data
print('Public IP after waiting: {}'.format(public_ip))
print('\n================================\n')
# We can list public IPs. When doing this, we need to provide a scope - for ephemeral public IPs the scope is
# AVAILABILITY_DOMAIN. We can also provide the availability domain we want to retrieve public IPs for.
# Since listing public IPs is a paginated operation, we can use the functionality in oci.pagination
# to get all results
all_ad_scoped_public_ips = oci.pagination.list_call_get_all_results(
virtual_network.list_public_ips,
'AVAILABILITY_DOMAIN',
compartment_id,
availability_domain=availability_domain
)
print('All ephemeral public IPs in {}: {}'.format(availability_domain, all_ad_scoped_public_ips.data))
print('\n================================\n')
# We can delete an ephemeral public IP, but it will also disappear when we terminate the instance
terminate_instance(compute, instance)
oci.wait_until(
virtual_network,
get_public_ip_response,
'lifecycle_state',
'TERMINATED',
succeed_on_not_found=True
)
print('Terminated instance - ephemeral public IP also deleted')
delete_vcn_and_subnet(virtual_network, vcn_and_subnet)
print('Script finished')