Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/nvue-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ name = "nvue_client"
[features]

[dependencies]
carbide-instrument = { path = "../instrument" }
reqwest = { features = ["rustls", "json"], workspace = true }
serde = { features = ["derive"], workspace = true }
serde_json = { workspace = true }
Expand Down
50 changes: 29 additions & 21 deletions crates/nvue-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;

use carbide_instrument::red;
use reqwest::header::{ACCEPT, HeaderMap, HeaderValue};
use reqwest::{Client, ClientBuilder, Method, Response, Url};
pub use serde_json::Value as JsonValue;
Expand Down Expand Up @@ -75,39 +76,46 @@ impl NvueClient {
Ok(builder)
}

async fn execute(&self, request: reqwest::Request) -> Result<Response, NvueClientError> {
async fn execute(
&self,
operation: &'static str,
request: reqwest::Request,
) -> Result<Response, NvueClientError> {
let method = request.method().clone();
let url = request.url().clone();
let body = request
.body()
.and_then(|b| b.as_bytes())
.map(|b| String::from_utf8_lossy(b).into_owned());
self.client
.execute(request)
.await
.and_then(|response| response.error_for_status())
.map_err(|source| {
NvueClientError::RequestFailed(Box::new(RequestFailed {
method,
url,
body,
source,
}))
})
red::instrumented("nvue", operation, async move {
self.client
.execute(request)
.await
.and_then(|response| response.error_for_status())
})
.await
.map_err(|source| {
NvueClientError::RequestFailed(Box::new(RequestFailed {
method,
url,
body,
source,
}))
})
}

pub async fn get_api(&self) -> Result<Response, NvueClientError> {
const PATH: &str = "/nvue_v1/system/api?rev=applied";
let request = self.request(Method::GET, PATH)?.build()?;
self.execute(request).await
self.execute("get_api", request).await
}

/// Return the config that is tagged as "applied" (in other words, the one
/// that is currently running on the system).
pub async fn get_applied_config(&self) -> Result<NvueConfigWithHeader, NvueClientError> {
const PATH: &str = "/nvue_v1/?rev=applied&filled=false";
let request = self.request(Method::GET, PATH)?.build()?;
let response = self.execute(request).await?;
let response = self.execute("get_applied_config", request).await?;
let nvue_config = response.json().await?;
Ok(nvue_config)
}
Expand All @@ -116,7 +124,7 @@ impl NvueClient {
pub async fn create_config_revision(&self) -> Result<String, NvueClientError> {
const PATH: &str = "/nvue_v1/revision";
let request = self.request(Method::POST, PATH)?.build()?;
let response = self.execute(request).await?;
let response = self.execute("create_config_revision", request).await?;
let revision: NvueRevision = response.json().await?;
let revision_id = revision
.get_revision_id()
Expand All @@ -138,12 +146,12 @@ impl NvueClient {
let empty_config: HashMap<String, String> = HashMap::new();
let builder = builder.json(&empty_config);
let request = builder.build()?;
let _response = self.execute(request).await?;
let _response = self.execute("replace_config.delete", request).await?;

let builder = self.request(Method::PATCH, &revision_path)?;
let builder = builder.json(&config);
let request = builder.build()?;
let _response = self.execute(request).await?;
let _response = self.execute("replace_config.patch", request).await?;
Ok(())
}

Expand All @@ -153,7 +161,7 @@ impl NvueClient {
let body = NvueApplyData::force_apply();
let builder = builder.json(&body);
let request = builder.build()?;
let _response = self.execute(request).await?;
let _response = self.execute("apply_config_revision", request).await?;

// FIXME: we should poll on the revision path until it reaches an
// "applied" state
Expand All @@ -178,7 +186,7 @@ impl NvueClient {
let path = "/nvue_v1/system";
let builder = self.request(Method::GET, path)?;
let request = builder.build()?;
let response = self.execute(request).await?;
let response = self.execute("system_info", request).await?;
let resonse_body = response.json().await?;
Ok(resonse_body)
}
Expand Down Expand Up @@ -216,7 +224,7 @@ impl NvueClient {
let path = format!("/nvue_v1/bridge/domain/{bridge_domain}/mac-table");
let builder = self.request(Method::GET, &path)?;
let request = builder.build()?;
let response = self.execute(request).await?;
let response = self.execute("bridge_mac_table", request).await?;
let resonse_body: BTreeMap<String, _> = response.json().await?;
let response = resonse_body.into_values().collect();
Ok(response)
Expand Down
Loading