|
1 | | -#!/usr/bin/env perl |
2 | | -use strict; |
3 | | -use warnings; |
4 | | - |
5 | | -use LWP::UserAgent; |
6 | | -use HTTP::Request; |
7 | | -use JSON; |
8 | | -use Getopt::Long qw(GetOptions); |
9 | | - |
10 | | -use FindBin; |
11 | | -use lib "$FindBin::RealBin/../../snippets/perl"; |
12 | | - |
13 | | -use gateway; |
14 | | -use catalog_images; |
15 | | - |
16 | | -# Constants |
17 | | -my $CONTENT_TYPE_V2 = "application/vnd.netbackup+json; version=2.0"; |
18 | | - |
19 | | -# Variables |
20 | | -my $master_server; |
21 | | -my $username; |
22 | | -my $password; |
23 | | -my $domain_name; |
24 | | -my $domain_type; |
25 | | -my $verbose; |
26 | | - |
27 | | -sub printUsage { |
28 | | - print "\nUsage : perl api_requests_images.pl -nbmaster <master_server> -username <username> -password <password> [-domainname <domain_name>] [-domaintype <domain_type>]\n\n"; |
29 | | - die; |
30 | | -} |
31 | | - |
32 | | -GetOptions( |
33 | | -'nbmaster=s' => \$master_server, |
34 | | -'username=s' => \$username, |
35 | | -'password=s' => \$password, |
36 | | -'domainname=s' => \$domain_name, |
37 | | -'domaintype=s' => \$domain_type, |
38 | | -'verbose' => \$verbose, |
39 | | -) or printUsage(); |
40 | | - |
41 | | -if (!$master_server || !$username || !$password) { |
42 | | - printUsage(); |
43 | | -} |
44 | | - |
45 | | -my $token = gateway::perform_login($master_server, $username, $password, $domain_name, $domain_type); |
46 | | - |
47 | | -my $json_results = catalog_images::get_images($master_server, $token); |
48 | | -if ($verbose and defined $json_results){ |
49 | | - print JSON->new->pretty->encode($json_results); |
50 | | -} |
51 | | -if (exists $json_results->{"data"}[0]{"id"}){ |
52 | | - my $backup_id = $json_results->{"data"}[0]{"id"}; |
53 | | - my $files = get_all_image_contents($backup_id); |
54 | | - my $file_count = scalar(@$files); |
55 | | - print "Found $file_count files in image with backup ID $backup_id\n"; |
56 | | -} |
57 | | -else { |
58 | | - print "No images were found\n"; |
59 | | -} |
60 | | - |
61 | | -gateway::perform_logout($master_server, $token); |
62 | | - |
63 | | -=head1 get_all_image_contents |
64 | | -
|
65 | | - SYNOPSIS |
66 | | - This subroutine retrieves a list of all files in a specified image |
67 | | -
|
68 | | - PARAMETERS |
69 | | - $_[0] - string |
70 | | - The backup ID of the image for which you wish to list all contents |
71 | | -
|
72 | | - RETURNS |
73 | | - An array reference containing the absolute path to each file in the image |
74 | | -
|
75 | | -=cut |
76 | | -sub get_all_image_contents { |
77 | | - my $page_limit = 200; #This matches the default value |
78 | | - my @files; |
79 | | - my $backup_id = $_[0]; |
80 | | - if (not defined $backup_id){ |
81 | | - print "Error: backup ID is a required field\n"; |
82 | | - return \@files; |
83 | | - } |
84 | | - |
85 | | - my $request_id = catalog_images::get_request_id($master_server, $token, $backup_id); |
86 | | - my $json_results = catalog_images::get_image_contents($master_server, $token, $request_id); |
87 | | - if ($verbose and defined $json_results) { |
88 | | - print JSON->new->pretty->encode($json_results); |
89 | | - } |
90 | | - |
91 | | - # continute calling image contents API with the same request ID until we get a 404 |
92 | | - while (defined $json_results) { |
93 | | - # Save the file names |
94 | | - foreach my $data_entry (@{$json_results->{"data"}}) { |
95 | | - push @files, $data_entry->{"attributes"}{"filePath"}; |
96 | | - } |
97 | | - |
98 | | - $json_results = catalog_images::get_image_contents($master_server, $token, $request_id); |
99 | | - if ($verbose and defined $json_results){ |
100 | | - print JSON->new->pretty->encode($json_results); |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - print "\nPrevious 404 marks the end of image contents\n"; |
105 | | - return \@files; |
106 | | -} |
| 1 | +#!/usr/bin/env perl |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | + |
| 5 | +use LWP::UserAgent; |
| 6 | +use HTTP::Request; |
| 7 | +use JSON; |
| 8 | +use Getopt::Long qw(GetOptions); |
| 9 | + |
| 10 | +use FindBin; |
| 11 | +use lib "$FindBin::RealBin/../../snippets/perl"; |
| 12 | + |
| 13 | +use gateway; |
| 14 | +use catalog_images; |
| 15 | + |
| 16 | +# Constants |
| 17 | +my $CONTENT_TYPE_V2 = "application/vnd.netbackup+json; version=2.0"; |
| 18 | + |
| 19 | +# Variables |
| 20 | +my $master_server; |
| 21 | +my $username; |
| 22 | +my $password; |
| 23 | +my $domain_name; |
| 24 | +my $domain_type; |
| 25 | +my $verbose; |
| 26 | + |
| 27 | +sub printUsage { |
| 28 | + print "\nUsage : perl api_requests_images.pl -nbmaster <master_server> -username <username> -password <password> [-domainname <domain_name>] [-domaintype <domain_type>]\n\n"; |
| 29 | + die; |
| 30 | +} |
| 31 | + |
| 32 | +GetOptions( |
| 33 | +'nbmaster=s' => \$master_server, |
| 34 | +'username=s' => \$username, |
| 35 | +'password=s' => \$password, |
| 36 | +'domainname=s' => \$domain_name, |
| 37 | +'domaintype=s' => \$domain_type, |
| 38 | +'verbose' => \$verbose, |
| 39 | +) or printUsage(); |
| 40 | + |
| 41 | +if (!$master_server || !$username || !$password) { |
| 42 | + printUsage(); |
| 43 | +} |
| 44 | + |
| 45 | +my $token = gateway::perform_login($master_server, $username, $password, $domain_name, $domain_type); |
| 46 | + |
| 47 | +my $json_results = catalog_images::get_images($master_server, $token); |
| 48 | +if ($verbose and defined $json_results){ |
| 49 | + print JSON->new->pretty->encode($json_results); |
| 50 | +} |
| 51 | +if (exists $json_results->{"data"}[0]{"id"}){ |
| 52 | + my $backup_id = $json_results->{"data"}[0]{"id"}; |
| 53 | + my $files = get_all_image_contents($backup_id); |
| 54 | + my $file_count = scalar(@$files); |
| 55 | + print "Found $file_count files in image with backup ID $backup_id\n"; |
| 56 | +} |
| 57 | +else { |
| 58 | + print "No images were found\n"; |
| 59 | +} |
| 60 | + |
| 61 | +gateway::perform_logout($master_server, $token); |
| 62 | + |
| 63 | +=head1 get_all_image_contents |
| 64 | +
|
| 65 | + SYNOPSIS |
| 66 | + This subroutine retrieves a list of all files in a specified image |
| 67 | +
|
| 68 | + PARAMETERS |
| 69 | + $_[0] - string |
| 70 | + The backup ID of the image for which you wish to list all contents |
| 71 | +
|
| 72 | + RETURNS |
| 73 | + An array reference containing the absolute path to each file in the image |
| 74 | +
|
| 75 | +=cut |
| 76 | +sub get_all_image_contents { |
| 77 | + my $page_limit = 200; #This matches the default value |
| 78 | + my @files; |
| 79 | + my $backup_id = $_[0]; |
| 80 | + if (not defined $backup_id){ |
| 81 | + print "Error: backup ID is a required field\n"; |
| 82 | + return \@files; |
| 83 | + } |
| 84 | + |
| 85 | + my $request_id = catalog_images::get_request_id($master_server, $token, $backup_id); |
| 86 | + my $json_results = catalog_images::get_image_contents($master_server, $token, $request_id); |
| 87 | + if ($verbose and defined $json_results) { |
| 88 | + print JSON->new->pretty->encode($json_results); |
| 89 | + } |
| 90 | + |
| 91 | + # continute calling image contents API with the same request ID until we get a 404 |
| 92 | + while (defined $json_results) { |
| 93 | + # Save the file names |
| 94 | + foreach my $data_entry (@{$json_results->{"data"}}) { |
| 95 | + push @files, $data_entry->{"attributes"}{"filePath"}; |
| 96 | + } |
| 97 | + |
| 98 | + $json_results = catalog_images::get_image_contents($master_server, $token, $request_id); |
| 99 | + if ($verbose and defined $json_results){ |
| 100 | + print JSON->new->pretty->encode($json_results); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + print "\nPrevious 404 marks the end of image contents\n"; |
| 105 | + return \@files; |
| 106 | +} |
0 commit comments