forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopySubnetResourcesFromVcnExample.java
More file actions
192 lines (173 loc) · 9.6 KB
/
CopySubnetResourcesFromVcnExample.java
File metadata and controls
192 lines (173 loc) · 9.6 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
/**
* 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 java.util.Collections;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.core.VirtualNetworkClient;
import com.oracle.bmc.core.model.CreateDhcpDetails;
import com.oracle.bmc.core.model.CreateRouteTableDetails;
import com.oracle.bmc.core.model.CreateSecurityListDetails;
import com.oracle.bmc.core.model.DhcpOptions;
import com.oracle.bmc.core.model.RouteTable;
import com.oracle.bmc.core.model.SecurityList;
import com.oracle.bmc.core.model.Subnet;
import com.oracle.bmc.core.model.UpdateSubnetDetails;
import com.oracle.bmc.core.model.Vcn;
import com.oracle.bmc.core.requests.CreateDhcpOptionsRequest;
import com.oracle.bmc.core.requests.CreateRouteTableRequest;
import com.oracle.bmc.core.requests.CreateSecurityListRequest;
import com.oracle.bmc.core.requests.GetDhcpOptionsRequest;
import com.oracle.bmc.core.requests.GetRouteTableRequest;
import com.oracle.bmc.core.requests.GetSecurityListRequest;
import com.oracle.bmc.core.requests.GetSubnetRequest;
import com.oracle.bmc.core.requests.GetVcnRequest;
import com.oracle.bmc.core.requests.UpdateSubnetRequest;
import com.oracle.bmc.core.responses.CreateDhcpOptionsResponse;
import com.oracle.bmc.core.responses.CreateRouteTableResponse;
import com.oracle.bmc.core.responses.CreateSecurityListResponse;
import com.oracle.bmc.core.responses.GetSubnetResponse;
import com.oracle.bmc.core.responses.GetVcnResponse;
import com.oracle.bmc.core.responses.UpdateSubnetResponse;
public class CopySubnetResourcesFromVcnExample {
/**
* If the provided subnet inherited its containing VCN's default route table, DHCP options, and security list,
* then this example will copy those objects and attach the subnet to the newly created versions. This allows you
* to modify them freely without affecting your other subnets.
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO: Fill in these values
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
String subnetId = "ocid1.subnet.FILL THIS IN";
Region region = Region.US_PHOENIX_1;
// 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 provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
try (VirtualNetworkClient vcnClient = new VirtualNetworkClient(provider)) {
vcnClient.setRegion(region);
Subnet subnet = getSubnet(vcnClient, subnetId);
Vcn vcn = getVcn(vcnClient, subnet.getVcnId());
UpdateSubnetDetails.Builder details = UpdateSubnetDetails.builder();
boolean needToUpdate = false;
if (subnet.getRouteTableId().equals(vcn.getDefaultRouteTableId())) {
System.out.println("Copying subnet's route table");
RouteTable newRouteTable = copyRouteTable(vcnClient, vcn.getDefaultRouteTableId());
details.routeTableId(newRouteTable.getId());
needToUpdate = true;
}
if (subnet.getDhcpOptionsId().equals(vcn.getDefaultDhcpOptionsId())) {
System.out.println("Copying subnet's DHCP options");
DhcpOptions newDhcpOptions =
copyDhcpOptions(vcnClient, vcn.getDefaultDhcpOptionsId());
details.dhcpOptionsId(newDhcpOptions.getId());
needToUpdate = true;
}
if (subnet.getSecurityListIds()
.equals(Collections.singletonList(vcn.getDefaultSecurityListId()))) {
System.out.println("Copying subnet's security list");
SecurityList newSecurityList =
copySecurityList(vcnClient, vcn.getDefaultSecurityListId());
details.securityListIds(Collections.singletonList(newSecurityList.getId()));
needToUpdate = true;
}
if (needToUpdate) {
System.out.println("Updating subnet");
subnet = updateSubnet(vcnClient, subnetId, details.build());
System.out.println("Subnet update done!");
} else {
System.out.println("Not updating subnet; none of its settings are non-default");
}
}
}
public static Vcn getVcn(VirtualNetworkClient vcnClient, String vcnId) {
GetVcnResponse response = vcnClient.getVcn(GetVcnRequest.builder().vcnId(vcnId).build());
return response.getVcn();
}
public static Subnet getSubnet(VirtualNetworkClient vcnClient, String subnetId) {
GetSubnetResponse response =
vcnClient.getSubnet(GetSubnetRequest.builder().subnetId(subnetId).build());
return response.getSubnet();
}
public static Subnet updateSubnet(
VirtualNetworkClient vcnClient, String subnetId, UpdateSubnetDetails details) {
UpdateSubnetResponse response =
vcnClient.updateSubnet(
UpdateSubnetRequest.builder()
.subnetId(subnetId)
.updateSubnetDetails(details)
.build());
return response.getSubnet();
}
public static RouteTable copyRouteTable(VirtualNetworkClient vcnClient, String routeTableId) {
RouteTable oldRouteTable =
vcnClient
.getRouteTable(GetRouteTableRequest.builder().rtId(routeTableId).build())
.getRouteTable();
// Note that this does not include all route table parameters, only the required ones.
CreateRouteTableResponse response =
vcnClient.createRouteTable(
CreateRouteTableRequest.builder()
.createRouteTableDetails(
CreateRouteTableDetails.builder()
.compartmentId(oldRouteTable.getCompartmentId())
.vcnId(oldRouteTable.getVcnId())
.routeRules(oldRouteTable.getRouteRules())
.build())
.build());
return response.getRouteTable();
}
public static DhcpOptions copyDhcpOptions(
VirtualNetworkClient vcnClient, String dhcpOptionsId) {
DhcpOptions oldDhcpOptions =
vcnClient
.getDhcpOptions(
GetDhcpOptionsRequest.builder().dhcpId(dhcpOptionsId).build())
.getDhcpOptions();
// Note that this does not include all DHCP options parameters, only the required ones.
CreateDhcpOptionsResponse response =
vcnClient.createDhcpOptions(
CreateDhcpOptionsRequest.builder()
.createDhcpDetails(
CreateDhcpDetails.builder()
.compartmentId(oldDhcpOptions.getCompartmentId())
.vcnId(oldDhcpOptions.getVcnId())
.options(oldDhcpOptions.getOptions())
.build())
.build());
return response.getDhcpOptions();
}
public static SecurityList copySecurityList(
VirtualNetworkClient vcnClient, String securityListId) {
SecurityList oldSecurityList =
vcnClient
.getSecurityList(
GetSecurityListRequest.builder()
.securityListId(securityListId)
.build())
.getSecurityList();
// Note that this does not include all security list parameters, only the required ones.
CreateSecurityListResponse response =
vcnClient.createSecurityList(
CreateSecurityListRequest.builder()
.createSecurityListDetails(
CreateSecurityListDetails.builder()
.compartmentId(oldSecurityList.getCompartmentId())
.vcnId(oldSecurityList.getVcnId())
.ingressSecurityRules(
oldSecurityList.getIngressSecurityRules())
.egressSecurityRules(
oldSecurityList.getEgressSecurityRules())
.build())
.build());
return response.getSecurityList();
}
}