Created
December 7, 2018 09:41
-
-
Save fr33dz/bfdec8458d10df48177a2f248975f032 to your computer and use it in GitHub Desktop.
get customAttributes From HP Network Node Manager i via API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Nov 23 14:12:57 2018 | |
@author: yacine.bouslahi@gmail.com | |
""" | |
from lxml import etree | |
import requests | |
def prepare_xml_request(deviceId, includeCustomAttributes="true"): | |
""" this function prepare the XML request | |
return XML File | |
-------------- | |
Example: | |
>>> xml = prepare_xml_request(10764024107) | |
""" | |
return """ | |
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> | |
<env:Body> | |
<ns1:getNodes xmlns:ns1="http://node.sdk.nms.ov.hp.com/"> | |
<arg0 xsi:type="ns3:expression" xmlns:ns3="http://filter.sdk.nms.ov.hp.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<operator>AND</operator> | |
<subFilters xsi:type="ns3:condition"> | |
<name>id</name> | |
<operator>EQ</operator> | |
<value>{0}</value> | |
</subFilters> | |
<subFilters xsi:type="ns3:constraint"> | |
<name>includeCustomAttributes</name> | |
<value>{1}</value> | |
</subFilters> | |
</arg0> | |
</ns1:getNodes> | |
</env:Body> | |
</env:Envelope> | |
""".format(deviceId, includeCustomAttributes) | |
def parse_response(resp_file): | |
""" | |
this function parse the resp variable and return hostname (longName) | |
and dict of customAttributes | |
------------- | |
Example : | |
>>> get_custom_attributes(resp) | |
'3ZEOEFFGDDFFA1_P', {'IER': '120', 'AZE': 'YES'} | |
""" | |
tree = etree.parse(resp_file) | |
root = tree.getroot() | |
customAttributes = {} | |
hostname = "" | |
for customAttribut in root.iter("customAttributes"): | |
att = [] | |
for element in customAttribut.iter("*"): | |
if element.tag != 'customAttributes': | |
att.append(element.text) | |
customAttributes[att[0]] = att[1] | |
for longName in root.iter("longName"): | |
hostname = longName.text | |
return (hostname, customAttributes) | |
def get_infos_node(node_id): | |
""" | |
this function return node's information (hostname, customAttributes) | |
via Node SOAP Service. | |
Parameters : | |
------------ | |
node_id : str | |
Device's id | |
Returns: | |
------------ | |
(hostname, customAttributes) : tuple | |
device's hostname and custom attributes | |
Example: | |
------------- | |
>>> hostname, customAttributes = get_infos_node('10764024107') | |
'38MONEREFTA1_P', {'AZE': '120', 'AZER': 'YES'} | |
""" | |
xml_req = prepare_xml_request(node_id) | |
resp = requests.post('domaine/NodeBeanService/NodeBean?wsdl', data=xml_req, auth=('username', 'password')).text | |
f = open('node_resp.xml', mode='w', encoding='utf-8') | |
f.write(resp) | |
f.close() | |
# parser le resp et recuper le hostname et les customattributes | |
(hostname, customAttributes) = parse_response('node_resp.xml') | |
# return hostname, customAttributes | |
return (hostname, customAttributes) | |
# TODO : add logging | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment