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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use error::Error;
use std::time::Duration;
use tokio::time::timeout;
use url::Url;

// Hyper imports.
use hyper::body::Buf;
use hyper::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE, USER_AGENT};
use hyper::{Method, Request};
#[cfg(feature = "rustls")]
type HttpsConnector = hyper_rustls::HttpsConnector<hyper::client::HttpConnector>;
#[cfg(feature = "rust-native-tls")]
use hyper_tls;
#[cfg(feature = "rust-native-tls")]
type HttpsConnector = hyper_tls::HttpsConnector<hyper::client::HttpConnector>;

pub mod error;
pub mod resource_url;

pub mod kubeversion;

// Environment variables from Cargo.
static PKG_NAME: &str = env!("CARGO_PKG_NAME");
static PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

/// `Client` struct is used to make calls to the MKS API.
pub struct Client {
    client: hyper::Client<HttpsConnector>,
    token: String,
    base_endpoint: url::Url,
    user_agent: String,
    timeout: Duration,
}

impl Client {
    /// Construct the new Client struct with default configuration.
    ///
    /// Use `Builder` to configure the client.
    pub fn new(base_endpoint: &str, token: &str) -> Result<Client, Error> {
        Client::with_builder(base_endpoint, token, Client::builder())
    }

    fn with_builder(base_endpoint: &str, token: &str, builder: Builder) -> Result<Client, Error> {
        // Check token.
        if token.is_empty() {
            return Err(Error::EmptyTokenError);
        }
        let token = String::from(token);

        // Check base endpoint.
        let base_endpoint = Url::parse(base_endpoint).map_err(|_| Error::EndpointError)?;

        // Use the provided Hyper client or configure a new one.
        let client = match builder.client {
            Some(client) => client,
            None => {
                #[cfg(feature = "rustls")]
                let client = hyper::Client::builder().build(HttpsConnector::new());
                #[cfg(feature = "rust-native-tls")]
                let client = hyper::Client::builder().build(HttpsConnector::new()?);

                client
            }
        };

        Ok(Client {
            client,
            token,
            base_endpoint,
            user_agent: Client::user_agent(),
            timeout: builder.timeout,
        })
    }

    fn user_agent() -> String {
        format!("{}/{}", PKG_NAME, PKG_VERSION)
    }

    /// Get a default builder.
    pub fn builder() -> Builder {
        Builder::default()
    }

    // Prepare a new request.
    fn new_request(
        &self,
        method: Method,
        path: &str,
        body: Option<String>,
    ) -> Result<Request<hyper::Body>, Error> {
        // Build a final Hyper URI.
        let uri = self.make_uri(path)?;

        // Prepare a new Hyper request.
        let mut req = Request::new(hyper::Body::empty());
        *req.method_mut() = method;
        *req.uri_mut() = uri;

        // Add user-agent header.
        req.headers_mut().insert(
            USER_AGENT,
            HeaderValue::from_str(self.user_agent.as_str()).map_err(|_| Error::RequestError)?,
        );

        // Add x-auth-token header.
        req.headers_mut().insert(
            "x-auth-token",
            HeaderValue::from_str(self.token.as_str()).map_err(|_| Error::RequestError)?,
        );

        // Add body into the new request if it's provided.
        if let Some(body) = body {
            // Add content-length header if body is provided.
            let len =
                HeaderValue::from_str(&body.len().to_string()).map_err(|_| Error::RequestError)?;
            req.headers_mut().insert(CONTENT_LENGTH, len);

            // Add content-type header if body is provided.
            req.headers_mut().insert(
                CONTENT_TYPE,
                HeaderValue::from_str("application/json").map_err(|_| Error::RequestError)?,
            );

            *req.body_mut() = hyper::Body::from(body);
        }

        Ok(req)
    }

    #[tokio::main]
    async fn do_request(&self, req: hyper::Request<hyper::Body>) -> Result<String, Error> {
        let duration = self.timeout;
        let handle = async {
            let raw_resp = self.client.request(req).await?;

            let status = raw_resp.status();
            let body = hyper::body::aggregate(raw_resp).await?.to_bytes();
            let body = String::from_utf8_lossy(&body);

            Ok::<_, hyper::Error>((body.to_string(), status))
        };

        let raw_resp = timeout(duration, handle).await??;

        let (body, status) = raw_resp;

        if !status.is_success() {
            return Err(Error::HttpError(status.as_u16(), body));
        }

        Ok(body)
    }

    fn make_uri(&self, path: &str) -> Result<hyper::Uri, Error> {
        let url = self
            .base_endpoint
            .clone()
            .join(path)
            .map_err(|_| Error::UrlError)?;

        url.as_str()
            .parse::<hyper::Uri>()
            .map_err(|_| Error::UrlError)
    }
}

/// Methods to work with Kubernetes versions.
impl Client {
    /// List all Kubernetes versions.
    pub fn list_kube_versions(&self) -> Result<Vec<kubeversion::schemas::KubeVersion>, Error> {
        kubeversion::api::list_kube_versions(self)
    }
}

/// Builder for `Client`.
pub struct Builder {
    /// Hyper client to use for the connection.
    client: Option<hyper::Client<HttpsConnector>>,

    /// Request timeout.
    timeout: Duration,
}

// Default timeout for requests.
const DEFAULT_TIMEOUT: u64 = 30;

impl Default for Builder {
    fn default() -> Self {
        Self {
            client: None,
            timeout: Duration::from_secs(DEFAULT_TIMEOUT),
        }
    }
}

impl Builder {
    /// Set Hyper client.
    ///
    /// By default this library will instantiate a new HttpsConnector.
    /// It will use hyper_rustls or hyper_tls depending on selected library features.
    pub fn client(mut self, client: hyper::Client<HttpsConnector>) -> Self {
        self.client = Some(client);
        self
    }

    /// Set request timeout.
    ///
    /// Default is 30 seconds.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Create `Client` with the configuration in this builder.
    pub fn build(self, base_endpoint: &str, token: &str) -> Result<Client, Error> {
        Client::with_builder(base_endpoint, token, self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_client_default_builder() {
        let client = Client::new("https://example.org", "token_a").unwrap();

        assert_eq!(
            client.base_endpoint,
            Url::parse("https://example.org").unwrap()
        );
        assert_eq!(client.token, String::from("token_a"));
        assert_eq!(client.user_agent, format!("{}/{}", PKG_NAME, PKG_VERSION));
        assert_eq!(client.timeout, Duration::from_secs(DEFAULT_TIMEOUT));
    }

    #[test]
    fn new_client_with_builder() {
        let client = Client::builder()
            .timeout(Duration::from_secs(10))
            .build("https://example.com", "token_b")
            .unwrap();

        assert_eq!(
            client.base_endpoint,
            Url::parse("https://example.com").unwrap()
        );
        assert_eq!(client.token, String::from("token_b"));
        assert_eq!(client.user_agent, format!("{}/{}", PKG_NAME, PKG_VERSION));
        assert_eq!(client.timeout, Duration::from_secs(10));
    }
}