Instantly share code, notes, and snippets.

Embed
What would you like to do?
// update the first 2 with the information of your Network Insight instance and credentials
var baseUrl = 'https://network-insight.platform.local';
var content = '{ "password": "VMware1!", "username": "admin@local", "domain": { "value": "local", "domain_type": "LOCAL" } }';
// leave these ones
var queryString = '/api/ni/auth/token';
var httpMethod = 'POST';
// create REST object to perform API calls with
var restHost = RESTHostManager.createHost("DynamicRequest");
var transientHost = RESTHostManager.createTransientHostFrom(restHost);
transientHost.url = baseUrl;
// put together the call to get an authtoken for Network Insight
var requestUrl = baseUrl + queryString;
System.log("Request full URL: " + requestUrl);
var request = transientHost.createRequest(httpMethod, queryString, content);
request.contentType = 'application/json';
// energize!
var response;
response = request.execute();
// check response
System.log("Status code: " + response.statusCode);
System.log("Content as string: " + response.contentAsString);
// determine auth token from JSON output
var jsonObj = JSON.parse(response.contentAsString);
var authToken = jsonObj.token;
System.log("Auth token: " + authToken);
/////// create application
// put together the API call params
queryString = '/api/ni/groups/applications';
content = '{ "name": "' + deploymentName + '" }';
requestUrl = baseUrl + queryString;
// update to new params
System.log("Request full URL: " + requestUrl);
request = transientHost.createRequest(httpMethod, queryString, content);
request.setHeader("Authorization", "NetworkInsight " + authToken);
request.contentType = 'application/json';
// energize!
var response;
response = request.execute();
System.log("Status code: " + response.statusCode);
System.log("Content as string: " + response.contentAsString);
// get application entity ID
var jsonObj = JSON.parse(response.contentAsString);
var appEntityID = jsonObj.entity_id;
/////// create application tiers
queryString = "/api/ni/groups/applications/" + appEntityID + "/tiers";
// go through each tier that's in tierInfo and put together an API call for each tier
for(var tier in tierInfo)
{
System.log("2nd task: Tier name: " + tier);
var vmFilter = '';
var vm_list = tierInfo[tier];
// find VMs in this tier and put their names in the filter format
for(var vmName in vm_list)
{
System.log("2nd task: index: " + vm_list[vmName]);
if(vmFilter != "") {
vmFilter += " or ";
}
vmFilter += "name = '" + vm_list[vmName] + "'";
}
// put together the API call params
content = '{ "name": "' + tier + '", "group_membership_criteria": [ { "membership_type": "SearchMembershipCriteria", "search_membership_criteria": { "entity_type": "BaseVirtualMachine", "filter": "' + vmFilter + '" } } ] }';
requestUrl = baseUrl + queryString;
// update to new params
System.log("Request full URL: " + requestUrl);
request = transientHost.createRequest(httpMethod, queryString, content);
request.setHeader("Authorization", "NetworkInsight " + authToken);
request.contentType = 'application/json';
// energize!
var response;
response = request.execute();
// check status of tier creation
System.log("Status code: " + response.statusCode);
System.log("Content as string: " + response.contentAsString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment