forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateDbHomeBackupDestinationExample.java
More file actions
239 lines (214 loc) · 10.1 KB
/
CreateDbHomeBackupDestinationExample.java
File metadata and controls
239 lines (214 loc) · 10.1 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
/**
* 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.database.DatabaseClient;
import com.oracle.bmc.database.model.*;
import com.oracle.bmc.database.requests.*;
import com.oracle.bmc.database.responses.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* This class provides a basic example of how to create a DbHome with BackupDestination using the Java SDK. This will cover:
* <p></p>
* <ul>
* <li>Create a BackupDestination for the DbHome to be created.</li>
* <li>
* Creating DBHome with BackupDestination. See:
* <a href="https://docs.cloud.oracle.com/Content/Database/Concepts/overview.htm">overview</a>
* for more information</li>
* </ul>
*/
public class CreateDbHomeBackupDestinationExample {
private static final String CONFIG_LOCATION = "~/.oci/config";
private static final String CONFIG_PROFILE = "DEFAULT";
private static final String DEFAULT_VERSION = "18.0.0.0";
private static DatabaseClient databaseClient = null;
private static final Map<String, Opts> mappings =
Arrays.stream(Opts.values()).collect(Collectors.toMap(Opts::getArgName, o -> o));
/**
* Defines the arguments for this example.
*/
private enum Opts {
VM_CLUSTER_OCID("--vmClusterOcid", "The OCID of a VmCluster.", true, null),
DB_NAME(
"--dbName",
"A DbName. Generates a value if not specified.",
false,
o -> RandomStringUtils.randomAlphabetic(8)),
DB_UNIQUE_NAME(
"--dbUniqueName",
"A DbUniqueName. Generates a value if not specified.",
false,
dbName -> dbName + "_" + RandomStringUtils.randomAlphabetic(2, 16)),
DB_PASSWORD(
"--dbPassword",
"The admin password for your DB. Generates a value if not specified.",
false,
o ->
RandomStringUtils.random(16, "abcdefgABCDEFG#-_1234567890")
+ RandomStringUtils.random(2, "abcdefg")
+ RandomStringUtils.random(2, "ABCDEFG")
+ RandomStringUtils.random(2, "#-_")
+ RandomStringUtils.random(2, "1234567890")),
DB_VERSION(
"--dbVersion",
String.format(
"The version to use. Defaults to %s if not specified.", DEFAULT_VERSION),
false,
o -> DEFAULT_VERSION),
BACKUP_DESTINATION_OCID(
"--backupDestinationOcid", "The OCID of a backup destination.", true, null),
BACKUP_DESTINATION_TYPE(
"--backupDestinationType", "Type of backup destination", true, null),
VPC_USER("--vpcUser", "vpcuser for the ZDLRA type of backup destination", false, null),
VPC_PASSWORD(
"--vpcPassword",
"vpcPassword for the ZDLRA type of backup destination",
false,
null);
Opts(
String argName,
String description,
boolean required,
Function<Object, String> defaultSupplier) {
this.argName = argName;
this.description = description;
this.required = required;
this.defaultSupplier = defaultSupplier;
}
public final String argName;
public final String description;
public final boolean required;
public final Function<Object, String> defaultSupplier;
public String getArgName() {
return argName;
}
public String getDescription() {
return description;
}
public boolean isRequired() {
return required;
}
public Function<Object, String> getDefaultSupplier() {
return defaultSupplier;
}
}
/**
* A helper method for parsing command line arguments.
*
* @param argv the arguments as passed
* @return a mapping of argument to its value. Arguments may be missing from the map if they were not supplied by
* the user and not required.
*/
private static Map<Opts, String> parseOpts(String[] argv) {
if (argv == null) {
throw new IllegalArgumentException("Arguments passed are null");
}
final Iterable<String> iterable = Arrays.asList(argv);
final Iterator<String> iterator = iterable.iterator();
final Map<Opts, String> argsMap = new HashMap<>();
while (iterator.hasNext()) {
String token = iterator.next();
if (mappings.containsKey(token)) {
if (iterator.hasNext()) {
argsMap.put(mappings.get(token), iterator.next());
} else {
throw new IllegalArgumentException("Missing value for parameter " + token);
}
} else {
throw new IllegalArgumentException("Unknown parameter " + token);
}
}
Arrays.stream(Opts.values())
.filter(Opts::isRequired)
.filter(opt -> !argsMap.containsKey(opt))
.findAny()
.ifPresent(
opts -> {
throw new IllegalArgumentException(
"Missing required parameter " + opts.argName);
});
return argsMap;
}
/**
* The entry point for the example.
*
* @param args Arguments to provide to the example. The following arguments are expected:
* <ul>
* <li>VM-cluster OCID</li>
* <li>Compartment OCID</li>
* <li>Database Name</li>
* <li>Database Unique Name</li>
* <li>Database Password</li>
* <li>DatabaseVersion</li>
* </ul>
*/
public static void main(String[] args) throws Exception {
final Map<Opts, String> argumentMap = parseOpts(args);
final String vmClusterOcid = argumentMap.get(Opts.VM_CLUSTER_OCID);
final String dbName =
argumentMap.getOrDefault(
Opts.DB_NAME, Opts.DB_NAME.getDefaultSupplier().apply(null));
final String dbUniqueName =
argumentMap.getOrDefault(
Opts.DB_UNIQUE_NAME,
Opts.DB_UNIQUE_NAME.getDefaultSupplier().apply(dbName));
final String dbPassword =
argumentMap.getOrDefault(
Opts.DB_PASSWORD, Opts.DB_PASSWORD.getDefaultSupplier().apply(null));
final String version =
argumentMap.getOrDefault(
Opts.DB_VERSION, Opts.DB_VERSION.getDefaultSupplier().apply(null));
final String backupDestinationId = argumentMap.get(Opts.BACKUP_DESTINATION_OCID);
final String backupDestinationType = argumentMap.get(Opts.BACKUP_DESTINATION_TYPE);
final String vpcUser = argumentMap.getOrDefault(Opts.VPC_USER, null);
final String vpcPassword = argumentMap.getOrDefault(Opts.VPC_PASSWORD, null);
// 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);
databaseClient = new DatabaseClient(provider);
final BackupDestinationDetails backupDestinationDetails =
BackupDestinationHelper.backupDestinationDetailsCreater(
backupDestinationType, backupDestinationId, vpcUser, vpcPassword);
List<BackupDestinationDetails> list = new ArrayList<>();
list.add(backupDestinationDetails);
final CreateDatabaseDetails databaseDetails =
CreateDatabaseDetails.builder()
.adminPassword(dbPassword)
.dbName(dbName)
.dbBackupConfig(
DbBackupConfig.builder().backupDestinationDetails(list).build())
.dbUniqueName(dbUniqueName)
.build();
final CreateDbHomeBase details =
CreateDbHomeWithVmClusterIdDetails.builder()
.displayName(RandomStringUtils.randomPrint(4, 96))
.database(databaseDetails)
.vmClusterId(vmClusterOcid)
.dbVersion(version)
.build();
final CreateDbHomeRequest request =
CreateDbHomeRequest.builder().createDbHomeWithDbSystemIdDetails(details).build();
CreateDbHomeResponse response = databaseClient.createDbHome(request);
if (response == null) {
throw new RuntimeException("Response from server was null.");
} else if (response.getDbHome() == null
|| StringUtils.isBlank(response.getDbHome().getId())) {
throw new RuntimeException(
"Response from server did not contain expected data! request id: "
+ response.getOpcRequestId());
} else {
System.out.println(response.getDbHome().getId());
}
}
}