Various small API doc improvements
[ocaml-libvirt.git] / examples / list_secrets.ml
1 (* Simple demo program showing how to list out secrets.
2    Usage: list_secrets [URI]
3    (C) Copyright 2018 Pino Toscano, Red Hat Inc.
4    https://libvirt.org/
5  *)
6
7 open Printf
8
9 module C = Libvirt.Connect
10 module S = Libvirt.Secret
11
12 let string_of_secret_usage_type = function
13   | S.NoType -> "none"
14   | S.Volume -> "volume"
15   | S.Ceph -> "ceph"
16   | S.ISCSI -> "iscsi"
17   | S.TLS -> "tls"
18
19 let () =
20   try
21     let name =
22       if Array.length Sys.argv >= 2 then
23         Some (Sys.argv.(1))
24       else
25         None in
26     let conn = C.connect_auth_readonly ?name (C.get_auth_default ()) in
27
28     (* List all the secrets. *)
29     let secrets = C.list_secrets conn (C.num_of_secrets conn) in
30     let secrets = Array.to_list secrets in
31     let secrets = List.map (S.lookup_by_uuid_string conn) secrets in
32     List.iter (
33       fun secret ->
34         let uuid = S.get_uuid_string secret in
35         let usageType = string_of_secret_usage_type (S.get_usage_type secret) in
36         let usageId = S.get_usage_id secret in
37         printf "%*s  %-7s  %s\n%!"
38           (Libvirt.uuid_string_length) uuid usageType usageId
39     ) secrets
40   with
41     Libvirt.Virterror err ->
42       eprintf "error: %s\n" (Libvirt.Virterror.to_string err)
43
44 let () =
45   (* Run the garbage collector which is a good way to check for
46    * memory corruption errors and reference counting issues in libvirt.
47    *)
48   Gc.compact ()