@@ -9,7 +9,8 @@ use LWP::UserAgent;
99use LWP::Protocol::https;
1010
1111$ENV {PERL_LWP_SSL_VERIFY_HOSTNAME } = 0;
12- $CONTENT_TYPE = " application/vnd.netbackup+json; version=1.0" ;
12+ $CONTENT_TYPE_V1 = " application/vnd.netbackup+json; version=1.0" ;
13+ $CONTENT_TYPE_V2 = " application/vnd.netbackup+json; version=2.0" ;
1314$NB_PORT = 1556;
1415
1516
@@ -48,7 +49,7 @@ sub login {
4849 }
4950
5051 my $req = HTTP::Request-> new(POST => $token_url );
51- $req -> header(' content-type' => " $CONTENT_TYPE " );
52+ $req -> header(' content-type' => " $CONTENT_TYPE_V1 " );
5253 $req -> content($post_data );
5354
5455 my $ua = LWP::UserAgent-> new(
@@ -61,7 +62,7 @@ sub login {
6162 if ($resp -> is_success) {
6263 my $message = decode_json($resp -> content);
6364 my $token = $message -> {" token" };
64- print " Successfully completed Login Request.\n " ;
65+ print " Successfully completed Login Request.\n\n " ;
6566 return $token ;
6667 }
6768 else {
@@ -100,7 +101,7 @@ sub logout {
100101 ssl_opts => { verify_hostname => 0, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE },
101102 );
102103
103- print " Performing Logout Request on $logout_url \n " ;
104+ print " \n\n Performing Logout Request on $logout_url \n " ;
104105 my $resp = $ua -> request($logout_req );
105106 if ($resp -> is_success) {
106107 print " Successfully completed Logout Request.\n " ;
@@ -133,7 +134,7 @@ sub getJobs {
133134 my $url = " https://$fqdn_hostname :$NB_PORT /netbackup/admin/jobs" ;
134135 my $jobs_req = HTTP::Request-> new(GET => $url );
135136 $jobs_req -> header(' Authorization' => $token );
136- $jobs_req -> header(' content-type' => " $CONTENT_TYPE " );
137+ $jobs_req -> header(' content-type' => " $CONTENT_TYPE_V1 " );
137138
138139 my $ua = LWP::UserAgent-> new(
139140 timeout => 500,
@@ -177,7 +178,7 @@ sub getCatalogImages {
177178 my $url = " https://$fqdn_hostname :$NB_PORT /netbackup/catalog/images" ;
178179 my $catalog_req = HTTP::Request-> new(GET => $url );
179180 $catalog_req -> header(' Authorization' => $token );
180- $catalog_req -> header(' content-type' => " $CONTENT_TYPE " );
181+ $catalog_req -> header(' content-type' => " $CONTENT_TYPE_V1 " );
181182
182183 my $ua = LWP::UserAgent-> new(
183184 timeout => 500,
@@ -316,5 +317,182 @@ sub displayCatalogImages {
316317
317318}
318319
320+ #
321+ # This function returns a list of Asset based on
322+ # a filter parameter
323+ #
324+
325+ sub getAssetsByFilter {
326+
327+ my $arguments_count = scalar (@_ );
328+ if ($arguments_count != 3) {
329+ print " ERROR :: Incorrect number of arguments passed to getAssetsByFilter()\n " ;
330+ print " Usage : getAssetsByFilter( <Asset filter> ) \n " ;
331+ return ;
332+ }
333+
334+ my $fqdn_hostname = $_ [0];
335+ my $token = $_ [1];
336+ my $filter = $_ [2];
337+
338+ my $url = " https://$fqdn_hostname :$NB_PORT /netbackup/assets?filter=$filter " ;
339+ my $assets_req = HTTP::Request-> new(GET => $url );
340+ $assets_req -> header(' Authorization' => $token );
341+ $assets_req -> header(' content-type' => " $CONTENT_TYPE_V1 " );
342+
343+ my $ua = LWP::UserAgent-> new(
344+ timeout => 1000,
345+ ssl_opts => { verify_hostname => 0, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE },
346+ );
347+
348+ print " Performing Get Assets Request on $url \n " ;
349+ my $response = $ua -> request($assets_req );
350+ if ($response -> is_success) {
351+ print " Successfully completed Get Assets by filter Request.\n " ;
352+
353+ $data = decode_json($response -> content);
354+ my $pretty = JSON-> new-> pretty-> encode($data );
355+ return $pretty ;
356+ }
357+ else {
358+ print " ERROR :: Get Assets Request Failed!\n " ;
359+ print " HTTP GET error code: " , $response -> code, " \n " ;
360+ print " HTTP GET error message: " , $response -> message, " \n " ;
361+ }
362+
363+ }
364+
365+ #
366+ # # This function displays the JSON content returned from Asset API
367+ # # using query filter in a tabular format
368+ # #
369+ sub displayAssets {
370+
371+ my $arguments_count = scalar (@_ );
372+ if ($arguments_count != 1) {
373+ print " ERROR :: Incorrect number of arguments passed to displayAssets()\n " ;
374+ print " Usage : displayAssets( <JSON content returned from Assets API> ) \n " ;
375+ return ;
376+ }
377+
378+ my $jsonstring = $_ [0];
379+ my $json = decode_json($jsonstring );
380+ my @assets = @{$json -> {' data' }};
381+
382+ my @tablerows ;
383+
384+ foreach (@assets ) {
385+ my $asset = $_ ;
386+
387+ my $assetId = $asset -> {' id' };
388+ my $assetType = $asset -> {' attributes' }-> {' assetType' };
389+ my $workloadType = $asset -> {' attributes' }-> {' workloadType' };
390+ my $displayName = $asset -> {' attributes' }-> {' displayName' };
391+ my $version = $asset -> {' attributes' }-> {' version' };
392+
393+ my @tablerow = ($assetId , $assetType , $workloadType , $displayName , $version );
394+ push @tablerows , \@tablerow ;
395+ }
396+
397+ my @title = (" Asset ID" , " Asset Type" , " Workload Type" , " Display Name" , " Version" );
398+ print " \n " ;
399+ displayDataInTable(\@title , \@tablerows );
400+ print " \n " ;
401+
402+ }
403+
404+ #
405+ # # This function create the Json payload for the Asset Cleanup API
406+ # # It receives 2 paramters, the Json response from the GetAssetByFilter
407+ # # and the cleanupTime. The response of this function is a proper payload
408+ # # with all Assets from the filter.
409+ # #
410+ sub createAssetCleanupPayload {
411+
412+ my $arguments_count = scalar (@_ );
413+ if ($arguments_count != 2) {
414+ print " ERROR :: Incorrect number of arguments passed to createAssetCleanupPayload()\n " ;
415+ print " Usage : createAssetCleanupPayload( <Json content returned from Assets API>, <cleanupTime> ) \n " ;
416+ return ;
417+ }
418+
419+ my $jsonstring = $_ [0];
420+ my $cleanupTime = $_ [1];
421+ my $valid_input = " false" ;
422+ my $json = decode_json($jsonstring );
423+ my @assets = @{$json -> {' data' }};
424+
425+ my $payload = " { \" data\" :{ \" type\" :\" assetCleanup\" , \" id\" :\" cleanupId\" ," ;
426+ $payload = " $payload \" attributes\" : { \" cleanupTime\" :\" $cleanupTime \" ," ;
427+ $payload = " $payload \" assetIds\" :[" ;
428+
429+ foreach (@assets ) {
430+ my $asset = $_ ;
431+
432+ my $assetId = $asset -> {' id' };
433+ $payload = " $payload \" $assetId \" ," ;
434+ $valid_input = " true" ;
435+ }
436+
437+ $payload = substr ($payload , 0, (length $payload ) - 1);
438+ $payload = " $payload ] } } }" ;
439+
440+ if ($valid_input eq " false" ){
441+ return $valid_input ;
442+ } else {
443+ return $payload ;
444+ }
445+
446+ }
447+
448+ #
449+ # # This function makes the call to the Asset Cleanup API.
450+ # # If the web service goes successfully a HTTP 204 code is returned.
451+ # # Any other HTTP response code will be considered a error in the Asset Cleanup API.
452+ # #
453+ sub cleanAssets {
454+
455+ my $arguments_count = scalar (@_ );
456+ if ($arguments_count != 4) {
457+ print " ERROR :: Incorrect number of arguments passed to cleanAssets()\n " ;
458+ print " Usage : cleanAssets( <Host Name>, <Authorization Token>, <Json content returned from Assets API>, <Cleanup Time> ) \n " ;
459+ return ;
460+ }
461+
462+ my $hostname = $_ [0];
463+ my $myToken = $_ [1];
464+ my $jsonstring = $_ [2];
465+ my $cleanuptime = $_ [3];
466+
467+ my $payload = createAssetCleanupPayload($jsonstring , $cleanuptime );
468+
469+ if ($payload ne " false" ){
470+
471+ my $asset_cleanup_url = " https://$hostname :$NB_PORT /netbackup/assets/asset-cleanup" ;
472+ my $asset_cleanup_req = HTTP::Request-> new(POST => $asset_cleanup_url );
473+ $asset_cleanup_req -> header(' Authorization' => $myToken );
474+ $asset_cleanup_req -> header(' content-type' => " $CONTENT_TYPE_V2 " );
475+ $asset_cleanup_req -> content($payload );
476+
477+ my $ua = LWP::UserAgent-> new(
478+ timeout => 1000,
479+ ssl_opts => { verify_hostname => 0, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE },
480+ );
481+
482+ print " \n\n Performing Asset Cleanup Request on $asset_cleanup_url \n " ;
483+ my $resp = $ua -> request($asset_cleanup_req );
484+ if ($resp -> is_success) {
485+ print " Successfully completed Asset Cleanup Request.\n " ;
486+ }
487+ else {
488+ print " ERROR :: Asset Cleanup Request Failed!\n " ;
489+ print " HTTP POST error code: " , $resp -> code, " \n " ;
490+ print " HTTP POST error message: " , $resp -> message, " \n " ;
491+ }
492+ } else {
493+ print " There is no asset to be clean\n " ;
494+
495+ }
496+ }
319497
3204981;
0 commit comments