netbox 0.8.0

ergonomic rust client for NetBox 4.x REST API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! client configuration

use crate::error::{Error, Result};
use crate::hooks::HttpHooks;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use std::sync::Arc;
use std::time::Duration;
use url::Url;

/// configuration for the netbox client
#[derive(Clone)]
pub struct ClientConfig {
    /// original base url input
    pub(crate) raw_base_url: String,

    /// base url of the netbox instance (e.g., "<https://netbox.example.com>")
    pub(crate) base_url: Url,

    /// whether the provided base url parsed successfully
    pub(crate) base_url_valid: bool,

    /// api authentication token
    pub(crate) token: String,

    /// request timeout duration
    pub(crate) timeout: Duration,

    /// maximum number of retries for failed requests
    pub(crate) max_retries: u32,

    /// user agent string
    pub(crate) user_agent: String,

    /// whether to verify ssl certificates
    pub(crate) verify_ssl: bool,

    /// additional headers to send with every request
    pub(crate) extra_headers: HeaderMap,

    /// optional prebuilt reqwest client.
    pub(crate) http_client: Option<reqwest::Client>,

    /// optional callback to customize reqwest client builder.
    pub(crate) http_client_builder:
        Option<Arc<dyn Fn(reqwest::ClientBuilder) -> reqwest::ClientBuilder + Send + Sync>>,

    /// optional request/response hooks.
    pub(crate) http_hooks: Option<Arc<dyn HttpHooks>>,
}

impl ClientConfig {
    /// create a new client configuration
    ///
    /// # Arguments
    ///
    /// * `base_url` - the base url of the netbox instance (with or without trailing slash)
    /// * `token` - the api authentication token
    ///
    /// # Example
    ///
    /// ```
    /// use netbox::ClientConfig;
    ///
    /// let config = ClientConfig::new("https://netbox.example.com", "your-token-here");
    /// ```
    pub fn new(base_url: impl AsRef<str>, token: impl Into<String>) -> Self {
        let base_url_str = base_url.as_ref();

        let normalized = base_url_str.trim_end_matches('/');

        // parse URL, this will be validated when building the client
        let (base_url, base_url_valid) = match Url::parse(normalized)
            .or_else(|_| Url::parse(&format!("https://{}", normalized)))
        {
            Ok(url) => (url, true),
            Err(_) => (Url::parse("https://invalid.invalid").unwrap(), false),
        };

        Self {
            raw_base_url: base_url_str.to_string(),
            base_url,
            base_url_valid,
            token: token.into(),
            timeout: Duration::from_secs(30),
            max_retries: 3,
            user_agent: format!("netbox-rs/{} (Rust)", env!("CARGO_PKG_VERSION")),
            verify_ssl: true,
            extra_headers: HeaderMap::new(),
            http_client: None,
            http_client_builder: None,
            http_hooks: None,
        }
    }

