Add http request sample to itop. Boulala....

Bobo tete....
This commit is contained in:
Uggla 2015-09-03 21:00:21 +02:00
parent bc4675efc6
commit 0ee7e29dfd
3 changed files with 61 additions and 20 deletions

View File

@ -5,8 +5,8 @@ port=8080
[itop]
drvtype=cmdb
endpoint=http://itop/rest
loginItop=itopuser
passwordItop=itoppassword
loginItop=admin
passwordItop=linux1
[redfish]
drvtype=hw

View File

@ -60,3 +60,6 @@ class AlexandriaConfiguration(object):
def get_alexandria_port(self):
return self.config.get("alexandria", "port")
def get_driver_parameters(self, drivername, parameters):
return self.config.get(drivername, parameters)

View File

@ -1,9 +1,11 @@
# coding=utf-8
import types
import pprint
import config
import json
import urllib
import requests
import types
class Driver(object):
@ -16,21 +18,57 @@ class Driver(object):
def get_driver_type(self):
return self.driver_type
def get_ci(self,ci):
def get_ci(self, ci):
pass
def push_ci(self,ci):
def push_ci(self, ci):
pass
class Itop(Driver):
def get_ci(self,ci):
def get_ci(self, ci):
print "Get from itop"
return True
def push_ci(self,ci):
pass
def push_ci(self, ci):
username = config.alexandria.conf_file.get_driver_parameters("itop", "loginItop")
password = config.alexandria.conf_file.get_driver_parameters("itop", "passwordItop")
config.logger.debug("login : {}, password : {}".format(
username,
password
)
)
# Craft request body and header
urlbase = "http://10.3.8.40/itop/webservices/rest.php"
request = '{"operation":"core/update","comment":"boulalala","class":"Server","key":{"name":"Server1"},"output_fields":"id,friendlyname,ram","fields":{"ram":"28"}}'
urlparam = {'version' : '1.0',
'auth_user' : username,
'auth_pwd' : password,
'json_data' : request
}
header = {'Content-type': 'application/json'}
url = urlbase + '?' + urllib.urlencode(urlparam)
config.logger.debug(url)
#=======================================================================
# answer = requests.post(url,
# headers=header,
# verify="False"
# )
#=======================================================================
answer = requests.post(url,
auth=(username,password)
)
config.logger.debug(answer.status_code)
config.logger.debug(answer.text)
class Redfish(Driver):
@ -48,9 +86,9 @@ class Mondorescue(Driver):
class Fakecmdb(Driver):
def push_ci(self, ci):
def push_ci(self, ci):
# Determine ci type so we can do the proper action.
pp = pprint.PrettyPrinter(indent=4)
pp = pprint.PrettyPrinter(indent=4)
if ci.ci_type == "Manager":
print "We are in Fakecmdb driver !"
pp.pprint(ci.data)
@ -59,33 +97,33 @@ class Fakecmdb(Driver):
json.dump(ci.data, jsonfile, indent=4)
jsonfile.close()
#
#
#=======================================================================
class Fakeprovider(Driver):
def get_ci(self,ci):
def get_ci(self, ci):
# Simulate a driver that will provide Manager data.
# TODO a connect method must be implemented
# Assuming the connection is ok.
# Now create a copy of manager model from reference model.
ci.ci_type = "Manager"
ci.data = config.alexandria.model.get_model("Manager")
# Update the structure with data
# TODO : think to encapsulate to not edit ci.data directly.
# This could be also a way to check source of truth.
# If data provided by our driver is not the source of truth
# then discard it.
ci.data["ManagerType"] = "BMC"
ci.data["Model"] = "Néné Manager"
ci.data["FirmwareVersion"] = "1.00"
@ -93,7 +131,7 @@ class Fakeprovider(Driver):
# print "identical"
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(ci.ci_type)