Forum Discussion

Jeff_W_'s avatar
2 years ago

Using Python SDK to create a DeviceGroup and subGroups

I’m working on trying to build a script that will take some input (csv or stdin) to create a device group, and then also create multiple device groups under this one device group being specified.

I have the python script working to create the first device group I need, but the “subGroups” feature does not seem to work or be populated and I was curious if anyone else had any luck with it?

A snippet of my code looks like:

infra_root = 'Network Infrastructure'
infra_address = 'Test Location'

infra_sub_groups = [
{"name":"Aggregation","fullPath":infra_root+"/"+infra_address+"/Aggregation"},
{"name":"Access","fullPath":infra_root+"/"+infra_address+"/Access"},
{"name":"Environmental","fullPath":infra_root+"/"+infra_address+"/Environmental"},
{"name":"PDU","fullPath":infra_root+"/"+infra_address+"/PDU"},
{"name":"UPS","fullPath":infra_root+"/"+infra_address+"/UPS"}
]

body = {
#"fullPath":infra_root+"/"+infra_address,
"groupType":"Normal",
"disableAlerting":False,
"subGroups":infra_sub_groups,
"enableNetflow":False,
"parentId":18,
"name":infra_address
}

The script fires off without error and creates he “infra_address” group no problem (under the parent I have). But the output of the script shows me sub_groups is blank:

{'applies_to': '',
'aws_regions_info': '',
'aws_test_result': None,
'aws_test_result_code': 200,
'azure_regions_info': '',
'azure_test_result': None,
'azure_test_result_code': 200,
'created_on': 1676065949,
'custom_properties': [],
'default_collector_description': None,
'default_collector_id': 0,
'description': '',
'disable_alerting': False,
'effective_alert_enabled': True,
'enable_netflow': False,
'extra': {},
'full_path': 'Network Infrastructure/Test Location',
'gcp_regions_info': '',
'gcp_test_result': None,
'gcp_test_result_code': 200,
'group_status': 'normal',
'group_type': 'Normal',
'has_netflow_enabled_devices': False,
'id': 164,
'name': 'Test Location',
'num_of_aws_devices': 0,
'num_of_azure_devices': 0,
'num_of_direct_devices': 0,
'num_of_direct_sub_groups': 0,
'num_of_gcp_devices': 0,
'num_of_hosts': 0,
'parent_id': 18,
'sub_groups': [],
'user_permission': 'write'}

Doing it this way will make my life easier instead of running a script over and over again to populate the sub device groups.

Thanks!

  • Anonymous's avatar
    Anonymous

    Assuming you’re using the .add_device_group() method:

    While the documentation states that the method accepts subgroups as part of the post, the underlying api endpoint does not accept it. Go here, then switch over to the model. You’ll see that no part of the posted payload allows for subgroups.

    You’ll have to post the parent group, grab the id from the response, then post each of the child groups using the parent group in the parentId field. I had to do the same thing when i was building out my group structure maintainer script.

  • I can’t verify this (I no longer have access to LM API myself), but I’m pretty sure subGroups is not a writable value in the REST/Python API, although I don’t see that spelled out in the newer docs. I believe you will need to keep using seperate /device/groups POSTs for each subgroup but updating the parent_idParent to refer to the new parent group.