TIm W Posted July 8, 2020 Share Posted July 8, 2020 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 More sharing options...
Steve G Posted July 8, 2020 Share Posted July 8, 2020 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. 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: 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 1 Link to comment Share on other sites More sharing options...
Steve G Posted July 8, 2020 Share Posted July 8, 2020 @TIm W Also, with regards to updating assets using the API's, check this post out as it contains some more useful information: Cheers, Steve Link to comment Share on other sites More sharing options...
TIm W Posted July 28, 2020 Author Share Posted July 28, 2020 Thanks for the help. I've managed to get what I needed to do done. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now