7 years ago
API/Script
Hi
I want to create device group named as "Fileserver" in /device/servers This is the first time I am running the script, so could you please check if my script is ok.
Also please le...
Many many thanks Joe.
But now I am having different problem. it creates one group and shows following error.
for example it created A22-17 CAPORN STREET group but error appeared during A23-19 CAPORN STREET group creation and script terminated.
c:\temp>python cg-csv.py
{"name":"A22-17 CAPORN STREET","parentId":150}
{"name":"A23-19 CAPORN STREET","parentId":150}
Traceback (most recent call last):
File "cg-csv.py", line 86, in <module>
hmac = hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
AttributeError: 'str' object has no attribute 'new'
Script I am running
import csv
import requests
import json
import hashlib
import base64
import time
import hmac
build_data = ['{"name":"A22-17 CAPORN STREET","parentId":150}',
'{"name":"A23-19 CAPORN STREET","parentId":150}',
'{"name":"A24-21 CAPORN STREET","parentId":150}',
'{"name":"A25-23 CAPORN STREET","parentId":150}',
'{"name":"A26-25A CAPORN STREET","parentId":150}',
'{"name":"A28-30 CAPORN STREET","parentId":150}',
'{"name":"A29-1/32-38 CAPORN STREET","parentId":150}',
'{"name":"A30-33 CAPORN STREET","parentId":150}',
'{"name":"A31-35 CAPORN STREET","parentId":150}',
'{"name":"A32-37 CAPORN STREET","parentId":150}',
'{"name":"A33-12 COOK STREET","parentId":150}',
'{"name":"A34-13A COOK STREET (Vacant Land)","parentId":150}',
'{"name":"A35-1/21A COOK STREET","parentId":150}',
'{"name":"A36-23 COOK STREET","parentId":150}',
'{"name":"A37-37 MYERS STREET","parentId":150}',
'{"name":"A37-28 COOK STREET","parentId":150}',
'{"name":"A38-29 COOK STREET (Vacant Land)","parentId":150}',
'{"name":"A39-1/33 COOK STREET","parentId":150}',
'{"name":"A40-1/36 COOK STREET","parentId":150}',
'{"name":"A41-12 EVERETT STREET","parentId":150}',
'{"name":"A42-1/13 EVERETT STREET","parentId":150}',
'{"name":"A43-1/14-16 EVERETT STREET","parentId":150}',
'{"name":"A44-15 EVERETT STREET","parentId":150}',
'{"name":"A45-18 EVERETT STREET","parentId":150}',
'{"name":"A46-19 EVERETT STREET","parentId":150}',
'{"name":"A47-20 EVERETT STREET","parentId":150}',
'{"name":"A48-21 EVERETT STREET","parentId":150}',
'{"name":"A49-22 EVERETT STREET","parentId":150}',
'{"name":"A50-26 EVERETT STREET","parentId":150}',
'{"name":"A51-28 EVERETT STREET","parentId":150}',
'{"name":"A52-30 EVERETT STREET","parentId":150}',
'{"name":"A53-39 EVERETT STREET","parentId":150}',
'{"name":"A54-1/116 FAIRWAY","parentId":150}',
'{"name":"A55-1/13 MYERS STREET","parentId":150}',
'{"name":"A56-1/15 MYERS STREET","parentId":150}',
'{"name":"A57-1/17 MYERS STREET","parentId":150}',
'{"name":"A58-19 MYERS STREET","parentId":150}',
'{"name":"A59-1/21 MYERS STREET","parentId":150}',
'{"name":"A60-1/23 MYERS STREET","parentId":150}',
'{"name":"A61-25 MYERS STREET","parentId":150}',
'{"name":"A62-27A MYERS STREET","parentId":150}',
'{"name":"A63-29 MYERS STREET","parentId":150}',
'{"name":"A64-31 MYERS STREET","parentId":150}',
'{"name":"A65-33 MYERS STREET","parentId":150}',
'{"name":"A66-35 MYERS STREET","parentId":150}',
'{"name":"A69-11 PARKWAY","parentId":150}'
]
#Account Info
AccessId ='XXXXXX'
AccessKey ='YYYYYYYY'
Company = 'contoso'
#Request Info
httpVerb ='POST'
resourcePath = '/device/groups'
l=len(build_data)
for i in range (0, l):
print (build_data)
data = build_data
#Construct URL
url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath
#Get current time in milliseconds
epoch = str(int(time.time() * 1000))
#Concatenate Request details
requestVars = httpVerb + epoch + data + resourcePath
#Construct signature
#signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())
hmac = hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac.encode())
#Construct headers
#auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch
#headers = {'Content-Type':'application/json','Authorization':auth}
auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth}
#Make request
response = requests.post(url, data=data, headers=headers)
#Print status and body of response
#print 'Response Status:',response.status_code
#print 'Response Body:',response.content