reubenmiller / go-c8y-cli

go c8y cli tool for interacting with the Cumulocity IoT API
https://goc8ycli.netlify.app/
MIT License
36 stars 7 forks source link

Supporting reference-by-name for "c8y devicegroups|devices children assign" #250

Closed ButKor closed 1 year ago

ButKor commented 1 year ago

Currently reference-by-name seems not to be supported for:

Reference by name concept is already existing for e.g. c8y devices get --id {name} and c8y devicegroups get --id {name} - so I would think it can also be added to this subcommand?

Here's an example:

-> echo "device_1\ndevice_2" | c8y devices create -f -o json -c
{"id":"96162278562","lastUpdated":"2023-05-24T20:08:14.985Z","name":"device_1"}
{"id":"90162279546","lastUpdated":"2023-05-24T20:08:15.213Z","name":"device_2"}

-> c8y devices children assign --id device_1 --child device_2 --childType device -f
2023-05-24T21:08:53.360+0100    ERROR   serverError: Finding device data from database failed : Some managedObjects don't exist in database: '[ID [type=com_cumulocity_model_idtype_GId, value=device_2]]' POST https://{...}/inventory/managedObjects/96162278562/childDevices: 404 inventory/Not Found Finding device data from database failed : Some managedObjects don't exist in database: '[ID [type=com_cumulocity_model_idtype_GId, value=device_2]]'

-> echo "group_1\ngroup_2" | c8y devicegroups create -f -o json -c --select id,name,type
{"id":"16162277869","name":"group_1","type":"c8y_DeviceGroup"}
{"id":"46162277010","name":"group_2","type":"c8y_DeviceGroup"}

-> c8y devicegroups children assign --id group_1 --child group_2 --childType asset -f
2023-05-24T21:09:37.369+0100    ERROR   serverError: Finding device data from database failed : Some managedObjects don't exist in database: '[ID [type=com_cumulocity_model_idtype_GId, value=group_2]]' POST https://{...}/inventory/managedObjects/16162277869/childAssets: 404 inventory/Not Found Finding device data from database failed : Some managedObjects don't exist in database: '[ID [type=com_cumulocity_model_idtype_GId, value=group_2]]'
reubenmiller commented 1 year ago

This was actually left out on purpose as the named lookups of child entities is somewhat ambiguous. For example adding a child asset, the child could either be a devicegroup, device, managed object...so doing a named lookup might find some unexpected results.

One proposal would be to use multiple flags to set the same value, for example --childdevice would lookup devices by name, whereas --childgroup would lookup a device group. But both flags would be mapped to the same output value, so ideally these flags would be mutually exclusive.

# child lookup for devices
c8y devices children assign --id device_1 --childdevice device_2 --childType device

# child lookup for device groups
c8y devices children assign --id device_1 --childgroup device_2 --childType device

# child with no named lookups
c8y devices children assign --id device_1 --childasset device_2 --childType device

The other option would be support an generic "lookup" syntax, e.g. --child devicegroup::<name>, where devicegroup:: would tell what kind of lookup should be used...though I think there are a lot of other aspects to this proposal, so that would take longer to implement.

ButKor commented 1 year ago

Well...to be honest I thought it was simply forgotten ;-) Your explanation makes sense. I would also be happy with closing this one (and maybe adding a note to the docs).