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

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

 

 

var colApplication = 0;

var colMajor = 1;
var colMinor = 2;
var colMicro = 3;

var objDate;
	objDate = new Date();

var intYear;
	intYear = objDate.getFullYear();

var intMonth;
	intMonth = objDate.getMonth();
	intMonth = ( intMonth + 1 )

if ( intMonth < 10 )
{
	intMonth = "0" + intMonth;
}

var intDay;
	intDay = objDate.getDate();

var intHour;
	intHour = objDate.getHours();

for ( var i = 0; i < arrApplications.length; i++ )
{
	var objApplication;
		objApplication = arrApplications[i];

	var strApplication = objApplication[colApplication];
	var intMajor = objApplication[colMajor];
	var intMinor = objApplication[colMinor];
	var intMicro = objApplication[colMicro];

	var strJson;
		strJson = '';
		strJson += '{';
		strJson += '	"stripSecuredProperties":false,';
		strJson += '	"substituteSecuredProperties":true,';
		strJson += '	"exportRequestParameter": ';
		strJson += '	[';
		strJson += '		{';
		strJson += '			"name":"' + strApplication + '",';
		strJson += '			"major":' + intMajor + ',';
		strJson += '			"minor":' + intMinor + ',';
		strJson += '			"micro":' + intMicro + ',';
		strJson += '			"qualifier": "",';
		strJson += '			"type":"APPLICATION_VERSION"';
		strJson += '		}';
		strJson += '	]';
		strJson += '}';

	System.log("===== ATTEMPTING TO EXPORT: " + strApplication + " v" + intMajor + "." + intMinor + "." + intMicro);

	var objRESTOperation;
		objRESTOperation = new RESTOperation("RESTOperation");
		objRESTOperation.defaultContentType = "application/json";
		objRESTOperation.method = "GET";
		objRESTOperation.urlTemplate = "/darwin/api/1.0/action/export";
		objRESTOperation.host = objRESTHostvFAD;

	var objRESTRequest;
		objRESTRequest = objRESTOperation.createRequest([], strJson);
		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 strContentAsString;
			strContentAsString = objRESTResponse.contentAsString;

		var objJSON;
			objJSON = JSON.parse(strContentAsString);

		if ( objJSON.errors == false )
		{
			System.log("===== SUCCESSFULLY EXPORTED: " + strApplication + " v" + intMajor + "." + intMinor + "." + intMicro);

			var strResult;
				strResult = objJSON.result;

			try
			{
				var strFilePath;
					strFilePath = "C:\\vFabricExport\\" + intYear + "\\" + intMonth + "\\" + intYear + "-" + intMonth + "-" + intDay + "-" + intHour + "-" + strApplication + "-" + intMajor + "-" + intMinor + "-" + intMicro + ".export";

				var objFile;
					objFile = new File(strFilePath);
					objFile.createFile();
					objFile.write(strResult);

				System.log("===== SUCCESSFULLY SAVED TO A FILE: " + strApplication + " v" + intMajor + "." + intMinor + "." + intMicro + " to \\\\vco01.vcoflow.co.uk\\C$\\vFabricExport\\" + intYear + "\\" + intMonth + "\\");
			}
			catch (objException)
			{
				System.error("===== FAILED WHILST ATTEMPTING SAVE TO A FILE: " + strApplication + " v" + intMajor + "." + intMinor + "." + intMicro);
			}
		}
		else
		{
			System.error("===== FAILED TO EXPORT: " + strApplication + " v" + intMajor + "." + intMinor + "." + intMicro);

			var arrMessageList;
				arrMessageList = objJSON.messageList;

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

				System.error("ERROR: " + objMessage.message);
			}
		}
	}
}

var strFilePath;
	strFilePath = "C:\\vFabricExport\\" + intYear + "\\" + intMonth + "\\";

var objFile;
	objFile = new File(strFilePath);

var arrFile;
	arrFile = objFile.list(".export");

var strFiles;
	strFiles = "";

for ( var i = 0; i < arrFile.length; i++ )
{
	var strFile;
		strFile = arrFile[i];

	strFiles += strFile + "<br />";
}

objCustomActions.EMailMessage("<h2>vFabric Application Director</h2><p>Reported The Application Backup Has Finished.</p><h3></h3><p></p><h4>The Following Files Were Successfully Backed Up</h4><p>" + strFiles + "</p>");

Leave a Reply