The script was written with Python 3.7.3 on a Debian 10 distribution.
When you execute the script, you will see the following:
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:
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:
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:
[vrops user]
#usrName=vrops-adm
#usrPass=adminpassword
#authSource=Local
usrName=idm-user
usrPass=idm-user-password
authSource=VM-IDM
usrDomain=thewhiteshouse.net
[vrops server]
#!/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