forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListCompartmentsExample.java
More file actions
186 lines (166 loc) · 8.69 KB
/
ListCompartmentsExample.java
File metadata and controls
186 lines (166 loc) · 8.69 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
/**
* 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.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.identity.Identity;
import com.oracle.bmc.identity.IdentityClient;
import com.oracle.bmc.identity.model.Compartment;
import com.oracle.bmc.identity.requests.ListCompartmentsRequest;
import com.oracle.bmc.identity.responses.ListCompartmentsResponse;
import shared.ExampleCompartmentHelper;
public class ListCompartmentsExample {
public static void main(String[] args) throws Exception {
// TODO: Fill in this value
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
// 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);
String compartmentId = provider.getTenantId();
final String tenantId = provider.getTenantId();
Identity identityClient = new IdentityClient(provider);
identityClient.setRegion(Region.US_ASHBURN_1);
/*
*****************************************************************
Setup the compartments like the following tree
Here is the compartment Tree in this Test
Tenancy
|
--- CP-1
|
|
--- CP-2
| |
| --- CP-21
| |
| --- CP-211
|
--- CP-3
|
--- CP-31
*****************************************************************
*/
Compartment cp1 = null;
Compartment cp2 = null;
Compartment cp3 = null;
Compartment cp21 = null;
Compartment cp31 = null;
Compartment cp211 = null;
try {
// Setup the first level compartments (CP-1, CP-2, CP-3)
cp1 = ExampleCompartmentHelper.createCompartment(identityClient, tenantId, "CP-1");
cp2 = ExampleCompartmentHelper.createCompartment(identityClient, tenantId, "CP-2");
cp3 = ExampleCompartmentHelper.createCompartment(identityClient, tenantId, "CP-3");
// If we create/update and then try to use compartments straight away, sometimes we can get a 404. To try and avoid this, the script
// adds a short delay between the compartment management operations
Thread.sleep(10000);
// Setup the second level compartments (CP-21, CP-31)
cp21 = ExampleCompartmentHelper.createCompartment(identityClient, cp2.getId(), "CP-21");
cp31 = ExampleCompartmentHelper.createCompartment(identityClient, cp3.getId(), "CP-31");
Thread.sleep(10000);
// Setup the third level compartments (CP-211)
cp211 =
ExampleCompartmentHelper.createCompartment(
identityClient, cp2.getId(), "CP-211");
// List all compartments within tenancy with Accessible compartment filter
String nextPageToken = null;
System.out.println(
"ListCompartments: with compartmentIdInSubtree == true and AccessLevel==Accessible");
do {
ListCompartmentsResponse response =
identityClient.listCompartments(
ListCompartmentsRequest.builder()
.limit(3)
.compartmentId(compartmentId)
.accessLevel(ListCompartmentsRequest.AccessLevel.Accessible)
.compartmentIdInSubtree(Boolean.TRUE)
.page(nextPageToken)
.build());
for (Compartment compartment : response.getItems()) {
System.out.println(compartment);
}
nextPageToken = response.getOpcNextPage();
} while (nextPageToken != null);
// List all compartments within tenancy without Accessible compartment filter
System.out.println("ListCompartments: with compartmentIdInSubtree == true");
nextPageToken = null;
do {
ListCompartmentsResponse response =
identityClient.listCompartments(
ListCompartmentsRequest.builder()
.limit(3)
.compartmentId(compartmentId)
.compartmentIdInSubtree(Boolean.TRUE)
.page(nextPageToken)
.build());
for (Compartment compartment : response.getItems()) {
System.out.println(compartment);
}
nextPageToken = response.getOpcNextPage();
} while (nextPageToken != null);
// List single level compartments within tenancy with Accessible compartment filter
System.out.println("ListCompartments: with AccessLevel == Accessible");
do {
ListCompartmentsResponse response =
identityClient.listCompartments(
ListCompartmentsRequest.builder()
.limit(3)
.compartmentId(compartmentId)
.accessLevel(ListCompartmentsRequest.AccessLevel.Accessible)
.page(nextPageToken)
.build());
for (Compartment compartment : response.getItems()) {
System.out.println(compartment);
}
nextPageToken = response.getOpcNextPage();
} while (nextPageToken != null);
// List single level compartments within tenancy without Accessible compartment filter
System.out.println("ListCompartments");
do {
ListCompartmentsResponse response =
identityClient.listCompartments(
ListCompartmentsRequest.builder()
.limit(3)
.compartmentId(compartmentId)
.page(nextPageToken)
.build());
for (Compartment compartment : response.getItems()) {
System.out.println(compartment);
}
nextPageToken = response.getOpcNextPage();
} while (nextPageToken != null);
} finally {
if (cp211 != null) {
System.out.println("Cleaning up Compartment: " + cp211.getName());
ExampleCompartmentHelper.deleteCompartment(identityClient, cp211);
}
if (cp31 != null) {
System.out.println("Cleaning up Compartment: " + cp31.getName());
ExampleCompartmentHelper.deleteCompartment(identityClient, cp31);
}
if (cp21 != null) {
System.out.println("Cleaning up Compartment: " + cp21.getName());
ExampleCompartmentHelper.deleteCompartment(identityClient, cp21);
}
if (cp3 != null) {
System.out.println("Cleaning up Compartment: " + cp3.getName());
ExampleCompartmentHelper.deleteCompartment(identityClient, cp3);
}
if (cp2 != null) {
System.out.println("Cleaning up Compartment: " + cp2.getName());
ExampleCompartmentHelper.deleteCompartment(identityClient, cp2);
}
if (cp1 != null) {
System.out.println("Cleaning up Compartment: " + cp1.getName());
ExampleCompartmentHelper.deleteCompartment(identityClient, cp1);
}
}
}
}