Skip to content

Commit

Permalink
v0.1.3, add support for fetching VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Poussin committed May 12, 2021
1 parent 2745ad5 commit d047179
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 18 deletions.
10 changes: 5 additions & 5 deletions 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.2"
version = "0.1.3"
authors = ["Mathieu Poussin <[email protected]>"]
edition = "2018"
description = "Synchronization tool between netbox and netshot"
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ cargo install netbox2netshot
Most parameters can be set either via command line arguments or environment variables

```bash
USAGE:
netbox2netshot [FLAGS] [OPTIONS] --netbox-token <netbox-token> --netbox-url <netbox-url> --netshot-url <netshot-url>
netbox2netshot [FLAGS] [OPTIONS] --netbox-url <netbox-url> --netshot-domain-id <netshot-domain-id> --netshot-token <netshot-token> --netshot-url <netshot-url>

FLAGS:
-c, --check Check mode, will not push any change to Netshot
Expand All @@ -34,12 +33,16 @@ OPTIONS:
--netbox-devices-filter <netbox-devices-filter>
The querystring to use to select the devices from netbox [env: NETBOX_DEVICES_FILTER=] [default: ]

--netbox-token <netbox-token> The Netbox token [env: NETBOX_TOKEN] [default: ]
--netbox-token <netbox-token> The Netbox token [env: NETBOX_TOKEN] [default: ]
--netbox-url <netbox-url> The Netbox API URL [env: NETBOX_URL=]
--netshot-token <netshot-token> The Netshot token [env: NETSHOT_TOKEN]
--netshot-url <netshot-url> The Netshot API URL [env: NETSHOT_URL=]
--netbox-vms-filter <netbox-vms-filter>
The querystring to use to select the VM from netbox [env: NETBOX_VMS_FILTER=]

--netshot-domain-id <netshot-domain-id>
The domain ID to use when importing a new device [env: NETSHOT_DOMAIN_ID=]

--netshot-token <netshot-token> The Netshot token [env: NETSHOT_TOKEN]
--netshot-url <netshot-url> The Netshot API URL [env: NETSHOT_URL=]
```
The query-string format need to be like this (url query string without the `?`):
Expand Down
24 changes: 20 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use anyhow::{Error, Result};
use flexi_logger::{Duplicate, Logger, LogTarget};
use flexi_logger::{Duplicate, LogTarget, Logger};
use structopt::StructOpt;

use rest::{netbox, netshot};
Expand Down Expand Up @@ -47,6 +47,13 @@ struct Opt {
)]
netbox_devices_filter: String,

#[structopt(
long,
help = "The querystring to use to select the VM from netbox",
env
)]
netbox_vms_filter: Option<String>,

#[structopt(short, long, help = "Check mode, will not push any change to Netshot")]
check: bool,
}
Expand Down Expand Up @@ -86,12 +93,21 @@ async fn main() -> Result<(), Error> {
.collect();

log::info!("Getting devices list from Netbox");
let netbox_devices = netbox_client
let mut netbox_devices = netbox_client
.get_devices(&opt.netbox_devices_filter)
.await?;

if opt.netbox_vms_filter.is_some() {
log::info!("Getting VMS list rom Netbox");
let mut vms = netbox_client
.get_vms(&opt.netbox_vms_filter.unwrap())
.await?;
log::debug!("Merging VMs and Devices lists");
netbox_devices.append(&mut vms);
}

log::debug!("Building netbox devices hashmap");
let netbox_hashmap: HashMap<_, _> = netbox_devices
let mut netbox_hashmap: HashMap<_, _> = netbox_devices
.into_iter()
.filter_map(|device| match device.primary_ip4 {
Some(x) => Some((
Expand Down Expand Up @@ -144,7 +160,7 @@ async fn main() -> Result<(), Error> {

#[cfg(test)]
mod tests {
use flexi_logger::{Duplicate, Logger, LogTarget};
use flexi_logger::{Duplicate, LogTarget, Logger};

#[ctor::ctor]
fn enable_logging() {
Expand Down
41 changes: 38 additions & 3 deletions src/rest/netbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use crate::common::APP_USER_AGENT;
use anyhow::{anyhow, Error, Result};
use reqwest::header::{HeaderMap, HeaderValue};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

const API_LIMIT: u32 = 100;
const PATH_PING: &str = "/api/dcim/devices/?name=netbox2netshot-ping";
const PATH_DCIM_DEVICES: &str = "/api/dcim/devices/";
const PATH_VIRT_VM: &str = "/api/virtualization/virtual-machines/";

/// The Netbox client
#[derive(Debug)]
pub struct NetboxClient {
pub url: String,
Expand Down Expand Up @@ -94,13 +95,14 @@ impl NetboxClient {
/// Get a single device page
pub async fn get_devices_page(
&self,
path: &str,
query_string: &String,
limit: u32,
offset: u32,
) -> Result<NetboxDCIMDeviceList, Error> {
let url = format!(
"{}{}?limit={}&offset={}&{}",
self.url, PATH_DCIM_DEVICES, limit, offset, query_string
self.url, path, limit, offset, query_string
);
let page: NetboxDCIMDeviceList = self.client.get(url).send().await?.json().await?;
Ok(page)
Expand All @@ -113,7 +115,7 @@ impl NetboxClient {

loop {
let mut response = self
.get_devices_page(&query_string, API_LIMIT, offset)
.get_devices_page(PATH_DCIM_DEVICES, &query_string, API_LIMIT, offset)
.await?;

devices.append(&mut response.results);
Expand All @@ -138,6 +140,39 @@ impl NetboxClient {
log::info!("Fetched {} devices from Netbox", devices.len());
Ok(devices)
}

/// Get the VMs as device using the given filter
pub async fn get_vms(&self, query_string: &String) -> Result<Vec<Device>, Error> {
let mut devices: Vec<Device> = Vec::new();
let mut offset = 0;

loop {
let mut response = self
.get_devices_page(PATH_VIRT_VM, &query_string, API_LIMIT, offset)
.await?;

devices.append(&mut response.results);

let pages_count = response.count / API_LIMIT;
log::debug!(
"Got {} VM devices on the {} matches (page {}/{})",
devices.len(),
response.count,
(offset / API_LIMIT),
pages_count
);

match response.next {
Some(x) => {
offset = extract_offset(&x)?;
}
None => break,
}
}

log::info!("Fetched {} VM devices from Netbox", devices.len());
Ok(devices)
}
}

#[cfg(test)]
Expand Down

0 comments on commit d047179

Please sign in to comment.