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
/**
* Copyright 2019 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#[macro_use]
extern crate nom;
#[macro_use]
extern crate point_derive;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate xml_attributes_derive;

use crate::error::MetricsResult;
use std::fmt::Debug;

use log::trace;
use reqwest::header::ACCEPT;
use serde::de::{Deserialize, DeserializeOwned};
use serde::Deserializer;

pub mod brocade;
pub mod error;
pub mod hitachi;
pub mod ir;
#[cfg(feature = "isilon-library")]
pub mod isilon;
pub mod netapp;
pub mod openstack;
pub mod scaleio;
pub mod solidfire;
pub mod telegraf;
pub mod vmax;
pub mod vnx;
pub mod xtremio;

pub trait IntoPoint {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<ir::TsPoint>;
}

pub trait ChildPoint {
    fn sub_point(&self, p: &mut ir::TsPoint);
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum StringOrInt {
    String(String),
    Int(i64),
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum StringOrFloat {
    String(String),
    Float(f64),
}

fn deserialize_string_or_int<'de, D>(deserializer: D) -> ::std::result::Result<i64, D::Error>
where
    D: Deserializer<'de>,
{
    use serde::de::Error;
    match StringOrInt::deserialize(deserializer)? {
        StringOrInt::String(s) => s.parse().map_err(D::Error::custom),
        StringOrInt::Int(i) => Ok(i),
    }
}

fn deserialize_string_or_float<'de, D>(deserializer: D) -> ::std::result::Result<f64, D::Error>
where
    D: Deserializer<'de>,
{
    use serde::de::Error;
    match StringOrFloat::deserialize(deserializer)? {
        StringOrFloat::String(s) => s.parse().map_err(D::Error::custom),
        StringOrFloat::Float(i) => Ok(i),
    }
}

pub fn get<T>(
    client: &reqwest::Client,
    endpoint: &str,
    user: &str,
    pass: Option<&str>,
) -> MetricsResult<T>
where
    T: DeserializeOwned + Debug,
{
    let res = client
        .get(endpoint)
        .basic_auth(user, pass)
        .header(ACCEPT, "application/json")
        .send()?
        .error_for_status()?
        .text()?;
    trace!("server returned: {}", res);
    let json: Result<T, serde_json::Error> = serde_json::from_str(&res);
    trace!("json result: {:?}", json);
    Ok(json?)
}