import groovy.json.JsonSlurper // EPM Instance Variables serverUrl="https://instancename.pbcs.us2.oraclecloud.com" pbcsDomain="a123456" pbcsUserName="user@somedomain.com" pbcsPassword="password" // Setup User Credentials and Authorization Header userCredentials = pbcsDomain + "." + pbcsUserName + ":" + pbcsPassword basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userCredentials.getBytes()) // ***************************** // ** Common Helper Functions ** // ***************************** // Fetch Response Function from the Common Helper Functions def fetchResponse(is) { BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line+"\n"); } br.close(); return sb.toString(); } // Fetch Job Status from Response Function from the Common Helper Functions def fetchJobStatusFromResponse(response) { def object = new JsonSlurper().parseText(response) def status = object.status if (status == -1) return "Processing" else if (status == 0) return "Completed" else return object.detail } // Execute Request Function from the Common Helper Functions def executeRequest(url, requestType, payload, contentType) { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setUseCaches(false); connection.setDoInput(true); connection.setRequestMethod(requestType); connection.setRequestProperty("Content-Type", contentType); connection.setRequestProperty("Authorization", basicAuth); if (payload != null) { OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(payload); writer.flush(); } int statusCode try { statusCode = connection.responseCode; } catch (all) { println "Error connecting to the URL" System.exit(0); } def response if (statusCode == 200 || statusCode == 201) { if (connection.getContentType() != null && !connection.getContentType().startsWith("application/json")) { println "Error occurred in server" // System.exit(0) } InputStream is = connection.getInputStream(); if (is != null) response = fetchResponse(is) } else { println "Error occurred while executing request" println "Response error code : " + statusCode InputStream is = connection.getErrorStream(); if (is != null && connection.getContentType() != null && connection.getContentType().startsWith("application/json")) println fetchJobStatusFromResponse(fetchResponse(is)) // System.exit(0); } connection.disconnect(); return response; } // Get LCM Versions Function from the Common Helper Functions def getLCMVersions() { def url; try { url = new URL(serverUrl + "/interop/rest/") } catch (MalformedURLException e) { println "Malformed URL. Please pass valid URL" // System.exit(0); } response = executeRequest(url, "GET", null, "application/x-www-form-urlencoded"); def object = new JsonSlurper().parseText(response) def status = object.status if (status == 0 ) { def items = object.items println "List of versions :" items.each{ println "Version : " + it.version println "Lifecycle : " + it.lifecycle println "Latest : " + it.latest println "Link : " + it.links[0].href + "\n" } } else { println "Error occurred while listing versions" if (object.details != null) println "Error details: " + object.details } } // List Files Function from the Common Helper Functions def listFiles() { def url; try { url = new URL(serverUrl + "/interop/rest/" + apiVersion + "/applicationsnapshots") } catch (MalformedURLException e) { println "Malformed URL. Please pass valid URL" // System.exit(0); } response = executeRequest(url, "GET", null, "application/x-www-form-urlencoded"); def object = new JsonSlurper().parseText(response) def status = object.status if (status == 0 ) { def items = object.items if (items == null) { println "No files found" } else { println "List of files :" items.each{ println it.name } } } else { println "Error occurred while listing files" if (object.details != null) println "Error details: " + object.details } } // ****************************** // ** Call the Functions Above ** // ****************************** getLCMVersions(); listFiles(); // End null