digitalocean_api/api/custom_image.rs
1use super::{HasResponse, HasValue};
2use crate::method::Create;
3use crate::request::CustomImageRequest;
4use crate::request::Request;
5use crate::{ROOT_URL, STATIC_URL_ERROR};
6use chrono::{DateTime, Utc};
7use getset::{Getters, Setters};
8use serde::Deserialize;
9use serde::Serialize;
10use std::fmt::Display;
11
12const IMAGES_SEGMENT: &str = "images";
13
14/// A custom image is an image with an user-supplied raw image.
15/// The body must contain a url attribute pointing to a Linux virtual machine image to be imported into DigitalOcean.
16/// The image must be in the raw, qcow2, vhdx, vdi, or vmdk format.
17/// It may be compressed using gzip or bzip2 and must be smaller than 100 GB after being decompressed.
18///
19/// [Digital Ocean Documentation.](https://www.digitalocean.com/docs/images/custom-images/)
20#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
21pub struct CustomImage {
22 /// A unique number that can be used to identify and reference a specific
23 /// image.
24 id: usize,
25
26 /// The display name that has been given to an image. This is what is shown
27 /// in the control panel and is generally a descriptive title for the image
28 /// in question.
29 name: String,
30
31 /// The kind of image, describing the duration of how long the image is
32 /// stored. This is either "snapshot" or "backup".
33 ///
34 /// *Note:* Since `type` is a keyword in Rust `kind` is used instead.
35 #[serde(rename = "type")]
36 kind: String,
37 // 'type' is reserved in Rust.
38 /// This attribute describes the base distribution used for this image.
39 distribution: String,
40
41 /// This attribute is an array of the regions that the image is available
42 /// in. The regions are represented by their identifying slug values.
43 regions: Vec<String>,
44
45 /// Tags to quickly find an image or to group multiple images
46 /// under a common name
47 tags: Vec<String>,
48
49 /// A time value given in ISO8601 combined date and time format that
50 /// represents when the Image was created.
51 created_at: DateTime<Utc>,
52
53 /// A brief description about the image
54 description: String,
55
56 /// The status of the image
57 status: String,
58}
59
60impl CustomImage {
61 /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-custom-image)
62 pub fn create<S: AsRef<str> + Display + Serialize>(
63 name: S,
64 image_url: S,
65 region: S,
66 distribution: S,
67 desc: S,
68 tags: Vec<S>,
69 ) -> CustomImageRequest<Create, CustomImage> {
70 let mut url = ROOT_URL.clone();
71 url.path_segments_mut()
72 .expect(STATIC_URL_ERROR)
73 .push(IMAGES_SEGMENT);
74
75 let mut req = Request::new(url);
76 req.set_body(json!({
77 "name": name,
78 "url": image_url,
79 "region": region,
80 "distribution": distribution,
81 "description": desc,
82 "tags": tags
83 }));
84 req
85 }
86}
87
88/// Response type returned from Digital Ocean.
89#[derive(Deserialize, Serialize, Debug, Clone)]
90pub struct CustomImageResponse {
91 image: CustomImage,
92}
93
94impl HasResponse for CustomImage {
95 type Response = CustomImageResponse;
96}
97
98impl HasValue for CustomImageResponse {
99 type Value = CustomImage;
100
101 fn value(self) -> CustomImage {
102 self.image
103 }
104}