Skip to content

Commit

Permalink
v0.1.4: Add HTTP(s) proxies and timeouts support (#3)
Browse files Browse the repository at this point in the history
* v0.1.4: Add HTTP(s) proxies and timeouts support

* Update README
  • Loading branch information
kedare authored Aug 26, 2021
1 parent d047179 commit eb370b3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 26 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netbox2netshot"
version = "0.1.3"
version = "0.1.4"
authors = ["Mathieu Poussin <[email protected]>"]
edition = "2018"
description = "Synchronization tool between netbox and netshot"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ OPTIONS:
--netbox-devices-filter <netbox-devices-filter>
The querystring to use to select the devices from netbox [env: NETBOX_DEVICES_FILTER=] [default: ]

--netbox-proxy <netbox-proxy>
HTTP(s) proxy to use to connect to Netbox [env: NETBOX_PROXY=]

--netbox-token <netbox-token> The Netbox token [env: NETBOX_TOKEN] [default: ]
--netbox-url <netbox-url> The Netbox API URL [env: NETBOX_URL=]
--netbox-vms-filter <netbox-vms-filter>
Expand All @@ -41,6 +44,9 @@ OPTIONS:
--netshot-domain-id <netshot-domain-id>
The domain ID to use when importing a new device [env: NETSHOT_DOMAIN_ID=]

--netshot-proxy <netshot-proxy>
HTTP(s) proxy to use to connect to Netshot [env: NETSHOT_PROXY=]

--netshot-token <netshot-token> The Netshot token [env: NETSHOT_TOKEN]
--netshot-url <netshot-url> The Netshot API URL [env: NETSHOT_URL=]
```
Expand Down
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct Opt {
#[structopt(long, help = "The domain ID to use when importing a new device", env)]
netshot_domain_id: u32,

#[structopt(long, help = "HTTP(s) proxy to use to connect to Netshot", env)]
netshot_proxy: Option<String>,

#[structopt(long, help = "The Netbox API URL", env)]
netbox_url: String,

Expand Down Expand Up @@ -54,6 +57,9 @@ struct Opt {
)]
netbox_vms_filter: Option<String>,

#[structopt(long, help = "HTTP(s) proxy to use to connect to Netbox", env)]
netbox_proxy: Option<String>,

#[structopt(short, long, help = "Check mode, will not push any change to Netshot")]
check: bool,
}
Expand All @@ -77,10 +83,12 @@ async fn main() -> Result<(), Error> {
log::info!("Logger initialized with level {}", logging_level);
log::debug!("CLI Parameters : {:#?}", opt);

let netbox_client = netbox::NetboxClient::new(opt.netbox_url, opt.netbox_token)?;
let netbox_client =
netbox::NetboxClient::new(opt.netbox_url, opt.netbox_token, opt.netbox_proxy)?;
netbox_client.ping().await?;

let netshot_client = netshot::NetshotClient::new(opt.netshot_url, opt.netshot_token)?;
let netshot_client =
netshot::NetshotClient::new(opt.netshot_url, opt.netshot_token, opt.netshot_proxy)?;
netshot_client.ping().await?;

log::info!("Getting devices list from Netshot");
Expand Down
36 changes: 22 additions & 14 deletions src/rest/netbox.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::common::APP_USER_AGENT;
use anyhow::{anyhow, Error, Result};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Proxy;
use serde::{Deserialize, Serialize};
use std::time::Duration;

const API_LIMIT: u32 = 100;
const PATH_PING: &str = "/api/dcim/devices/?name=netbox2netshot-ping";
Expand Down Expand Up @@ -60,26 +62,32 @@ impl Device {

impl NetboxClient {
/// Create a client without authentication
pub fn new_anonymous(url: String) -> Result<Self, Error> {
NetboxClient::new(url, String::from(""))
pub fn new_anonymous(url: String, proxy: Option<String>) -> Result<Self, Error> {
NetboxClient::new(url, String::from(""), proxy)
}

/// Create a client with the given authentication token
pub fn new(url: String, token: String) -> Result<Self, Error> {
pub fn new(url: String, token: String, proxy: Option<String>) -> Result<Self, Error> {
log::debug!("Creating new Netbox client to {}", url);
let mut http_headers = HeaderMap::new();
if token != "" {
let header_value = HeaderValue::from_str(format!("Token {}", token).as_str())?;
http_headers.insert("Authorization", header_value);
}
let http_client = reqwest::Client::builder()
let mut http_client = reqwest::Client::builder()
.user_agent(APP_USER_AGENT)
.default_headers(http_headers)
.build()?;
.timeout(Duration::from_secs(5))
.default_headers(http_headers);

http_client = match proxy {
Some(p) => http_client.proxy(Proxy::all(p)?),
None => http_client,
};

Ok(Self {
url,
token,
client: http_client,
client: http_client.build()?,
})
}

Expand Down Expand Up @@ -183,7 +191,7 @@ mod tests {
#[test]
fn anonymous_initialization() {
let url = mockito::server_url();
let client = NetboxClient::new_anonymous(url.clone()).unwrap();
let client = NetboxClient::new_anonymous(url.clone(), None).unwrap();
assert_eq!(client.token, "");
assert_eq!(client.url, url);
}
Expand All @@ -192,7 +200,7 @@ mod tests {
fn authenticated_initialization() {
let url = mockito::server_url();
let token = String::from("hello");
let client = NetboxClient::new(url.clone(), token.clone()).unwrap();
let client = NetboxClient::new(url.clone(), token.clone(), None).unwrap();
assert_eq!(client.token, token);
assert_eq!(client.url, url);
}
Expand All @@ -205,7 +213,7 @@ mod tests {
.with_status(403)
.create();

let client = NetboxClient::new_anonymous(url.clone()).unwrap();
let client = NetboxClient::new_anonymous(url.clone(), None).unwrap();
let ping = client.ping().await.unwrap();
assert_eq!(ping, false);
}
Expand All @@ -218,7 +226,7 @@ mod tests {
.with_body_from_file("tests/data/netbox/ping.json")
.create();

let client = NetboxClient::new_anonymous(url.clone()).unwrap();
let client = NetboxClient::new_anonymous(url.clone(), None).unwrap();
let ping = client.ping().await.unwrap();
assert_eq!(ping, true);
}
Expand All @@ -232,7 +240,7 @@ mod tests {
.with_body_from_file("tests/data/netbox/single_good_device.json")
.create();

let client = NetboxClient::new_anonymous(url.clone()).unwrap();
let client = NetboxClient::new_anonymous(url.clone(), None).unwrap();
let devices = client.get_devices(&String::from("")).await.unwrap();

assert_eq!(devices.len(), 1);
Expand All @@ -254,7 +262,7 @@ mod tests {
.with_body_from_file("tests/data/netbox/single_device_without_primary_ip.json")
.create();

let client = NetboxClient::new_anonymous(url.clone()).unwrap();
let client = NetboxClient::new_anonymous(url.clone(), None).unwrap();
let devices = client.get_devices(&String::from("")).await.unwrap();

assert_eq!(devices.len(), 1);
Expand All @@ -273,7 +281,7 @@ mod tests {
.with_body_from_file("tests/data/netbox/single_device_without_name.json")
.create();

let client = NetboxClient::new_anonymous(url.clone()).unwrap();
let client = NetboxClient::new_anonymous(url.clone(), None).unwrap();
let devices = client.get_devices(&String::from("")).await.unwrap();

assert_eq!(devices.len(), 1);
Expand Down
24 changes: 16 additions & 8 deletions src/rest/netshot.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::common::APP_USER_AGENT;
use anyhow::{anyhow, Error, Result};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Proxy;
use serde;
use serde::{Deserialize, Serialize};
use std::time::Duration;

const PATH_DEVICES: &str = "/api/devices";

Expand Down Expand Up @@ -52,20 +54,26 @@ pub struct NewDeviceCreatedPayload {

impl NetshotClient {
/// Create a client with the given authentication token
pub fn new(url: String, token: String) -> Result<Self, Error> {
pub fn new(url: String, token: String, proxy: Option<String>) -> Result<Self, Error> {
log::debug!("Creating new Netshot client to {}", url);
let mut http_headers = HeaderMap::new();
let header_value = HeaderValue::from_str(token.as_str())?;
http_headers.insert("X-Netshot-API-Token", header_value);
http_headers.insert("Accept", HeaderValue::from_str("application/json")?);
let http_client = reqwest::Client::builder()
let mut http_client = reqwest::Client::builder()
.user_agent(APP_USER_AGENT)
.default_headers(http_headers)
.build()?;
.timeout(Duration::from_secs(5))
.default_headers(http_headers);

http_client = match proxy {
Some(p) => http_client.proxy(Proxy::all(p)?),
None => http_client,
};

Ok(Self {
url,
token,
client: http_client,
client: http_client.build()?,
})
}

Expand Down Expand Up @@ -131,7 +139,7 @@ mod tests {
fn authenticated_initialization() {
let url = mockito::server_url();
let token = String::from("hello");
let client = NetshotClient::new(url.clone(), token.clone()).unwrap();
let client = NetshotClient::new(url.clone(), token.clone(), None).unwrap();
assert_eq!(client.token, token);
assert_eq!(client.url, url);
}
Expand All @@ -145,7 +153,7 @@ mod tests {
.with_body_from_file("tests/data/netshot/single_good_device.json")
.create();

let client = NetshotClient::new(url.clone(), String::new()).unwrap();
let client = NetshotClient::new(url.clone(), String::new(), None).unwrap();
let devices = client.get_devices().await.unwrap();

assert_eq!(devices.len(), 1);
Expand All @@ -167,7 +175,7 @@ mod tests {
.with_body_from_file("tests/data/netshot/good_device_registration.json")
.create();

let client = NetshotClient::new(url.clone(), String::new()).unwrap();
let client = NetshotClient::new(url.clone(), String::new(), None).unwrap();
let registration = client
.register_device(&String::from("1.2.3.4"), 2)
.await
Expand Down

0 comments on commit eb370b3

Please sign in to comment.