|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use LWP::UserAgent; |
| 4 | +use LWP::Protocol::https; |
| 5 | +print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n"; |
| 6 | +print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n"; |
| 7 | +use JSON; |
| 8 | +use Getopt::Long qw(GetOptions); |
| 9 | + |
| 10 | + |
| 11 | +# |
| 12 | +# The token is the key to the NetBackup AuthN/AuthZ scheme. You must login and get a token |
| 13 | +# and use this token in your Authorization header for all subsequent requests. Token validity |
| 14 | +# is fixed at 24 hours |
| 15 | +# |
| 16 | + |
| 17 | +my $content_type = "application/vnd.netbackup+json; version=4.0"; |
| 18 | +my $protocol = "https"; |
| 19 | +my $port = "1556"; |
| 20 | +my $nbmaster; |
| 21 | +my $username; |
| 22 | +my $password; |
| 23 | +my $domainName; |
| 24 | +my $domainType; |
| 25 | +my $token; |
| 26 | +my $base_url; |
| 27 | + |
| 28 | +my $ua = LWP::UserAgent->new( |
| 29 | + ssl_opts => { verify_hostname => 0, verify_peer => 0}, |
| 30 | + ); |
| 31 | + |
| 32 | +# subroutines for printing usage and library information required to run the script. |
| 33 | +sub print_usage { |
| 34 | + print("\n\nUsage:"); |
| 35 | + print("\nperl get_vmware_assets -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] \n\n\n"); |
| 36 | +} |
| 37 | + |
| 38 | +sub print_disclaimer { |
| 39 | + print("--------------------------------------------------------\n"); |
| 40 | + print("-- This script requires Perl 5.20.2 or later --\n"); |
| 41 | + print("--------------------------------------------------------\n"); |
| 42 | + print("Executing this library requires some additional libraries like \n\t'LWP' \n\t'JSON'\ \n\t'Getopt'\ \n\n"); |
| 43 | + print("You can specify the 'nbmaster', 'username', 'password', 'domainName', 'domainType' as command-line parameters\n"); |
| 44 | + print_usage(); |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +# subroutine to process user input |
| 49 | +sub user_input { |
| 50 | + GetOptions( |
| 51 | + 'nbmaster=s' => \$nbmaster, |
| 52 | + 'username=s' => \$username, |
| 53 | + 'password=s' => \$password, |
| 54 | + 'domainName=s' => \$domainName, |
| 55 | + 'domainType=s' => \$domainType, |
| 56 | + ) or (print_usage() && exit); |
| 57 | + |
| 58 | + if ($nbmaster eq "") { |
| 59 | + print("Please provide the value for 'nbmaster'"); |
| 60 | + exit; |
| 61 | + } |
| 62 | + |
| 63 | + if ($username eq "") { |
| 64 | + print("Please provide the value for 'username'"); |
| 65 | + exit; |
| 66 | + } |
| 67 | + |
| 68 | + if ($password eq "") { |
| 69 | + print("Please provide the value for 'password'"); |
| 70 | + exit; |
| 71 | + } |
| 72 | + |
| 73 | + $base_url = "$protocol://$nbmaster:$port/netbackup"; |
| 74 | +} |
| 75 | + |
| 76 | +sub login { |
| 77 | + my @argument_list = @_; |
| 78 | + $base_url = $argument_list[0]; |
| 79 | + my $username = $argument_list[1]; |
| 80 | + my $password = $argument_list[2]; |
| 81 | + my $domainName = $argument_list[3]; |
| 82 | + my $domainType = $argument_list[4]; |
| 83 | + |
| 84 | + my $url = "$base_url/login"; |
| 85 | + |
| 86 | + my $req = HTTP::Request->new(POST => $url); |
| 87 | + $req->header('content-type' => 'application/json'); |
| 88 | + |
| 89 | + if ($domainName eq "" && $domainType eq "") { |
| 90 | + $post_data = qq({ "userName": "$username", "password": "$password" }); |
| 91 | + } |
| 92 | + else { |
| 93 | + $post_data = qq({ "domainType": "$domainType", "domainName": "$domainName", "userName": "$username", "password": "$password" }); |
| 94 | + } |
| 95 | + $req->content($post_data); |
| 96 | + |
| 97 | + |
| 98 | + print "\n\n**************************************************************"; |
| 99 | + print "\n\n Making POST Request to login to get token \n\n"; |
| 100 | + |
| 101 | + my $resp = $ua->request($req); |
| 102 | + if ($resp->is_success) { |
| 103 | + my $message = decode_json($resp->content); |
| 104 | + $token = $message->{"token"}; |
| 105 | + print "Login succeeded with status code: ", $resp->code, "\n"; |
| 106 | + } |
| 107 | + else { |
| 108 | + print "Request failed with status code: ", $resp->code, "\n"; |
| 109 | + } |
| 110 | + return $token; |
| 111 | +} |
| 112 | + |
| 113 | +# subroutine to Create Asset Group |
| 114 | +sub create_assetGroup { |
| 115 | + my $url = "$base_url/asset-service/queries"; |
| 116 | + |
| 117 | + my $asset_data = qq({ |
| 118 | + "data": { |
| 119 | + "type": "query", |
| 120 | + "attributes": { |
| 121 | + "queryName": "create-or-update-assets", |
| 122 | + "workloads": ["vmware"], |
| 123 | + "parameters": { |
| 124 | + "objectList": [ |
| 125 | + { |
| 126 | + "correlationId": "cor-1234", |
| 127 | + "type": "vmwareGroupAsset", |
| 128 | + "assetGroup": { |
| 129 | + "description": "sampleDescription", |
| 130 | + "assetType": "vmGroup", |
| 131 | + "filterConstraint": "sampleFilterConstaint", |
| 132 | + "oDataQueryFilter": "commonAssetAttributes/displayName eq 'testing1234'", |
| 133 | + "commonAssetAttributes": { |
| 134 | + "displayName": "sampleGroup1234", |
| 135 | + "workloadType": "vmware", |
| 136 | + "protectionCapabilities": { |
| 137 | + "isProtectable": "YES", |
| 138 | + "isProtectableReason": "sampleReason", |
| 139 | + "isRecoverable": "NO", |
| 140 | + "isRecoverableReason": "sampleReason" |
| 141 | + }, |
| 142 | + "detection": { |
| 143 | + "detectionMethod": "MANUAL" |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + ] |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +}); |
| 153 | + |
| 154 | + my $req = HTTP::Request->new(POST => $url); |
| 155 | + $req->header('Content-Type' => $content_type); |
| 156 | + $req->header('Authorization' => $token); |
| 157 | + $req->content($asset_data); |
| 158 | + |
| 159 | + print "\n\n**************************************************************"; |
| 160 | + print "\n\n Making POST Request to create assetGroup\n\n"; |
| 161 | + |
| 162 | + my $resp = $ua->request($req); |
| 163 | + |
| 164 | + my $message = $resp->decoded_content; |
| 165 | + print "Create vmware Asset Group succeeded with status code: ", $resp->code, "\n\n\n"; |
| 166 | + print "=======================\n"; |
| 167 | + |
| 168 | + my $json = decode_json($message); |
| 169 | + my $id = $json->{'data'}->{'id'}; |
| 170 | + print "id: ", $id, "\n\n\n"; |
| 171 | + |
| 172 | + my $url = "$base_url/asset-service/queries/$id"; |
| 173 | + my $req = HTTP::Request->new(GET => $url); |
| 174 | + $req->header('Accept' => $content_type); |
| 175 | + $req->header('Authorization' => $token); |
| 176 | + |
| 177 | + print "\n\n**************************************************************"; |
| 178 | + print "\n\n Making GET Request for the QueryID response\n\n"; |
| 179 | + |
| 180 | + my $resp = $ua->request($req); |
| 181 | + my $message = $resp->decoded_content; |
| 182 | + |
| 183 | + print "Create vmware Asset Group query id : ", $resp->code, "\n\n\n"; |
| 184 | + |
| 185 | + my $json = decode_json($message); |
| 186 | + my @responses = @{$json->{'data'}}; |
| 187 | + for my $response (@responses) { |
| 188 | + my @workitems = @{$response->{'attributes'}->{'workItemResponses'}}; |
| 189 | + for my $workItem (@workitems) { |
| 190 | + my $status = $workItem->{'statusDetails'}->{'message'}; |
| 191 | + if ($status eq 'Created') { |
| 192 | + print "VMware Asset Group : ", $status ,"\n"; |
| 193 | + } else { |
| 194 | + print "VMware Asset Group Not Created due to: ", $status ,"\n"; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +print_disclaimer(); |
| 201 | + |
| 202 | +user_input(); |
| 203 | + |
| 204 | +$token = login($base_url, $username, $password, $domain_name, $domain_type); |
| 205 | + |
| 206 | +create_assetGroup(); |
0 commit comments