Skip to content

Commit 6b51a7e

Browse files
committed
Adding perl examples for image APIs
1 parent 5389a5f commit 6b51a7e

File tree

5 files changed

+775
-0
lines changed

5 files changed

+775
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
=head1 get_all_image_contents
62+
63+
SYNOPSIS
64+
This subroutine retrieves a list of all files in a specified image
65+
66+
PARAMETERS
67+
$_[0] - string
68+
The backup ID of the image for which you wish to list all contents
69+
70+
RETURNS
71+
An array reference containing the absolute path to each file in the image
72+
73+
=cut
74+
sub get_all_image_contents {
75+
my $page_limit = 200; #This matches the default value
76+
my @files;
77+
my $backup_id = $_[0];
78+
if (not defined $backup_id){
79+
print "Error: backup ID is a required field\n";
80+
return \@files;
81+
}
82+
83+
my $request_id = catalog_images::get_request_id($master_server, $token, $backup_id);
84+
my $json_results = catalog_images::get_image_contents($master_server, $token, $request_id);
85+
if ($verbose and defined $json_results) {
86+
print JSON->new->pretty->encode($json_results);
87+
}
88+
89+
# continute calling image contents API with the same request ID until we get a 404
90+
while (defined $json_results) {
91+
# Save the file names
92+
foreach my $data_entry (@{$json_results->{"data"}}) {
93+
push @files, $data_entry->{"attributes"}{"filePath"};
94+
}
95+
96+
$json_results = catalog_images::get_image_contents($master_server, $token, $request_id);
97+
if ($verbose and defined $json_results){
98+
print JSON->new->pretty->encode($json_results);
99+
}
100+
}
101+
102+
print "\nPrevious 404 marks the end of image contents\n";
103+
return \@files;
104+
}
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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+
=head1 get_all_images
100+
101+
SYNOPSIS
102+
This subroutine calls get_images with proper pagination.
103+
104+
PARAMETERS
105+
$_[0] - string - optional
106+
The filter to use while gathering images
107+
$_[1] - int - optional
108+
The number of pages to get. Must be 1 or greater
109+
110+
RETURNS
111+
An array reference of the backup IDs found
112+
113+
=cut
114+
sub get_all_images {
115+
my $filter;
116+
my $num_pages;
117+
my $page_limit = 10; #This matches the default value
118+
my $offset = 0;
119+
my @backup_ids;
120+
121+
if (exists $_[0]){
122+
$filter = $_[0];
123+
}
124+
if (exists $_[1]){
125+
$num_pages = $_[1];
126+
if ($num_pages <= 1){
127+
print "Number of pages must be 1 or more";
128+
return \@backup_ids;
129+
}
130+
}
131+
132+
my $json_results = catalog_images::get_images($master_server, $token, $filter, $page_limit, $offset);
133+
if ($verbose and defined $json_results){
134+
print JSON->new->pretty->encode($json_results);
135+
}
136+
137+
my $pages_so_far = 1;
138+
while (defined $json_results) {
139+
# Save the first backup id, if it exists
140+
foreach my $data_entry (@{$json_results->{"data"}}) {
141+
push @backup_ids, $data_entry->{"id"};
142+
}
143+
144+
# Determine if there is a next page, and if so call the API again
145+
if (exists $json_results->{"meta"}{"pagination"}{"next"} and $num_pages > $pages_so_far){
146+
$json_results = catalog_images::get_images($master_server, $token, $filter, $page_limit, $json_results->{"meta"}{"pagination"}{"next"});
147+
$pages_so_far = $pages_so_far + 1;
148+
if ($verbose and defined $json_results){
149+
print JSON->new->pretty->encode($json_results);
150+
}
151+
}
152+
else {
153+
last;
154+
}
155+
}
156+
157+
return \@backup_ids;
158+
}
159+
160+
=head1 get_all_vmware_images
161+
162+
SYNOPSIS
163+
This subroutine prints information about all vmware images on the system.
164+
165+
PARAMETERS
166+
$_[0] - string - optional
167+
The filter to use while gathering images
168+
$_[1] - int - optional
169+
The number of pages to retrieve. Must be 1 or more.
170+
171+
RETURNS
172+
An array reference of the backup IDs found
173+
174+
=cut
175+
sub get_all_vmware_images {
176+
my $filter;
177+
my $num_pages;
178+
my $page_limit = 10; #This matches the default value
179+
my $offset = 0;
180+
my @backup_ids;
181+
if (exists $_[0]){
182+
$filter = $_[0];
183+
}
184+
if (exists $_[1]){
185+
$num_pages = $_[1];
186+
}
187+
188+
my $json_results = catalog_images::get_vmware_images($master_server, $token, $filter, $page_limit, $offset);
189+
if ($verbose and defined $json_results){
190+
print JSON->new->pretty->encode($json_results);
191+
}
192+
193+
my $pages_so_far = 1;
194+
while (defined $json_results) {
195+
# Save the backup IDs
196+
foreach my $data_entry (@{$json_results->{"data"}}) {
197+
push @backup_ids, $data_entry->{"id"};
198+
}
199+
200+
# Determine if there is a next page, and if so call the API again
201+
if (exists $json_results->{"meta"}{"pagination"}{"next"} and $num_pages > $pages_so_far){
202+
$json_results = catalog_images::get_vmware_images($master_server, $token, $filter, $page_limit, $json_results->{"meta"}{"pagination"}{"next"});
203+
if ($verbose and defined $json_results){
204+
print JSON->new->pretty->encode($json_results);
205+
}
206+
$pages_so_far = $pages_so_far + 1;
207+
}
208+
else {
209+
last;
210+
}
211+
}
212+
213+
return \@backup_ids;
214+
}

0 commit comments

Comments
 (0)