Post data to CMDB usring python rest

jbrewster
Kilo Explorer

We are try to write a lambda function to write directly to the CMDB.  Has anyone complete this task? If so please post .

3 REPLIES 3

simonbergstedt
Tera Guru

Not done it myself but I have collegues who populate our CMDB using Python. They managed to do it with the examples I gave them from the "Rest API Explorer" where you will get code examples for Python, Powershell, etc. Have you had a look at that?

jbrewster
Kilo Explorer

First of all Thanks! Do they have any code examples?


Here is some code I have that is not working: I have itil role access only. What roles do I need to preform CRUD operation on the CMDB via rest calls?

---------------------------------------

#!/usr/bin/python
import requests
import json

# Set the request parameters
#url = 'https://XXXX.service-now.com/api/now/cmdb/instance/cmdb_ci_linux_server?sysparm_limit=10'
url = 'https://XXXXXXX.service-now.com/api/now/cmdb/instance/cmdb_ci_linux_server'
user = 'XXXXXXXXXXXXXXXX'
pwd = 'XXXXXXXXXXXXXXXX'


payload = "{ \"attributes\": { \"Serial number\": \"123456798\",\"Model ID\": \"2048\",\"Manufacturer\": \"SUN\", \"Data Classification\": \"General Information\", \"Category\": \"CMDB\", \"Class\": \"Linux Server\", \"Name\": \"test.server.\", \"Owend by\": \"jbrewster\", \"Support group\": \"WindowsEngineering-L3\", \"Short description\": \"This is a test post\", \"CI Company domain\": \"myco\", \"Location\": \"Bronx\", \"Status\": \"Installed\", \"Business Service\": \"Windows\" }, \"source\": \"ServiceNow\" }"


# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers, data=json.dumps(payload))
#response = requests.post(url, auth=(user, pwd), headers=headers, data=payload)
print response

# Check for HTTP codes other than 200

if response.status_code != 201:
print " ---- YES THIS BAD!! ----"
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()

# Decode the JSON response into a dictionary and use the data

print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())
print('Cookies', response.cookies)

I belive you need Rest Service role and if you are using an import set table you need the import set transformer and perhaps also other import set roles, cant remember off the top of my head tbh 🙂