-
Notifications
You must be signed in to change notification settings - Fork 152
/
NewRegionAndRealmSupportWithoutSDKUpdate.java
87 lines (72 loc) · 3.99 KB
/
NewRegionAndRealmSupportWithoutSDKUpdate.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
/**
* Copyright (c) 2016, 2024, 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.Realm;
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.requests.ListRegionsRequest;
import com.oracle.bmc.identity.responses.ListRegionsResponse;
import java.io.IOException;
/**
* This sample demonstrates how to use the SDK with new regions (and realms) without upgrading the
* SDK.
*
* <p>When using either the {@link ConfigFileAuthenticationDetailsProvider} or the {@link
* com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider}, register the new region
* (and realm) before instantiating the provider. If you call the {@code setRegion} method to target
* the new region, pass in the region object returned from prior {@link Region#register(String,
* Realm)} method invocation.
*/
public class NewRegionAndRealmSupportWithoutSDKUpdate {
public static void main(String args[]) throws IOException {
// Assume a new region us-foo-1 is launched in OC2 realm
// Register the new region
final Region fooRegion = Region.register("us-foo-1", Realm.OC2);
// Create the client
Identity identityClient = createIdentityClient();
// If the config file contains a value for region, then it will be picked up automatically
// by the SDK,
// else set it up manually by calling setRegion
System.out.println("Setting region to " + fooRegion.getRegionId());
identityClient.setRegion(fooRegion);
// Use the client to make calls to the endpoint for the new region
listRegions(identityClient);
// Now, assume a new region us-bar-1 is launched in OCX realm
// (having secondLevelDomain oracle-baz-cloud.com)
// Register the new region and realm
final Region barRegion =
Region.register("us-bar-1", Realm.register("ocx", "oracle-baz-cloud.com"));
// Call setRegion to use the endpoint in the new region
System.out.println("Setting region to " + barRegion.getRegionId());
identityClient.setRegion(barRegion);
// The client now makes calls to the endpoint for the new region
listRegions(identityClient);
// If you use InstancePrincipals, register the new region (and realm) before initializing
// the
// InstancePrincipalsAuthenticationDetailsProvider.
}
private static Identity createIdentityClient() throws IOException {
final String configurationFilePath = "~/.oci/config";
final String configurationProfile = "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 authenticationDetailsProvider =
new ConfigFileAuthenticationDetailsProvider(configFile);
return IdentityClient.builder().build(authenticationDetailsProvider);
}
private static void listRegions(final Identity identityClient) {
System.out.println("Querying for list of regions");
final ListRegionsResponse response =
identityClient.listRegions(ListRegionsRequest.builder().build());
System.out.println("List of regions: " + response.getItems());
}
}