digitalocean_api/api/
certificate.rs

1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Create, Delete, Get, List};
4use crate::request::CertificateRequest;
5use crate::request::Request;
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use chrono::{DateTime, Utc};
8use getset::{Getters, Setters};
9use serde::Deserialize;
10use serde::Serialize;
11use std::fmt::Display;
12use url::Url;
13
14const CERTIFICATES_SEGMENT: &str = "certificates";
15
16/// SSL certificates may be uploaded to DigitalOcean where they will be placed
17/// in a fully encrypted and isolated storage system. They may then be used to
18/// perform SSL termination on Load Balancers.
19///
20/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#certificates)
21#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
22#[get = "pub"]
23pub struct Certificate {
24    /// A unique ID that can be used to identify and reference a certificate.
25    id: String,
26
27    /// A unique human-readable name referring to a certificate.
28    name: String,
29
30    /// A time value given in ISO8601 combined date and time format that
31    /// represents the certificate's expiration date.
32    not_after: DateTime<Utc>,
33
34    /// A unique identifier generated from the SHA-1 fingerprint of the
35    /// certificate.
36    sha1_fingerprint: String,
37
38    /// A time value given in ISO8601 combined date and time format that
39    /// represents when the certificate was created.
40    created_at: DateTime<Utc>,
41}
42
43impl Certificate {
44    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-new-certificate)
45    pub fn create<S>(
46        name: S,
47        private_key: S,
48        leaf_certificate: S,
49    ) -> CertificateRequest<Create, Certificate>
50    where
51        S: AsRef<str> + Serialize + Display,
52    {
53        let mut url = ROOT_URL.clone();
54        url.path_segments_mut()
55            .expect(STATIC_URL_ERROR)
56            .push(CERTIFICATES_SEGMENT);
57
58        let mut req = Request::new(url);
59
60        req.set_body(json!({
61            "name": name,
62            "private_key": private_key,
63            "leaf_certificate": leaf_certificate,
64        }));
65
66        req
67    }
68
69    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-certificates)
70    pub fn list() -> CertificateRequest<List, Vec<Certificate>> {
71        let mut url = ROOT_URL.clone();
72        url.path_segments_mut()
73            .expect(STATIC_URL_ERROR)
74            .push(CERTIFICATES_SEGMENT);
75
76        Request::new(url)
77    }
78
79    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-certificate)
80    pub fn get<N: AsRef<str> + Display>(id: N) -> CertificateRequest<Get, Certificate> {
81        let mut url = ROOT_URL.clone();
82        url.path_segments_mut()
83            .expect(STATIC_URL_ERROR)
84            .push(CERTIFICATES_SEGMENT)
85            .push(id.as_ref());
86
87        Request::new(url)
88    }
89
90    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#delete-a-certificate)
91    pub fn delete<N: AsRef<str> + Display>(id: N) -> CertificateRequest<Delete, ()> {
92        let mut url = ROOT_URL.clone();
93        url.path_segments_mut()
94            .expect(STATIC_URL_ERROR)
95            .push(CERTIFICATES_SEGMENT)
96            .push(id.as_ref());
97
98        Request::new(url)
99    }
100}
101
102impl CertificateRequest<Create, Certificate> {
103    /// The full PEM-formatted trust chain between the certificate authority's
104    /// certificate and your domain's SSL certificate.
105    ///
106    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-new-certificate)
107    pub fn certificate_chain<S: AsRef<str> + Serialize + Display>(mut self, val: S) -> Self {
108        self.body_mut()["certificate_chain"] = json!(val);
109        self
110    }
111}
112
113/// Response type returned from Digital Ocean.
114#[derive(Deserialize, Serialize, Debug, Clone)]
115pub struct CertificateResponse {
116    certificate: Certificate,
117}
118
119impl HasResponse for Certificate {
120    type Response = CertificateResponse;
121}
122
123impl HasValue for CertificateResponse {
124    type Value = Certificate;
125
126    fn value(self) -> Certificate {
127        self.certificate
128    }
129}
130
131/// Response type returned from Digital Ocean.
132#[derive(Deserialize, Serialize, Debug, Clone)]
133pub struct CertificateListResponse {
134    certificates: Vec<Certificate>,
135    links: ApiLinks,
136    meta: ApiMeta,
137}
138
139impl HasResponse for Vec<Certificate> {
140    type Response = CertificateListResponse;
141}
142
143impl HasPagination for CertificateListResponse {
144    fn next_page(&self) -> Option<Url> {
145        self.links.next()
146    }
147}
148
149impl HasValue for CertificateListResponse {
150    type Value = Vec<Certificate>;
151
152    fn value(self) -> Vec<Certificate> {
153        self.certificates
154    }
155}