netbox-cli 0.6.0

full-featured cli client for the netbox api
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use reqwest::Method;
use serde_json::Value;

use super::output::{print_dry_run, print_output};
use super::util::{load_json, normalize_api_path, request_raw_with_context};
use crate::config::{ConfigFile, config_path, validate_profile};
use crate::{
    ApiClient, AvailabilityAction, BranchAction, ConfigAction, DashboardAction, NamedLookupAction,
    OutputConfig, TaskAction, TraceableResource,
};

pub(crate) async fn handle_dashboard_action(
    client: &impl ApiClient,
    output: &OutputConfig,
    action: DashboardAction,
) -> Result<(), Box<dyn std::error::Error>> {
    match action {
        DashboardAction::Get => {
            let response =
                request_raw_with_context(client, Method::GET, "extras/dashboard/", None).await?;
            print_output(&response, output)?;
        }
        DashboardAction::Update { input } => {
            let body: Value = load_json(&input)?;
            if output.dry_run {
                print_dry_run(Method::PUT, "extras/dashboard/", None, Some(&body))?;
            } else {
                let response =
                    request_raw_with_context(client, Method::PUT, "extras/dashboard/", Some(&body))
                        .await?;
                print_output(&response, output)?;
            }
        }
        DashboardAction::Patch { input } => {
            let body: Value = load_json(&input)?;
            if output.dry_run {
                print_dry_run(Method::PATCH, "extras/dashboard/", None, Some(&body))?;
            } else {
                let response = request_raw_with_context(
                    client,
                    Method::PATCH,
                    "extras/dashboard/",
                    Some(&body),
                )
                .await?;
                print_output(&response, output)?;
            }
        }
        DashboardAction::Delete => {
            if output.dry_run {
                print_dry_run(Method::DELETE, "extras/dashboard/", None, None)?;
            } else {
                let response =
                    request_raw_with_context(client, Method::DELETE, "extras/dashboard/", None)
                        .await?;
                if response == Value::Null {
                    println!("deleted dashboard");
                } else {
                    print_output(&response, output)?;
                }
            }
        }
    }

    Ok(())
}

pub(crate) async fn handle_named_lookup(
    client: &impl ApiClient,
    output: &OutputConfig,
    base_path: &str,
    action: NamedLookupAction,
) -> Result<(), Box<dyn std::error::Error>> {
    let base_path = normalize_api_path(base_path);
    match action {
        NamedLookupAction::List => {
            let response = request_raw_with_context(client, Method::GET, &base_path, None).await?;
            print_output(&response, output)?;
        }
        NamedLookupAction::Get { name } => {
            let path = format!("{}/{}/", base_path.trim_end_matches('/'), name);
            let response = request_raw_with_context(client, Method::GET, &path, None).await?;
            print_output(&response, output)?;
        }
    }

    Ok(())
}

pub(crate) async fn handle_branch_action(
    client: &impl ApiClient,
    output: &OutputConfig,
    id: u64,
    action: BranchAction,
) -> Result<(), Box<dyn std::error::Error>> {
    let (suffix, body) = match action {
        BranchAction::Merge { input } => ("merge", load_json(&input)?),
        BranchAction::Revert { input } => ("revert", load_json(&input)?),
        BranchAction::Sync { input } => ("sync", load_json(&input)?),
    };

    let path = format!("plugins/branching/branches/{}/{}/", id, suffix);
    if output.dry_run {
        print_dry_run(Method::POST, &path, None, Some(&body))?;
    } else {
        let response = request_raw_with_context(client, Method::POST, &path, Some(&body)).await?;
        print_output(&response, output)?;
    }
    Ok(())
}

pub(crate) async fn handle_availability_action(
    client: &impl ApiClient,
    output: &OutputConfig,
    path: &str,
    action: AvailabilityAction,
) -> Result<(), Box<dyn std::error::Error>> {
    match action {
        AvailabilityAction::List => {
            let response = request_raw_with_context(client, Method::GET, path, None).await?;
            print_output(&response, output)?;
        }
        AvailabilityAction::Create { input } => {
            let body: Value = load_json(&input)?;
            if output.dry_run {
                print_dry_run(Method::POST, path, None, Some(&body))?;
            } else {
                let response =
                    request_raw_with_context(client, Method::POST, path, Some(&body)).await?;
                print_output(&response, output)?;
            }
        }
    }
    Ok(())
}

pub(crate) async fn handle_task_action(
    client: &impl ApiClient,
    output: &OutputConfig,
    id: &str,
    action: TaskAction,
) -> Result<(), Box<dyn std::error::Error>> {
    let suffix = match action {
        TaskAction::Enqueue => "enqueue",
        TaskAction::Stop => "stop",
        TaskAction::Requeue => "requeue",
        TaskAction::Delete => "delete",
    };

    let path = format!("core/background-tasks/{}/{}/", id, suffix);
    if output.dry_run {
        print_dry_run(Method::POST, &path, None, None)?;
    } else {
        let response =
            request_raw_with_context(client, Method::POST, &path, Some(&Value::Null)).await?;
        print_output(&response, output)?;
    }
    Ok(())
}

