Skip to content

Commit

Permalink
add tunnels
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontwong committed May 16, 2024
1 parent 1e03a5b commit 74f5337
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod products;
pub mod products_v2;
pub mod releases;
pub mod signing_keys;
pub mod tunnels;
pub mod webhooks;

use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
Expand Down Expand Up @@ -51,6 +52,7 @@ pub use products_v2::ProductsV2Api;
pub use releases::ReleasesApi;
pub use reqwest::Body;
pub use signing_keys::SigningKeysApi;
pub use tunnels::TunnelsApi;
pub use users::UsersApi;
pub use webhooks::WebhooksApi;

Expand Down Expand Up @@ -423,6 +425,10 @@ impl Api {
SigningKeysApi(self)
}

pub fn tunnels(&self) -> TunnelsApi {
TunnelsApi(self)
}

pub fn users(&self) -> UsersApi {
UsersApi(self)
}
Expand Down
109 changes: 109 additions & 0 deletions src/api/tunnels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::{json_body, Api};

use super::Error;
use snafu::ResultExt;

#[derive(Debug, Deserialize, Serialize)]
pub struct Tunnel {
pub device_proxy_ip_address: String,
pub device_proxy_port: u16,
pub device_public_key: String,
pub device_tunnel_port: u16,
pub server_proxy_ip_address: String,
pub server_proxy_port: u16,
pub server_public_key: String,
pub server_tunnel_port: u16,
pub expires_at: String,
pub prn: String,
pub state: String,
}

#[derive(Debug, Serialize)]
pub struct CreateTunnelParams {
pub device_prn: String,
pub ttl: Option<u16>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct CreateTunnelResponse {
pub tunnel: Tunnel,
}

#[derive(Debug, Serialize)]
pub struct ListTunnelsParams {
pub limit: Option<u8>,
pub order: Option<String>,
pub search: String,
pub page: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ListTunnelsResponse {
pub tunnels: Vec<Tunnel>,
pub next_page: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct UpdateTunnelParams {
pub prn: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub state: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UpdateTunnelResponse {
pub tunnel: Tunnel,
}

pub struct TunnelsApi<'a>(pub &'a Api);

impl<'a> TunnelsApi<'a> {
pub async fn create(
&'a self,
params: CreateTunnelParams,
) -> Result<Option<CreateTunnelResponse>, Error> {
self.0
.execute(Method::POST, "/tunnels", Some(json_body!(&params)))
.await
}

pub async fn list(
&'a self,
params: ListTunnelsParams,
) -> Result<Option<ListTunnelsResponse>, Error> {
let mut query_params = vec![("search".to_string(), params.search)];

if let Some(limit) = params.limit {
query_params.push(("limit".to_string(), limit.to_string()))
}
if let Some(order) = params.order {
query_params.push(("order".to_string(), order))
}

if let Some(page) = params.page {
query_params.push(("page".to_string(), page))
}
self.0
.execute_with_params(Method::GET, "/tunnels".to_string(), None, query_params)
.await
}

pub async fn update(
&'a self,
params: UpdateTunnelParams,
) -> Result<Option<UpdateTunnelResponse>, Error> {
let tunnel_prn: &String = &params.prn;

self.0
.execute(
Method::PATCH,
format!("/tunnels/{tunnel_prn}"),
Some(json_body!(&params)),
)
.await
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/tunnels-create-201.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"tunnel": {
"device_proxy_ip_address": "10.0.1.1",
"device_proxy_port": 47539,
"device_public_key": "device_public_key",
"device_tunnel_port": 22,
"server_proxy_ip_address": "10.0.0.1",
"server_proxy_port": 49293,
"server_public_key": "server_public_Key",
"server_tunnel_ip_address": "3.82.23.99",
"server_tunnel_port": 47532,
"expires_at": "2001-01-01T00:00:00Z",
"prn": "prn",
"state": "requested"
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/tunnels-update-200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"tunnel": {
"device_proxy_ip_address": "10.0.1.1",
"device_proxy_port": 47539,
"device_public_key": "device_public_key",
"device_tunnel_port": 22,
"server_proxy_ip_address": "10.0.0.1",
"server_proxy_port": 49293,
"server_public_key": "server_public_Key",
"server_tunnel_ip_address": "3.82.23.99",
"server_tunnel_port": 47532,
"expires_at": "2001-01-01T00:00:00Z",
"prn": "prn",
"state": "closed"
}
}
73 changes: 73 additions & 0 deletions tests/tunnels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
mod common;

use common::API_KEY;
use mockito::{mock, server_url as mock_server_url};

use peridio_sdk::api::tunnels::{CreateTunnelParams, UpdateTunnelParams};

use peridio_sdk::api::Api;
use peridio_sdk::api::ApiOptions;

#[tokio::test]
async fn create_tunnel() {
let device_prn = "device_prn";
let ttl = 3600;

let api = Api::new(ApiOptions {
api_key: API_KEY.into(),
endpoint: Some(mock_server_url()),
ca_bundle_path: None,
});

let m = mock("POST", &*format!("/tunnels"))
.with_status(201)
.with_header("content-type", "application/json")
.with_body_from_file("tests/fixtures/tunnels-create-201.json")
.create();

let params = CreateTunnelParams {
device_prn: device_prn.to_string(),
ttl: Some(ttl),
};

match api.tunnels().create(params).await.unwrap() {
Some(tunnel) => {
assert_eq!(tunnel.tunnel.expires_at, "2000-01-01T00:00:00Z");
}
_ => panic!(),
}

m.assert();
}

#[tokio::test]
async fn update_tunnel() {
let expected_state = "closed";
let expected_prn = "1";

let api = Api::new(ApiOptions {
api_key: API_KEY.into(),
endpoint: Some(mock_server_url()),
ca_bundle_path: None,
});

let m = mock("PATCH", &*format!("/tunnels/{expected_prn}"))
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_file("tests/fixtures/tunnels-update-200.json")
.create();

let params = UpdateTunnelParams {
prn: expected_prn.to_string(),
state: Some(expected_state.to_string()),
};

match api.tunnels().update(params).await.unwrap() {
Some(tunnel) => {
assert_eq!(tunnel.tunnel.state, expected_state.to_string());
}
_ => panic!(),
}

m.assert();
}

0 comments on commit 74f5337

Please sign in to comment.