forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupDestinationExample.java
More file actions
269 lines (235 loc) · 11.5 KB
/
BackupDestinationExample.java
File metadata and controls
269 lines (235 loc) · 11.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
/**
* 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.DatabaseWaiters;
import com.oracle.bmc.database.model.CreateBackupDestinationDetails;
import com.oracle.bmc.database.model.CreateNFSBackupDestinationDetails;
import com.oracle.bmc.database.model.CreateRecoveryApplianceBackupDestinationDetails;
import com.oracle.bmc.database.model.UpdateBackupDestinationDetails;
import com.oracle.bmc.database.requests.CreateBackupDestinationRequest;
import com.oracle.bmc.database.requests.DeleteBackupDestinationRequest;
import com.oracle.bmc.database.requests.GetBackupDestinationRequest;
import com.oracle.bmc.database.requests.UpdateBackupDestinationRequest;
import com.oracle.bmc.database.responses.CreateBackupDestinationResponse;
import com.oracle.bmc.database.responses.DeleteBackupDestinationResponse;
import com.oracle.bmc.database.responses.GetBackupDestinationResponse;
import com.oracle.bmc.database.responses.UpdateBackupDestinationResponse;
import java.util.*;
import java.util.stream.Collectors;
/**
* This class provides an example of CRUD(Create, read, update, delete) operations on BackupDestination using the Java SDK.
* <p></p>
* <ul>
* <li>
* For general documentation refer to :-
* <a href="https://docs.cloud.oracle.com/Content/Database/Concepts/overview.htm">overview</a>
* for more information</li>
* </ul>
*/
public class BackupDestinationExample {
private static final String CONFIG_LOCATION = "~/.oci/config";
private static final String CONFIG_PROFILE = "DEFAULT";
private static final String displayName = "displayName";
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 {
COMPARTMENT_OCID("--compartmentOcid", "The OCID of compartment.", true),
BACKUP_DESTINATION_TYPE("--backupDestinationType", "Type of backup destination", true),
VPC_USER("--vpcUser", "vpcuser for the ZDLRA type of backup destination", false),
LOCAL_MOUNT_PATH(
"--localMountPath", "localMountPath for NFS type of backup destination", false),
CONNECTION_STRING(
"--connectionString",
"connectionString for ZDLRA type of backup destination",
false);
Opts(String argName, String description, boolean required) {
this.argName = argName;
this.description = description;
this.required = required;
}
public final String argName;
public final String description;
public final boolean required;
public String getArgName() {
return argName;
}
public String getDescription() {
return description;
}
public boolean isRequired() {
return required;
}
}
/**
* 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("Missing arguments to be parsed");
}
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>Compartment OCID</li>
* </ul>
*/
public static void main(String[] args) throws Exception {
final Map<Opts, String> argumentMap = parseOpts(args);
final String compartmentId = argumentMap.get(Opts.COMPARTMENT_OCID);
final String backupdestinationType = argumentMap.get(Opts.BACKUP_DESTINATION_TYPE);
final String localMountPath = argumentMap.get(Opts.LOCAL_MOUNT_PATH);
final String vpcUser = argumentMap.getOrDefault(Opts.VPC_USER, null);
final String connectionString = argumentMap.getOrDefault(Opts.CONNECTION_STRING, 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(CONFIG_LOCATION, CONFIG_PROFILE);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
databaseClient = new DatabaseClient(provider);
List<String> vpcusers = new ArrayList<>();
vpcusers.add(vpcUser);
//Create Backup Destination
CreateBackupDestinationDetails backupDestinationDetails =
createBackupDestinationDetails(
compartmentId,
backupdestinationType,
vpcusers,
connectionString,
localMountPath);
CreateBackupDestinationResponse createBackupDestinationResponse =
databaseClient.createBackupDestination(
CreateBackupDestinationRequest.builder()
.createBackupDestinationDetails(backupDestinationDetails)
.build());
com.oracle.bmc.database.model.BackupDestination backupDestination =
createBackupDestinationResponse.getBackupDestination();
//Get Backup Destination
backupDestination =
databaseClient
.getBackupDestination(
GetBackupDestinationRequest.builder()
.backupDestinationId(backupDestination.getId())
.build())
.getBackupDestination();
System.out.println("GET request returned :" + backupDestination);
//Update
UpdateBackupDestinationDetails updateBackupDestinationDetails =
updateBackupDestinationDetails(backupdestinationType, vpcusers);
UpdateBackupDestinationResponse updateResponse =
databaseClient.updateBackupDestination(
UpdateBackupDestinationRequest.builder()
.updateBackupDestinationDetails(updateBackupDestinationDetails)
.backupDestinationId(backupDestination.getId())
.build());
backupDestination = updateResponse.getBackupDestination();
//Delete
DeleteBackupDestinationResponse deleteResponse =
databaseClient.deleteBackupDestination(
DeleteBackupDestinationRequest.builder()
.backupDestinationId(backupDestination.getId())
.build());
DatabaseWaiters waiter = databaseClient.getWaiters();
GetBackupDestinationResponse response =
waiter.forBackupDestination(
GetBackupDestinationRequest.builder()
.backupDestinationId(backupDestination.getId())
.build(),
com.oracle.bmc.database.model.BackupDestination.LifecycleState
.Deleted)
.execute();
databaseClient.close();
}
private static UpdateBackupDestinationDetails updateBackupDestinationDetails(
String type, List<String> vpcUsers) throws Exception {
UpdateBackupDestinationDetails updateBackupDestinationDetails = null;
switch (type) {
case "ZDLRA":
vpcUsers.add("New User");
updateBackupDestinationDetails =
UpdateBackupDestinationDetails.builder().vpcUsers(vpcUsers).build();
break;
case "NFS":
updateBackupDestinationDetails =
UpdateBackupDestinationDetails.builder()
.localMountPointPath("Updated Path")
.build();
break;
default:
throw new Exception("Enter a valid type value from NFS or ZDLRA");
}
return updateBackupDestinationDetails;
}
private static CreateBackupDestinationDetails createBackupDestinationDetails(
String compartmentId,
String type,
List<String> vpcUsers,
String connectionString,
String localMountPath)
throws Exception {
CreateBackupDestinationDetails createBackupDestinationDetails = null;
switch (type) {
case "ZDLRA":
createBackupDestinationDetails =
CreateRecoveryApplianceBackupDestinationDetails.builder()
.compartmentId(compartmentId)
.displayName(displayName)
.vpcUsers(vpcUsers)
.connectionString(connectionString)
.build();
break;
case "NFS":
createBackupDestinationDetails =
CreateNFSBackupDestinationDetails.builder()
.compartmentId(compartmentId)
.displayName(displayName)
.localMountPointPath(localMountPath)
.build();
break;
default:
throw new Exception("Enter a valid type value from NFS or ZDLRA");
}
return createBackupDestinationDetails;
}
}