    /// set the request timeout
    ///
    /// default: 30 seconds
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// set the maximum number of retries
    ///
    /// default: 3
    ///
    /// retries apply to get/delete requests (and operations marked idempotent) for transient network errors and 429/5xx responses.
    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// set a custom user agent string
    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = user_agent.into();
        self
    }

    /// disable ssl certificate verification (not recommended for production)
    ///
    /// default: enabled
    pub fn with_ssl_verification(mut self, verify: bool) -> Self {
        self.verify_ssl = verify;
        self
    }

    /// add a header to every request
    pub fn with_header(mut self, name: HeaderName, value: HeaderValue) -> Self {
        self.extra_headers.insert(name, value);
        self
    }

    /// add a set of headers to every request
    pub fn with_headers(mut self, headers: HeaderMap) -> Self {
        self.extra_headers.extend(headers);
        self
    }

    /// inject a prebuilt reqwest client.
    ///
    /// when set, the client uses this reqwest instance directly.
    /// this takes precedence over `with_http_client_builder`.
    ///
    /// use this when you need full control over reqwest behavior
    /// (custom middleware stack, proxying, transport tuning, etc.).
    pub fn with_http_client(mut self, http_client: reqwest::Client) -> Self {
        self.http_client = Some(http_client);
        self
    }

    /// customize the reqwest client builder before build.
    ///
    /// this is ignored if `with_http_client` is also used.
    ///
    /// use this when you want to adjust builder settings while retaining
    /// netbox default header/auth/timeout wiring.
    pub fn with_http_client_builder<F>(mut self, builder: F) -> Self
    where
        F: Fn(reqwest::ClientBuilder) -> reqwest::ClientBuilder + Send + Sync + 'static,
    {
        self.http_client_builder = Some(Arc::new(builder));
        self
    }

    /// attach hooks for request/response lifecycle customization.
    ///
    /// hooks can mutate outgoing requests and observe responses/errors.
    /// avoid putting secrets into logs/metrics from hook implementations.
    pub fn with_http_hooks<H>(mut self, hooks: H) -> Self
    where
        H: HttpHooks + 'static,
    {
        self.http_hooks = Some(Arc::new(hooks));
        self
    }

    /// access extra headers configured on this client
    pub fn extra_headers(&self) -> &HeaderMap {
        &self.extra_headers
    }

    /// validate the configuration
    pub(crate) fn validate(&self) -> Result<()> {
        if !self.base_url_valid {
            return Err(Error::Config(format!(
                "Invalid base URL: {}",
                self.raw_base_url
            )));
        }

        if self.base_url.scheme() != "http" && self.base_url.scheme() != "https" {
            return Err(Error::Config(format!(
                "Invalid URL scheme: {}. Must be http or https",
                self.base_url.scheme()
            )));
        }

        if self.token.is_empty() {
            return Err(Error::Config("API token cannot be empty".to_string()));
        }

        Ok(())
    }

    /// build the full api url by joining with a path
    ///
    /// this handles trailing slashes correctly. netbox returns absolute
    /// `next`/`previous` pagination cursors; when `path` is already an absolute
    /// url it is followed directly, but only if its origin matches the base url
    /// (so a rogue server can't redirect token-bearing requests to another host).
    pub(crate) fn build_url(&self, path: &str) -> Result<Url> {
        if is_absolute_http_url(path) {
            let url = Url::parse(path)?;
            if url.origin() != self.base_url.origin() {
                return Err(Error::Pagination(format!(
                    "pagination cursor points to unexpected origin {} (expected {}); refusing to follow it",
                    url.origin().ascii_serialization(),
                    self.base_url.origin().ascii_serialization(),
                )));
            }
            return Ok(url);
        }

        let path = path.trim_start_matches('/');
        let base_str = self.base_url.as_str().trim_end_matches('/');
        let url_str = format!("{}/api/{}", base_str, path);
        Url::parse(&url_str).map_err(Error::from)
    }
}

/// whether `path` is an absolute http(s) url (as returned in netbox pagination
/// cursors) rather than a relative api path. the scheme match is ascii-case
/// insensitive; slicing on byte prefixes avoids panicking on non-ascii input.
fn is_absolute_http_url(path: &str) -> bool {
    let bytes = path.as_bytes();
    bytes
        .get(..7)
        .is_some_and(|p| p.eq_ignore_ascii_case(b"http://"))
        || bytes
            .get(..8)
            .is_some_and(|p| p.eq_ignore_ascii_case(b"https://"))
}

