line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package API::PureStorage; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
11074
|
use Data::Dumper; |
|
1
|
|
|
|
|
6791
|
|
|
1
|
|
|
|
|
51
|
|
4
|
1
|
|
|
1
|
|
414
|
use REST::Client; |
|
1
|
|
|
|
|
33216
|
|
|
1
|
|
|
|
|
28
|
|
5
|
1
|
|
|
1
|
|
674
|
use JSON; |
|
1
|
|
|
|
|
7951
|
|
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
519
|
use Net::SSL; |
|
1
|
|
|
|
|
22050
|
|
|
1
|
|
|
|
|
17
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
370
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
9
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
664
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$API::PureStorage::VERSION = '0.03'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our %ENV; |
14
|
|
|
|
|
|
|
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $debug = 0; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
0
|
|
|
0
|
0
|
|
my $class = shift @_; |
20
|
0
|
|
|
|
|
|
my $self = { |
21
|
|
|
|
|
|
|
cookie_file => '/tmp/cookies.txt', |
22
|
|
|
|
|
|
|
host => $_[0], |
23
|
|
|
|
|
|
|
token => $_[1] |
24
|
|
|
|
|
|
|
}; |
25
|
0
|
|
|
|
|
|
bless $self, $class; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my $client = REST::Client->new( follow => 1 ); |
28
|
0
|
|
|
|
|
|
$client->setHost('https://'.$self->{host}); |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
$client->addHeader('Content-Type', 'application/json'); |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
$client->getUseragent()->cookie_jar({ file => $self->{cookie_file} }); |
33
|
0
|
|
|
|
|
|
$client->getUseragent()->ssl_opts(verify_hostname => 0); |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$self->{client} = $client; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Check API compatibility |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my @versions = $self->version(); |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my %api_versions; |
42
|
0
|
|
|
|
|
|
for my $version (@versions) { |
43
|
0
|
|
|
|
|
|
$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
|
0
|
0
|
|
|
|
|
$api_versions{'1.0'} ? '1.0' : |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
50
|
|
|
|
|
|
|
undef; |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
unless ( $api_version ) { |
53
|
0
|
|
|
|
|
|
die "API version 1.3 or 1.4 is not supported by host: $self->{host}\n"; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
$self->{api_version} = $api_version; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
### Set the Session Cookie |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $ret = $self->_api_post("/api/$api_version/auth/session", { api_token => $self->{token} }); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
return $self; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub DESTROY { |
66
|
0
|
|
|
0
|
|
|
my $self = shift @_; |
67
|
0
|
0
|
|
|
|
|
my $ret = $self->{client}->DELETE("/api/$self->{api_version}/auth/session") if defined $self->{api_version}; |
68
|
0
|
|
|
|
|
|
unlink $self->{cookie_file}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
### Methods |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub array_info { |
74
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
75
|
0
|
|
|
|
|
|
my $ref = $self->_api_get("/api/$self->{api_version}/array?space=true"); |
76
|
0
|
0
|
|
|
|
|
return wantarray ? @$ref : $ref; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub volume_detail { |
80
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
81
|
0
|
|
|
|
|
|
my $name = shift @_; |
82
|
0
|
|
|
|
|
|
my $ref = $self->_api_get("/api/$self->{api_version}/volume/".$name); |
83
|
0
|
0
|
|
|
|
|
return wantarray ? @$ref : $ref; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub volume_info { |
87
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
88
|
0
|
|
|
|
|
|
my $ref = $self->_api_get("/api/$self->{api_version}/volume?space=true"); |
89
|
0
|
0
|
|
|
|
|
return wantarray ? @$ref : $ref; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub version { |
93
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
94
|
0
|
|
|
|
|
|
my $ref = $self->_api_get('/api/api_version'); |
95
|
0
|
0
|
|
|
|
|
return wantarray ? @{$ref->{version}} : $ref->{version}; |
|
0
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
### Subs |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _api_get { |
101
|
0
|
|
|
0
|
|
|
my $self = shift @_; |
102
|
0
|
|
|
|
|
|
my $url = shift @_; |
103
|
0
|
|
|
|
|
|
my $ret = $self->{client}->GET($url); |
104
|
0
|
|
|
|
|
|
my $num = $ret->responseCode(); |
105
|
0
|
|
|
|
|
|
my $con = $ret->responseContent(); |
106
|
0
|
0
|
|
|
|
|
if ( $num == 500 ) { |
107
|
0
|
|
|
|
|
|
die "API returned error 500 for '$url' - $con\n"; |
108
|
|
|
|
|
|
|
} |
109
|
0
|
0
|
|
|
|
|
if ( $num != 200 ) { |
110
|
0
|
|
|
|
|
|
die "API returned code $num for URL '$url'\n"; |
111
|
|
|
|
|
|
|
} |
112
|
0
|
0
|
|
|
|
|
print 'DEBUG: GET ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug; |
113
|
0
|
|
|
|
|
|
return from_json($con); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _api_post { |
117
|
0
|
|
|
0
|
|
|
my $self = shift @_; |
118
|
0
|
|
|
|
|
|
my $url = shift @_; |
119
|
0
|
|
|
|
|
|
my $data = shift @_; |
120
|
0
|
|
|
|
|
|
my $ret = $self->{client}->POST($url, to_json($data)); |
121
|
0
|
|
|
|
|
|
my $num = $ret->responseCode(); |
122
|
0
|
|
|
|
|
|
my $con = $ret->responseContent(); |
123
|
0
|
0
|
|
|
|
|
if ( $num == 500 ) { |
124
|
0
|
|
|
|
|
|
die "API returned error 500 for '$url' - $con\n"; |
125
|
|
|
|
|
|
|
} |
126
|
0
|
0
|
|
|
|
|
if ( $num != 200 ) { |
127
|
0
|
|
|
|
|
|
die "API returned code $num for URL '$url'\n"; |
128
|
|
|
|
|
|
|
} |
129
|
0
|
0
|
|
|
|
|
print 'DEBUG: POST ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug; |
130
|
0
|
|
|
|
|
|
return from_json($con); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |
134
|
|
|
|
|
|
|
__END__ |