Skip to content

Commit f61a9a3

Browse files
committed
feat(core): Pull IAM URL from VCAP_SERVICES
1 parent 25c7a7d commit f61a9a3

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

core/src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ public abstract class WatsonService {
101101
public WatsonService(final String name) {
102102
this.name = name;
103103
String iamApiKey = CredentialUtils.getIAMKey(name);
104-
if (iamApiKey != null) {
105-
tokenManager = new IamTokenManager(new IamOptions.Builder().apiKey(iamApiKey).build());
104+
String iamUrl = CredentialUtils.getIAMUrl(name);
105+
if (iamApiKey != null && iamUrl != null) {
106+
IamOptions iamOptions = new IamOptions.Builder()
107+
.apiKey(iamApiKey)
108+
.url(iamUrl)
109+
.build();
110+
tokenManager = new IamTokenManager(iamOptions);
106111
}
107112
apiKey = CredentialUtils.getAPIKey(name);
108113
String url = CredentialUtils.getAPIUrl(name);

core/src/main/java/com/ibm/watson/developer_cloud/util/CredentialUtils.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public String getUsername() {
7777
/** The Constant APIKEY. */
7878
private static final String APIKEY = "apikey";
7979

80+
/** The Constant IAM_API_KEY_NAME. */
81+
private static final String IAM_API_KEY_NAME = "iam_apikey_name";
82+
8083
/** The Constant CREDENTIALS. */
8184
private static final String CREDENTIALS = "credentials";
8285

@@ -101,6 +104,9 @@ public String getUsername() {
101104
/** The Constant URL. */
102105
private static final String URL = "url";
103106

107+
/** The Constant IAM_URL. */
108+
private static final String IAM_URL = "iam_url";
109+
104110
/** The Constant PLAN_EXPERIMENTAL. */
105111
public static final String PLAN_EXPERIMENTAL = "experimental";
106112

@@ -205,7 +211,7 @@ public static String getIAMKey(String serviceName) {
205211
}
206212

207213
final JsonObject credentials = getCredentialsObject(services, serviceName, null);
208-
if (credentials != null && credentials.get(APIKEY) != null && credentials.get("iam_apikey_name") != null) {
214+
if (credentials != null && credentials.get(APIKEY) != null && credentials.get(IAM_API_KEY_NAME) != null) {
209215
return credentials.get(APIKEY).getAsString();
210216
}
211217

@@ -369,6 +375,21 @@ public static String getAPIUrl(String serviceName, String plan) {
369375
return null;
370376
}
371377

378+
public static String getIAMUrl(String serviceName) {
379+
final JsonObject services = getVCAPServices();
380+
381+
if (serviceName == null || services == null) {
382+
return null;
383+
}
384+
385+
final JsonObject credentials = getCredentialsObject(services, serviceName, null);
386+
if (credentials != null && credentials.get(IAM_URL) != null) {
387+
return credentials.get(IAM_URL).getAsString();
388+
}
389+
390+
return null;
391+
}
392+
372393
/**
373394
* Sets the VCAP_SERVICES variable. This is utility variable for testing
374395
*

core/src/test/resources/vcap_services.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"iam_apikey_name": "auto-generated-apikey-111-222-333",
7171
"iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",
7272
"iam_serviceid_crn": "crn:v1:staging:public:iam-identity::a/::serviceid:ServiceID-1234",
73-
"url": "https://gateway.watsonplatform.net/language-translator/api"
73+
"url": "https://gateway.watsonplatform.net/language-translator/api",
74+
"iam_url": "https://iam.ng.bluemix.net/identity/token"
7475
}
7576
}
7677
],

0 commit comments

Comments
 (0)