Jump to content

Recommended Posts

Hi,

I've been tasked with adding assets into Hornbill from our internal CMDB using python, all of which is a bit new to me and I'm struggling with where to start and find examples of how to use the Python API.

Can anyone provide an example of using the Python API to find an asset by it's name, add an asset and update an asset.

Thanks in advance.

Tim

Link to comment
Share on other sites

Hi @TIm W,

Sure. Here are a couple of examples of how you can do what you need using the Hornbill Python API Library, which contains examples and is documented over on Github.

This is a basic code snippet that will return details of an asset with an exact match on name, where the name of the asset is SGMAC2011:

from xmlmc import XmlmcService
from xmlmc import XmlmcHelper

xmlmc = XmlmcService("yourinstanceid")
xmlmc.set_api_key("yourapikey")
xmlmc.add_param("application", "com.hornbill.servicemanager")
xmlmc.add_param("entity", "Asset")
elem = xmlmc.add_param("searchFilter")
elem.add_child("column", "h_name")
elem.add_child("value", "SGMAC2011")
elem.add_child("matchType", "exact")
json_string = xmlmc.invoke("data", "entityBrowseRecords2")
if XmlmcHelper.is_call_success(json_string):
    assetRecords = XmlmcHelper.get_param_value(json_string, "params/rowData/row")[0]
    assetId = assetRecords['h_pk_asset_id']
    assetClass = assetRecords['h_class']
    for attr, value in assetRecords.items():
        print (attr, value)
else:
    print(XmlmcHelper.get_error_message(json_string))

Note, we're using the data::entityBrowseRecords2 API to search through the Asset entity in Service Manager, and return the record. We're providing the instance ID (stevegdev here, but replace that with whatever yours is), and an API Key to authenticate the request. The image below shows the returned fields (I'm just looping through the properties of the returned object), and how they can be assigned to new variables you use later in the script.

image.png

To add an asset, you can use the relevant Asset Creation API for the class of Asset you are creating. An example being AssetsComputer::addAssetComputer API, which will create a computer type asset:

from xmlmc import XmlmcService
from xmlmc import XmlmcHelper

xmlmc = XmlmcService("yourinstanceid")
xmlmc.set_api_key("yourapikey")
xmlmc.add_param("name", "MYNEWASSET")
xmlmc.add_param("type", "1")
xmlmc.add_param("version", "1")
json_string = xmlmc.invoke("apps/com.hornbill.servicemanager/AssetsComputer", "addAssetComputer")
if XmlmcHelper.is_call_success(json_string):
    assetId = XmlmcHelper.get_param_value(json_string, "params/assetId")
    print(assetId)
else:
    print(XmlmcHelper.get_error_message(json_string))

Note, if you have a look at the API documentation, it gives you a list of the input parameters. name, type and version are mandatory, and the rest are optional. 

With regards to the type input, this is the primary key of the asset type that the new asset should be. The easiest way to find out what value you should use here is in the Asset Type Details form in the main app:

image.png

To update assets of class Computer, you can use the AssetsComputer::updateAssetComputer API. The code for that will be almost identical to the create code above, except you need to provide the asset ID to update.

If you do a page search for addAsset, or updateAsset in the Service Manager API documentation, you can find the API's for creating and updating assets of different class.

Hope this helps,

Steve

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...