forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeInstanceCompartmentExample.java
More file actions
125 lines (104 loc) · 5.99 KB
/
ChangeInstanceCompartmentExample.java
File metadata and controls
125 lines (104 loc) · 5.99 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
/**
* 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.ComputeClient;
import com.oracle.bmc.core.model.ChangeInstanceCompartmentDetails;
import com.oracle.bmc.core.model.Instance;
import com.oracle.bmc.core.model.Instance.LifecycleState;
import com.oracle.bmc.core.requests.ChangeInstanceCompartmentRequest;
import com.oracle.bmc.core.requests.GetInstanceRequest;
import com.oracle.bmc.core.responses.ChangeInstanceCompartmentResponse;
import com.oracle.bmc.core.responses.GetInstanceResponse;
import com.oracle.bmc.workrequests.WorkRequestClient;
import com.oracle.bmc.workrequests.WorkRequestWaiters;
import com.oracle.bmc.workrequests.model.WorkRequest;
import com.oracle.bmc.workrequests.requests.GetWorkRequestRequest;
import com.oracle.bmc.workrequests.responses.GetWorkRequestResponse;
/**
* This example shows how to change the compartment in which an instance lives.
* It makes use of the Work Request service to know that this operation has
* completed and whether it was successful.
*
* Requirements for running this example:
* - Java SDK 1.4.4 or later (you can check this by running oci --version)
* - An existing, functional CLI configuration. If it is not in the
* default location, you will need to change the code near the beginning
* of "main" to match your setup.
*
* Usage: ChangeInstanceCompartmentExample instanceid compartmentid
*/
public class ChangeInstanceCompartmentExample {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage: ChangeInstanceCompartment instance-id new-compartment-id");
return;
}
// TODO: Fill in these values if they do not match your configuration
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
String instanceId = args[0];
String newCompartmentId = args[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 (ComputeClient computeClient = new ComputeClient(provider);
WorkRequestClient workRequestsClient = new WorkRequestClient(provider)) {
// First get the instance Id so we know what state it is in.
// this will help us know when the move is complete.
GetInstanceRequest getRequest =
GetInstanceRequest.builder().instanceId(instanceId).build();
GetInstanceResponse getResponse = computeClient.getInstance(getRequest);
// This will help is ensure that the instance doesn't change between now
// and the move call
String eTag = getResponse.getEtag();
Instance instance = getResponse.getInstance();
String oldCompartment = instance.getCompartmentId();
LifecycleState state = instance.getLifecycleState();
System.out.println("About to move instance!");
System.out.println("\tCurrent compartment = " + oldCompartment);
System.out.println("\tNew compartment = " + newCompartmentId);
System.out.println("\tLifecycle state = " + state);
// We have everything we need to know. Let's move it!
ChangeInstanceCompartmentDetails details =
ChangeInstanceCompartmentDetails.builder()
.compartmentId(newCompartmentId)
.build();
ChangeInstanceCompartmentRequest changeRequest =
ChangeInstanceCompartmentRequest.builder()
.instanceId(instanceId)
.ifMatch(eTag)
.changeInstanceCompartmentDetails(details)
.build();
ChangeInstanceCompartmentResponse changeResponse =
computeClient.changeInstanceCompartment(changeRequest);
String workRequestId = changeResponse.getOpcWorkRequestId();
// We can use the work request Id from the above request to
// wait for completion. Once it completes we'll know if it succeeded or failed.
System.out.println(
"Change Instance Compartment initiated. Waiting for the move to complete.");
System.out.println("Work request ID is " + workRequestId);
WorkRequestWaiters waiter = workRequestsClient.getWaiters();
GetWorkRequestResponse response =
waiter.forWorkRequest(
GetWorkRequestRequest.builder()
.workRequestId(workRequestId)
.build())
.execute();
System.out.println("Finished waiting for instanceID " + instanceId);
// Now let's check the response to see if the move was successful.
WorkRequest wr = response.getWorkRequest();
if (wr.getStatus() == WorkRequest.Status.Succeeded) {
System.out.println("Move was successful!");
} else {
System.out.println("Move was not successful. Status is " + wr.getStatus());
}
}
}
}