Thursday, April 23, 2020

Resource Creator for vRealize Operations HTTP Post Adapter

This is just a quick piece of code that allows a user to create a resource for the HTTP Post adapter.  The Post adapter ships with vRealize Operations and is available out of the box.  You will need the product licensed for advanced or higher.

The script was written with Python 3.7.3 on a Debian 10 distribution.

When you execute the script, you will see the following:

Resource Creation for vRealize Operations HTTP Post Adapter

You will need three items
1. Resource Type - (i.e. Finances, VirtualMachine, Entity Type)
2. Resource Name - (i.e. Stock, VM-Name, Entity Name)
3. Description - (i.e. Something that describes the Resource

Current List of Resource Types:
External
Finances
Home
Http Post Adapter Instance
Stock

1. Enter a resource type: 


You will see a brief description of the items needed for the script to execute.  The Resource Type, Resource Name and Description.  If you think in terms of a virtual machine in the vRealize Operations data, it is a resource on the VMWARE adapter with resource type 'VirtualMachine'.  The resource name is the virtual machine name.  For me, I created a resource type called 'Home'.   I then created a resource named 'Shed Office' with a description 'Shed Office space'.  The metric for the Shed Office will be the temperature of the room.  Another example may be a resource type called 'Finances' with a resource called 'Stocks'.  The stocks resource could then have metrics like a stock ticker.  I'll show the metric process in another post.

Here is the capture of the Shed Office process:

Resource Creation for vRealize Operations HTTP Post Adapter

You will need three items
1. Resource Type - (i.e. Finances, VirtualMachine, Entity Type)
2. Resource Name - (i.e. Stock, VM-Name, Entity Name)
3. Description - (i.e. Something that describes the Resource

Current List of Resource Types:
External
Finances
Home
Http Post Adapter Instance
Stock

1. Enter a resource type: Home
2. Enter Name for the new resource: Shed Office
3. Enter a description for the resource: Shed Office space


We allow for a duplicate resource type name, as you might expect.  We do not allow for a duplicate resource name under the resource type.

If a duplicate exists, you will see the following:

"Resource" with identifier "{ adapterKind: Http Post, resourceKind: Home, name: Shed Office }" already exists.

If the resource is successfully created, you will see:

Resource was successfully created

At this point, you will have a new resource on the Http Post adapter.  I have another post where you can gather a metric and associate the metric to the created resource.

This is the object in vRealize Operations with the metric:



In the script below, I'm using configparser to read a file named 'config.ini'.  In the example below, I'm showing the local authentication as well as the vIDM authentication.  The local stuff is commented out.  For the vIDM user, I could have combined the user name and user domain into a single parameter.  I opted to leave them separated here and combine them in the Python script.

<--- Begin config.ini --->


[vrops user]

#usrName=vrops-adm

#usrPass=adminpassword

#authSource=Local

usrName=idm-user

usrPass=idm-user-password

authSource=VM-IDM

usrDomain=thewhiteshouse.net


[vrops server]

srvName=your vrops server ip or FQDN

<--- End config.ini --->

Here is the python code for the resource creation:

#!/usr/bin/python3


#imports

import sys, json, requests, configparser

from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


#Your vROps environment parameters

# Read Config parameters

config = configparser.ConfigParser()

config.read('config.ini')


#Your vROps environment parameters

srvName = config['vrops server']['srvName']

usrName = config['vrops user']['usrName']

usrPass = config['vrops user']['usrPass']

authSource = config['vrops user']['authSource']


if (authSource != "LOCAL"):

usrDom = config['vrops user']['usrDomain']

usrName = usrName+"@"+usrDom


baseURL = "https://" + srvName


# Authenticate (vROps local user - consider updating script to allow for IDM)

def Authenticate():

tokenURL = baseURL + "/suite-api/api/auth/token/acquire"

authJSON = {"username": usrName,"authSource": authSource,"password": usrPass,"others": [],"otherAttributes": {}}

authHeaders = {"Content-Type":"application/json","Accept":"application/json"}

authResponse = requests.post(tokenURL,data=json.dumps(authJSON),headers=authHeaders,verify=False)

if (authResponse.status_code != 200):

print('probably invalid credentials')

sys.exit('Returned Status Code: ' + str(authResponse.status_code))

else:

authToken = "vRealizeOpsToken " + authResponse.json()['token']

return authToken


def RetrieveCurrentResourceTypes(token):

adapterKey = "Http Post"

Headers = {"Content-Type":"application/json","Authorization":token,"Accept":"application/json"}

vropsURL = baseURL+"/suite-api/api/adapterkinds/"+adapterKey+"/resourcekinds"

response = requests.get(vropsURL,headers=Headers,verify=False)

if (response.status_code == 200):

print ("Current List of Resource Types:")

for y in range(len(response.json()['resource-kind'])):

resourceKindKey = response.json()['resource-kind'][y]['key']

print(" " + resourceKindKey)


def CreateResource(token):

adapterKey = "Http Post"

resData = {

"description" : resDesc,

"creationTime" : None,

"resourceKey" : {

"name" : resName,

"adapterKindKey" : adapterKey,

"resourceKindKey" : resType,

"resourceIdentifiers" : [],

"others" : [],

"otherAttributes" : {}

},

"credentialInstanceId" : "",

"resourceStatusStates" : [],

"dtEnabled" : True,

"monitoringInterval" : 5,

"badges" : [],

"relatedResources" : [],

"others" : [],

"otherAttributes" : {},

"identifier" : ""

}

Headers = {"Content-Type":"application/json","Authorization":token,"Accept":"application/json"}

vropsURL = baseURL+"/suite-api/api/resources/adapterkinds/"+adapterKey

response = requests.post(vropsURL,headers=Headers,data=json.dumps(resData),verify=False)

if (response.status_code == 500):

print ("There is an error and the resource was not created")

elif (response.status_code == 201):

print ("Resource was successfully created")

# print (response.json())

else:

print (response.json()['message'])


def Logout(token):

releaseURL = baseURL + "/suite-api/api/auth/token/release"

authHeaders = {"Content-Type":"application/json","Authorization":token,"Accept":"application/json"}

authResponse = requests.post(releaseURL,headers=authHeaders,verify=False)


# Script Starts Here

authToken = Authenticate()

print ("Resource Creation for vRealize Operations HTTP Post Adapter")

print ()

print ("You will need three items")

print (" 1. Resource Type - (i.e. Finances, VirtualMachine, Entity Type)")

print (" 2. Resource Name - (i.e. Stock, VM-Name, Entity Name)")

print (" 3. Description - (i.e. Something that describes the Resource")

print ()

RetrieveCurrentResourceTypes(authToken)

print ()

resType = input("1. Enter a resource type: ")

resName = input("2. Enter Name for the new resource: ")

resDesc = input("3. Enter a description for the resource: ")

CreateResource(authToken)

Logout(authToken)





No comments:

Post a Comment