pub(crate) async fn handle_sync_action(
    client: &impl ApiClient,
    output: &OutputConfig,
    path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    if output.dry_run {
        print_dry_run(Method::POST, path, None, None)?;
    } else {
        let response =
            request_raw_with_context(client, Method::POST, path, Some(&Value::Null)).await?;
        print_output(&response, output)?;
    }
    Ok(())
}

pub(crate) async fn handle_get_action(
    client: &impl ApiClient,
    output: &OutputConfig,
    path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let response = request_raw_with_context(client, Method::GET, path, None).await?;
    print_output(&response, output)?;
    Ok(())
}

pub(crate) async fn handle_trace_action(
    client: &impl ApiClient,
    output: &OutputConfig,
    resource: TraceableResource,
) -> Result<(), Box<dyn std::error::Error>> {
    let path = match resource {
        TraceableResource::Interface { id } => format!("dcim/interfaces/{}/trace/", id),
        TraceableResource::ConsolePort { id } => format!("dcim/console-ports/{}/trace/", id),
        TraceableResource::ConsoleServerPort { id } => {
            format!("dcim/console-server-ports/{}/trace/", id)
        }
        TraceableResource::PowerPort { id } => format!("dcim/power-ports/{}/trace/", id),
        TraceableResource::PowerOutlet { id } => format!("dcim/power-outlets/{}/trace/", id),
        TraceableResource::PowerFeed { id } => format!("dcim/power-feeds/{}/trace/", id),
    };
    let response = request_raw_with_context(client, Method::GET, &path, None).await?;
    print_output(&response, output)?;
    Ok(())
}

