lxd/
image.rs

1use serde::{Deserialize, Serialize};
2use std::collections::BTreeMap;
3use std::io;
4
5use super::{lxc_output, Location};
6
7#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
8/// LXD image information
9pub struct Image {
10    pub auto_update: bool,
11    pub properties: BTreeMap<String, String>,
12    pub public: bool,
13    pub aliases: Vec<BTreeMap<String, String>>,
14    pub architecture: String,
15    pub cached: bool,
16    pub filename: String,
17    pub fingerprint: String,
18    pub size: u64,
19    #[serde(default = "BTreeMap::new")]
20    pub update_source: BTreeMap<String, String>,
21    pub created_at: String,
22    pub expires_at: String,
23    pub last_used_at: String,
24    pub uploaded_at: String,
25}
26
27impl Image {
28    /// Retrieve LXD container image information from all images
29    ///
30    /// # Arguments
31    ///
32    /// * `location` - The location of the host
33    ///
34    /// # Return
35    ///
36    /// The LXD image information
37    ///
38    /// # Errors
39    ///
40    /// Errors that are encountered while retrieving image info will be returned
41    ///
42    /// # Example
43    ///
44    /// ```
45    /// use lxd::{Image, Location};
46    ///
47    /// let images = Image::all(Location::Local).unwrap();
48    /// ```
49    pub fn all(location: Location) -> io::Result<Vec<Self>> {
50        let json = match location {
51            Location::Local => lxc_output(&["image", "list", "--format", "json"])?,
52            Location::Remote(remote) => lxc_output(&["image", "list", &format!("{}:", remote), "--format", "json"])?
53        };
54
55        serde_json::from_slice::<Vec<Self>>(&json).map_err(|err| {
56            io::Error::new(
57                io::ErrorKind::Other,
58                format!("LXD image: failed to parse json: {}", err)
59            )
60        })
61    }
62
63    /// Retrieve LXD image information from one image
64    ///
65    /// # Arguments
66    ///
67    /// * `location` - The location of the host
68    /// * `name` - The name of the container
69    ///
70    /// # Return
71    ///
72    /// The LXD image information
73    ///
74    /// # Errors
75    ///
76    /// Errors that are encountered while retrieving image info will be returned
77    /// ```
78    pub fn new(location: Location, name: &str) -> io::Result<Self> {
79        let json = match location {
80            Location::Local => lxc_output(&["image", "list", name, "--format", "json"])?,
81            Location::Remote(remote) => lxc_output(&["image", "list", &format!("{}:", remote), name, "--format", "json"])?
82        };
83
84        match serde_json::from_slice::<Vec<Self>>(&json) {
85            Ok(mut list) => if list.len() == 1 {
86                Ok(list.remove(0))
87            } else {
88                Err(io::Error::new(
89                    io::ErrorKind::NotFound,
90                    format!("LXD image: {} not found", name)
91                ))
92            },
93            Err(err) => {
94                Err(io::Error::new(
95                    io::ErrorKind::Other,
96                    format!("LXD image: failed to parse json: {}", err)
97                ))
98            }
99        }
100    }
101}