initial commit
This commit is contained in:
234
src/dls.rs
Normal file
234
src/dls.rs
Normal file
@ -0,0 +1,234 @@
|
||||
use bytes::Bytes;
|
||||
use futures_util::Stream;
|
||||
use log::debug;
|
||||
use reqwest::Method;
|
||||
use std::collections::BTreeMap as Map;
|
||||
use std::fmt::Display;
|
||||
use std::net::IpAddr;
|
||||
|
||||
pub struct Client {
|
||||
base_url: String,
|
||||
token: String,
|
||||
http_client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(base_url: String, token: String) -> Self {
|
||||
Self {
|
||||
base_url,
|
||||
token,
|
||||
http_client: reqwest::Client::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_proxy(self, proxy: String) -> reqwest::Result<Self> {
|
||||
let proxy = reqwest::Proxy::all(proxy)?;
|
||||
Ok(Self {
|
||||
http_client: reqwest::Client::builder().proxy(proxy).build()?,
|
||||
..self
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn clusters(&self) -> Result<Vec<String>> {
|
||||
self.get_json("clusters").await
|
||||
}
|
||||
|
||||
pub fn cluster(&self, name: String) -> Cluster {
|
||||
Cluster { dls: self, name }
|
||||
}
|
||||
|
||||
pub async fn hosts(&self) -> Result<Vec<String>> {
|
||||
self.get_json("hosts").await
|
||||
}
|
||||
|
||||
pub fn host(&self, name: String) -> Host {
|
||||
Host { dls: self, name }
|
||||
}
|
||||
|
||||
pub async fn get_json<T: serde::de::DeserializeOwned>(&self, path: impl Display) -> Result<T> {
|
||||
let req = self.get(&path)?.header("Accept", "application/json");
|
||||
let resp = do_req(req, &self.token).await?;
|
||||
|
||||
let body = resp.bytes().await.map_err(Error::Read)?;
|
||||
serde_json::from_slice(&body).map_err(Error::Parse)
|
||||
}
|
||||
pub async fn get_bytes(&self, path: impl Display) -> Result<Vec<u8>> {
|
||||
let resp = do_req(self.get(&path)?, &self.token).await?;
|
||||
Ok(resp.bytes().await.map_err(Error::Read)?.to_vec())
|
||||
}
|
||||
pub fn get(&self, path: impl Display) -> Result<reqwest::RequestBuilder> {
|
||||
self.req(Method::GET, path)
|
||||
}
|
||||
|
||||
pub fn req(&self, method: Method, path: impl Display) -> Result<reqwest::RequestBuilder> {
|
||||
let uri = format!("{}/{path}", self.base_url);
|
||||
|
||||
Ok((self.http_client.request(method, uri))
|
||||
.header("Authorization", format!("Bearer {}", self.token)))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Cluster<'t> {
|
||||
dls: &'t Client,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl<'t> Cluster<'t> {
|
||||
pub async fn config(&self) -> Result<ClusterConfig> {
|
||||
self.dls.get_json(format!("clusters/{}", self.name)).await
|
||||
}
|
||||
|
||||
pub async fn ca_cert(&self, ca_name: &str) -> Result<Vec<u8>> {
|
||||
self.dls
|
||||
.get_bytes(format!("clusters/{}/CAs/{ca_name}/certificate", self.name))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn token(&self, name: &str) -> Result<String> {
|
||||
self.dls
|
||||
.get_json(format!("clusters/{}/tokens/{name}", self.name))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn addons(&self) -> Result<Vec<u8>> {
|
||||
self.dls
|
||||
.get_bytes(format!("clusters/{}/addons", self.name))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn sign_ssh_user_pubkey(&self, sign_req: &SshSignReq) -> Result<Vec<u8>> {
|
||||
let req = self.dls.req(
|
||||
Method::POST,
|
||||
format!("clusters/{}/ssh/user-ca/sign", self.name),
|
||||
)?;
|
||||
let req = req.json(sign_req);
|
||||
|
||||
let resp = do_req(req, &self.dls.token).await?;
|
||||
Ok(resp.bytes().await.map_err(Error::Read)?.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Host<'t> {
|
||||
dls: &'t Client,
|
||||
name: String,
|
||||
}
|
||||
impl<'t> Host<'t> {
|
||||
pub async fn config(&self) -> Result<HostConfig> {
|
||||
self.dls.get_json(format!("hosts/{}", self.name)).await
|
||||
}
|
||||
|
||||
pub async fn asset(
|
||||
&self,
|
||||
asset_name: &str,
|
||||
) -> Result<impl Stream<Item = reqwest::Result<Bytes>>> {
|
||||
let req = self.dls.get(format!("hosts/{}/{asset_name}", self.name))?;
|
||||
let resp = do_req(req, &self.dls.token).await?;
|
||||
Ok(resp.bytes_stream())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
pub struct ClusterConfig {
|
||||
pub name: String,
|
||||
pub bootstrap_pods: String,
|
||||
pub addons: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
pub struct HostConfig {
|
||||
pub name: String,
|
||||
pub cluster_name: Option<String>,
|
||||
|
||||
pub annotations: Map<String, String>,
|
||||
pub bootstrap_config: String,
|
||||
#[serde(rename = "IPXE")]
|
||||
pub ipxe: Option<String>,
|
||||
#[serde(rename = "IPs")]
|
||||
pub ips: Vec<IpAddr>,
|
||||
pub initrd: String,
|
||||
pub kernel: String,
|
||||
pub labels: Map<String, String>,
|
||||
pub versions: Map<String, String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
pub struct SshSignReq {
|
||||
pub pub_key: String,
|
||||
pub principal: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub validity: Option<String>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub options: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
struct ServerError {
|
||||
code: u16,
|
||||
message: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
details: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
pub async fn do_req(req: reqwest::RequestBuilder, token: &str) -> Result<reqwest::Response> {
|
||||
let (client, req) = req.build_split();
|
||||
let req = req.map_err(Error::Build)?;
|
||||
|
||||
let method = req.method().clone();
|
||||
let path = req.url().path().replace(token, "<token>"); // clone required anyway, so replace the token as we copy
|
||||
|
||||
debug!("request: {} {}", req.method(), req.url());
|
||||
|
||||
let resp = client.execute(req).await.map_err(Error::Send)?;
|
||||
let status = resp.status();
|
||||
|
||||
if !status.is_success() {
|
||||
let body = resp.bytes().await.map_err(Error::ErrorRead)?;
|
||||
let srv_err: ServerError =
|
||||
serde_json::from_slice(&body).map_err(|e| Error::ErrorParse {
|
||||
error: e,
|
||||
raw: body.to_vec(),
|
||||
})?;
|
||||
|
||||
return Err(Error::ServerReject {
|
||||
method,
|
||||
path,
|
||||
status,
|
||||
message: srv_err.message,
|
||||
details: srv_err.details,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("request build failed: {0}")]
|
||||
Build(reqwest::Error),
|
||||
#[error("request send failed: {0}")]
|
||||
Send(reqwest::Error),
|
||||
#[error("response error read failed: {0}")]
|
||||
ErrorRead(reqwest::Error),
|
||||
#[error("response error parsing failed: {error}")]
|
||||
ErrorParse {
|
||||
error: serde_json::Error,
|
||||
raw: Vec<u8>,
|
||||
},
|
||||
#[error("response read failed: {0}")]
|
||||
Read(reqwest::Error),
|
||||
#[error("rejected by server: {method} {path}: {} {message}", status.as_u16())]
|
||||
ServerReject {
|
||||
method: reqwest::Method,
|
||||
path: String,
|
||||
status: reqwest::StatusCode,
|
||||
message: String,
|
||||
details: Option<serde_json::Value>,
|
||||
},
|
||||
#[error("response parsing failed: {0}")]
|
||||
Parse(serde_json::Error),
|
||||
}
|
||||
Reference in New Issue
Block a user