To run the example, create a Code Engine project from the Console or Code Engine CLI. Next, from the root directory, set the following environment variables, build the project, and execute the IbmCloudCodeEngineExamples
class:
export CE_API_KEY=<Your IBM Cloud API key>
export CE_PROJECT_ID=<Your Code Engine project ID>
export CE_PROJECT_REGION=<The region (e.g. 'us-south') of your Code Engine project>
mvn package
java -jar ./modules/examples/target/code-engine-examples-1.0.0-SNAPSHOT.jar
IamAuthenticator authenticator = new IamAuthenticator(ceApiKey);
authenticator.setClientIdAndSecret("bx", "bx");
IbmCloudCodeEngine ceClient = new IbmCloudCodeEngine("Code Engine Client", authenticator);
ceClient.setServiceUrl("https://api." + System.getenv("CE_PROJECT_REGION") + ".codeengine.cloud.ibm.com/api/v1");
This example uses Java's HttpURLConnection
, but you may use the HTTP client of your choice.
URL iamUrl = new URL("https://iam.cloud.ibm.com/identity/token?"
+ "grant_type=urn:ibm:params:oauth:grant-type:apikey&"
+ "response_type=delegated_refresh_token&"
+ "receiver_client_ids=ce&"
+ "delegated_refresh_token_expiry=3600&"
+ "apikey="
+ System.getenv("CE_API_KEY"));
HttpURLConnection iamConnection = (HttpURLConnection) iamUrl.openConnection();
iamConnection.setRequestMethod("POST");
iamConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
BufferedReader iamInput = new BufferedReader(new InputStreamReader(iamConnection.getInputStream()));
String iamResponse = "";
String iamInputLine = "";
while ((iamInputLine = iamInput.readLine()) != null) {
iamResponse = iamResponse + iamInputLine;
}
iamInput.close();
JSONObject iamJson = new JSONObject(iamResponse);
String delegatedRefreshToken = iamJson.getString("delegated_refresh_token");
GetKubeconfigOptions options = new GetKubeconfigOptions.Builder()
.id(System.getenv("CE_PROJECT_ID"))
.xDelegatedRefreshToken(delegatedRefreshToken)
.build();
Response<String> kubeConfigResponse = ceClient.getKubeconfig(options).execute();
The /namespaces/{id}/config
endpoint function, listKubeconfig()
, is deprecated, and will be removed before Code Engine is out of Beta. Please use the getKubeconfig()
function, demonstrated in the example above.