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
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
/**
* 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
*/
use std::collections::HashMap;
use std::fmt::Debug;

use crate::error::MetricsResult;
use crate::ir::{TsPoint, TsValue};
use crate::IntoPoint;

use chrono::offset::Utc;
use chrono::DateTime;
use log::debug;
use serde::de::DeserializeOwned;
use uuid::Uuid;

#[derive(Clone, Deserialize, Debug)]
pub struct SolidfireConfig {
    /// The solidfire endpoint to use
    pub endpoint: String,
    pub user: String,
    pub password: String,
    /// Optional certificate file to use against the server
    /// der encoded
    pub certificate: Option<String>,
    /// The region this cluster is located in
    pub region: String,
}

pub struct Solidfire {
    client: reqwest::Client,
    config: SolidfireConfig,
}

impl Solidfire {
    pub fn new(client: &reqwest::Client, config: SolidfireConfig) -> Self {
        Solidfire {
            client: client.clone(),
            config,
        }
    }
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct AddressBlock {
    pub available: String,
    pub size: u64,
    pub start: String,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct Cluster {
    pub cipi: String,
    pub cluster: String,
    pub encryption_capable: bool,
    pub ensemble: Vec<String>,
    pub mipi: String,
    pub name: String,
    #[serde(rename = "nodeID")]
    pub node_id: String,
    #[serde(rename = "pendingNodeID")]
    pub pending_node_id: u64,
    pub role: u64,
    pub sipi: String,
    pub state: ClusterState,
    pub version: String,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct ClusterCapacity {
    active_block_space: u64,
    active_sessions: u64,
    #[serde(rename = "averageIOPS")]
    average_iops: u64,
    #[serde(rename = "clusterRecentIOSize")]
    cluster_recent_io_size: u64,
    #[serde(rename = "currentIOPS")]
    current_iops: u64,
    #[serde(rename = "maxIOPS")]
    max_iops: u64,
    max_over_provisionable_space: u64,
    max_provisioned_space: u64,
    max_used_metadata_space: u64,
    max_used_space: u64,
    non_zero_blocks: u64,
    peak_active_sessions: u64,
    #[serde(rename = "peakIOPS")]
    peak_iops: u64,
    provisioned_space: u64,
    timestamp: String,
    total_ops: u64,
    unique_blocks: u64,
    unique_blocks_used_space: u64,
    used_metadata_space: u64,
    used_metadata_space_in_snapshots: u64,
    used_space: u64,
    zero_blocks: u64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClusterCapacityResult {
    cluster_capacity: ClusterCapacity,
}

impl IntoPoint for ClusterCapacityResult {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.cluster_capacity.into_point(name, is_time_series)
    }
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct ClusterFullThreshold {
    pub block_fullness: FullnessStatus,
    pub fullness: String,
    pub max_metadata_over_provision_factor: u16,
    pub metadata_fullness: FullnessStatus,
    pub slice_reserve_used_threshold_pct: u16,
    pub stage2_aware_threshold: u16,
    pub stage2_block_threshold_bytes: u64,
    pub stage3_block_threshold_bytes: u64,
    pub stage3_block_threshold_percent: u16,
    pub stage3_low_threshold: i64,
    pub stage4_block_threshold_bytes: u64,
    pub stage4_critical_threshold: i64,
    pub stage5_block_threshold_bytes: u64,
    pub sum_total_cluster_bytes: u64,
    pub sum_total_metadata_cluster_bytes: u64,
    pub sum_used_cluster_bytes: u64,
    pub sum_used_metadata_cluster_bytes: u64,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct ClusterInfo {
    pub attributes: HashMap<String, String>,
    pub encryption_at_rest_state: EncryptionState,
    pub ensemble: Vec<String>,
    pub mvip: String,
    pub mvip_interface: String,
    #[serde(rename = "mvipNodeID")]
    pub mvip_node_id: u16,
    pub mvip_vlan_tag: String,
    pub name: String,
    pub rep_count: u16,
    pub svip: String,
    pub svip_interface: String,
    #[serde(rename = "svipNodeID")]
    pub svip_node_id: u16,
    pub svip_vlan_tag: String,
    #[serde(rename = "uniqueID")]
    pub unique_id: String,
    pub uuid: Uuid,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClusterInfoResult {
    pub cluster_info: ClusterInfo,
}

impl IntoPoint for ClusterInfoResult {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.cluster_info.into_point(name, is_time_series)
    }
}

#[derive(Debug, Deserialize)]
pub enum ClusterState {
    Available,
    Pending,
    Active,
    PendingActive,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct ClusterStat {
    #[serde(rename = "actualIOPS")]
    pub actual_iops: u64,
    #[serde(rename = "averageIOPSize")]
    pub average_iop_size: u64,
    pub client_queue_depth: u64,
    pub cluster_utilization: f64,
    pub latency_u_sec: u64,
    #[serde(rename = "normalizedIOPS")]
    pub normalized_iops: u64,
    // Total cumulative bytes read from the cluster since creation
    pub read_bytes: u64,
    pub read_bytes_last_sample: u64,
    pub read_latency_u_sec: u64,
    pub read_latency_u_sec_total: u64,
    pub read_ops: u64,
    pub read_ops_last_sample: u64,
    pub sample_period_msec: u64,
    pub services_count: u64,
    pub services_total: u64,
    pub timestamp: String,
    pub unaligned_reads: u64,
    pub unaligned_writes: u64,
    pub write_bytes: u64,
    pub write_bytes_last_sample: u64,
    pub write_latency_u_sec: u64,
    pub write_latency_u_sec_total: u64,
    pub write_ops: u64,
    pub write_ops_last_sample: u64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClusterStatsResult {
    pub cluster_stats: ClusterStat,
}

impl IntoPoint for ClusterStatsResult {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.cluster_stats.into_point(name, is_time_series)
    }
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct Drive {
    pub attributes: HashMap<String, String>,
    pub capacity: u64,
    pub classic_slot: String,
    #[serde(rename = "driveID")]
    pub drive_id: u64,
    #[serde(rename = "nodeID")]
    pub node_id: u64,
    pub serial: String,
    pub status: DriveStatus,
    #[serde(rename = "drive")]
    pub drive_type: DriveType,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum DriveStatus {
    Available,
    Active,
    Erasing,
    Failed,
    Removing,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum DriveType {
    Block,
    Unknown,
    Volume,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EncryptionState {
    Enabling,
    Enabled,
    Disabling,
    Disabled,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum FullnessStatus {
    Stage1Happy,
    Stage2Aware,
    Stage3Low,
    Stage4Critical,
    Stage5CompletelyConsumed,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct MetadataHosts {
    dead_secondaries: Vec<u64>,
    live_secondaries: Vec<u64>,
    primary: u64,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct Node {
    #[serde(rename = "associatedFServiceID")]
    pub associated_f_service_id: u64,
    #[serde(rename = "associatedMasterServiceID")]
    pub associated_master_service_id: u64,
    pub attributes: HashMap<String, String>,
    pub cip: String,
    pub cipi: String,
    pub fibre_channel_target_port_group: Option<u64>,
    pub mip: String,
    pub mipi: String,
    pub name: String,
    #[serde(rename = "nodeID")]
    pub node_id: u64,
    pub node_slot: String,
    pub platform_info: PlatformInfo,
    #[serde(rename = "protocolEndpointIDs")]
    pub protocol_endpoints_ids: Option<Vec<Uuid>>,
    pub sip: String,
    pub sipi: String,
    pub software_version: String,
    pub uuid: Uuid,
    pub virtual_networks: Vec<VirtualNetwork>,
}

#[derive(Debug, Deserialize)]
pub struct Nodes {
    pub nodes: Vec<Node>,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct NodeStats {
    pub count: u64,
    pub cpu: u16,
    pub cpu_total: u64,
    // Bytes in on the cluster interface
    pub c_bytes_in: u64,
    // Bytes out on the cluster interface
    pub c_bytes_out: u64,
    // Bytes in on the storage interface
    pub s_bytes_in: u64,
    // Bytes out on the storage interface
    pub s_bytes_out: u64,
    // Bytes in on the management interface
    pub m_bytes_in: u64,
    // Bytes out on the management interface
    pub m_bytes_out: u64,
    pub network_utilization_cluster: u16,
    pub network_utilization_storage: u16,
    pub read_latency_u_sec_total: u64,
    pub read_ops: u64,
    pub timestamp: String,
    pub used_memory: u64,
    pub write_latency_u_sec_total: u64,
    pub write_ops: u64,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct PlatformInfo {
    pub chassis_type: String,
    pub cpu_model: String,
    #[serde(rename = "nodeMemoryGB")]
    pub node_memory_gb: u64,
    pub node_type: String,
    pub platform_config_version: String,
}

#[derive(Debug, Deserialize)]
pub enum ReplicationMode {
    Async,
    Sync,
    SnapshotsOnly,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct RemoteReplication {
    pub mode: ReplicationMode,
    pub pause_limit: u64,
    #[serde(rename = "remoteServiceID")]
    pub remote_service_id: u64,
    pub resume_details: Option<String>,
    pub snapshot_replication: SnapshotReplication,
    pub state: String,
    pub state_details: Option<String>,
}

#[derive(Debug, Deserialize)]
pub enum RemoveStatus {
    Present,
    NotPresent,
    Syncing,
    Deleted,
}

#[derive(Debug, Deserialize, IntoPoint)]
pub struct Snapshot {
    pub attributes: HashMap<String, String>,
    pub checksum: String,
    pub create_time: String,
    pub enable_remote_replication: String,
    pub expiration_reason: String,
    pub expiration_time: String,
    #[serde(rename = "groupID")]
    pub group_id: u64,
    #[serde(rename = "groupSnapshotUUID")]
    pub group_snapshot_uuid: Uuid,
    pub name: String,
    pub remote_status: RemoveStatus,
    #[serde(rename = "snapshotID")]
    pub snapshot_id: String,
    #[serde(rename = "snapshotUUID")]
    pub snapshot_uuid: Uuid,
    pub status: SnapshotStatus,
    pub total_size: u64,
    #[serde(rename = "virtualVolumeID")]
    pub virtual_volume_id: Uuid,
    #[serde(rename = "volumeID")]
    pub volume_id: u64,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct SnapshotReplication {
    pub state: String,
    pub state_details: String,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct StorageContainer {
    #[serde(rename = "accountID")]
    pub account_id: u64,
    pub initiator_secret: String,
    pub name: String,
    pub protocol_endpoint_type: String,
    pub status: StorageContainerStatus,
    #[serde(rename = "storageContainerID")]
    pub storage_container_id: Uuid,
    pub target_secret: String,
    pub virtual_volumes: Vec<Uuid>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SnapshotStatus {
    Unknown,
    Preparing,
    RemoteSyncing,
    Done,
    Active,
    Cloning,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum StorageContainerStatus {
    Active,
    Locked,
}

#[derive(Debug, Deserialize, IntoPoint)]
pub struct VolumesAttributesImageInfo {
    pub image_created_at: String,
    pub image_id: String,
    pub image_name: String,
    pub image_updated_at: String,
}

/*
#[derive(Debug, Deserialize)]
pub struct VolumesAttribute {
    pub cloned_count: Option<i64>,
    pub image_info: VolumesAttributesImageInfo,
}
*/

#[derive(Debug, Deserialize, IntoPoint)]
pub struct VolumesQosCurve {
    #[serde(rename = "1048576")]
    pub one_mb: i64,
    #[serde(rename = "131072")]
    pub onehundred_twentyeight_kb: i64,
    #[serde(rename = "16384")]
    pub sixteen_kb: i64,
    #[serde(rename = "262144")]
    pub twohundred_fiftysix_kb: i64,
    #[serde(rename = "32768")]
    pub thirtytwo_kb: i64,
    #[serde(rename = "4096")]
    pub four_kb: i64,
    #[serde(rename = "524288")]
    pub fivehundred_kb: i64,
    #[serde(rename = "65536")]
    pub sixtyfive_kb: i64,
    #[serde(rename = "8192")]
    pub eight_kb: i64,
}

#[derive(Debug, Deserialize, IntoPoint)]
pub struct VolumesQos {
    #[serde(rename = "burstIOPS")]
    pub burst_iops: i64,
    #[serde(rename = "burstTime")]
    pub burst_time: i64,
    pub curve: VolumesQosCurve,
    #[serde(rename = "maxIOPS")]
    pub max_iops: i64,
    #[serde(rename = "minIOPS")]
    pub min_iops: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum VolumeAccess {
    ReadOnly,
    ReadWrite,
    Locked,
    ReplicationTarget,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum VolumeStatus {
    Cloning,
    Waiting,
    Ready,
}

#[derive(Debug, Deserialize, IntoPoint)]
pub struct VolumePair {
    #[serde(rename = "clusterPairID")]
    pub cluster_pair_id: i64,
    pub remote_replication: RemoteReplication,
    #[serde(rename = "remoteSliceID")]
    pub remote_slice_id: u64,
    #[serde(rename = "remoteVolumeID")]
    pub remote_volume_id: u64,
    pub remote_volume_name: String,
    #[serde(rename = "volumePairUUID")]
    pub volume_pair_uuid: String,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct VirtualNetwork {
    pub address_blocks: Vec<AddressBlock>,
    pub attributes: HashMap<String, String>,
    pub name: String,
    pub netmask: String,
    pub svip: String,
    pub gateway: String,
    #[serde(rename = "virtualNetworkID")]
    pub virtual_network_id: u64,
    pub virtual_network_tag: u64,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct VirtualVolume {
    pub bindings: Vec<Uuid>,
    pub children: Vec<Uuid>,
    pub descendants: Vec<Uuid>,
    pub metadata: HashMap<String, String>,
    #[serde(rename = "parentVirtualVolumeID")]
    pub parent_virtual_volume_id: Uuid,
    #[serde(rename = "snapshotID")]
    pub snapshot_id: u64,
    pub snapshot_info: Snapshot,
    pub status: VolumeStatus,
    pub storage_container: StorageContainer,
    #[serde(rename = "virtualVolumeID")]
    pub virtual_volume_id: Uuid,
    pub virtual_volume_type: String,
    #[serde(rename = "volumeID")]
    pub volume_id: u64,
    pub volume_info: Option<Volume>,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct Volume {
    pub access: VolumeAccess,
    #[serde(rename = "accountID")]
    pub account_id: u64,
    pub attributes: HashMap<String, serde_json::Value>,
    pub block_size: u64,
    pub create_time: String,
    pub delete_time: String,
    pub enable512e: bool,
    pub enable_snap_mirror_replication: bool,
    pub iqn: String,
    pub last_access_time: Option<String>,
    #[serde(rename = "lastAccessTimeIO")]
    pub last_access_time_io: Option<String>,
    pub name: String,
    pub purge_time: String,
    pub qos: VolumesQos,
    #[serde(rename = "qosPolicyID")]
    pub qos_policy_id: Option<i64>,
    #[serde(rename = "scsiEUIDeviceID")]
    pub scsi_eui_device_id: String,
    #[serde(rename = "scsiNAADeviceID")]
    pub scsi_naa_device_id: String,
    pub slice_count: i64,
    pub status: String,
    pub total_size: i64,
    #[serde(rename = "virtualVolumeID")]
    pub virtual_volume_id: Option<Uuid>,
    pub volume_access_groups: Vec<u64>,
    #[serde(rename = "volumeConsistencyGroupUUID")]
    pub volume_consistency_group_uuid: Uuid,
    #[serde(rename = "volumeID")]
    pub volume_id: u64,
    pub volume_pairs: Vec<VolumePair>,
    #[serde(rename = "volumeUUID")]
    pub volume_uuid: Uuid,
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
pub struct VolumeStats {
    #[serde(rename = "accountID")]
    pub account_id: u64,
    #[serde(rename = "actualIOPS")]
    pub actual_iops: u64,
    pub async_delay: Option<String>,
    #[serde(rename = "averageIOPSize")]
    pub average_iop_size: u64,
    #[serde(rename = "burstIOPSCredit")]
    pub burst_iops_credit: u64,
    pub client_queue_depth: u64,
    pub cluster_utilization: Option<f64>,
    pub desired_metadata_hosts: Option<serde_json::Value>,
    pub latency_u_sec: u64,
    pub metadata_hosts: MetadataHosts,
    pub non_zero_blocks: u64,
    pub read_bytes: u64,
    pub read_bytes_last_sample: u64,
    pub read_latency_u_sec: u64,
    pub read_latency_u_sec_total: u64,
    pub read_ops: u64,
    pub read_ops_last_sample: u64,
    pub sample_period_m_sec: u64,
    pub throttle: f64,
    pub timestamp: String,
    pub unaligned_reads: u64,
    pub unaligned_writes: u64,
    pub volume_access_groups: Vec<u64>,
    #[serde(rename = "volumeID")]
    pub volume_id: u64,
    pub volume_size: u64,
    pub volume_utilization: f64,
    pub write_bytes: u64,
    pub write_bytes_last_sample: u64,
    pub write_latency_u_sec: u64,
    pub write_latency_u_sec_total: u64,
    pub write_ops: u64,
    pub write_ops_last_sample: u64,
    pub zero_blocks: u64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VolumeStatsResult {
    pub volume_stats: VolumeStats,
}

impl IntoPoint for VolumeStatsResult {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.volume_stats.into_point(name, is_time_series)
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DriveHardwareResult {
    nodes: Vec<HardwareNode>,
}

impl IntoPoint for DriveHardwareResult {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.nodes
            .iter()
            .flat_map(|n| n.into_point(name, is_time_series))
            .collect::<Vec<TsPoint>>()
    }
}

#[test]
fn test_get_drive_hardware() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/list_drive_hardware.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<HardwareNodes> = serde_json::from_str(&buff).unwrap();
    println!(
        "JsonResult: {:?}",
        r.result.into_point(Some("solidfire_drive_hardware"), true)
    );
}

#[derive(Debug, Deserialize)]
struct HardwareNodes {
    nodes: Vec<HardwareNode>,
}

impl IntoPoint for HardwareNodes {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.nodes
            .iter()
            .flat_map(|n| n.into_point(name, is_time_series))
            .collect::<Vec<TsPoint>>()
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct HardwareNode {
    #[serde(rename = "nodeID")]
    node_id: u64,
    result: DriveHardware,
}

impl IntoPoint for HardwareNode {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.result.into_point(name, is_time_series)
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DriveHardware {
    drive_hardware: Vec<HardwareDrive>,
}

impl IntoPoint for DriveHardware {
    fn into_point(&self, name: Option<&str>, is_time_series: bool) -> Vec<TsPoint> {
        self.drive_hardware
            .iter()
            .flat_map(|n| n.into_point(name, is_time_series))
            .collect::<Vec<TsPoint>>()
    }
}

#[derive(Debug, Deserialize, IntoPoint)]
#[serde(rename_all = "camelCase")]
struct HardwareDrive {
    canonical_name: String,
    connected: bool,
    dev: u64,
    dev_path: String,
    drive_type: String,
    life_remaining_percent: u64,
    lifetime_read_bytes: u64,
    lifetime_write_bytes: u64,
    name: String,
    path: String,
    path_link: String,
    power_on_hours: u64,
    product: String,
    reallocated_sectors: u64,
    reserve_capacity_percent: u64,
    scsi_compat_id: String,
    scsi_state: String,
    security_at_maximum: bool,
    security_enabled: bool,
    security_frozen: bool,
    security_locked: bool,
    security_supported: bool,
    serial: String,
    size: u64,
    slot: u64,
    uncorrectable_errors: u64,
    #[serde(rename = "uuid")]
    hardware_uuid: Uuid,
    vendor: String,
    version: String,
}

#[derive(Debug, Deserialize)]
pub struct Volumes {
    pub volumes: Vec<Volume>,
}

#[derive(Debug, Deserialize)]
pub struct JsonResult<T> {
    pub id: Option<String>,
    pub result: T,
}

#[test]
fn test_get_cluster_capacity() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/get_cluster_capacity.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<ClusterCapacityResult> = serde_json::from_str(&buff).unwrap();
    println!("JsonResult: {:?}", r);
}

#[test]
fn test_get_cluster_fullness() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/get_cluster_full_threshold.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<ClusterFullThreshold> = serde_json::from_str(&buff).unwrap();
    println!("JsonResult: {:?}", r);
}

#[test]
fn test_get_cluster_info() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/get_cluster_info.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<ClusterInfoResult> = serde_json::from_str(&buff).unwrap();
    println!("JsonResult: {:?}", r);
}

#[test]
fn test_get_cluster_stats() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/get_cluster_stats.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<ClusterStatsResult> = serde_json::from_str(&buff).unwrap();
    println!("JsonResult: {:?}", r);
}

#[test]
fn test_get_volume_stats() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/get_volume_stats.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<VolumeStatsResult> = serde_json::from_str(&buff).unwrap();
    println!("JsonResult: {:?}", r);
}

#[test]
fn test_list_sf_volumes() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/list_volumes.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<Volumes> = serde_json::from_str(&buff).unwrap();
    println!("JsonResult: {:?}", r);
}

#[test]
fn test_list_sf_nodes() {
    use std::fs::File;
    use std::io::Read;

    let mut f = File::open("tests/solidfire/list_active_nodes.json").unwrap();
    let mut buff = String::new();
    f.read_to_string(&mut buff).unwrap();

    let r: JsonResult<Nodes> = serde_json::from_str(&buff).unwrap();
    println!("JsonResult: {:?}", r);
}

impl Solidfire {
    // Call out to solidfire and return the result as a json deserialized struct
    pub fn get<T>(
        &self,
        method: &str,
        params: Option<HashMap<String, String>>,
        force: bool,
    ) -> MetricsResult<T>
    where
        T: DeserializeOwned + Debug,
    {
        let mut url = format!(
            "https://{}/json-rpc/8.4?method={}",
            self.config.endpoint, method
        );
        if force {
            url.push_str("&force=true");
        }
        if let Some(p) = params {
            url.push_str(
                &p.into_iter()
                    .map(|(k, v)| format!("&{}={}", k, v))
                    .collect::<Vec<String>>()
                    .join(""),
            );
        }
        let j: T = crate::get(
            &self.client,
            &url,
            &self.config.user,
            Some(&self.config.password),
        )?;

        Ok(j)
    }

    pub fn get_drive_hardware_info(&self, t: DateTime<Utc>) -> MetricsResult<Vec<TsPoint>> {
        debug!("get_hardware_info");
        let info = self.get::<JsonResult<HardwareNodes>>("ListDriveHardware", None, true)?;
        Ok(info
            .result
            .into_point(Some("solidfire_drive_hardware"), true)
            .into_iter()
            .map(|mut p| {
                p.timestamp = Some(t);
                p
            })
            .collect::<Vec<TsPoint>>())
    }

    pub fn get_cluster_capacity(&self, t: DateTime<Utc>) -> MetricsResult<Vec<TsPoint>> {
        debug!("get_cluster_capacity");
        let info =
            self.get::<JsonResult<ClusterCapacityResult>>("GetClusterCapacity", None, false)?;
        Ok(info
            .result
            .into_point(Some("solidfire_cluster_capacity"), true)
            .into_iter()
            .map(|mut p| {
                p.timestamp = Some(t);
                p
            })
            .collect::<Vec<TsPoint>>())
    }

    pub fn get_cluster_fullness(&self, t: DateTime<Utc>) -> MetricsResult<Vec<TsPoint>> {
        debug!("get_cluster_fullness");
        let info =
            self.get::<JsonResult<ClusterFullThreshold>>("GetClusterFullThreshold", None, false)?;
        Ok(info
            .result
            .into_point(Some("solidfire_cluster_full_threshold"), true)
            .into_iter()
            .map(|mut p| {
                p.timestamp = Some(t);
                p
            })
            .collect::<Vec<TsPoint>>())
    }

    pub fn get_cluster_info(&self) -> MetricsResult<ClusterInfoResult> {
        debug!("get_cluster_info");
        let info = self.get::<JsonResult<ClusterInfoResult>>("GetClusterInfo", None, false)?;
        Ok(info.result)
    }

    pub fn get_cluster_stats(&self, t: DateTime<Utc>) -> MetricsResult<Vec<TsPoint>> {
        debug!("get_cluster_stats");
        let info = self.get::<JsonResult<ClusterStatsResult>>("GetClusterStats", None, false)?;
        Ok(info
            .result
            .into_point(Some("solidfire_cluster_stats"), true)
            .into_iter()
            .map(|mut p| {
                p.timestamp = Some(t);
                p
            })
            .collect::<Vec<TsPoint>>())
    }

    pub fn get_volume_stats(
        &self,
        volume_id: u64,
        t: DateTime<Utc>,
    ) -> MetricsResult<Vec<TsPoint>> {
        let mut params = HashMap::new();
        params.insert("volumeID".to_string(), volume_id.to_string());

        debug!("get_volume_stats");
        let info =
            self.get::<JsonResult<VolumeStatsResult>>("GetVolumeStats", Some(params), false)?;
        Ok(info
            .result
            .into_point(Some("solidfire_volume_stats"), true)
            .into_iter()
            .map(|mut p| {
                p.timestamp = Some(t);
                p
            })
            .collect::<Vec<TsPoint>>())
    }

    //pub fn get_node_stats() -> MetricsResult<Vec<TsPoint>> {
    //
    //}

    pub fn list_volumes(&self) -> MetricsResult<Volumes> {
        debug!("list_volumes");
        let info = self.get::<JsonResult<Volumes>>("ListVolumes", None, false)?;
        Ok(info.result)
    }

    pub fn list_volume_ids(&self) -> MetricsResult<Vec<u64>> {
        debug!("list_volume_ids");
        let info = self.get::<JsonResult<Volumes>>("ListVolumes", None, false)?;
        Ok(info
            .result
            .volumes
            .into_iter()
            .map(|v| v.volume_id)
            .collect::<Vec<u64>>())
    }
}