Skip to content

Commit ae67610

Browse files
committed
added script for creating policy without default values
1 parent ea567d8 commit ae67610

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ Pre-requisites:
3535
- Perl 5.20.2 or higher
3636

3737
Use the following commands to run the perl samples.
38-
- `perl create_policy_with_defaults.pl -nbmaster <masterServer> -username <username> -password <password> -domainName <domainName> -domainType <domainType>`
38+
- `perl create_policy_with_defaults.pl -nbmaster <masterServer> -username <username> -password <password> -domainName <domainName> -domainType <domainType>`
39+
- `perl create_policy_without_defaults.pl -nbmaster <masterServer> -username <username> -password <password> -domainName <domainName> -domainType <domainType>`

recipes/perl/api_requests.pl

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ sub perform_login {
5454
}
5555
}
5656

57-
# create VMWare policy with the name veritas_policy1
57+
# create VMWare policy with the name veritas_policy1 with default values
5858
sub create_policy_with_defaults {
5959

6060
my $url = "$base_url/config/policies";
@@ -81,6 +81,50 @@ sub create_policy_with_defaults {
8181
}
8282
}
8383

84+
# create VMWare policy with the name veritas_policy1
85+
sub create_policy {
86+
87+
my $url = "$base_url/config/policies";
88+
my $policy_name = "veritas_policy1";
89+
90+
my $req = HTTP::Request->new(POST => $url);
91+
$req->header('content-type' => $content_type_v2);
92+
$req->header('Authorization' => $token);
93+
94+
my $backupSelections = qq("backupSelections": {
95+
"selections": [ "vmware:/?filter=Displayname Equal \\\"Redacted-Test\\\"" ] });
96+
my $clients = qq("clients": [
97+
{ "hardware": "VMware", "OS": "VMware", "hostName": "MEDIA_SERVER" } ]);
98+
my $schedules = qq("schedules": [ {"acceleratorForcedRescan": false, "backupType": "Full Backup", "backupCopies": {
99+
"priority": 9999, "copies": [ { "mediaOwner": "owner1", "storage": null, "retentionPeriod": {
100+
"value": 9, "unit": "WEEKS" }, "volumePool": "NetBackup", "failStrategy": "Continue"}]},
101+
"excludeDates": { "lastDayOfMonth": true, "recurringDaysOfWeek": [ "4:6", "2:5" ], "recurringDaysOfMonth": [ 10 ],
102+
"specificDates": [ "2000-1-1", "2016-2-30" ] }, "frequencySeconds": 4800, "includeDates": {
103+
"lastDayOfMonth": true, "recurringDaysOfWeek": [ "2:3", "3:4" ], "recurringDaysOfMonth": [ 10,13], "specificDates": [
104+
"2016-12-31" ] }, "mediaMultiplexing":2, "retriesAllowedAfterRunDay": true, "scheduleType": "Calendar", "snapshotOnly": false,
105+
"startWindow": [ { "dayOfWeek": 1, "startSeconds": 14600, "durationSeconds": 24600 }, { "dayOfWeek": 2, "startSeconds": 14600, "durationSeconds": 24600 },
106+
{ "dayOfWeek": 3, "startSeconds": 14600, "durationSeconds": 24600 }, { "dayOfWeek": 4, "startSeconds": 14600, "durationSeconds": 24600 },
107+
{ "dayOfWeek": 5, "startSeconds": 14600, "durationSeconds": 24600 }, { "dayOfWeek": 6, "startSeconds": 14600, "durationSeconds": 24600 },
108+
{ "dayOfWeek": 7, "startSeconds": 14600, "durationSeconds": 24600 } ], "syntheticBackup": false, "storageIsSLP": false, "scheduleName": "sched-9-weeks" } ]);
109+
my $policy_attributes = qq("policyAttributes": { "active": true, "snapshotMethodArgs": "skipnodisk=0", "jobLimit": 10} );
110+
111+
my $post_data = qq({ "data": { "type": "policy", "id": "$policy_name", "attributes": {
112+
"policy": { "policyName": "$policy_name", "policyType": "VMware", $policy_attributes, $clients, $backupSelections, $schedules } } } });
113+
$req->content($post_data);
114+
115+
print "\n\n**************************************************************";
116+
print "\n\n Making POST Request to create VMWare policy with defaults \n\n";
117+
118+
my $resp = $ua->request($req);
119+
if ($resp->is_success) {
120+
print "Policy [$policy_name] with default values is create with status code: ", $resp->code, "\n";
121+
}
122+
else {
123+
print "HTTP POST error code: ", $resp->code, "\n";
124+
print "HTTP POST error message: ", $resp->message, "\n";
125+
}
126+
}
127+
84128
# subroutine to list policies
85129
sub list_policies {
86130
my $url = "$base_url/config/policies";
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env perl
2+
3+
use LWP::UserAgent;
4+
use LWP::Protocol::https;
5+
print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n";
6+
print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n";
7+
use JSON;
8+
use Getopt::Long qw(GetOptions);
9+
10+
require 'api_requests.pl';
11+
12+
#
13+
# The token is the key to the NetBackup AuthN/AuthZ scheme. You must login and get a token
14+
# and use this token in your Authorization header for all subsequent requests. Token validity
15+
# is fixed at 24 hours
16+
#
17+
my $token;
18+
19+
my $protocol = "https";
20+
my $port = "1556";
21+
my $nbmaster;
22+
my $username;
23+
my $password;
24+
my $domainName;
25+
my $domainType;
26+
my $base_url;
27+
28+
29+
#change this as per your host name
30+
$fqdn_hostname = "localhost";
31+
32+
# subroutines for printing usage and library information required to run the script.
33+
sub print_usage {
34+
print("\n\nUsage:");
35+
print("\nperl create_policy_with_defaults -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]\n\n\n");
36+
}
37+
38+
sub print_disclaimer {
39+
print("--------------------------------------------------------\n");
40+
print("-- This script requires Perl 5.20.2 or later --\n");
41+
print("--------------------------------------------------------\n");
42+
print("Executing this library requires some additional libraries like \n\t'LWP' \n\t'JSON'\ \n\t'Getopt'\ \n\n");
43+
print("You can specify the 'nbmaster', 'username' and 'password' as command-line parameters\n");
44+
print_usage();
45+
}
46+
47+
48+
# subroutine to process user input
49+
sub user_input {
50+
GetOptions(
51+
'nbmaster=s' => \$nbmaster,
52+
'username=s' => \$username,
53+
'password=s' => \$password,
54+
'domainName=s' => \$domainName,
55+
'domainType=s' => \$domainType,
56+
) or die print_usage();
57+
58+
if ($nbmaster eq "") {
59+
print("Please provide the value for 'nbmaster', 'username' and 'password'");
60+
print_usage();
61+
exit;
62+
}
63+
64+
if ($username eq "") {
65+
print("Please provide the value for 'nbmaster', 'username' and 'password'");
66+
print_usage();
67+
exit;
68+
}
69+
70+
if ($password eq "") {
71+
print("Please provide the value for 'nbmaster', 'username' and 'password'");
72+
print_usage();
73+
exit;
74+
}
75+
76+
$base_url = "$protocol://$nbmaster:$port/netbackup";
77+
}
78+
79+
sub policy_automation {
80+
perform_login($base_url, $username, $password, $domainName, $domainType);
81+
create_policy();
82+
list_policies();
83+
read_policy();
84+
delete_policy();
85+
list_policies();
86+
}
87+
88+
print_disclaimer();
89+
90+
user_input();
91+
92+
policy_automation();

0 commit comments

Comments
 (0)