-
Hi!
I did spend a lot of time within the last days to get API-calls with REST and/or xo-cli working.
I am definitely not a coder, but the different featuresets about filtering, etc. with the provided documentation made it hard for me to use it.
Would't it be good to have an extra-sub-forum for API-questions, etc?
If somebody in the community can benefit from my findings and open points - here some examples:
- Getting list of VMs with some criterias:
xo-cli xo.getAllObjects filter="type:/^VM$/ power_state:Running tags:!nobackup \$pool:POOLNAME"
- Checking logs for a job for the last day
xo-cli backupNg.getLogs jobName=JOBNAME status=success after=$(($(date --date "-24 hours" +%s%N)/1000000))
- Limits with REST
xo-cli rest get backups/logs limit=10
--> Does export 100 events. Limit does not seem to be used - differing to the documentation
I was not able to to "OR" filters with backupNg.getLogs
I was not filter with exclusions (get backup-logs without rolling-snapshots)For me, it feels like everything is there, but filters seem to work different with different parts of the API.
Best wishes
KPS -
Pinging @julien-f
-
@KPS Both APIs are completely different, the oldest one has never been intended for public consumption, it was developed for XO's internal needs even though we are not preventing anyone from using it
The REST API has been created exactly to address the need of a public API, use it if it covers your need.
Also, feel free to provide feedback and we'lll improve it.
Regarding the
limit
issue, it has been fixed 6 weeks ago, please make sure you are up to date. -
-
@julien-f
I think, there is still a problem, when limit is >100.xo-cli rest get vms fields=name_label,uuid limit=150
--> Does also return only 100 entries.
-
@KPS It works as expected on my side.
-
@julien-f
Do you have any idea, what I could do wrong?I am getting:
... uuid: 'abd06f55-8356-ab9f-1cd1-98a7953672d1' }, ... 22 more items ]
no matter, if limit=100 or limit=150.
Everything above 100 seems to give 100. -
@KPS My bad, you're right,
xo-cli rest
is indeed limited to 100 entriesI thought you were talking about the REST API itself, but this one is working fine.
xo-cli
has been primarily developed to test and explore the API by hand, not to automate stuff. For instance the output is not valid JSON and is using colors.But still, the situation could be improved, for instance the REST API support a
ndjson
param to get the result in newline delimited JSON format andxo-cli
does not supports it.Maybe just adding it and make
xo-cli
output all the records would be good enough for your use case?Example:
$ xo-cli rest get vms limits=150 ndjson { href: 'vms/2d561dc2-7dbc-55f2-c643-9661cb28010a', name_label: 'demo 2', uuid: '2d561dc2-7dbc-55f2-c643-9661cb28010a' } { href: 'vms/9344c80d-f8e0-25c6-9670-365c4e2b52a6', name_label: 'SVE Windows Server 2019 1809 US UEFI', uuid: '9344c80d-f8e0-25c6-9670-365c4e2b52a6' }
What do you think?
-
@julien-f said in Xen Orchestra API and documentation - Examples:
xo-cli rest get vms limits=150 ndjson
Hi Julien,
for me, that would be great, if I would be able to add a higher limit than 100 on the cli - ndjson sounds perfect. -
@KPS Published as
xo-cli@0.24.0
. -
List old snapshots:
xo-cli rest get vm-snapshots fields=name_label,parent,snapshot_time ndjson filter="snapshot_time:<$(($(date --date "-8 days" +%s%N)/1000000000))"
Or as script:
#!/bin/bash IFS=$'\n' export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /usr/local/bin/xo-cli register --alowUnauthorized --token xxxxxxxxxxx http://xoa echo "age/d;SnapName;SnapshotOf" output=$(for i in $(xo-cli rest get vm-snapshots fields='name_label,uuid,$snapshot_of,snapshot_time' ndjson filter="snapshot_time:<$(($(date --date "-8 days" +%s%N)/1000000000))" | grep uuid | awk '{print $2}' | awk -F "'" '{print $2}') do snapdetails=$(xo-cli rest get vm-snapshots fields='name_label,uuid,$snapshot_of,snapshot_time' ndjson filter="uuid:$i") snapshot_of=$(echo "$snapdetails" | grep snapshot_of | awk -F "'" '{print $4}') name_label=$(echo "$snapdetails" | grep name_label | awk -F "'" '{print $2}') snapshot_of_name=$(/usr/local/bin/xo-cli rest get vms/$snapshot_of| grep name_label | awk -F "'" '{print $2}') timestamp=$(echo "$snapdetails" | grep snapshot_time | awk -F " " '{print $2}' | awk -F "," '{print $1}') age=$(( ($(($(date +%s%N)/1000000000)) - $timestamp) / 86400 )) echo "$age;$name_label;$snapshot_of_name" done ) echo "$output" | sort -k1,1 -n -r
-
I think it might be easier to do so with the new REST API, what do you think @julien-f ?