The Now Platform® Washington DC release is live. Watch now!
on 08-07-2019 01:02 AM
As there is a demand for more and more custom day2 operations for VMware in field, In this Blog, I walk you through the TagsAssignment on resources,
If you go through these and look at other examples, you can mimic almost any operation on VMware, So, Lets start.
If you have a look at AssociateTags operation below, We are asking for 3 inputs.
1. ResourceId: mor_id if the resource
2.ResourceType: equivalent Resource Type for VMWare (for Virtual Server it is VirtualMachine)
3.Tags: A JSON containing the category and tag which needs to be attached to target resource.
Cloud API: Look at the Cloud API and observe how the method mappers are mapping the operations against specific script includes.
Method Mappers and Script :
VMWareRestAPI: This Script Uses, VMWareRestCloudAPIBase, which exposes basic GET and POST endpoints for vmware,the authentication gets automatically taken care of, All you will need to do is to call right GET/POST method with right endpoint and payload.
var VMWareRestCloudAPIBase = Class.create();
VMWareRestCloudAPIBase.prototype = Object.extendsObject(CloudAPIBase, {
/**
* Parameters and headers are expected to come from the exchange from the blueprint
* orchestrator on the instance side.
*/
initialize: function(parameters, headers) {
this.parameters = parameters;
this.headers = headers;
this.endpoint = this.parameters.get('Endpoint').replace('/sdk','').trim();
this.cloudApiBase = new CloudAPIBase();
this.sessionId= this._login();
},
/**
* Invoke Rest API Get
*/
invokeGet: function(apiPath){
var vmwareRequest= this.cloudApiBase.fetchClient(encodeURI(this.endpoint+apiPath));
vmwareRequest.addHeader("vmware-api-session-id",this.sessionId);
vmwareRequest.addHeader("Content-Type", "application/json");
var vmwareRequestOutput= vmwareRequest.get();
var response = this._getResponse(vmwareRequestOutput);
//this.logout();
return response;
},
/**
* VMWare "POST" Endpoint
*/
invokePost: function(apiPath,payload){
var vmwareRequest= this.cloudApiBase.fetchClient(encodeURI(this.endpoint+apiPath));
vmwareRequest.addHeader("vmware-api-session-id",this.sessionId);
vmwareRequest.addHeader("Content-Type", "application/json");
var vmwareOutput= vmwareRequest.post(payload);
var response = this._getResponse(vmwareOutput);
//this.logout();
return response;
},
/**
* VMWare "POST" Endpoint
*/
logout: function(){
var vmwareRequest= this.cloudApiBase.fetchClient(encodeURI(this.endpoint+"/rest/com/vmware/cis/session"));
vmwareRequest.addHeader("vmware-api-session-id",this.sessionId);
vmwareRequest.addHeader("Content-Type", "application/json");
var auth_response = vmwareRequest.del();
var status = auth_response.getStatusCode();
var responseBody = auth_response.getBody();
if(status < '200' || status >= '300' ){
throw new CMPVMwareAPIException(responseBody, status);
}
},
_login: function(){
var StringUtil= Packages.com.glide.util.StringUtil;
var userName = this.parameters.get('Identity');
var password = this.parameters.get('Credentials');
var basicAuth = 'Basic '+StringUtil.base64Encode(userName+":"+password);
var request = this.cloudApiBase.fetchClient(encodeURI(this.endpoint+'/rest/com/vmware/cis/session'));
request.addHeader("Authorization",basicAuth);
request.addHeader("Content-Type", "application/json");
var auth_response = request.post({});
var status = auth_response.getStatusCode();
var responseBody = auth_response.getBody();
if(status < '200' || status >= '300' ){
throw new CMPVMwareAPIException(responseBody, status);
}
var jsonObj = new JSON().decode(responseBody);
var session_id = jsonObj['value'];
return session_id;
},
/**
* HTTP Responses are parssed.
*/
_getResponse: function(response){
var status = response.getStatusCode();
var responseBody = response.getBody();
ms.log("@ VMWare Response ************** "+responseBody);
if(status < '200' || status >= '300' ){
throw new CMPVMwareAPIException(responseBody, status);
}
else{
var jsonObj = new JSON().decode(responseBody);
return jsonObj;
}
},
type: 'VMWareRestCloudAPIBase'
});
Look how we are calling attach tag (POST) endpoint with proper endpoints and payload:
assignTags: function(){
var catagories = [];
var resourceId = this.parameters.get("ResourceID");
var tagsJSON = this.parameters.get("Tags");
var resourceType = this.parameters.get("ResourceType");
var tagsJsobObj = new JSON().decode(tagsJSON);
try{
for (var index in tagsJsobObj){
var tagtagsObj = tagsJsobObj[index];
for (var categoryName in tagsJsobObj){
var categoryId = this.getCategoryByName(categoryName);
if (!categoryId) continue;
var tagId = this.getTagByNameWithCategory(categoryId,tagsObj[categoryName]);
if (!tagId) continue;
this.attchTagToResource(resourceId,tagId,resourceType);
}
}
}catch(e){
return e;
}
return this.listAttachedTags(resourceId,resourceType);
},
Similarly, Look how we are invoking GET operation to retrieve details of a VM
getVirtualMachine: function(vmId){
return this.invokeGet('/rest/vcenter/vm/'+vmId);
},
Now, As we have our API in place for Tags assignment, We will leverage this as one of our day2 operation, As part of the update set attached, you will also get a PSX Extension Interface, which contains AssignTags operation.
In Implementation, we will delegate the call to vSphere Datacenter > AssignTag operation, which will call the VMWare Custom API. As you will get all these as part of the update set, Lets look at the implementation, You can go to Cloud Portal > Resource > Find the Virtual Server to which you want to assign tags,
As part of invocation, You will need to provide the JSON array of the tags to be assigned in the following format.
[{”category_name":”tag_name"},…]
Also, you can invoke a GetOperation as pointed discovery to get the tags back in CMDB which is not in the scope if this Blog,
If you have any questions/doubts , Please let me know,
Download and Import the update set on share VMWare Day2 Operations and give this for a spin.
Hi Jayant,
it seems Images are not visible. can you make sure we can see images in this post for better understanding.
thank you in advance.
the images still dont show up.
My browser has these images cached i guess, have uploaded them again now, should be good.
My browser has these images cached i guess, have uploaded them again now, should be good.
Still no images 😞