Retrieve VM details from Azure using REST API integration hub ETL and load to CMDB
This article talks about how to fetch azure vm details and load them into ServiceNow CMDB table cmdb_ci_vm_instance
A. Register an application - Azure
1. Open Azure CLI
2. az account set --subscription
3. az ad sp create-for-rbac{"appId": "XXXXX","displayName": "XXXXX","name": "XXXXX","password": "XXXXX","tenant": "XXXXX"
}
4. az account show --query id -o tsv
5. Create Outbound REST Message - ServiceNow
Name : AzureEndPoint
Endpoint : https://management.azure.com
Authentication : No Authentication
6. Create Post HTTP Method - ServiceNow
Name = GetToken
EndPoint = https://login.microsoftonline.com/${tenantid}/oauth2/token
Authentication type = No Authentication
Content = client_id=&client_secret=&grant_type=client_credentials&resource=https://management.azure.com
Variable substitutions
Name = tenantid
Test Value = tenant id (captured in above steps # 3)
Save the record.
7. Get OAUTH Token - ServiceNow
Click on test to get the authentication token from Azure.
Test run should look like below
HTTP Status = 200
Response = {"token_type":"Bearer","expires_in":"3599","ext_expires_in":"3599","expires_on":"1617300913","not_before":"1617297013","resource":"https://management.azure.com","access\_token":"XXXXXXXXXX"}
B. Plugin installation - ServiceNow
1. Install plugin = com.glide.integration_studioRequest from store or from developer.servicenow.com (if its PDI) https://store.servicenow.com/sn\_appstore\_store.do#!/store/application/f1a5a5b60f94001067ae409dc4767e082\. install plugin = sn_cmdb_int_util2.5.0Request from store or from developer.servicenow.com (if its PDI)
3. Install plugin = sn_int_studio2.0.1
Dont load demo data in production
C. Integration Hub ETL configuration - ServiceNow
1. Create Data source of type REST (Integration hub)
2. Integration Hub action
Preprocessing script to fetch api token
(function execute(inputs, outputs) {// ... code ... var tenantId = 'XXXXXXXXXXXXXXXX';var AzrDetails = getazureResource(tenantId);var issueJson = JSON.parse(AzrDetails);var issueDescString = JSON.stringify(issueJson);gs.print("issueDescString=" + issueDescString); var accessToken = issueJson.access_token + '';gs.log(accessToken, 'azurerest'); outputs.apikey = 'Bearer ' + accessToken; outputs.apiversion = '2020-12-01';function getazureResource(tenantId) {try { var r = new sn_ws.RESTMessageV2('AzureSponsorEndPoint', 'GetToken'); r.setStringParameterNoEscape('tenantid', tenantId); r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
r.setRequestBody("client_id=XXXXXXXXXX&client_secret=XXXXXXXX &grant_type=client_credentials&resource=https://management.azure.com");
//override authentication profile //authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);
//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');
//if the message is configured to communicate through ECC queue, either//by setting a MID server or calling executeAsync, one needs to set skip_sensor//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);
var response = r.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode();}catch(ex) { var message = ex.message;} return responseBody; }
})(inputs, outputs);
REST API step
Define connection lnline.
Base url = https://management.azure.com/
Resource path = subscriptions//resourceGroups/varan2_rg/providers/Microsoft.Compute/virtualMachines/test1234
Query parameters
api-version
Headers
Authorization
2. Follow guided setup of ETL and map the attributes from Azure REST API JSON response to ServiceNow CMDB class = cmdb_ci_vm_instance, cmdb_ci_logical_datacenter
CMDB relationships = Virtual machine instance to logical data center
Video links
Get OAUTH token - https://youtu.be/6pbLcK1a%5Fxc
Plugin installation - https://youtu.be/AyT-ryWEqpE
Data source setup - https://youtu.be/KHMZymEfXEQ
Integration hub ETL setup - https://youtu.be/o%5FIf%5Fl1JDsM
References : https://blog.jongallant.com/2021/02/azure-rest-apis-postman-2021/
Labels:
https://www.servicenow.com/community/itom-articles/retrieve-vm-details-from-azure-using-rest-api-integration-hub/ta-p/2320596
