-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathLaunchExaDbSystemWithNsgExample.java
249 lines (225 loc) · 11.8 KB
/
LaunchExaDbSystemWithNsgExample.java
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
/**
* Copyright (c) 2016, 2025, 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.Subnet;
import com.oracle.bmc.core.model.Vcn;
import com.oracle.bmc.database.DatabaseClient;
import com.oracle.bmc.database.model.CreateDatabaseDetails;
import com.oracle.bmc.database.model.CreateDbHomeDetails;
import com.oracle.bmc.database.model.DbSystem;
import com.oracle.bmc.database.model.LaunchDbSystemDetails;
import com.oracle.bmc.database.model.UpdateDbSystemDetails;
import com.oracle.bmc.database.requests.GetDbSystemRequest;
import com.oracle.bmc.database.requests.LaunchDbSystemRequest;
import com.oracle.bmc.database.requests.TerminateDbSystemRequest;
import com.oracle.bmc.database.requests.UpdateDbSystemRequest;
import com.oracle.bmc.database.responses.GetDbSystemResponse;
import com.oracle.bmc.database.responses.LaunchDbSystemResponse;
import com.oracle.bmc.database.responses.UpdateDbSystemResponse;
import com.oracle.bmc.waiter.ExponentialBackoffDelayStrategy;
import com.oracle.bmc.waiter.MaxTimeTerminationStrategy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* This class provides a basic example of how to launch a DB system with Exadata shape using the
* Java SDK. This will cover:
*
* <p>
*
* <ul>
* <li>Create a VCN and subnets needed for the DB system and its related resources
* <li>Launch a DB system with Exadata shape and sparse disk group option. See: <a
* href="https://docs.oracle.com/iaas/Content/Database/Concepts/exaoverview.htm">overview</a>
* for more information
* </ul>
*
* <p>Resources created by this class will be removed when this example is done.
*
* <p>This class also makes assumptions on the following database parameters:
*
* <p>
*
* <ul>
* <li>DB system shape
* <li>Core count
* <li>DB edition
* <li>DB version
* <li>CIDR block for VCN
* </ul>
*/
public class LaunchExaDbSystemWithNsgExample {
private static final String CONFIG_LOCATION = "~/.oci/config";
private static final String CONFIG_PROFILE = "DEFAULT";
private static final String DB_SYSTEM_SHAPE = "Exadata.Quarter2.92";
private static final String DB_VERSION = "12.1.0.2";
private static final String DATABASE_ADMIN_PASSWORD = "DBaaS12345_#";
private static DatabaseClient databaseClient = null;
private static final long MAX_WAIT_IN_HOURS = 6;
private static final long DELAY_INTERVAL_IN_MINS = 15;
/**
* The entry point for the example.
*
* @param args Arguments to provide to the example. The following arguments are expected:
* <ul>
* <li>The OCID of the compartment which owns the DB system
* <li>The availability domain where the DB system will be launched
* <li>Sparse DiskGroup option: True, if Sparse Diskgroup is configured for Exadata
* dbsystem, False, if Sparse diskgroup was not configured.
* <li>Database admin password
* <li>Full path to SSH public key file
* </ul>
*/
public static void main(String[] args) throws Exception {
if (args.length != 2) {
throw new IllegalAccessException(
"This example expects 2 arguments: a compartment OCID, availability domain for the VCN ");
}
final String compartmentId = args[0];
final String availabilityDomain = args[1];
final String adminPassword = DATABASE_ADMIN_PASSWORD;
// 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(CONFIG_LOCATION, CONFIG_PROFILE);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
databaseClient = DatabaseClient.builder().build(provider);
final VirtualNetworkClient virtualNetworkClient =
VirtualNetworkClient.builder().build(provider);
Vcn vcn = null;
Subnet subnet = null;
Subnet backupSubnet = null;
String dbSystemId = null;
try {
vcn =
DatabaseExampleHelper.createVcn(
virtualNetworkClient, compartmentId, "10.0.0.0/16");
subnet =
DatabaseExampleHelper.createSubnet(
virtualNetworkClient, vcn, availabilityDomain, "10.0.0.0/24");
backupSubnet =
DatabaseExampleHelper.createSubnet(
virtualNetworkClient, vcn, availabilityDomain, "10.0.1.0/24");
List<String> nsgIds =
DatabaseExampleHelper.createNSGs(
virtualNetworkClient, vcn.getId(), compartmentId);
List<String> backupNetworkNsgIds =
DatabaseExampleHelper.createNSGs(
virtualNetworkClient, vcn.getId(), compartmentId);
LaunchDbSystemDetails launchDbSystemDetails =
LaunchDbSystemDetails.builder()
.availabilityDomain(availabilityDomain)
.compartmentId(compartmentId)
.displayName("testdb")
.hostname("javasdk-host")
.shape(DB_SYSTEM_SHAPE)
.cpuCoreCount(2)
.timeZone("US/Pacific")
.dbHome(
CreateDbHomeDetails.builder()
.dbVersion(DB_VERSION)
.displayName("java sdk example db home")
.database(
CreateDatabaseDetails.builder()
.adminPassword(adminPassword)
.dbName("testdb")
.build())
.build())
.databaseEdition(
LaunchDbSystemDetails.DatabaseEdition
.EnterpriseEditionExtremePerformance)
.subnetId(subnet.getId())
.backupSubnetId(backupSubnet.getId())
.nsgIds(nsgIds)
.backupNetworkNsgIds(backupNetworkNsgIds)
.sshPublicKeys(
Arrays.asList(
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDokoG4lDsCN/E9F7KsaVZmYYKZMdljJwxjwsoyFdTTQop0TH0lMSDiZeZ3w/8wsZ5esR5uiW/WS5RMBxrfy1/z12qHbOP8vTLCZ19X33UDS8yhEarDldmBbeQ5penrjHM54+lLBTKolGfXvszbUP8ohsN/If7nfn2caUNRiE9Ep9T+xIr/ElmSyq9qrBbUdg0ltz3yIbkmv7SwgxW+NZSD2mhmfYJJDWq5QpJqc3lcNSCU/0f1mQGm754qI7wJKGEhSEOJkBvS7YrfDRINtNABaKGIDUCijwp+dl7btRaxBDwiF/+zHrX43ZEypVcbgVnOcxtz3E/CgxRHZ1plfEpd dhshshah@dhshshah-Mac"))
.build();
LaunchDbSystemResponse launchDbSystemResponse =
databaseClient.launchDbSystem(
LaunchDbSystemRequest.builder()
.launchDbSystemDetails(launchDbSystemDetails)
.build());
System.out.println("Launched Exadata DB System");
System.out.println("===========================");
System.out.println(launchDbSystemResponse.getDbSystem().toString());
GetDbSystemResponse getDbSystemResponse =
databaseClient
.getWaiters()
.forDbSystem(
GetDbSystemRequest.builder()
.dbSystemId(
launchDbSystemResponse.getDbSystem().getId())
.build(),
DbSystem.LifecycleState.Provisioning,
new MaxTimeTerminationStrategy(
TimeUnit.HOURS.toMillis(MAX_WAIT_IN_HOURS)),
new ExponentialBackoffDelayStrategy(
TimeUnit.MINUTES.toMillis(DELAY_INTERVAL_IN_MINS)))
.execute();
System.out.println("Exadata DB System is in Provisioning state");
System.out.println("===========================");
System.out.println(getDbSystemResponse.getDbSystem().toString());
System.out.println(
"With sparse disk group option: "
+ getDbSystemResponse.getDbSystem().getSparseDiskgroup());
dbSystemId = getDbSystemResponse.getDbSystem().getId();
System.out.println("Launched DB System : " + dbSystemId);
List<String> updateNsgIds =
DatabaseExampleHelper.createNSGs(
virtualNetworkClient, vcn.getId(), compartmentId);
UpdateDbSystemDetails updateDbSystemDetails =
UpdateDbSystemDetails.builder().nsgIds(updateNsgIds).build();
UpdateDbSystemRequest updateDbSystemRequest =
UpdateDbSystemRequest.builder()
.dbSystemId(dbSystemId)
.updateDbSystemDetails(updateDbSystemDetails)
.build();
UpdateDbSystemResponse updateDbSystemResponse =
databaseClient.updateDbSystem(updateDbSystemRequest);
System.out.println(
"DbSystem : "
+ dbSystemId
+ " updated. "
+ updateDbSystemResponse.getDbSystem().getNsgIds());
} catch (Exception e) {
System.out.println("Exception " + e.getMessage());
} finally {
System.out.println("Begin terminating DbSystems");
terminateDBSystem(dbSystemId);
if (subnet != null) {
DatabaseExampleHelper.deleteSubnet(virtualNetworkClient, subnet);
}
if (backupSubnet != null) {
DatabaseExampleHelper.deleteSubnet(virtualNetworkClient, backupSubnet);
}
if (vcn != null) {
DatabaseExampleHelper.deleteVcn(virtualNetworkClient, vcn);
}
}
}
private static void terminateDBSystem(String dbSystemId) throws Exception {
/*
Once we're done with the DB system, we can terminate it and wait for it to be terminated.
*/
databaseClient.terminateDbSystem(
TerminateDbSystemRequest.builder().dbSystemId(dbSystemId).build());
databaseClient
.getWaiters()
.forDbSystem(
GetDbSystemRequest.builder().dbSystemId(dbSystemId).build(),
DbSystem.LifecycleState.Terminated)
.execute();
System.out.println("Terminated Exadata DB system : " + dbSystemId);
}
}