Get Information about a Specific Application Snapshot Sample Code

Example 6-23 Java Sample – getInfoAboutSpecificSnapshots.java

Prerequisites: json.jar

Common Functions: See Common Helper Functions for Java

//
// BEGIN - Get application snapshot details
//
public void getApplicationSnapshotDetails(String snapshotName) throws Exception {
	String urlString = String.format("%s/interop/rest/%s/applicationsnapshots/%s", serverUrl, apiVersion, snapshotName);
	String response = executeRequest(urlString, "GET", null);
	JSONObject json = new JSONObject(response);
	
	int resStatus = json.getInt("status");
	if (resStatus == 0) {
		System.out.println("Application details :");
		JSONArray itemsArray = json.getJSONArray("items");
		JSONObject item = (JSONObject) itemsArray.get(0);
		System.out.println("Application snapshot name : " + item.getString("name"));
		System.out.println("Application snapshot type : " + item.getString("type"));
		System.out.println("Can be exported flag : " + item.getString("canExport"));
		System.out.println("Can be imported flag : " + item.getString("canImport"));
		System.out.println("Can be uploaded flag : " + item.getString("canUpload"));
		System.out.println("Can be downloaded flag : " + item.getString("canDownload"));

		JSONArray linksArray = json.getJSONArray("links");
		JSONObject jObj = null;
		System.out.println("Services details :");
		for(int i=0; i < linksArray.length(); i++){
			jObj = (JSONObject)linksArray.get(i);
			System.out.println("Service :" + jObj.getString("rel"));
			System.out.println("URL :" + jObj.getString("href"));
			System.out.println("Action :" + jObj.getString("action") + "\n");
		}
	}
}
//
// END - Get application snapshot details
//

Example 6-24 cURL Sample – GetInfoAboutSpecificSnapshots.sh

Prerequisites: jq (http://stedolan.github.io/jq/download/linux64/jq)

Common Functions: See Common Helper Functions for cURL

funcGetApplicationSnapshotDetails() {
	url=$SERVER_URL/interop/rest/$API_VERSION/applicationsnapshots/$1
	funcExecuteRequest "GET" $url

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
	if [ $status == 0 ]; then
		echo "Application details :"
		echo "Application snapshot name : " `echo $output | jq '.items[0].name'`
		echo "Application snapshot type : " `echo $output | jq '.items[0].type'`
		echo "Can be exported flag : " `echo $output | jq '.items[0].canExport'`
		echo "Can be imported flag : " `echo $output | jq '.items[0].canImport'`
		echo "Can be uploaded flag : " `echo $output | jq '.items[0].canUpload'`
		echo "Can be downloaded flag : " `echo $output | jq '.items[0].canDownload'`
		count=`echo $output | jq '.links | length'`
		i=0
		echo "Services details :"
		while [ $i -lt $count ]; do
			echo "Service : " `echo $output | jq '.links['$i'].rel'`
			echo "URL :" `echo $output | jq '.links['$i'].href'`
			echo "Action :" `echo $output | jq '.links['$i'].action'`
			echo ""
			i=`expr $i + 1`
		done
	else
		error=`echo $output | jq '.details'`
		echo "Error occurred. " $error
	fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

Example 6-25 Groovy Sample – GetInfoAboutSpecificSnapshots.groovy

Prerequisites: json.jar

Common Functions: See CSS Common Helper Functions for Groovy

def getApplicationSnapshotDetails(applicationSnapshotName) {
	def url;
	try {
		String snapshotName = URLEncoder.encode(applicationSnapshotName, "UTF-8");
		url = new URL(serverUrl + "/interop/rest/" + apiVersion + "/applicationsnapshots/" + snapshotName)
	} catch (MalformedURLException e) {
		println "Malformed URL. Please pass valid URL"
		System.exit(0);
	}
	response = executeRequest(url, "GET", null);
	def object = new JsonSlurper().parseText(response)
	def status = object.status
	if (status == 0 ) {
		println "Application details :"
		println "Application snapshot name : " + object.items[0].name
		println "Application snapshot type : " + object.items[0].type
		println "Can be exported flag : " + object.items[0].canExport
		println "Can be imported flag : " + object.items[0].canImport
		println "Can be uploaded flag : " + object.items[0].canUpload
		println "Can be downloaded flag : " + object.items[0].canDownload
		def links = object.links
		println "Services details :"
		links.each{
			println "Service : " + it.rel
			println "URL : " + it.href
			println "Action : " + it.action + "\n"
		}
	} else {
		println "Error occurred while fetching application snapshot details"
		if (object.details != null)
				println "Error details: " + object.details
	}