Skip to content

Commit

Permalink
Merge pull request #39 from LRZ-BADW/wire-crate
Browse files Browse the repository at this point in the history
Wire crate
  • Loading branch information
gierens authored Sep 12, 2024
2 parents 037785c + 4d71de6 commit 3e6c31e
Show file tree
Hide file tree
Showing 49 changed files with 1,292 additions and 1,147 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=mold"]

[target.aarch64-unknown-linux-gnu]
rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=mold"]
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
wire:
- changed-files:
- any-glob-to-any-file: "wire/*"

api:
- changed-files:
- any-glob-to-any-file: "api/*"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- name: Install mold
run: sudo apt update && sudo apt install mold -y
- name: linting
run: cargo clippy -- -D warnings

Expand Down
14 changes: 3 additions & 11 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ resolver = "2"
members = [
"api",
"lib",
"cli"
"cli",
"wire",
]

[workspace.dependencies.cargo-husky]
Expand Down
16 changes: 8 additions & 8 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ path = "src/main.rs"
[features]
default = ["all"]
all = ["accounting", "budgeting", "hello", "pricing", "quota", "resources", "user"]
accounting = []
budgeting = []
hello = []
pricing = []
quota = []
resources = []
user = []
accounting = ["lrzcc/accounting"]
budgeting = ["lrzcc/budgeting"]
hello = ["lrzcc/hello"]
pricing = ["lrzcc/pricing"]
quota = ["lrzcc/quota"]
resources = ["lrzcc/resources"]
user = ["lrzcc/user"]

