Skip to content

Commit

Permalink
chore(backend): Refactor backend and some other code, no functional c…
Browse files Browse the repository at this point in the history
…hange
  • Loading branch information
sasa-tomic committed Jul 27, 2023
1 parent ff41844 commit 9efc6e4
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 337 deletions.
16 changes: 16 additions & 0 deletions rs/ic-management-backend/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ic_management_types::Network;
use std::str::FromStr;
use url::Url;

pub fn target_network() -> Network {
Network::from_str(&std::env::var("NETWORK").expect("Missing NETWORK environment variable"))
.expect("Invalid network")
}

pub fn nns_url() -> String {
std::env::var("NNS_URL").expect("NNS_URL environment variable not provided")
}

pub fn nns_nodes_urls() -> Vec<Url> {
vec![Url::parse(&nns_url()).expect("Cannot parse NNS_URL environment variable as a valid URL")]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::nns_nodes_urls;
use super::super::config::nns_nodes_urls;
use super::*;
use ic_canisters::governance_canister_version;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn get_decentralization_analysis(
node_ids_to_remove
.iter()
.filter_map(|n| registry_nodes.get(n))
.map(|n| decentralization::network::Node::from(n))
.map(decentralization::network::Node::from)
.collect::<Vec<_>>()
});
let updated_subnet = match &nodes_to_remove {
Expand Down
6 changes: 3 additions & 3 deletions rs/ic-management-backend/src/endpoints/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn change_preview(
let registry_nodes: BTreeMap<PrincipalId, Node> = registry.read().await.nodes();

get_proposed_subnet_changes(&registry_nodes, subnet)
.map_err(|err| error::ErrorBadRequest(err))
.map_err(error::ErrorBadRequest)
.map(|r| HttpResponse::Ok().json(r))
}
Err(e) => Err(error::ErrorInternalServerError(format!(
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn replace(
let nodes_to_replace = nodes
.iter()
.filter_map(|n| all_nodes.get(n))
.map(|n| decentralization::network::Node::from(n))
.map(decentralization::network::Node::from)
.collect::<Vec<_>>();
registry.without_nodes(nodes_to_replace).await?
}
Expand Down Expand Up @@ -129,7 +129,7 @@ async fn replace(
let req_replace_nodes = req_replace_node_ids
.iter()
.filter_map(|n| all_nodes.get(n))
.map(|n| decentralization::network::Node::from(n))
.map(decentralization::network::Node::from)
.collect::<Vec<_>>();
replacements_unhealthy.extend(req_replace_nodes);
};
Expand Down
33 changes: 33 additions & 0 deletions rs/ic-management-backend/src/factsdb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use gitlab::{api::AsyncQuery, AsyncGitlab};
use hyper::StatusCode;
use ic_management_types::{FactsDBGuest, Guest};
use log::warn;

pub async fn query_guests(gitlab_client: AsyncGitlab, network: String) -> anyhow::Result<Vec<Guest>> {
::gitlab::api::raw(
::gitlab::api::projects::repository::files::FileRaw::builder()
.ref_("refs/heads/main")
.project("dfinity-lab/core/release")
.file_path(format!("factsdb/data/{}_guests.csv", network))
.build()
.expect("failed to build API endpoint"),
)
.query_async(&gitlab_client)
.await
.map(|r| {
csv::Reader::from_reader(r.as_slice())
.deserialize()
.map(|r| {
let fdbg: FactsDBGuest = r.expect("record failed to parse");
Guest::from(fdbg)
})
.collect::<Vec<_>>()
})
.or_else(|e| match e {
::gitlab::api::ApiError::Gitlab { msg } if msg.starts_with(&StatusCode::NOT_FOUND.as_u16().to_string()) => {
warn!("No factsdb guests file found for network {network}: {msg}");
Ok(vec![])
}
_ => Err(anyhow::anyhow!(e)),
})
}
11 changes: 11 additions & 0 deletions rs/ic-management-backend/src/gitlab/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod commit_refs;
pub use commit_refs::CommitRefs;
use gitlab::{AsyncGitlab, GitlabBuilder};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -8,3 +9,13 @@ pub struct CommitRef {
pub kind: String,
pub name: String,
}

pub async fn authenticated_client(env: &str) -> AsyncGitlab {
GitlabBuilder::new(
"gitlab.com",
std::env::var(env).unwrap_or_else(|_| panic!("missing {} env variable", env)),
)
.build_async()
.await
.expect("unable to initialize gitlab token")
}
3 changes: 3 additions & 0 deletions rs/ic-management-backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub mod config;
pub mod factsdb;
pub mod gitlab;
pub mod health;
pub mod prometheus;
pub mod proposal;
pub mod public_dashboard;
pub mod registry;
pub mod release;
pub mod subnets;
Loading

0 comments on commit 9efc6e4

Please sign in to comment.