From 0ee7e29dfde8662b870a1753c6ff4f67ad038e5b Mon Sep 17 00:00:00 2001 From: Uggla Date: Thu, 3 Sep 2015 21:00:21 +0200 Subject: [PATCH] Add http request sample to itop. Boulala.... Bobo tete.... --- alexandria/alexandria.conf | 4 +-- alexandria/config.py | 3 ++ alexandria/drivers.py | 74 ++++++++++++++++++++++++++++---------- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/alexandria/alexandria.conf b/alexandria/alexandria.conf index 2baf260..c9e5a02 100644 --- a/alexandria/alexandria.conf +++ b/alexandria/alexandria.conf @@ -5,8 +5,8 @@ port=8080 [itop] drvtype=cmdb endpoint=http://itop/rest -loginItop=itopuser -passwordItop=itoppassword +loginItop=admin +passwordItop=linux1 [redfish] drvtype=hw diff --git a/alexandria/config.py b/alexandria/config.py index dbe4ff8..aca9280 100644 --- a/alexandria/config.py +++ b/alexandria/config.py @@ -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) diff --git a/alexandria/drivers.py b/alexandria/drivers.py index a48a37d..0490b1d 100644 --- a/alexandria/drivers.py +++ b/alexandria/drivers.py @@ -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)