Skip to content

Commit edd2595

Browse files
committed
Merge branch 'master' into feature/policy-without-defaults-API-powershell
2 parents 15cbb9c + 3c8a805 commit edd2595

20 files changed

+2290
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ Pre-requisites:
1414
- NetBackup 8.1.1 or higher
1515
- See the script's README for the corresponding requirements and usage
1616

17+
#### Executing the recipes in Perl
18+
19+
Pre-requisites:
20+
- NetBackup 8.1.2 or higher
21+
- Perl 5.20.2 or higher
22+
23+
Use the following commands to run the perl samples.
24+
- `perl create_policy_step_by_step.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
25+
- `perl create_policy_in_one_step.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
26+
- `perl rbac_filtering_in_policy.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
27+
- `perl api_requests_images.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
28+
- `perl api_requests_image_contents.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
29+
1730
#### Executing the recipes in Python
1831

1932
Pre-requisites:
@@ -36,6 +49,7 @@ Use the following commands to run the python samples.
3649
- `python -W ignore create_policy_in_one_step.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
3750
- `python -W ignore rbac_filtering_in_policy.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
3851

52+
3953
#### Tools
4054
The `tools` folder contains utilities that have proven useful in the development of projects using
4155
NetBackup REST APIs, but do not provide any API usage examples. Again, these tools are not for

recipes/perl/api_requests.pl

Lines changed: 406 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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

Comments
 (0)