Skip to content

Commit 1772123

Browse files
committed
automation scripts for policy in perl
1 parent c82ef24 commit 1772123

File tree

3 files changed

+321
-17
lines changed

3 files changed

+321
-17
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ Pre-requisites:
2929
- NetBackup 8.1.1 or higher
3030
- See script README for perl requirements and usage
3131

32+
#### Executing the recipes in Perl
33+
Pre-requisites:
34+
- NetBackup 8.1.1 or higher
35+
- Perl 5.20.2 or higher
36+
37+
Use the following commands to run the perl samples.
38+
- `perl create_policy_with_defaults.pl -nbmaster localhost -username testuser -password testpass -domainName test -domainType vx`

recipes/perl/api_requests.pl

Lines changed: 256 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
#!/usr/bin/env perl
22
use LWP::UserAgent;
33

4-
my $content_type_v2 = "application/json";
4+
my $content_type_v2 = "application/vnd.netbackup+json; version=2.0";
55

66
#We will get this token using login api and the token will be used
77
#in subsequent api requests of policy
88
my $token;
99

10+
my $json = JSON->new;
11+
my $ua = LWP::UserAgent->new(
12+
ssl_opts => { verify_hostname => 0, verify_peer => 0},
13+
);
14+
15+
my $base_url;
16+
17+
# subroutine to call login api using user credentials to get the token to be used
18+
# by subsequent API calls
1019
sub perform_login {
11-
my $base_url;
1220

13-
foreach $item (@_) {
14-
$base_url = $item;
15-
}
21+
my @argument_list = @_;
22+
$base_url = $argument_list[0];
23+
my $username = $argument_list[1];
24+
my $password = $argument_list[2];
25+
my $domainName = $argument_list[3];
26+
my $domainType = $argument_list[4];
1627

1728
my $url = "$base_url/login";
18-
print "\nUsing url: $url\n";
1929

2030
my $req = HTTP::Request->new(POST => $url);
21-
$req->header('content-type' => $content_type_v2);
22-
my $post_data = '{ "domainType": "nt", "domainName": "rmnus", "userName": "akumar1", "password": "FEELDheat9193" }';
31+
$req->header('content-type' => 'application/json');
32+
33+
if ($domainName eq "" && $domainType eq "") {
34+
$post_data = qq({ "userName": "$username", "password": "$password" });
35+
}
36+
else {
37+
$post_data = qq({ "domainType": "$domainType", "domainName": "$domainName", "userName": "$username", "password": "$password" });
38+
}
2339
$req->content($post_data);
2440

25-
my $ua = LWP::UserAgent->new(
26-
ssl_opts => { verify_hostname => 0, verify_peer => 0},
27-
);
2841

2942
print "\n\n**************************************************************";
30-
print "\n\n Making Post Request to login to get token \n\n";
43+
print "\n\n Making POST Request to login to get token \n\n";
3144

3245
my $resp = $ua->request($req);
3346
if ($resp->is_success) {
@@ -41,7 +54,236 @@ sub perform_login {
4154
}
4255
}
4356

44-
sub customtest {
45-
print "we made it here";
57+
# create VMWare policy with the name veritas_policy1
58+
sub create_policy_with_defaults {
59+
60+
my $url = "$base_url/config/policies";
61+
my $policy_name = "veritas_policy1";
62+
63+
my $req = HTTP::Request->new(POST => $url);
64+
$req->header('content-type' => $content_type_v2);
65+
$req->header('Authorization' => $token);
66+
67+
my $post_data = qq({ "data": { "type": "policy", "id": "$policy_name", "attributes": {
68+
"policy": { "policyName": "$policy_name", "policyType": "VMware" } } } });
69+
$req->content($post_data);
70+
71+
print "\n\n**************************************************************";
72+
print "\n\n Making POST Request to create VMWare policy with defaults \n\n";
73+
74+
my $resp = $ua->request($req);
75+
if ($resp->is_success) {
76+
print "Policy [$policy_name] with default values is create with status code: ", $resp->code, "\n";
77+
}
78+
else {
79+
print "HTTP POST error code: ", $resp->code, "\n";
80+
print "HTTP POST error message: ", $resp->message, "\n";
81+
}
82+
}
83+
84+
# subroutine to list policies
85+
sub list_policies {
86+
my $url = "$base_url/config/policies";
87+
88+
my $req = HTTP::Request->new(GET => $url);
89+
$req->header('content-type' => $content_type_v2);
90+
$req->header('Authorization' => $token);
91+
92+
print "\n\n**************************************************************";
93+
print "\n\n Making GET Request to list policies \n\n";
94+
95+
my $resp = $ua->request($req);
96+
if ($resp->is_success) {
97+
my $message = $resp->decoded_content;
98+
print "List policy succeeded with status code: ", $resp->code, "\n";
99+
print "Compact Json body for list policy: \n", $message, "\n\n";
100+
}
101+
else {
102+
print "HTTP GET error code: ", $resp->code, "\n";
103+
print "HTTP GET error message: ", $resp->message, "\n";
104+
}
105+
}
106+
107+
# subroutine to read policy
108+
sub read_policy {
109+
my $policy_name = "veritas_policy1";
110+
my $url = "$base_url/config/policies/$policy_name";
111+
112+
my $req = HTTP::Request->new(GET => $url);
113+
$req->header('content-type' => $content_type_v2);
114+
$req->header('Authorization' => $token);
115+
116+
print "\n\n**************************************************************";
117+
print "\n\n Making GET Request to read policy \n\n";
118+
119+
my $resp = $ua->request($req);
120+
if ($resp->is_success) {
121+
my $message = $resp->decoded_content;
122+
print "Get policy on [$policy_name] succeeded with status code: ", $resp->code, "\n";
123+
print "Compact Json body for list policy: \n", $message, "\n\n";
124+
}
125+
else {
126+
print "HTTP GET error code: ", $resp->code, "\n";
127+
print "HTTP GET error message: ", $resp->message, "\n";
128+
}
129+
}
130+
131+
# subroutine to create client. For VIP query, we expect
132+
# hostName to be MEDIA_SERVER, OS and hardware to be VMWare.
133+
sub add_clients {
134+
my $policy_name = "veritas_policy1";
135+
my $url = "$base_url/config/policies/$policy_name/clients/MEDIA_SERVER";
136+
137+
my $req = HTTP::Request->new(PUT => $url);
138+
$req->header('content-type' => $content_type_v2);
139+
$req->header('Authorization' => $token);
140+
141+
my $post_data = qq({ "data": { "type": "client", "id": "MEDIA_SERVER", "attributes": {
142+
"hardware": "VMware", "OS": "VMware", "hostName": "MEDIA_SERVER" } } } );
143+
$req->content($post_data);
144+
145+
print "\n\n**************************************************************";
146+
print "\n\n Making PUT Request to add clients to policy \n\n";
147+
148+
my $resp = $ua->request($req);
149+
if ($resp->is_success) {
150+
print "Client is added to policy [$policy_name] with status code: ", $resp->code, "\n";
151+
}
152+
else {
153+
print "HTTP PUT error code: ", $resp->code, "\n";
154+
print "HTTP PUT error message: ", $resp->message, "\n";
155+
}
156+
}
157+
158+
# subroutine to add backupSelections to a policy
159+
sub add_backupselections {
160+
my $policy_name = "veritas_policy1";
161+
my $url = "$base_url/config/policies/$policy_name/backupselections";
162+
163+
my $req = HTTP::Request->new(PUT => $url);
164+
$req->header('content-type' => $content_type_v2);
165+
$req->header('Authorization' => $token);
166+
167+
my $post_data = qq({ "data": { "type": "backupSelection", "attributes": {
168+
"selections": [ "vmware:/?filter=Displayname Equal \\\"Redacted-Test\\\"" ] } } } );
169+
$req->content($post_data);
170+
171+
print "\n\n**************************************************************";
172+
print "\n\n Making PUT Request to add backupselection to policy \n\n";
173+
174+
my $resp = $ua->request($req);
175+
if ($resp->is_success) {
176+
print "BackupSelection is added to policy [$policy_name] with status code: ", $resp->code, "\n";
177+
}
178+
else {
179+
print "HTTP PUT error code: ", $resp->code, "\n";
180+
print "HTTP PUT error message: ", $resp->message, "\n";
181+
}
182+
}
183+
184+
# subroutine to add schedule to a policy
185+
sub add_schedule {
186+
my $policy_name = "veritas_policy1";
187+
my $schedule_name = "schedule1";
188+
my $url = "$base_url/config/policies/$policy_name/schedules/$schedule_name";
189+
190+
my $req = HTTP::Request->new(PUT => $url);
191+
$req->header('content-type' => $content_type_v2);
192+
$req->header('Authorization' => $token);
193+
194+
my $post_data = qq({ "data": { "type": "schedule", "id": "$schedule_name", "attributes": {
195+
"acceleratorForcedRescan": false, "backupType": "Full Backup", "backupCopies": {
196+
"priority": 9999, "copies": [ { "mediaOwner": "owner1", "storage": null, "retentionPeriod": {
197+
"value": 9, "unit": "WEEKS" }, "volumePool": "NetBackup", "failStrategy": "Continue"}]},
198+
"excludeDates": { "lastDayOfMonth": true, "recurringDaysOfWeek": [ "4:6", "2:5" ], "recurringDaysOfMonth": [ 10 ],
199+
"specificDates": [ "2000-1-1", "2016-2-30" ] }, "frequencySeconds": 4800, "includeDates": {
200+
"lastDayOfMonth": true, "recurringDaysOfWeek": [ "2:3", "3:4" ], "recurringDaysOfMonth": [ 10,13], "specificDates": [
201+
"2016-12-31" ] }, "mediaMultiplexing":2, "retriesAllowedAfterRunDay": true, "scheduleType": "Calendar", "snapshotOnly": false,
202+
"startWindow": [ { "dayOfWeek": 1, "startSeconds": 14600, "durationSeconds": 24600 }, { "dayOfWeek": 2, "startSeconds": 14600, "durationSeconds": 24600 },
203+
{ "dayOfWeek": 3, "startSeconds": 14600, "durationSeconds": 24600 }, { "dayOfWeek": 4, "startSeconds": 14600, "durationSeconds": 24600 },
204+
{ "dayOfWeek": 5, "startSeconds": 14600, "durationSeconds": 24600 }, { "dayOfWeek": 6, "startSeconds": 14600, "durationSeconds": 24600 },
205+
{ "dayOfWeek": 7, "startSeconds": 14600, "durationSeconds": 24600 } ], "syntheticBackup": false, "storageIsSLP": false } } } );
206+
$req->content($post_data);
207+
208+
print "\n\n**************************************************************";
209+
print "\n\n Making PUT Request to add schedule to policy \n\n";
210+
211+
my $resp = $ua->request($req);
212+
if ($resp->is_success) {
213+
print "Schedule [$schedule_name] is added to policy [$policy_name] with status code: ", $resp->code, "\n";
214+
}
215+
else {
216+
print "HTTP PUT error code: ", $resp->code, "\n";
217+
print "HTTP PUT error message: ", $resp->message, "\n";
218+
}
219+
}
220+
221+
# subroutine to delete client from a policy
222+
sub delete_client {
223+
my $policy_name = "veritas_policy1";
224+
my $url = "$base_url/config/policies/$policy_name/clients/MEDIA_SERVER";
225+
226+
my $req = HTTP::Request->new(DELETE => $url);
227+
$req->header('content-type' => $content_type_v2);
228+
$req->header('Authorization' => $token);
229+
230+
print "\n\n**************************************************************";
231+
print "\n\n Making DELETE Request to remove clients from the policy \n\n";
232+
233+
my $resp = $ua->request($req);
234+
if ($resp->is_success) {
235+
print "Client [MEDIA_SERVER] is deleted from policy [$policy_name] with status code: ", $resp->code, "\n";
236+
}
237+
else {
238+
print "HTTP DELETE error code: ", $resp->code, "\n";
239+
print "HTTP DELETE error message: ", $resp->message, "\n";
240+
}
241+
}
242+
243+
# subroutine to delete schedule from a policy
244+
sub delete_schedule {
245+
my $policy_name = "veritas_policy1";
246+
my $schedule_name = "schedule1";
247+
my $url = "$base_url/config/policies/$policy_name/schedules/$schedule_name";
248+
249+
my $req = HTTP::Request->new(DELETE => $url);
250+
$req->header('content-type' => $content_type_v2);
251+
$req->header('Authorization' => $token);
252+
253+
print "\n\n**************************************************************";
254+
print "\n\n Making DELETE Request to remove schedule from the policy \n\n";
255+
256+
my $resp = $ua->request($req);
257+
if ($resp->is_success) {
258+
print "Schedule [$schedule_name] is deleted from policy [$policy_name] with status code: ", $resp->code, "\n";
259+
}
260+
else {
261+
print "HTTP DELETE error code: ", $resp->code, "\n";
262+
print "HTTP DELETE error message: ", $resp->message, "\n";
263+
}
264+
}
265+
266+
# subroutine to delete policy
267+
sub delete_policy {
268+
269+
my $policy_name = "veritas_policy1";
270+
my $url = "$base_url/config/policies/$policy_name";
271+
272+
my $req = HTTP::Request->new(DELETE => $url);
273+
$req->header('content-type' => $content_type_v2);
274+
$req->header('Authorization' => $token);
275+
276+
print "\n\n**************************************************************";
277+
print "\n\n Making DELETE Request to remove the policy \n\n";
278+
279+
my $resp = $ua->request($req);
280+
if ($resp->is_success) {
281+
print "Policy is deleted with status code: ", $resp->code, "\n";
282+
}
283+
else {
284+
print "HTTP DELETE error code: ", $resp->code, "\n";
285+
print "HTTP DELETE error message: ", $resp->message, "\n";
286+
}
46287
}
288+
47289
1;

0 commit comments

Comments
 (0)