API-PureStorage

 view release on metacpan or  search on metacpan

lib/API/PureStorage.pm  view on Meta::CPAN

package API::PureStorage;

use Data::Dumper;
use REST::Client;
use JSON;
use Net::SSL;

use warnings;
use strict;

$API::PureStorage::VERSION = '0.03';

our %ENV;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

my $debug = 0;

sub new {
    my $class = shift @_;
    my $self = {
        cookie_file => '/tmp/cookies.txt',
        host => $_[0],
        token => $_[1]
    };
    bless $self, $class;

    my $client = REST::Client->new( follow => 1 );
    $client->setHost('https://'.$self->{host});

    $client->addHeader('Content-Type', 'application/json');

    $client->getUseragent()->cookie_jar({ file => $self->{cookie_file} });
    $client->getUseragent()->ssl_opts(verify_hostname => 0);

    $self->{client} = $client;

    # Check API compatibility

    my @versions = $self->version();

    my %api_versions;
    for my $version (@versions) {
        $api_versions{$version}++;
    }

    my $api_version = $api_versions{'1.4'} ? '1.4' :
                      $api_versions{'1.3'} ? '1.3' :
                      $api_versions{'1.1'} ? '1.1' :
                      $api_versions{'1.0'} ? '1.0' :
                      undef;

    unless ( $api_version ) {
      die "API version 1.3 or 1.4 is not supported by host: $self->{host}\n";
    }

    $self->{api_version} = $api_version;

    ### Set the Session Cookie

    my $ret = $self->_api_post("/api/$api_version/auth/session", { api_token => $self->{token} });

    return $self;
}

sub DESTROY {
  my $self = shift @_;
  my $ret = $self->{client}->DELETE("/api/$self->{api_version}/auth/session") if defined $self->{api_version};
  unlink $self->{cookie_file};
}

### Methods

sub array_info {
    my $self = shift @_;
    my $ref = $self->_api_get("/api/$self->{api_version}/array?space=true");
    return wantarray ? @$ref : $ref;
}

sub volume_detail {
    my $self = shift @_;
    my $name = shift @_;
    my $ref = $self->_api_get("/api/$self->{api_version}/volume/".$name);
    return wantarray ? @$ref : $ref;
}

sub volume_info {
    my $self = shift @_;
    my $ref = $self->_api_get("/api/$self->{api_version}/volume?space=true");
    return wantarray ? @$ref : $ref;
}

sub version {
    my $self = shift @_;



( run in 1.071 second using v1.01-cache-2.11-cpan-119454b85a5 )