pub(crate) fn handle_config_command(
    action: &ConfigAction,
    profile_name: &str,
    config_file: Option<&ConfigFile>,
) -> Result<(), Box<dyn std::error::Error>> {
    match action {
        ConfigAction::Path => match config_path() {
            Some(path) => println!("{}", path.display()),
            None => println!("(could not determine config directory)"),
        },
        ConfigAction::List => match config_file {
            Some(cf) => {
                let mut names: Vec<_> = cf.profile_names();
                names.sort();
                for name in names {
                    if name == profile_name {
                        println!("{} (active)", name);
                    } else {
                        println!("{}", name);
                    }
                }
            }
            None => {
                println!("(no config file found)");
                if let Some(path) = config_path() {
                    println!("expected at: {}", path.display());
                }
            }
        },
        ConfigAction::Show => match config_file {
            Some(cf) => {
                if let Some(profile) = cf.get_profile(profile_name) {
                    let toml = toml::to_string_pretty(profile)?;
                    println!("[{}]", profile_name);
                    print!("{}", toml);
                } else {
                    return Err(format!("profile '{}' not found", profile_name).into());
                }
            }
            None => {
                return Err("no config file found".into());
            }
        },
        ConfigAction::Validate => {
            match config_file {
                Some(cf) => {
                    if let Some(profile) = cf.get_profile(profile_name) {
                        match validate_profile(profile) {
                            Ok(()) => {
                                println!("profile '{}' is valid", profile_name);
                                // try to resolve token to catch command errors
                                match profile.resolve_token() {
                                    Ok(Some(_)) => println!("  token: ok"),
                                    Ok(None) => println!(
                                        "  token: (not set, will need --token or NETBOX_TOKEN)"
                                    ),
                                    Err(e) => println!("  token: error - {}", e),
                                }
                            }
                            Err(e) => {
                                return Err(
                                    format!("profile '{}' invalid: {}", profile_name, e).into()
                                );
                            }
                        }
                    } else {
                        return Err(format!("profile '{}' not found", profile_name).into());
                    }
                }
                None => {
                    return Err("no config file found".into());
                }
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::JsonInput;
    use crate::cli::test_util::*;
    use serde_json::{Value, json};

    #[tokio::test]
    async fn handle_dashboard_action_paths() {
        let client = FakeApiClient::new(Value::Null);
        handle_dashboard_action(&client, &output_config(), DashboardAction::Get)
            .await
            .unwrap();
        handle_dashboard_action(&client, &output_config(), DashboardAction::Delete)
            .await
            .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].path, "extras/dashboard/");
        assert_eq!(calls[1].path, "extras/dashboard/");
    }

    #[tokio::test]
    async fn handle_named_lookup_get_builds_path() {
        let client = FakeApiClient::new(json!({"ok": true}));
        let action = NamedLookupAction::Get {
            name: "queue-1".to_string(),
        };
        handle_named_lookup(&client, &output_config(), "core/background-queues/", action)
            .await
            .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].path, "core/background-queues/queue-1/");
    }

    #[tokio::test]
    async fn handle_branch_action_builds_path() {
        let client = FakeApiClient::new(json!({"ok": true}));
        let input = JsonInput {
            json: Some(r#"{"confirm":true}"#.to_string()),
            file: None,
        };
        handle_branch_action(&client, &output_config(), 9, BranchAction::Merge { input })
            .await
            .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].method, Method::POST);
        assert_eq!(calls[0].path, "plugins/branching/branches/9/merge/");
    }

    #[tokio::test]
    async fn handle_availability_action_list_calls_get() {
        let client = FakeApiClient::new(json!([]));
        handle_availability_action(
            &client,
            &output_config(),
            "ipam/prefixes/1/available-ips/",
            AvailabilityAction::List,
        )
        .await
        .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].method, Method::GET);
        assert_eq!(calls[0].path, "ipam/prefixes/1/available-ips/");
    }

    #[tokio::test]
    async fn handle_availability_action_create_calls_post() {
        let client = FakeApiClient::new(json!([]));
        let input = JsonInput {
            json: Some(r#"[{"description":"test"}]"#.to_string()),
            file: None,
        };
        handle_availability_action(
            &client,
            &output_config(),
            "ipam/prefixes/1/available-ips/",
            AvailabilityAction::Create { input },
        )
        .await
        .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].method, Method::POST);
        assert_eq!(calls[0].path, "ipam/prefixes/1/available-ips/");
    }

    #[tokio::test]
    async fn handle_task_action_builds_paths() {
        let client = FakeApiClient::new(json!({}));
        handle_task_action(&client, &output_config(), "abc123", TaskAction::Enqueue)
            .await
            .unwrap();
        handle_task_action(&client, &output_config(), "abc123", TaskAction::Stop)
            .await
            .unwrap();
        handle_task_action(&client, &output_config(), "abc123", TaskAction::Requeue)
            .await
            .unwrap();
        handle_task_action(&client, &output_config(), "abc123", TaskAction::Delete)
            .await
            .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].path, "core/background-tasks/abc123/enqueue/");
        assert_eq!(calls[1].path, "core/background-tasks/abc123/stop/");
        assert_eq!(calls[2].path, "core/background-tasks/abc123/requeue/");
        assert_eq!(calls[3].path, "core/background-tasks/abc123/delete/");
        for call in &calls {
            assert_eq!(call.method, Method::POST);
        }
    }

    #[tokio::test]
    async fn handle_sync_action_calls_post() {
        let client = FakeApiClient::new(json!({}));
        handle_sync_action(&client, &output_config(), "core/data-sources/7/sync/")
            .await
            .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].method, Method::POST);
        assert_eq!(calls[0].path, "core/data-sources/7/sync/");
    }

    #[tokio::test]
    async fn handle_get_action_calls_get() {
        let client = FakeApiClient::new(json!({}));
        handle_get_action(
            &client,
            &output_config(),
            "extras/custom-field-choice-sets/5/choices/",
        )
        .await
        .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].method, Method::GET);
        assert_eq!(calls[0].path, "extras/custom-field-choice-sets/5/choices/");
    }

    #[tokio::test]
    async fn handle_trace_action_builds_paths() {
        let client = FakeApiClient::new(json!({}));
        handle_trace_action(
            &client,
            &output_config(),
            TraceableResource::Interface { id: 1 },
        )
        .await
        .unwrap();
        handle_trace_action(
            &client,
            &output_config(),
            TraceableResource::ConsolePort { id: 2 },
        )
        .await
        .unwrap();
        handle_trace_action(
            &client,
            &output_config(),
            TraceableResource::ConsoleServerPort { id: 3 },
        )
        .await
        .unwrap();
        handle_trace_action(
            &client,
            &output_config(),
            TraceableResource::PowerPort { id: 4 },
        )
        .await
        .unwrap();
        handle_trace_action(
            &client,
            &output_config(),
            TraceableResource::PowerOutlet { id: 5 },
        )
        .await
        .unwrap();
        handle_trace_action(
            &client,
            &output_config(),
            TraceableResource::PowerFeed { id: 6 },
        )
        .await
        .unwrap();
        let calls = client.calls();
        assert_eq!(calls[0].path, "dcim/interfaces/1/trace/");
        assert_eq!(calls[1].path, "dcim/console-ports/2/trace/");
        assert_eq!(calls[2].path, "dcim/console-server-ports/3/trace/");
        assert_eq!(calls[3].path, "dcim/power-ports/4/trace/");
        assert_eq!(calls[4].path, "dcim/power-outlets/5/trace/");
        assert_eq!(calls[5].path, "dcim/power-feeds/6/trace/");
        for call in &calls {
            assert_eq!(call.method, Method::GET);
        }
    }
}