-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e03a5b
commit 74f5337
Showing
5 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(¶ms))) | ||
.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 = ¶ms.prn; | ||
|
||
self.0 | ||
.execute( | ||
Method::PATCH, | ||
format!("/tunnels/{tunnel_prn}"), | ||
Some(json_body!(¶ms)), | ||
) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |