1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#![allow(non_snake_case)]
use serde_json;
use std::collections::HashMap;
use docker::DockerApiError;
use docker::{get_response_from_api_static, invalid_api_resp, Client, Method};
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Image {
pub Id: String,
pub ParentId: String,
pub RepoTags: Option<Vec<String>>,
pub RepoDigests: Option<Vec<String>>,
pub Created: u64,
pub Size: u64,
pub VirtualSize: u64,
pub SharedSize: i64,
pub Labels: Option<HashMap<String, String>>,
pub Containers: i32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ImageStatus {
pub status: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DeletedImage {
pub Untagged: Option<String>,
pub Deleted: Option<String>,
}
pub enum Source {
Image,
Src,
Repo,
}
impl Client for Image {}
impl Image {
pub fn create(
source: Source,
value: String,
tag: Option<String>,
) -> Result<Image, DockerApiError> {
let api_endpoint = "/images/create";
let param = match source {
Source::Image => &"?fromImage=",
Source::Src => &"?fromSrc=",
Source::Repo => &"?repo=",
};
let tag = if tag.is_some() {
format!("&tag={}", tag.unwrap())
} else {
"&tag=latest".to_owned()
};
let params = format!("{}{}{}", param, value, tag);
let query_params: Option<&str> = Some(¶ms);
let res =
get_response_from_api_static(api_endpoint, Method::POST, query_params, None).unwrap();
if res.status_code != 200 {
return Err(invalid_api_resp(res));
}
Ok(Image {
Id: value,
..Default::default()
})
}
pub fn pull(image: String, tag: Option<String>) -> Result<Image, DockerApiError> {
Image::create(Source::Image, image, tag)
}
pub fn list() -> Result<Vec<Image>, DockerApiError> {
let api_endpoint = "/images/json";
let res = get_response_from_api_static(api_endpoint, Method::GET, None, None).unwrap();
if res.status_code != 200 {
return Err(invalid_api_resp(res));
}
serde_json::from_str(&res.body).map_err(DockerApiError::JsonDeserializationError)
}
pub fn remove(self) -> Result<Vec<DeletedImage>, DockerApiError> {
let api_endpoint = format!("/images/{}", self.Id);
let res = self.get_response_from_api(&api_endpoint, Method::DELETE, None, None)?;
if res.status_code != 200 {
return Err(invalid_api_resp(res));
}
serde_json::from_str(&res.body).map_err(DockerApiError::JsonDeserializationError)
}
}