Skip to content

Commit c7b4bb7

Browse files
committed
Merge branch 'feature/create_policy_with_defaults' of https://github.com/VeritasOS/netbackup-api-code-samples into feature/create_policy_with_defaults
2 parents 3e6ee82 + 77ae62e commit c7b4bb7

21 files changed

+1443
-604
lines changed

README.md

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,13 @@ Contains code samples to invoke NetBackup REST API using different programming l
55
#### Disclaimer
66
These scripts are only meant to be used as a reference. Please do not use these in production.
77

8-
#### Executing the snippets in PowerShell
9-
Pre-requisites:
10-
- NetBackup 8.1.1 or higher
11-
- Powershell 5.0 or higher
12-
13-
Use the following commands to run the powershell samples.
14-
- `.\Get-NB-Images.ps1 -nbmaster <masterServer> -username <username> -password <password>`
15-
- `.\Get-NB-Jobs.ps1 -nbmaster <masterServer> -username <username> -password <password>`
16-
17-
#### Executing the snippets in Python
18-
Pre-requisites:
19-
- NetBackup 8.1.1 or higher
20-
- python 3.5 or higher
21-
- python modules: `requests, texttable`
8+
#### Executing the snippets for different programming languages
229

23-
Use the following commands to run the python samples.
24-
- `python -W ignore get_nb_images.py -nbmaster <masterServer> -username <username> -password <password>`
25-
- `python -W ignore get_nb_jobs.py -nbmaster <masterServer> -username <username> -password <password>`
10+
The `snippets` folder contains code samples to invoke NetBackup REST API using different programming languages.
2611

27-
#### Executing the snippets in Perl
2812
Pre-requisites:
2913
- NetBackup 8.1.1 or higher
30-
- See script README for perl requirements and usage
14+
- See the script's README for the corresponding requirements and usage
3115

3216
#### Executing the recipes in Perl
3317
Pre-requisites:
@@ -36,4 +20,11 @@ Pre-requisites:
3620

3721
Use the following commands to run the perl samples.
3822
- `perl create_policy_step_by_step.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
39-
- `perl create_policy_in_one_step.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
23+
- `perl create_policy_in_one_step.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
24+
- `perl api_requests_images.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
25+
- `perl api_requests_image_contents.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
26+
27+
#### Tools
28+
The `tools` folder contains utilities that have proven useful in the development of projects using
29+
NetBackup REST APIs, but do not provide any API usage examples. Again, these tools are not for
30+
production use, but they may be of some use in your work.
Lines changed: 106 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +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-
}
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

Comments
 (0)