Forum Discussion

MrJ's avatar
3 years ago

Change a description of device interface with Python SDK/Module

Hi I'm looking for a bit of guidance here. I would like to do the following;  - Get a list of devices so I can find a specific device  - Get the interfaces of that device  - Find ...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Here's how you would do it in the sdk, you'll need to provide the logic to sort through the data and find the one you want to update. Also, this only accounts for pagination on the device list. If your datasource count per device, or your instance count per datasource per device is higher than 1000, you would need to include pagination on those gets (you can use the same method i used on the get_device_list call). 

    from lm import lm
    #get the devices, find the id of the one we want
    
    #fetch items with pagination
    end_found = False
    offset = 0
    size = 1000
    devices = {}
    while not end_found:
        data = lm.get_device_list(size=size, offset=offset)
        devices.update({record.id: record for record in data.items})
        offset += size
        end_found = len(data.items) < size
    
    for k,v in devices.items(): print(k, v.name)
    #your logic here to select the desired one from the result
    deviceId = 152
    
    #get the datasources on that device, find the id of the one we want
    device_dses = lm.get_device_datasource_list(deviceId, size=1000).items
    for ds in device_dses:
        print(ds.data_source_display_name, ds.id)
    #your logic here to select the desired one from the result
    hdsId = 9571
    
    #get the instances for that device, for that ds
    instances = lm.get_device_datasource_instance_list(deviceId, hdsId, size=1000).items
    for instance in instances:
        print(instance.id, instance.description, instance.display_name, instance.name)
    #your logic here to select the desired one from the result
    id = 146160818
    
    #get the details for a particular instance
    instance_details = lm.get_device_datasource_instance_by_id(deviceId, hdsId, id)
    
    #update the description in the local copy of the instance
    instance_details.description = "Set by the python sdk"
    
    #send the update back to lm
    response = lm.patch_device_datasource_instance_by_id(deviceId, hdsId, id, instance_details)
    print(f"Description now set to {response.description}")