|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +package storage; |
| 4 | + |
| 5 | +use JSON; |
| 6 | +use warnings; |
| 7 | +use LWP::UserAgent; |
| 8 | +use HTTP::Request; |
| 9 | +use LWP::Protocol::https; |
| 10 | + |
| 11 | +$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; |
| 12 | +$CONTENT_TYPE = "application/vnd.netbackup+json;version=3.0"; |
| 13 | +$PROTOCOL = "https://"; |
| 14 | +$NB_PORT = 1556; |
| 15 | + |
| 16 | +my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0, verify_peer => 0}); |
| 17 | + |
| 18 | + |
| 19 | +sub send_http_request { |
| 20 | + my $url = $_[0]; |
| 21 | + my $request_type = $_[1]; |
| 22 | + my $token = $_[2]; |
| 23 | + my $body = $_[3]; |
| 24 | + my $accept = $_[4]; |
| 25 | + my $content_type = $_[5]; |
| 26 | + |
| 27 | + if (not defined $url or not defined $request_type){ |
| 28 | + print "Error: url and request type are required fields"; |
| 29 | + return undef; |
| 30 | + } |
| 31 | + |
| 32 | + print "Unencoded URL is $url\n"; |
| 33 | + # assume string is un-encoded, and '%' is a literal that needs to be replaced by '%25'. |
| 34 | + # All other types of encoding are handled gracefully by the LWP module except literal percent |
| 35 | + $url =~ s/%/%25/; |
| 36 | + |
| 37 | + # determine correct request type |
| 38 | + my $req; |
| 39 | + if (uc($request_type) eq "GET" ){ |
| 40 | + $req = HTTP::Request->new(GET => $url); |
| 41 | + } |
| 42 | + elsif ((uc($request_type) eq "POST")){ |
| 43 | + $req = HTTP::Request->new(POST => $url); |
| 44 | + } |
| 45 | + elsif ((uc($request_type) eq "DELETE")){ |
| 46 | + $req = HTTP::Request->new(DELETE => $url); |
| 47 | + } |
| 48 | + elsif ((uc($request_type) eq "PUT")) { |
| 49 | + $req = HTTP::Request->new(PUT => $url); |
| 50 | + } |
| 51 | + elsif ((uc($request_type) eq "PATCH")){ |
| 52 | + $req = HTTP::Request->new(PATCH => $url); |
| 53 | + } |
| 54 | + else { |
| 55 | + print "Unrecognized request type [$request_type]. If this is a valid HTTP request type, please update me"; |
| 56 | + return undef; |
| 57 | + } |
| 58 | + |
| 59 | + # print encoded url to the screen |
| 60 | + print "Encoded URL is ${$req->uri}\n"; |
| 61 | + |
| 62 | + if (defined $token) { |
| 63 | + $req->header('Authorization' => $token); |
| 64 | + } |
| 65 | + if (defined $accept) { |
| 66 | + $req->header('Accept' => $accept); |
| 67 | + } |
| 68 | + if (defined $content_type){ |
| 69 | + $req->header('Content-Type' => $content_type); |
| 70 | + } |
| 71 | + if (defined $body){ |
| 72 | + $req->content($body); |
| 73 | + } |
| 74 | + |
| 75 | + my $resp = $ua->request($req); |
| 76 | + if ($resp->is_success) { |
| 77 | + my $json_results; |
| 78 | + if (defined($resp->content) && $resp->content ne "") { |
| 79 | + $json_results = decode_json($resp->content); |
| 80 | + } |
| 81 | + else { |
| 82 | + $json_results = ""; |
| 83 | + } |
| 84 | + return $json_results; |
| 85 | + } |
| 86 | + else { |
| 87 | + print "HTTP error code: ", $resp->code, "\n"; |
| 88 | + print "HTTP response content: ", $resp->content, "\n"; |
| 89 | + return undef; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +sub perform_login { |
| 94 | + my @argument_list = @_; |
| 95 | + my $master_server = $argument_list[0]; |
| 96 | + my $username = $argument_list[1]; |
| 97 | + my $password = $argument_list[2]; |
| 98 | + |
| 99 | + my $token; |
| 100 | + |
| 101 | + # domainName and domainType are optional |
| 102 | + my $domainName = ""; |
| 103 | + my $domainType = ""; |
| 104 | + if (@argument_list >= 4) { |
| 105 | + $domainName = $argument_list[3]; |
| 106 | + } |
| 107 | + if (@argument_list == 5) { |
| 108 | + $domainType = $argument_list[4]; |
| 109 | + } |
| 110 | + |
| 111 | + # Construct url |
| 112 | + my $url = "https://$master_server:1556/netbackup/login"; |
| 113 | + |
| 114 | + # Construct request body |
| 115 | + my $post_data; |
| 116 | + if (not $domainName and not $domainType) { |
| 117 | + $post_data = qq({ "userName": "$username", "password": "$password" }); |
| 118 | + } |
| 119 | + else { |
| 120 | + $post_data = qq({ "domainType": "$domainType", "domainName": "$domainName", "userName": "$username", "password": "$password" }); |
| 121 | + } |
| 122 | + |
| 123 | + print "\n\n**************************************************************"; |
| 124 | + print "\n\n Making POST Request to login to get token \n\n"; |
| 125 | + |
| 126 | + my $json_results = send_http_request($url, "post", undef, $post_data, undef, "application/json"); |
| 127 | + |
| 128 | + if (defined $json_results){ |
| 129 | + $token = $json_results->{"token"}; |
| 130 | + } |
| 131 | + return $token; |
| 132 | +} |
| 133 | + |
| 134 | +# Create a storage server |
| 135 | +sub post_storage_server { |
| 136 | + my $arguments_count = scalar(@_); |
| 137 | + if ($arguments_count != 3) { |
| 138 | + print "ERROR :: Incorrect number of arguments passed to post_storage_server()\n"; |
| 139 | + print "Usage : post_storage_server( <Master Server Hostname>, <Token>, <Payload>) \n"; |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + my $master_server = $_[0]; |
| 144 | + my $token = $_[1]; |
| 145 | + my $filename = $_[2]; |
| 146 | + my $url = "$PROTOCOL$master_server:$NB_PORT/netbackup/storage/storage-servers"; |
| 147 | + open(my $fh, '<:encoding(UTF-8)', $filename) |
| 148 | + or die "Could not open file '$filename' $!"; |
| 149 | + |
| 150 | + my $payload = ""; |
| 151 | + while (my $row = <$fh>) { |
| 152 | + chomp $row; |
| 153 | + $payload .= $row; |
| 154 | + } |
| 155 | + print "payload: $payload\n"; |
| 156 | + |
| 157 | + my $json = send_http_request($url, "POST", $token, $payload, undef, $CONTENT_TYPE); |
| 158 | + |
| 159 | + if (defined $json) { |
| 160 | + print "Successfully completed POST Storage Server Request.\n"; |
| 161 | + |
| 162 | + my $pretty = JSON->new->pretty->encode($json); |
| 163 | + return $pretty; |
| 164 | + } |
| 165 | + else { |
| 166 | + print "ERROR :: POST Storage Server Request Failed!\n"; |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +# Create a storage unit |
| 172 | +sub post_storage_unit { |
| 173 | + my $arguments_count = scalar(@_); |
| 174 | + if ($arguments_count != 3) { |
| 175 | + print "ERROR :: Incorrect number of arguments passed to post_storage_unit()\n"; |
| 176 | + print "Usage : post_storage_server( <Master Server Hostname>, <Token>, <Payload>) \n"; |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + my $master_server = $_[0]; |
| 181 | + my $token = $_[1]; |
| 182 | + my $filename = $_[2]; |
| 183 | + my $url = "$PROTOCOL$master_server:$NB_PORT/netbackup/storage/storage-units"; |
| 184 | + open(my $fh, '<:encoding(UTF-8)', $filename) |
| 185 | + or die "Could not open file '$filename' $!"; |
| 186 | + |
| 187 | + my $payload = ""; |
| 188 | + while (my $row = <$fh>) { |
| 189 | + chomp $row; |
| 190 | + $payload .= $row; |
| 191 | + } |
| 192 | + print "payload: $payload\n"; |
| 193 | + |
| 194 | + my $json = send_http_request($url, "POST", $token, $payload, undef, $CONTENT_TYPE); |
| 195 | + |
| 196 | + if (defined $json) { |
| 197 | + print "Successfully completed POST Storage Unit Request.\n"; |
| 198 | + |
| 199 | + my $pretty = JSON->new->pretty->encode($json); |
| 200 | + return $pretty; |
| 201 | + } |
| 202 | + else { |
| 203 | + print "ERROR :: POST Storage Unit Request Failed!\n"; |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +# Create a Disk Pool |
| 208 | +sub post_disk_pool { |
| 209 | + my $arguments_count = scalar(@_); |
| 210 | + if ($arguments_count != 3) { |
| 211 | + print "ERROR :: Incorrect number of arguments passed to post_disk_pool()\n"; |
| 212 | + print "Usage : post_storage_server( <Master Server Hostname>, <Token>, <Payload>) \n"; |
| 213 | + return; |
| 214 | + } |
| 215 | + |
| 216 | + my $master_server = $_[0]; |
| 217 | + my $token = $_[1]; |
| 218 | + my $filename = $_[2]; |
| 219 | + my $url = "$PROTOCOL$master_server:$NB_PORT/netbackup/storage/disk-pools"; |
| 220 | + open(my $fh, '<:encoding(UTF-8)', $filename) |
| 221 | + or die "Could not open file '$filename' $!"; |
| 222 | + |
| 223 | + my $payload = ""; |
| 224 | + while (my $row = <$fh>) { |
| 225 | + chomp $row; |
| 226 | + $payload .= $row; |
| 227 | + } |
| 228 | + print "payload: $payload\n"; |
| 229 | + |
| 230 | + my $json = send_http_request($url, "POST", $token, $payload, undef, $CONTENT_TYPE); |
| 231 | + |
| 232 | + if (defined $json) { |
| 233 | + print "Successfully completed POST Disk Pool Request.\n"; |
| 234 | + |
| 235 | + my $pretty = JSON->new->pretty->encode($json); |
| 236 | + return $pretty; |
| 237 | + } |
| 238 | + else { |
| 239 | + print "ERROR :: POST Disk Pool Request Failed!\n"; |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +1; |
| 244 | + |
0 commit comments