Skip to content

Commit

Permalink
Merge pull request #35 from golemfactory/prek/url-map-fix
Browse files Browse the repository at this point in the history
Prek/url map fix
  • Loading branch information
prekucki authored Nov 17, 2022
2 parents 6726e78 + 087adfa commit a0448f4
Show file tree
Hide file tree
Showing 17 changed files with 453 additions and 108 deletions.
48 changes: 46 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ nix = "0.23"
#ya-runtime-sdk = { version = "0.4.0", features = ["macros"] }
ya-runtime-sdk = { git = "https://github.com/golemfactory/ya-runtime-sdk.git", rev = "85a88e5bd76fedd9ff9904952bc256856535e3cb", features = ["macros"]}
ya-http-proxy-client = { version = "0.2", path = "crates/ya-http-proxy-client" }
ya-http-proxy-model = { version = "0.2", path = "crates/ya-http-proxy-model" }

actix-rt = "2.7.0"
anyhow = "1.0"
Expand Down
4 changes: 4 additions & 0 deletions crates/ya-http-proxy-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ thiserror = {version ="1.0"}
# forced min versions
actix-tls = "3.0.3"
h2="0.3.15"

[dev-dependencies]
actix-rt = "2.7.0"
clap = { version = "4.0.24", features=["derive"] }
151 changes: 151 additions & 0 deletions crates/ya-http-proxy-client/examples/ya-proxy-cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use anyhow::Result;

use clap::{Parser, Subcommand};
use ya_http_proxy_model::{Addresses, CreateService, CreateUser, Service};

fn print_service(service: &Service) {
eprintln!("name: {:20}", service.inner.name);
eprintln!("from: {:20}", service.inner.from);
eprintln!("to: {:20}", service.inner.to);
eprintln!("[servers: {:?}]", service.inner.server_name);
eprintln!("[http: {:?}]", service.inner.bind_http);
eprintln!("[https: {:?}]", service.inner.bind_https);
}

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[command(subcommand)]
pub command: Commands,
}

impl Args {
async fn run(&self) -> Result<()> {
self.command.run().await
}
}

#[derive(Subcommand, Debug)]
pub enum Commands {
/// does testing things
Service {
#[command(subcommand)]
command: ServiceCommands,
},
User {
service: String,
#[command(subcommand)]
command: UserCommands,
},
}

impl Commands {
async fn run(&self) -> Result<()> {
match self {
Self::Service { command } => command.run().await?,
Self::User { service, command } => command.run(service).await?,
}
Ok(())
}
}

#[derive(Subcommand, Debug)]
pub enum ServiceCommands {
/// does testing things
List {},
Add {
name: String,
port: u16,
from: String,
to: String,
},
Delete {
name: String,
},
}

#[derive(Subcommand, Debug)]
pub enum UserCommands {
/// does testing things
List {},
Add {
user: String,
pass: String,
},
Delete {
name: String,
},
}

impl ServiceCommands {
async fn run(&self) -> Result<()> {
let api = ya_http_proxy_client::ManagementApi::try_default()?;
match self {
Self::List {} => {
eprintln!("{:?}", api.get_services().await?);
}
Self::Add {
name,
from,
to,
port,
} => {
let s = api
.create_service(&CreateService {
name: name.clone(),
server_name: vec![format!("box.local:{port}")],
bind_https: None,
bind_http: Some(Addresses::new([
(std::net::Ipv4Addr::UNSPECIFIED, *port).into()
])),
cert: None,
auth: None,
from: from.parse()?,
to: to.parse()?,
timeouts: None,
cpu_threads: None,
user: None,
})
.await?;
print_service(&s);
}
Self::Delete { name } => api.delete_service(name).await?,
}
Ok(())
}
}

