|
| 1 | +#!/usr/bin/env perl |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | + |
| 5 | +use FindBin; |
| 6 | +use lib "$FindBin::RealBin/../../snippets/perl"; |
| 7 | + |
| 8 | +use common; |
| 9 | +use gateway; |
| 10 | +use catalog_images; |
| 11 | + |
| 12 | +use LWP::UserAgent; |
| 13 | +use HTTP::Request; |
| 14 | +use JSON; |
| 15 | +use Getopt::Long qw(GetOptions); |
| 16 | + |
| 17 | +my $master_server; |
| 18 | +my $username; |
| 19 | +my $password; |
| 20 | +my $domain_name; |
| 21 | +my $domain_type; |
| 22 | +my $verbose; |
| 23 | + |
| 24 | +sub printUsage { |
| 25 | + print "\nUsage : perl api_requests_images.pl -nbmaster <master_server> -username <username> -password <password> [-domainname <domain_name>] [-domaintype <domain_type>]\n\n"; |
| 26 | + die; |
| 27 | +} |
| 28 | + |
| 29 | +GetOptions( |
| 30 | +'nbmaster=s' => \$master_server, |
| 31 | +'username=s' => \$username, |
| 32 | +'password=s' => \$password, |
| 33 | +'domainname=s' => \$domain_name, |
| 34 | +'domaintype=s' => \$domain_type, |
| 35 | +'verbose' => \$verbose, |
| 36 | +) or printUsage(); |
| 37 | + |
| 38 | +if (!$master_server || !$username || !$password) { |
| 39 | + printUsage(); |
| 40 | +} |
| 41 | + |
| 42 | +# |
| 43 | +# Login and get token |
| 44 | +# |
| 45 | +my $token = gateway::perform_login($master_server, $username, $password, $domain_name, $domain_type); |
| 46 | + |
| 47 | +# |
| 48 | +# Get the first two pages of images, and record their backup ids |
| 49 | +# |
| 50 | +my $backup_ids = get_all_images(undef, 2); |
| 51 | + |
| 52 | +# |
| 53 | +# Check if there are any backup ids in the list, and if so, list details for the first one |
| 54 | +# |
| 55 | +if (exists $backup_ids->[0]){ |
| 56 | + my $json_results = catalog_images::get_image_details($master_server, $token, $backup_ids->[0]); |
| 57 | + if ($verbose and defined $json_results){ |
| 58 | + print JSON->new->pretty->encode($json_results); |
| 59 | + } |
| 60 | +} |
| 61 | +else { |
| 62 | + print "No images were found\n\n"; |
| 63 | +} |
| 64 | + |
| 65 | +# |
| 66 | +# Get the first two pages of vmware images, and record their backup ids |
| 67 | +# |
| 68 | +my $vmware_backup_ids = get_all_vmware_images(undef, 2); |
| 69 | + |
| 70 | +# |
| 71 | +# Check if there are any backup ids in the list, and if so, list details for the first one |
| 72 | +# |
| 73 | +if (exists $vmware_backup_ids->[0]) { |
| 74 | + my $json_results = catalog_images::get_vmware_image_details($master_server, $token, $vmware_backup_ids->[0]); |
| 75 | + if ($verbose and defined $json_results){ |
| 76 | + print JSON->new->pretty->encode($json_results); |
| 77 | + } |
| 78 | +} |
| 79 | +else { |
| 80 | + print "No vmware images were found\n\n"; |
| 81 | +} |
| 82 | + |
| 83 | +# |
| 84 | +# Let's try a sample filter, to demonstrate encoding |
| 85 | +# |
| 86 | +my $filter = "backupId eq 'a%20b_123'"; |
| 87 | +$backup_ids = get_all_images($filter); |
| 88 | + |
| 89 | +# |
| 90 | +# By default, the get images and get vmware images APIs filter on images that are within the last 24 hours. |
| 91 | +# Let's try getting the first page of values since January 2nd 2016 |
| 92 | +# |
| 93 | +$filter = "backupTime ge '2016-01-02T00:00:00.000Z'"; |
| 94 | +my $json_results = catalog_images::get_images($master_server, $token, $filter); |
| 95 | +if ($verbose and defined $json_results){ |
| 96 | + print JSON->new->pretty->encode($json_results); |
| 97 | +} |
| 98 | + |
| 99 | +gateway::perform_logout($master_server, $token); |
| 100 | + |
| 101 | +=head1 get_all_images |
| 102 | +
|
| 103 | + SYNOPSIS |
| 104 | + This subroutine calls get_images with proper pagination. |
| 105 | +
|
| 106 | + PARAMETERS |
| 107 | + $_[0] - string - optional |
| 108 | + The filter to use while gathering images |
| 109 | + $_[1] - int - optional |
| 110 | + The number of pages to get. Must be 1 or greater |
| 111 | +
|
| 112 | + RETURNS |
| 113 | + An array reference of the backup IDs found |
| 114 | +
|
| 115 | +=cut |
| 116 | +sub get_all_images { |
| 117 | + my $filter; |
| 118 | + my $num_pages; |
| 119 | + my $page_limit = 10; #This matches the default value |
| 120 | + my $offset = 0; |
| 121 | + my @backup_ids; |
| 122 | + |
| 123 | + if (exists $_[0]){ |
| 124 | + $filter = $_[0]; |
| 125 | + } |
| 126 | + if (exists $_[1]){ |
| 127 | + $num_pages = $_[1]; |
| 128 | + if ($num_pages <= 1){ |
| 129 | + print "Number of pages must be 1 or more"; |
| 130 | + return \@backup_ids; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + my $json_results = catalog_images::get_images($master_server, $token, $filter, $page_limit, $offset); |
| 135 | + if ($verbose and defined $json_results){ |
| 136 | + print JSON->new->pretty->encode($json_results); |
| 137 | + } |
| 138 | + |
| 139 | + my $pages_so_far = 1; |
| 140 | + while (defined $json_results) { |
| 141 | + # Save the first backup id, if it exists |
| 142 | + foreach my $data_entry (@{$json_results->{"data"}}) { |
| 143 | + push @backup_ids, $data_entry->{"id"}; |
| 144 | + } |
| 145 | + |
| 146 | + # Determine if there is a next page, and if so call the API again |
| 147 | + if (exists $json_results->{"meta"}{"pagination"}{"next"} and $num_pages > $pages_so_far){ |
| 148 | + $json_results = catalog_images::get_images($master_server, $token, $filter, $page_limit, $json_results->{"meta"}{"pagination"}{"next"}); |
| 149 | + $pages_so_far = $pages_so_far + 1; |
| 150 | + if ($verbose and defined $json_results){ |
| 151 | + print JSON->new->pretty->encode($json_results); |
| 152 | + } |
| 153 | + } |
| 154 | + else { |
| 155 | + last; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return \@backup_ids; |
| 160 | +} |
| 161 | + |
| 162 | +=head1 get_all_vmware_images |
| 163 | +
|
| 164 | + SYNOPSIS |
| 165 | + This subroutine prints information about all vmware images on the system. |
| 166 | +
|
| 167 | + PARAMETERS |
| 168 | + $_[0] - string - optional |
| 169 | + The filter to use while gathering images |
| 170 | + $_[1] - int - optional |
| 171 | + The number of pages to retrieve. Must be 1 or more. |
| 172 | +
|
| 173 | + RETURNS |
| 174 | + An array reference of the backup IDs found |
| 175 | +
|
| 176 | +=cut |
| 177 | +sub get_all_vmware_images { |
| 178 | + my $filter; |
| 179 | + my $num_pages; |
| 180 | + my $page_limit = 10; #This matches the default value |
| 181 | + my $offset = 0; |
| 182 | + my @backup_ids; |
| 183 | + if (exists $_[0]){ |
| 184 | + $filter = $_[0]; |
| 185 | + } |
| 186 | + if (exists $_[1]){ |
| 187 | + $num_pages = $_[1]; |
| 188 | + } |
| 189 | + |
| 190 | + my $json_results = catalog_images::get_vmware_images($master_server, $token, $filter, $page_limit, $offset); |
| 191 | + if ($verbose and defined $json_results){ |
| 192 | + print JSON->new->pretty->encode($json_results); |
| 193 | + } |
| 194 | + |
| 195 | + my $pages_so_far = 1; |
| 196 | + while (defined $json_results) { |
| 197 | + # Save the backup IDs |
| 198 | + foreach my $data_entry (@{$json_results->{"data"}}) { |
| 199 | + push @backup_ids, $data_entry->{"id"}; |
| 200 | + } |
| 201 | + |
| 202 | + # Determine if there is a next page, and if so call the API again |
| 203 | + if (exists $json_results->{"meta"}{"pagination"}{"next"} and $num_pages > $pages_so_far){ |
| 204 | + $json_results = catalog_images::get_vmware_images($master_server, $token, $filter, $page_limit, $json_results->{"meta"}{"pagination"}{"next"}); |
| 205 | + if ($verbose and defined $json_results){ |
| 206 | + print JSON->new->pretty->encode($json_results); |
| 207 | + } |
| 208 | + $pages_so_far = $pages_so_far + 1; |
| 209 | + } |
| 210 | + else { |
| 211 | + last; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + return \@backup_ids; |
| 216 | +} |
0 commit comments