[dependencies]
lrzcc = "1.0"
lrzcc = { version = "1.0", path = "../lib" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = "1"
Expand Down
18 changes: 9 additions & 9 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "lrzcc-lib"
name = "lrzcc"
description = "Rust client library for the LRZ-specific features of the Openstack-based LRZ Compute Cloud."
authors = ["Sandro-Alessio Gierens <[email protected]>"]
categories = ["api-bindings"]
Expand All @@ -22,23 +22,23 @@ path = "src/lib.rs"
default = ["all"]
vendored-tls = ["reqwest/native-tls-vendored"]
all = ["accounting", "budgeting", "hello", "pricing", "quota", "resources", "user"]
accounting = []
budgeting = []
hello = []
pricing = []
quota = []
resources = []
user = []
accounting = ["lrzcc-wire/accounting"]
budgeting = ["lrzcc-wire/budgeting"]
hello = ["lrzcc-wire/hello"]
pricing = ["lrzcc-wire/pricing"]
quota = ["lrzcc-wire/quota"]
resources = ["lrzcc-wire/resources"]
user = ["lrzcc-wire/user"]

[dependencies]
reqwest = { version = "0.12", features = ["json", "blocking"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "1"
anyhow = "1"
tabled = "0.16"
chrono = { version = "0.4", features = ["serde"] }
jzon = "0.12"
lrzcc-wire = { version = "1.0", path = "../wire" }

[dev-dependencies]
cargo-husky = { workspace = true }
27 changes: 4 additions & 23 deletions lib/src/accounting/server_consumption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,15 @@ use crate::common::{request, SerializableNone};
use crate::error::ApiError;
use anyhow::Context;
use chrono::{DateTime, FixedOffset};
use lrzcc_wire::accounting::{
ServerConsumptionAll, ServerConsumptionFlavors, ServerConsumptionProject,
ServerConsumptionServer, ServerConsumptionUser,
};
use reqwest::blocking::Client;
use reqwest::{Method, StatusCode, Url};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use std::rc::Rc;

pub type ServerConsumptionFlavors = HashMap<String, f64>;
pub type ServerConsumptionServer = ServerConsumptionFlavors;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerConsumptionUser {
pub total: ServerConsumptionFlavors,
pub servers: HashMap<String, ServerConsumptionServer>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerConsumptionProject {
pub total: ServerConsumptionFlavors,
pub users: HashMap<String, ServerConsumptionUser>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerConsumptionAll {
pub total: ServerConsumptionFlavors,
pub projects: HashMap<String, ServerConsumptionProject>,
}

#[derive(Debug)]
pub struct ServerConsumptionRequest {
url: String,
Expand Down
39 changes: 4 additions & 35 deletions lib/src/accounting/server_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,14 @@ use crate::common::{request, SerializableNone};
use crate::error::ApiError;
use anyhow::Context;
use chrono::{DateTime, FixedOffset};
use lrzcc_wire::accounting::{
ServerCostAll, ServerCostProject, ServerCostServer, ServerCostSimple,
ServerCostUser,
};
use reqwest::blocking::Client;
use reqwest::{Method, StatusCode, Url};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use std::rc::Rc;
use tabled::Tabled;

#[derive(Clone, Debug, Deserialize, Serialize, Tabled)]
pub struct ServerCostSimple {
pub total: f64,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerCostServer {
pub total: f64,
pub flavors: HashMap<String, f64>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerCostUser {
pub total: f64,
pub flavors: HashMap<String, f64>,
pub servers: HashMap<String, ServerCostServer>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerCostProject {
pub total: f64,
pub flavors: HashMap<String, f64>,
pub users: HashMap<String, ServerCostUser>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerCostAll {
pub total: f64,
pub flavors: HashMap<String, f64>,
pub projects: HashMap<String, ServerCostProject>,
}

#[derive(Debug)]
pub struct ServerCostRequest {
Expand Down
106 changes: 5 additions & 101 deletions lib/src/accounting/server_state.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
use crate::common::{display_option, request, request_bare, SerializableNone};
use crate::common::{request, request_bare, SerializableNone};
use crate::error::ApiError;
use anyhow::Context;
use chrono::{DateTime, FixedOffset};
use lrzcc_wire::accounting::{
ServerState, ServerStateCreateData, ServerStateImport,
ServerStateModifyData,
};
use reqwest::blocking::Client;
use reqwest::Url;
use reqwest::{Method, StatusCode};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::rc::Rc;
use tabled::Tabled;

#[derive(Clone, Debug, Deserialize, Serialize, Tabled)]
pub struct ServerState {
pub id: u32,
pub begin: DateTime<FixedOffset>,
#[tabled(display_with = "display_option")]
pub end: Option<DateTime<FixedOffset>>,
pub instance_id: String, // UUIDv4
pub instance_name: String,
pub flavor: u32,
pub flavor_name: String,
pub status: String,
pub user: u32,
pub username: String,
}

impl Display for ServerState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("ServerState(id={})", self.id))
}
}

#[derive(Clone, Debug, Deserialize, Serialize, Tabled)]
pub struct ServerStateImport {
pub new_state_count: u32,
pub end_state_count: u32,
}

pub struct ServerStateApi {
pub url: String,
Expand Down Expand Up @@ -112,40 +86,6 @@ impl ServerStateListRequest {
}
}

#[derive(Clone, Debug, Serialize)]
struct ServerStateCreateData {
begin: DateTime<FixedOffset>,
#[serde(skip_serializing_if = "Option::is_none")]
end: Option<DateTime<FixedOffset>>,
instance_id: String, // UUIDv4
instance_name: String,
flavor: u32,
// TODO we need an enum here
status: String,
user: u32,
}

impl ServerStateCreateData {
fn new(
begin: DateTime<FixedOffset>,
instance_id: String, // UUIDv4
instance_name: String,
flavor: u32,
status: String,
user: u32,
) -> Self {
Self {
begin,
end: None,
instance_id,
instance_name,
flavor,
status,
user,
}
}
}

pub struct ServerStateCreateRequest {
url: String,
client: Rc<Client>,
Expand Down Expand Up @@ -195,42 +135,6 @@ impl ServerStateCreateRequest {
}
}

#[derive(Clone, Debug, Serialize)]
struct ServerStateModifyData {
id: u32,

#[serde(skip_serializing_if = "Option::is_none")]
begin: Option<DateTime<FixedOffset>>,
#[serde(skip_serializing_if = "Option::is_none")]
end: Option<DateTime<FixedOffset>>,
#[serde(skip_serializing_if = "Option::is_none")]
instance_id: Option<String>, // UUIDv4
#[serde(skip_serializing_if = "Option::is_none")]
instance_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
flavor: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
// TODO we need an enum here
status: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
user: Option<u32>,
}

impl ServerStateModifyData {
fn new(id: u32) -> Self {
Self {
id,
begin: None,
end: None,
instance_id: None,
instance_name: None,
flavor: None,
status: None,
user: None,
}
}
}

pub struct ServerStateModifyRequest {
url: String,
client: Rc<Client>,
Expand Down
14 changes: 1 addition & 13 deletions lib/src/budgeting/budget_bulk_create.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
use crate::common::request;
use crate::error::ApiError;
use lrzcc_wire::budgeting::{BudgetBulkCreate, BudgetBulkCreateData};
use reqwest::blocking::Client;
use reqwest::{Method, StatusCode};
use serde::{Deserialize, Serialize};
use std::rc::Rc;
use tabled::Tabled;

#[derive(Clone, Debug, Deserialize, Serialize, Tabled)]
pub struct BudgetBulkCreate {
pub new_user_budget_count: u32,
pub new_project_budget_count: u32,
}

pub struct BudgetBulkCreateApi {
pub url: String,
pub client: Rc<Client>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BudgetBulkCreateData {
pub year: i32,
}

impl BudgetBulkCreateApi {
pub fn new(base_url: &str, client: &Rc<Client>) -> BudgetBulkCreateApi {
BudgetBulkCreateApi {
Expand Down
Loading

0 comments on commit 3e6c31e

Please sign in to comment.