impl std::fmt::Debug for ClientConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientConfig")
            .field("base_url", &self.base_url)
            .field("timeout", &self.timeout)
            .field("max_retries", &self.max_retries)
            .field("user_agent", &self.user_agent)
            .field("verify_ssl", &self.verify_ssl)
            .field("extra_headers", &self.extra_headers.len())
            .field("http_client", &self.http_client.is_some())
            .field("http_client_builder", &self.http_client_builder.is_some())
            .field("http_hooks", &self.http_hooks.is_some())
            .field("token", &"<redacted>")
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hooks::HttpHooks;
    use reqwest::Method;
    use reqwest::header::{HeaderMap, HeaderName, HeaderValue};

    struct TestHooks;

    impl HttpHooks for TestHooks {
        fn on_request(
            &self,
            _method: &Method,
            _path: &str,
            _request: &mut reqwest::Request,
        ) -> Result<()> {
            Ok(())
        }
    }

    #[test]
    fn test_new_config() {
        let config = ClientConfig::new("https://netbox.example.com", "test-token");
        // URL parsing may add a trailing slash, so we check the trimmed version
        assert_eq!(
            config.base_url.as_str().trim_end_matches('/'),
            "https://netbox.example.com"
        );
        assert_eq!(config.token, "test-token");
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert_eq!(config.max_retries, 3);
        assert!(config.verify_ssl);
    }

    #[test]
    fn test_normalize_url_with_trailing_slash() {
        let config = ClientConfig::new("https://netbox.example.com/", "token");
        let config2 = ClientConfig::new("https://netbox.example.com", "token");
        assert_eq!(
            config.base_url.as_str().trim_end_matches('/'),
            config2.base_url.as_str().trim_end_matches('/')
        );
    }

    #[test]
    fn test_build_url() {
        let config = ClientConfig::new("https://netbox.example.com", "token");

        let url = config.build_url("/dcim/devices/").unwrap();
        assert_eq!(url.as_str(), "https://netbox.example.com/api/dcim/devices/");

        let url = config.build_url("dcim/devices/").unwrap();
        assert_eq!(url.as_str(), "https://netbox.example.com/api/dcim/devices/");
    }

    #[test]
    fn test_build_url_absolute_same_origin() {
        let config = ClientConfig::new("https://netbox.example.com", "token");

        // absolute pagination cursors on the same origin are followed as-is
        let url = config
            .build_url("https://netbox.example.com/api/dcim/devices/?limit=50&offset=50")
            .unwrap();
        assert_eq!(
            url.as_str(),
            "https://netbox.example.com/api/dcim/devices/?limit=50&offset=50"
        );

        // an explicit default port is the same origin (url normalizes it away)
        let url = config
            .build_url("https://netbox.example.com:443/api/dcim/devices/?offset=50")
            .unwrap();
        assert_eq!(
            url.as_str(),
            "https://netbox.example.com/api/dcim/devices/?offset=50"
        );
    }

    #[test]
    fn test_build_url_absolute_foreign_origin_rejected() {
        let config = ClientConfig::new("https://netbox.example.com", "token");

        let err = config
            .build_url("https://evil.example.com/api/dcim/devices/?offset=50")
            .unwrap_err();
        assert!(matches!(err, Error::Pagination(_)));
        let msg = err.to_string();
        assert!(msg.contains("evil.example.com"));
        assert!(msg.contains("netbox.example.com"));

        // same host, different scheme is a different origin too
        let err = config
            .build_url("http://netbox.example.com/api/dcim/devices/?offset=50")
            .unwrap_err();
        assert!(matches!(err, Error::Pagination(_)));

        // same host and scheme, different port is rejected
        let err = config
            .build_url("https://netbox.example.com:8443/api/dcim/devices/?offset=50")
            .unwrap_err();
        assert!(matches!(err, Error::Pagination(_)));
    }

    #[test]
    fn test_validation() {
        let config = ClientConfig::new("https://netbox.example.com", "token");
        assert!(config.validate().is_ok());

        let empty_token = ClientConfig::new("https://netbox.example.com", "");
        assert!(empty_token.validate().is_err());
    }

    #[test]
    fn test_builder_methods() {
        let config = ClientConfig::new("https://netbox.example.com", "token")
            .with_timeout(Duration::from_secs(60))
            .with_max_retries(5)
            .with_user_agent("custom-agent")
            .with_ssl_verification(false);

        assert_eq!(config.timeout, Duration::from_secs(60));
        assert_eq!(config.max_retries, 5);
        assert_eq!(config.user_agent, "custom-agent");
        assert!(!config.verify_ssl);
    }

    #[test]
    fn test_with_header() {
        let header_name = HeaderName::from_static("x-custom");
        let header_value = HeaderValue::from_static("value");
        let config = ClientConfig::new("https://netbox.example.com", "token")
            .with_header(header_name.clone(), header_value.clone());

        let stored = config.extra_headers.get(&header_name).unwrap();
        assert_eq!(stored, &header_value);
    }

    #[test]
    fn test_with_headers() {
        let mut headers = HeaderMap::new();
        headers.insert(
            HeaderName::from_static("x-one"),
            HeaderValue::from_static("one"),
        );
        headers.insert(
            HeaderName::from_static("x-two"),
            HeaderValue::from_static("two"),
        );

        let config =
            ClientConfig::new("https://netbox.example.com", "token").with_headers(headers.clone());

        for (name, value) in headers.iter() {
            assert_eq!(config.extra_headers.get(name).unwrap(), value);
        }
    }

    #[test]
    fn test_with_http_client() {
        let prebuilt = reqwest::Client::new();
        let config =
            ClientConfig::new("https://netbox.example.com", "token").with_http_client(prebuilt);
        assert!(config.http_client.is_some());
    }

    #[test]
    fn test_with_http_client_builder() {
        let config = ClientConfig::new("https://netbox.example.com", "token")
            .with_http_client_builder(|builder| builder.pool_max_idle_per_host(2));
        assert!(config.http_client_builder.is_some());
    }

    #[test]
    fn test_with_http_hooks() {
        let config =
            ClientConfig::new("https://netbox.example.com", "token").with_http_hooks(TestHooks);
        assert!(config.http_hooks.is_some());
    }
}