forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeNatGatewayCompartmentExample.java
More file actions
318 lines (286 loc) · 13.5 KB
/
ChangeNatGatewayCompartmentExample.java
File metadata and controls
318 lines (286 loc) · 13.5 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* 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.
*/
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.core.VirtualNetworkClient;
import com.oracle.bmc.core.model.ChangeNatGatewayCompartmentDetails;
import com.oracle.bmc.core.model.CreateVcnDetails;
import com.oracle.bmc.core.model.CreateNatGatewayDetails;
import com.oracle.bmc.core.model.NatGateway;
import com.oracle.bmc.core.model.Vcn;
import com.oracle.bmc.core.requests.ChangeNatGatewayCompartmentRequest;
import com.oracle.bmc.core.requests.CreateVcnRequest;
import com.oracle.bmc.core.requests.CreateNatGatewayRequest;
import com.oracle.bmc.core.requests.DeleteNatGatewayRequest;
import com.oracle.bmc.core.requests.DeleteVcnRequest;
import com.oracle.bmc.core.requests.GetNatGatewayRequest;
import com.oracle.bmc.core.requests.GetVcnRequest;
import com.oracle.bmc.core.responses.CreateNatGatewayResponse;
import com.oracle.bmc.core.responses.CreateVcnResponse;
import com.oracle.bmc.core.responses.GetNatGatewayResponse;
import com.oracle.bmc.core.responses.GetVcnResponse;
/**
* This class provides an example of how to change the compartment of a NAT Gateway in the Java SDK.
*
* This sample will create the VCN where the NAT Gateway will be created, and create the NAT Gateway that
* will be moved to a new compartment. Cleanup of NAT gateway and VCN is performed after completion of
* the change compartment operation.
*
* This example also makes some assumptions about the resources it will create:
* <ul>
* <li>The VCN created by this example will have a display name of java_sdk_natgw_example_vcn</li>
* <li>The VCN will have a private IP CIDR block of 10.0.0.0/16</li>
* <li>
* The configuration file used by service clients will be sourced from the default
* location (~/.oci/config) and the DEFAULT profile will be used
* </li>
* <ul>
*
*/
public class ChangeNatGatewayCompartmentExample {
private static final String VCN_DISPLAY_NAME = "java_sdk_natgw_example_vcn";
private static final String CIDR_BLOCK = "10.0.0.0/16";
private static final String CONFIG_LOCATION = "~/.oci/config";
private static final String CONFIG_PROFILE = "DEFAULT";
/**
* The entry point for the example.
*
* @param args Arguments to provide to the example. The following arguments are expected:
* <ul>
* <li><SRC_COMPARTMENT_ID>The OCID of the compartment where the NAT gateway and related resources will be created</li>
* <li><DEST_COMPARTMENT_ID>The OCID of the compartment where the NAT gateway will be moved to</li>
* </ul>
*/
public static void main(String[] args) throws Exception {
if (args.length != 2) {
throw new IllegalArgumentException(
"This example expects two arguments: the first argument is a source compartment OCID and second argument is the destination compartment ID where the NAT Gateway will be moved to");
}
final String SRC_COMPARTMENT_ID = args[0];
final String DEST_COMPARTMENT_ID = args[1];
final String natGatewayDisplayName = "changeCompartmentNGW";
System.out.println(
String.format(
"Performing operations to change NAT Gateway compartment from %s to %s",
SRC_COMPARTMENT_ID,
DEST_COMPARTMENT_ID));
System.out.println();
if (SRC_COMPARTMENT_ID == null
|| SRC_COMPARTMENT_ID.trim().isEmpty()
|| DEST_COMPARTMENT_ID == null
|| DEST_COMPARTMENT_ID.trim().isEmpty()) {
throw new IllegalStateException(
"Please provide valid src and destination compartment id");
}
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI config file
// "~/.oci/config", and a profile in that config with the name "DEFAULT". Make changes to the following
// line if needed and use ConfigFileReader.parse(configurationFilePath, profile);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider authProvider =
new ConfigFileAuthenticationDetailsProvider(configFile);
final VirtualNetworkClient virtualNetworkClient = new VirtualNetworkClient(authProvider);
Vcn vcn = null;
NatGateway natGateway = null;
try {
//A VCN is required to create a NAT gateway
vcn = createVcn(virtualNetworkClient, SRC_COMPARTMENT_ID);
/*
* Here we demonstrate:
*
* - Creating a NAT gateway
* - Changing the NAT gateway's compartment
*
* And we'll delete these resources when we're done
*/
natGateway =
createNatGateway(
virtualNetworkClient,
SRC_COMPARTMENT_ID,
natGatewayDisplayName,
vcn.getId());
changeNatGatewayCompartment(virtualNetworkClient, natGateway, DEST_COMPARTMENT_ID);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println("Clean Up");
System.out.println("==========");
if (natGateway != null) {
deleteNatGateway(virtualNetworkClient, natGateway);
}
if (vcn != null) {
deleteVcn(virtualNetworkClient, vcn);
}
}
}
/**
* Creates a VCN and waits for it to become available to use.
*
* @param vcnClient the service client to use to create the VCN
* @param compartmentId the OCID of the compartment where the VCN will be created
*
* @return the created VCN
*
* @throws Exception if there is an error waiting on the VCN to become available to use
*/
private static Vcn createVcn(final VirtualNetworkClient vcnClient, final String compartmentId)
throws Exception {
final CreateVcnResponse createVcnResponse =
vcnClient.createVcn(
CreateVcnRequest.builder()
.createVcnDetails(
CreateVcnDetails.builder()
.cidrBlock(CIDR_BLOCK)
.compartmentId(compartmentId)
.displayName(VCN_DISPLAY_NAME)
.build())
.build());
final GetVcnResponse getVcnResponse =
vcnClient
.getWaiters()
.forVcn(
GetVcnRequest.builder()
.vcnId(createVcnResponse.getVcn().getId())
.build(),
Vcn.LifecycleState.Available)
.execute();
Vcn vcn = getVcnResponse.getVcn();
System.out.println("Created Vcn : " + vcn.getId());
System.out.println();
return vcn;
}
/**
* Deletes a VCN and waits for it to be deleted.
*
* @param vcnClient the service client to use to delete the VCN
* @param vcn the VCN to delete
*
* @throws Exception if there is an error waiting on the VCN to be deleted
*/
private static void deleteVcn(final VirtualNetworkClient vcnClient, final Vcn vcn)
throws Exception {
vcnClient.deleteVcn(DeleteVcnRequest.builder().vcnId(vcn.getId()).build());
vcnClient
.getWaiters()
.forVcn(
GetVcnRequest.builder().vcnId(vcn.getId()).build(),
Vcn.LifecycleState.Terminated)
.execute();
System.out.println("Deleted Vcn");
}
/**
* Creates a NAT gateway and waits for it to become available.
*
* @param vcnClient the service client to use to create the NAT Gateway
* @param compartmentId the OCID of the compartment where the NAT Gateway will be created
* @param vcnId the OCID of the VCN where the NAT Gateway will be created
*
* @return the created NAT gateway
*
* @throws Exception if there is an error waiting on the NAT gateway to become available to use
*/
private static NatGateway createNatGateway(
final VirtualNetworkClient vcnClient,
final String compartmentId,
final String displayName,
final String vcnId)
throws Exception {
System.out.println("Creating NAT gateway");
System.out.println("=======================================");
final CreateNatGatewayDetails createDetails =
CreateNatGatewayDetails.builder()
.displayName(displayName)
.compartmentId(compartmentId)
.vcnId(vcnId)
.build();
final CreateNatGatewayResponse createResponse =
vcnClient.createNatGateway(
CreateNatGatewayRequest.builder()
.createNatGatewayDetails(createDetails)
.build());
final GetNatGatewayResponse getNatGatewayResponse =
vcnClient
.getWaiters()
.forNatGateway(
GetNatGatewayRequest.builder()
.natGatewayId(createResponse.getNatGateway().getId())
.build(),
NatGateway.LifecycleState.Available)
.execute();
System.out.println(
"Created NAT Gateway and waited for it to become available: "
+ getNatGatewayResponse.getNatGateway().toString());
System.out.println();
System.out.println();
return getNatGatewayResponse.getNatGateway();
}
/**
* Deletes a NAT gateway and waits for it to be deleted.
*
* @param vcnClient the service client used to communicate with the NAT Gateway service
* @param natGateway the NAT gateway to delete
*
* @throws Exception if there is an error waiting on the NAT gateway to be deleted
*/
private static void deleteNatGateway(
final VirtualNetworkClient vcnClient, final NatGateway natGateway) throws Exception {
vcnClient.deleteNatGateway(
DeleteNatGatewayRequest.builder().natGatewayId(natGateway.getId()).build());
vcnClient
.getWaiters()
.forNatGateway(
GetNatGatewayRequest.builder().natGatewayId(natGateway.getId()).build(),
NatGateway.LifecycleState.Terminated)
.execute();
System.out.println("Deleted NAT Gateway");
}
/**
* Gets a specified NAT gateway.
*
* @param vcnClient the service client used to communicate with the NAT Gateway service
* @param natGateway the NAT gateway to get
*
* @throws Exception if there is an error waiting on the NAT gateway to be retrieved
*/
private static NatGateway getNatGateway(
final VirtualNetworkClient vcnClient, final NatGateway natGateway) throws Exception {
final GetNatGatewayResponse getNatGatewayResponse =
vcnClient.getNatGateway(
GetNatGatewayRequest.builder().natGatewayId(natGateway.getId()).build());
return getNatGatewayResponse.getNatGateway();
}
/**
* Changes the NAT gateway's compartment
*
* @param vcnClient the service client used to communicate with the NAT Gateway service
* @param natGateway the NAT gateway to delete
* @param destinationCompartmentId the destination compartment OCID
*
* @throws Exception if there is an error changing the NAT gateway's compartment
*/
private static void changeNatGatewayCompartment(
final VirtualNetworkClient vcnClient,
final NatGateway natGateway,
final String destinationCompartmentId)
throws Exception {
System.out.println("Changing NAT gateway's compartment");
System.out.println("=======================================");
ChangeNatGatewayCompartmentDetails changeCompartmentDetails =
ChangeNatGatewayCompartmentDetails.builder()
.compartmentId(destinationCompartmentId)
.build();
ChangeNatGatewayCompartmentRequest request =
ChangeNatGatewayCompartmentRequest.builder()
.natGatewayId(natGateway.getId())
.changeNatGatewayCompartmentDetails(changeCompartmentDetails)
.build();
vcnClient.changeNatGatewayCompartment(request);
NatGateway changedNatGateway = getNatGateway(vcnClient, natGateway);
System.out.println(
"NAT Gateway's compartment has changed : " + changedNatGateway.toString());
System.out.println();
System.out.println();
}
}