I ended up using project-level tenant property (e.g. ${#Project#tenant}) to parametrize the endpoints as suggested by KarelHusa Thank you!
If anyone is interested, I also created a groovy script to grab and parse the token. Note, I'm a beginner at groovy script, so there may be an easier way to do this, but it worked for me 🙂
/****
@author: Scott Rixon
Class to get the bearer token from the oauth/accessToken endpoint
****/
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.client.AuthCache
import org.apache.http.impl.client.BasicAuthCache
import org.apache.http.impl.auth.BasicScheme
import org.apache.http.client.protocol.HttpClientContext
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.HttpEntity
import org.apache.http.StatusLine
import org.apache.http.client.HttpResponseException
import org.apache.http.client.ClientProtocolException
import org.apache.http.util.EntityUtils
import groovy.json.JsonSlurper
public class Bearer_Token
{
String env
String username
String password
String token_type
String access_token
String url
String uriPath
String response
String fullUrl
String host
public Bearer_Token(String env){
env = env
username = ""
password = ""
host = ""
url = "https://${host}"
uriPath = "/oauth/accessToken"
fullUrl = "${url}${uriPath}"
}
public send_request(log){
// Create Client
CloseableHttpClient httpclient = HttpClients.createDefault();
// Create Host
HttpHost targetHost = new HttpHost(host, 443, "https");
// Create Credential provider
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
// Set Credentials
credentialsPovider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(username, password));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsPovider);
context.setAuthCache(authCache);
// Create HTTPGet
HttpGet httpget = new HttpGet(fullUrl);
// Add headers
httpget.addHeader("Accept", "application/json");
httpget.addHeader("Content-Type", "application/json");
httpget.addHeader("Accept-Charset", "utf-8");
// Execute command
CloseableHttpResponse response = httpclient.execute(
targetHost, httpget, context);
// Get Status
StatusLine statusLine = response.getStatusLine()
// Get Entity
HttpEntity entity = response.getEntity();
// Throw exception if response code isn't successful
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(
statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
if (entity == null) {
throw new ClientProtocolException("Response contains no content");
}
String content = EntityUtils.toString(entity);
// Parse JSON
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(content)
def jsonResonse = "${object['token_type']} ${object['access_token']}"
log.info(jsonResonse)
response.close();
return jsonResonse
}
}
// Get Environment
String node = project.getPropertyValue("node")
String tenant = project.getPropertyValue("tenant")
if(!node){
log.error("No node is configured in the Project Properties")
}
if(!tenant){
log.error("No tenant is configured in the Project Properties")
}
// Create Environment string
String env = "${node}${tenant}"
log.info("The environment is: ${env}")
// Get Bearer token
def response
def t = new Bearer_Token(env)
try{
response = t.send_request(log)
}
catch(err){
log.error(err)
}
// Set bearer token
project.setPropertyValue("bearer_token", response)