vCO Workflow Script to Backup Application Blueprints from vFabric Application Director v5.2 – Part 1

The following script enables backing up Application Blueprints from vFabric Application Director v5.2 – Part 1

 

 

objCustomActions.EMailMessage("<h2>vFabric Application Director</h2><p>Reported The Application Backup Has Started.</p><h3></h3><p></p><h4></h4><p></p>");

System.log("==================================================");

var arrApplications;
	arrApplications = new Array();

var intPage;
	intPage = 0;

var intPageSize;
	intPageSize = 100;

var objRESTOperation;
	objRESTOperation = new RESTOperation("RESTOperation");
	objRESTOperation.defaultContentType = "application/json";
	objRESTOperation.method = "GET";
	objRESTOperation.urlTemplate = "/darwin/api/1.0/applications/page/" + intPage + "/page-size/" + intPageSize;
	objRESTOperation.host = objRESTHostvFAD;

var objRESTRequest;
	objRESTRequest = objRESTOperation.createRequest([intPage, intPageSize], null);
	objRESTRequest.contentType = "application/json";
	objRESTRequest.setHeader("Accept","application/json");
	if ( strVirtualDataCenter == "Specific" )
	{
		objRESTRequest.setHeader("Authorization", "Basic " + strPassWordSpecific);
	}
	else
	{
		objRESTRequest.setHeader("Authorization", "Basic " + strPassWordDefault);
	}

var objRESTResponse;
	objRESTResponse = objRESTRequest.execute();

if ( objRESTResponse.statusCode == 200 )
{
	var strContentAsStringApplication;
		strContentAsStringApplication = objRESTResponse.contentAsString;

	var objJSONApplication;
		objJSONApplication = JSON.parse(strContentAsStringApplication);

	if ( objJSONApplication.errors == false )
	{
		var arrJSONResultsApplication;
			arrJSONResultsApplication = objJSONApplication.results;

		for ( var i = 0; i < arrJSONResultsApplication.length; i++ )
		{
			var objJSONResultApplication;
				objJSONResultApplication = arrJSONResultsApplication[i];

			var intApplicationID;
				intApplicationID = objJSONResultApplication.id;

			var strApplicationName;
				strApplicationName = objJSONResultApplication.name;

			var objRESTOperationApplicationVersion;
				objRESTOperationApplicationVersion = new RESTOperation("RESTOperation");
				objRESTOperationApplicationVersion.defaultContentType = "application/json";
				objRESTOperationApplicationVersion.method = "GET";
				objRESTOperationApplicationVersion.urlTemplate = "/darwin/api/1.0/application-versions/" + intApplicationID + "/page/" + intPage + "/page-size/" + intPageSize;
				objRESTOperationApplicationVersion.host = objRESTHostvFAD;

			var objRESTRequestApplicationVersion;
				objRESTRequestApplicationVersion = objRESTOperationApplicationVersion.createRequest([intApplicationID, intPage, intPageSize], null);
				objRESTRequestApplicationVersion.contentType = "application/json";
				objRESTRequestApplicationVersion.setHeader("Accept","application/json");
				if ( strVirtualDataCenter == "Exchange" )
				{
					objRESTRequestApplicationVersion.setHeader("Authorization", "Basic " + strPassWordExchange);
				}
				else
				{
					objRESTRequestApplicationVersion.setHeader("Authorization", "Basic " + strPassWordDefault);
				}			

			var objRESTResponseApplicationVersion;
				objRESTResponseApplicationVersion = objRESTRequestApplicationVersion.execute();

			if ( objRESTResponseApplicationVersion.statusCode == 200 )
			{
				var strContentAsStringApplicationVersion;
					strContentAsStringApplicationVersion = objRESTResponseApplicationVersion.contentAsString;

				var objJSONApplicationVersion;
					objJSONApplicationVersion = JSON.parse(strContentAsStringApplicationVersion);

				if ( objJSONApplicationVersion.errors == false )
				{
					var arrJSONResultsApplicationVersion;
						arrJSONResultsApplicationVersion = objJSONApplicationVersion.results;

					for ( var ii = 0; ii < arrJSONResultsApplicationVersion.length; ii++ )
					{
						var objJSONResultApplicationVersion;
							objJSONResultApplicationVersion = arrJSONResultsApplicationVersion[ii];

						arrApplications.push( [ strApplicationName, objJSONResultApplicationVersion.major, objJSONResultApplicationVersion.minor, objJSONResultApplicationVersion.micro ] );

						System.log("=====FOUND: " + strApplicationName + " v" + objJSONResultApplicationVersion.major + "." + objJSONResultApplicationVersion.minor + "." + objJSONResultApplicationVersion.micro + ".");
					}
				}
				else
				{
					var arrMessageList;
						arrMessageList = objJSONApplicationVersion.messageList;

					var strErrorMessages;
						strErrorMessages = "";

					for ( var vii = 0; vii < arrMessageList.length; vii++ )
					{
						var objMessage;
							objMessage = arrMessageList[vii];

						strErrorMessages += vii + ") " + objMessage.message + "<br />";
					}					

					objCustomActions.EMailMessage("<h2>vFabric Application Director</h2><p>Reported an error parsing the JSON returned.</p><h3>The Reported Errors Are:</h3><p>" + strErrorMessages + "</p><h4>The Original Response Was</h4><p>" + strContentAsStringApplicationVersion + "</p>");
					System.error("===== JSON Error: " + objJSONApplicationVersion.errors);
					throw "JSON Error: " + objJSONApplicationVersion.errors;
				}
			}
			else
			{
				objCustomActions.EMailMessage("<h2>vFabric Application Director</h2><p>Reported a HTTP Error Code: " + objRESTResponse.statusCode + ".</p><h4>The Original Response Was</h4><p>" + strContentAsString.replace("{", "{<br />").replace("}", "<br />}") + "</p>");
				System.error("===== HTTP Error: " + objRESTResponse.statusCode);
				throw "HTTP Error: " + objRESTResponse.statusCode;
			}
		}
	}
	else
	{
		var arrMessageList;
			arrMessageList = objJSONApplication.messageList;

		var strErrorMessages;
			strErrorMessages = "";

		for ( var vii = 0; vii < arrMessageList.length; vii++ )
		{
			var objMessage;
				objMessage = arrMessageList[vii];

			strErrorMessages += vii + ") " + objMessage.message + "<br />";
		}						

		objCustomActions.EMailMessage("<h2>vFabric Application Director</h2><p>Reported an error parsing the JSON returned.</p><h3>The Reported Errors Are:</h3><p>" + strErrorMessages + "</p><h4>The Original Response Was</h4><p>" + strContentAsStringApplication + "</p>");
		System.error("===== JSON Error: " + objJSONApplication.errors);
		throw "JSON Error: " + objJSONApplication.errors;
	}
}
else
{
	objCustomActions.EMailMessage("<h2>vFabric Application Director</h2><p>Reported a HTTP Error Code: " + objRESTResponse.statusCode + ".</p><h4>The Original Response Was</h4><p>" + strContentAsString.replace("{", "{<br />").replace("}", "<br />}") + "</p>");
	System.error("===== HTTP Error: " + objRESTResponse.statusCode);
	throw "HTTP Error: " + objRESTResponse.statusCode;
}

System.log("==================================================");

Leave a Reply