-
Notifications
You must be signed in to change notification settings - Fork 1
/
hpsm.py
81 lines (65 loc) · 2.84 KB
/
hpsm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import urllib2
import base64
import json
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
class HpsmApi:
def __init__(self, config, options):
self.username = config['username']
self.password = config['password']
self.host = config['host']
self.protocol = config['protocol']
self.port = config['port']
self.api_version = config['api_version']
self.debug = options['debug']
self.dry_run = options['dry_run']
self.api_url = '%s://%s:%s/SM/%s/rest' % (self.protocol, self.host, self.port, self.api_version)
def insert_item(self, data, root, name):
if not self.dry_run:
print '\t Insert item %s/%s ' % (self.api_url, name)
print '\t %s ' % data
request = urllib2.Request('%s/%s' % (self.api_url, name), json.dumps(data))
base64string = base64.b64encode('%s:%s' % (self.username, self.password))
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header("Content-Type", "application/json")
try:
r = urllib2.urlopen(request, context=ctx)
body = r.read()
r.close()
json_object = json.loads(body)
if self.debug:
print json_object
except urllib2.HTTPError:
self.update_item(data, root, name)
def update_item(self, data, root, name):
logical_name = data[root]['logical.name'].replace(' ', '%20')
if not self.dry_run:
print '\t Update item %s/%s/%s ' % (self.api_url, name, logical_name)
print '\t %s ' % data
request = urllib2.Request('%s/%s/%s' % (self.api_url, name, logical_name), json.dumps(data))
base64string = base64.b64encode('%s:%s' % (self.username, self.password))
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header("Content-Type", "application/json")
request.get_method = lambda: 'PUT'
r = urllib2.urlopen(request, context=ctx)
body = r.read()
r.close()
json_object = json.loads(body)
if self.debug:
print json_object
def get_d42_items(self, obj_type, name):
if not self.dry_run:
print '\t Get Device42 Items %s/%s ' % (self.api_url, name)
request = urllib2.Request('%s/%s/?type=%s&view=expand' % (self.api_url, obj_type, name))
base64string = base64.b64encode('%s:%s' % (self.username, self.password))
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header("Content-Type", "application/json")
r = urllib2.urlopen(request, context=ctx)
body = r.read()
r.close()
json_object = json.loads(body)
if self.debug:
print json_object
return json_object