impl UserCommands {
async fn run(&self, service: &str) -> Result<()> {
let api = ya_http_proxy_client::ManagementApi::try_default()?;
match self {
Self::Delete { name } => api.delete_user(service, name).await?,
Self::Add { user, pass } => {
let user = api
.create_user(
service,
&CreateUser {
username: user.to_string(),
password: pass.to_string(),
},
)
.await?;
eprintln!("{user:?}");
}
Self::List {} => {
let users = api.get_users(service).await?;
for user in users {
eprintln!("{:?}", user);
}
}
}
Ok(())
}
}

#[actix_rt::main]
async fn main() -> Result<()> {
let args = Args::parse();

args.run().await
}
36 changes: 25 additions & 11 deletions crates/ya-http-proxy-client/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,82 @@
use crate::{web::WebClient, Result};
use ya_http_proxy_model::{
use crate::model::{
CreateService, CreateUser, GlobalStats, Service, User, UserEndpointStats, UserStats,
};
use crate::{web::WebClient, Result};

/// Handle to a proxy api.
#[derive(Clone)]
pub struct ManagementApi {
client: WebClient,
}

impl ManagementApi {
pub fn new(client: WebClient) -> Self {
Self { client }
/// Creates connection to proxy management api.
pub fn try_default() -> Result<Self> {
Ok(Self::new(WebClient::try_default()?))
}

/// Creates connection to proxy managmet api at given url.
pub fn try_from_url(url: &str) -> Result<Self> {
Ok(Self::new(WebClient::new(url)?))
}

// Service management
fn new(client: WebClient) -> Self {
Self { client }
}

/// Lists available services.
pub async fn get_services(&self) -> Result<Vec<Service>> {
self.client.get("services").await
}

/// Create new service from spec.
pub async fn create_service(&self, cs: &CreateService) -> Result<Service> {
self.client.post("services", cs).await
}

/// Gets service by name.
pub async fn get_service(&self, service_name: &str) -> Result<Service> {
let url = format!("services/{}", service_name);
self.client.get(&url).await
}

/// Drops service.
pub async fn delete_service(&self, service_name: &str) -> Result<()> {
let url = format!("services/{}", service_name);
self.client.delete(&url).await
}

// User management per service

/// User management per service
pub async fn get_users(&self, service_name: &str) -> Result<Vec<User>> {
let url = format!("services/{}/users", service_name);
self.client.get(&url).await
}

/// Add user to service
pub async fn create_user(&self, service_name: &str, cu: &CreateUser) -> Result<User> {
let url = format!("services/{}/users", service_name);
self.client.post(&url, cu).await
}

/// Get user info for service.
pub async fn get_user(&self, service_name: &str, username: &str) -> Result<User> {
let url = format!("services/{}/users/{}", service_name, username);
self.client.get(&url).await
}

/// Removes giver user from given server.
pub async fn delete_user(&self, service_name: &str, username: &str) -> Result<()> {
let url = format!("services/{}/users/{}", service_name, username);
self.client.delete(&url).await
}

// User statistics

/// User statistics
pub async fn get_user_stats(&self, service_name: &str, username: &str) -> Result<UserStats> {
let url = format!("services/{}/users/{}/stats", service_name, username);
self.client.get(&url).await
}

/// List user endpoints stats.
pub async fn get_endpoint_user_stats(
&self,
service_name: &str,
Expand All @@ -74,8 +89,7 @@ impl ManagementApi {
self.client.get(&url).await
}

// Global statistics

/// Global statistics.
pub async fn get_global_stats(&self) -> Result<GlobalStats> {
self.client.get("stats").await
}
Expand Down
2 changes: 2 additions & 0 deletions crates/ya-http-proxy-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use awc::error::{PayloadError, SendRequestError};
use http::uri::InvalidUri;
use http::{Method, StatusCode};

/// Specialized Error for proxy Management API
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
#[error("HTTP error requesting {method} {url}: {code}; msg: '{msg}'")]
SendRequestError {
Expand Down
Loading

0 comments on commit a0448f4

Please sign in to comment.