2014-02-07 20:08:32 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 17:32:48 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-19 19:29:58 -08:00
|
|
|
extern crate collections;
|
2014-02-21 14:18:39 -08:00
|
|
|
extern crate serialize;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2014-05-29 19:03:06 -07:00
|
|
|
use std::collections::HashMap;
|
2014-12-31 17:29:22 +13:00
|
|
|
use serialize::json::{self, Json};
|
2013-05-24 19:35:29 -07:00
|
|
|
use std::option;
|
2012-07-11 14:31:35 -07:00
|
|
|
|
2013-03-01 10:44:43 -08:00
|
|
|
enum object {
|
2012-07-11 14:31:35 -07:00
|
|
|
bool_value(bool),
|
|
|
|
int_value(i64),
|
|
|
|
}
|
|
|
|
|
2014-11-22 13:48:01 -05:00
|
|
|
fn lookup(table: json::Object, key: String, default: String) -> String
|
2012-07-11 14:31:35 -07:00
|
|
|
{
|
2015-01-01 23:53:35 -08:00
|
|
|
match table.get(&key) {
|
2014-11-28 11:57:41 -05:00
|
|
|
option::Option::Some(&Json::String(ref s)) => {
|
2014-06-28 17:27:29 +02:00
|
|
|
s.to_string()
|
2012-07-11 14:31:35 -07:00
|
|
|
}
|
2014-11-28 11:57:41 -05:00
|
|
|
option::Option::Some(value) => {
|
2014-10-14 21:07:11 -04:00
|
|
|
println!("{} was expected to be a string but is a {}", key, value);
|
2012-07-11 14:31:35 -07:00
|
|
|
default
|
|
|
|
}
|
2014-11-28 11:57:41 -05:00
|
|
|
option::Option::None => {
|
2012-07-11 14:31:35 -07:00
|
|
|
default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn add_interface(_store: int, managed_ip: String, data: json::Json) -> (String, object)
|
2012-07-11 14:31:35 -07:00
|
|
|
{
|
2013-07-02 12:47:32 -07:00
|
|
|
match &data {
|
2014-11-26 10:21:45 -08:00
|
|
|
&Json::Object(ref interface) => {
|
2014-06-28 17:27:29 +02:00
|
|
|
let name = lookup(interface.clone(),
|
2014-05-25 03:17:19 -07:00
|
|
|
"ifDescr".to_string(),
|
|
|
|
"".to_string());
|
2014-05-27 20:44:58 -07:00
|
|
|
let label = format!("{}-{}", managed_ip, name);
|
2012-07-11 14:31:35 -07:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
(label, object::bool_value(false))
|
2012-07-11 14:31:35 -07:00
|
|
|
}
|
2013-07-02 12:47:32 -07:00
|
|
|
_ => {
|
2014-10-14 21:07:11 -04:00
|
|
|
println!("Expected dict for {} interfaces, found {}", managed_ip, data);
|
2014-11-06 00:05:53 -08:00
|
|
|
("gnos:missing-interface".to_string(), object::bool_value(true))
|
2012-07-11 14:31:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn add_interfaces(store: int, managed_ip: String, device: HashMap<String, json::Json>)
|
|
|
|
-> Vec<(String, object)> {
|
2014-10-14 23:05:01 -07:00
|
|
|
match device["interfaces".to_string()]
|
2012-07-11 14:31:35 -07:00
|
|
|
{
|
2014-11-26 10:21:45 -08:00
|
|
|
Json::Array(ref interfaces) =>
|
2012-07-11 14:31:35 -07:00
|
|
|
{
|
2014-03-05 15:28:08 -08:00
|
|
|
interfaces.iter().map(|interface| {
|
2013-07-02 12:47:32 -07:00
|
|
|
add_interface(store, managed_ip.clone(), (*interface).clone())
|
2014-03-05 15:28:08 -08:00
|
|
|
}).collect()
|
2012-07-11 14:31:35 -07:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ =>
|
2012-07-11 14:31:35 -07:00
|
|
|
{
|
2014-10-14 21:07:11 -04:00
|
|
|
println!("Expected list for {} interfaces, found {}", managed_ip,
|
2014-10-14 23:05:01 -07:00
|
|
|
device["interfaces".to_string()]);
|
2014-03-05 14:02:44 -08:00
|
|
|
Vec::new()
|
2012-07-11 14:31:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {}
|