PureStorage.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« build.pl ↑ Summary
1 package API::PureStorage;
2  
3 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::SSL;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 use warnings;
9 use strict;
10  
11 $API::PureStorage::VERSION = '0.01';
12  
13 our %ENV;
14 $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
15  
16 my $debug = 0;
17  
18 sub new {
19 my $class = shift @_;
20 my $self = {
21 cookie_file => '/tmp/cookies.txt',
22 host => $_[0],
23 token => $_[1]
24 };
25 bless $self, $class;
26  
27 my $client = REST::Client->new( follow => 1 );
28 $client->setHost('https://'.$self->{host});
29  
30 $client->addHeader('Content-Type', 'application/json');
31  
32 $client->getUseragent()->cookie_jar({ file => $self->{cookie_file} });
33 $client->getUseragent()->ssl_opts(verify_hostname => 0);
34  
35 $self->{client} = $client;
36  
37 # Check API compatibility
38  
39 my @versions = $self->version();
40  
41 my %api_versions;
42 for my $version (@versions) {
43 $api_versions{$version}++;
44 }
45  
46 my $api_version = $api_versions{'1.4'} ? '1.4' :
47 $api_versions{'1.3'} ? '1.3' :
48 $api_versions{'1.1'} ? '1.1' :
49 $api_versions{'1.0'} ? '1.0' :
50 undef;
51  
52 unless ( $api_version ) {
53 die "API version 1.3 or 1.4 is not supported by host: $self->{host}\n";
54 }
55  
56 $self->{api_version} = $api_version;
57  
58 ### Set the Session Cookie
59  
60 my $ret = $self->_api_post("/api/$api_version/auth/session", { api_token => $self->{token} });
61  
62 return $self;
63 }
64  
65 sub DESTROY {
66 my $self = shift @_;
67 my $ret = $self->{client}->DELETE("/api/$self->{api_version}/auth/session");
68 unlink $self->{cookie_file};
69 }
70  
71 ### Methods
72  
73 sub array_info {
74 my $self = shift @_;
75 my $ref = $self->_api_get("/api/$self->{api_version}/array?space=true");
76 return wantarray ? @$ref : $ref;
77 }
78  
79 sub volume_info {
80 my $self = shift @_;
81 my $ref = $self->_api_get("/api/$self->{api_version}/volume?space=true");
82 return wantarray ? @$ref : $ref;
83 }
84  
85 sub version {
86 my $self = shift @_;
87 my $ref = $self->_api_get('/api/api_version');
88 return wantarray ? @{$ref->{version}} : $ref->{version};
89 }
90  
91 ### Subs
92  
93 sub _api_get {
94 my $self = shift @_;
95 my $url = shift @_;
96 my $ret = $self->{client}->GET($url);
97 my $num = $ret->responseCode();
98 my $con = $ret->responseContent();
99 if ( $num == 500 ) {
100 die "API returned error 500 for '$url' - $con\n";
101 }
102 if ( $num != 200 ) {
103 die "API returned code $num for URL '$url'\n";
104 }
105 print 'DEBUG: GET ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug;
106 return from_json($con);
107 }
108  
109 sub _api_post {
110 my $self = shift @_;
111 my $url = shift @_;
112 my $data = shift @_;
113 my $ret = $self->{client}->POST($url, to_json($data));
114 my $num = $ret->responseCode();
115 my $con = $ret->responseContent();
116 if ( $num == 500 ) {
117 die "API returned error 500 for '$url' - $con\n";
118 }
119 if ( $num != 200 ) {
120 die "API returned code $num for URL '$url'\n";
121 }
122 print 'DEBUG: POST ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug;
123 return from_json($con);
124 }
125  
126 1;
127 __END__
« build.pl ↑ Summary