Skip to content

Commit 5389a5f

Browse files
committed
fixing to use generation id for updating policy
1 parent a7e7e92 commit 5389a5f

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

recipes/perl/api_requests.pl

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ sub perform_login {
4646
if ($resp->is_success) {
4747
my $message = decode_json($resp->content);
4848
$token = $message->{"token"};
49-
print "Received token: $token\n";
5049
}
5150
else {
5251
print "HTTP POST error code: ", $resp->code, "\n";
@@ -165,6 +164,28 @@ sub read_policy {
165164
my $message = $resp->decoded_content;
166165
print "Get policy on [$policy_name] succeeded with status code: ", $resp->code, "\n";
167166
print "Compact Json body for list policy: \n", $message, "\n\n";
167+
# Etag (integer): The current generation ID of the policy.
168+
print "Respnse headers: \n", $resp->headers()->as_string, "\n\n";
169+
}
170+
else {
171+
print "HTTP GET error code: ", $resp->code, "\n";
172+
print "HTTP GET error message: ", $resp->message, "\n";
173+
}
174+
}
175+
176+
# subroutine to read policy and extract generation number from response
177+
my $genertion;
178+
sub read_policy_extract_Generation_Number_From_Response {
179+
my $policy_name = "vmware_test_policy";
180+
my $url = "$base_url/config/policies/$policy_name";
181+
182+
my $req = HTTP::Request->new(GET => $url);
183+
$req->header('content-type' => $content_type_v2);
184+
$req->header('Authorization' => $token);
185+
186+
my $resp = $ua->request($req);
187+
if ($resp->is_success) {
188+
$generation = $resp->header('ETag');
168189
}
169190
else {
170191
print "HTTP GET error code: ", $resp->code, "\n";
@@ -178,9 +199,14 @@ sub add_clients {
178199
my $policy_name = "vmware_test_policy";
179200
my $url = "$base_url/config/policies/$policy_name/clients/MEDIA_SERVER";
180201

202+
read_policy_extract_Generation_Number_From_Response();
203+
181204
my $req = HTTP::Request->new(PUT => $url);
182205
$req->header('content-type' => $content_type_v2);
183206
$req->header('Authorization' => $token);
207+
# The audit reason for chaning the policy (this header is optional)
208+
$req->header('X-NetBackup-Audit-Reason' => "adding client to the policy $policy_name");
209+
$req->header('If-Match' => $generation);
184210

185211
my $post_data = qq({ "data": { "type": "client", "id": "MEDIA_SERVER", "attributes": {
186212
"hardware": "VMware", "OS": "VMware", "hostName": "MEDIA_SERVER" } } } );
@@ -189,6 +215,8 @@ sub add_clients {
189215
print "\n\n**************************************************************";
190216
print "\n\n Making PUT Request to add clients to policy \n\n";
191217

218+
print "Using ETag : [", $generation, "] in the request If-Match to update the policy", "\n\n";
219+
192220
my $resp = $ua->request($req);
193221
if ($resp->is_success) {
194222
print "Client is added to policy [$policy_name] with status code: ", $resp->code, "\n";
@@ -204,9 +232,14 @@ sub add_backupselections {
204232
my $policy_name = "vmware_test_policy";
205233
my $url = "$base_url/config/policies/$policy_name/backupselections";
206234

235+
read_policy_extract_Generation_Number_From_Response();
236+
207237
my $req = HTTP::Request->new(PUT => $url);
208238
$req->header('content-type' => $content_type_v2);
209239
$req->header('Authorization' => $token);
240+
# The audit reason for chaning the policy (this header is optional)
241+
$req->header('X-NetBackup-Audit-Reason' => "adding backupSelection to the policy [$policy_name]");
242+
$req->header('If-Match' => $generation);
210243

211244
my $post_data = qq({ "data": { "type": "backupSelection", "attributes": {
212245
"selections": [ "vmware:/?filter=Displayname Equal \\\"Redacted-Test\\\"" ] } } } );
@@ -215,6 +248,8 @@ sub add_backupselections {
215248
print "\n\n**************************************************************";
216249
print "\n\n Making PUT Request to add backupselection to policy \n\n";
217250

251+
print "Using ETag : [", $generation, "] in the request If-Match to update the policy", "\n\n";
252+
218253
my $resp = $ua->request($req);
219254
if ($resp->is_success) {
220255
print "BackupSelection is added to policy [$policy_name] with status code: ", $resp->code, "\n";
@@ -231,9 +266,14 @@ sub add_schedule {
231266
my $schedule_name = "schedule1";
232267
my $url = "$base_url/config/policies/$policy_name/schedules/$schedule_name";
233268

269+
read_policy_extract_Generation_Number_From_Response();
270+
234271
my $req = HTTP::Request->new(PUT => $url);
235272
$req->header('content-type' => $content_type_v2);
236273
$req->header('Authorization' => $token);
274+
# The audit reason for chaning the policy (this header is optional)
275+
$req->header('X-NetBackup-Audit-Reason' => "adding schedule [$schedule_name] to the policy [$policy_name]");
276+
$req->header('If-Match' => $generation);
237277

238278
my $post_data = qq({ "data": { "type": "schedule", "id": "$schedule_name", "attributes": {
239279
"acceleratorForcedRescan": false, "backupType": "Full Backup", "backupCopies": {
@@ -252,6 +292,8 @@ sub add_schedule {
252292
print "\n\n**************************************************************";
253293
print "\n\n Making PUT Request to add schedule to policy \n\n";
254294

295+
print "Using ETag : [", $generation, "] in the request If-Match to update the policy", "\n\n";
296+
255297
my $resp = $ua->request($req);
256298
if ($resp->is_success) {
257299
print "Schedule [$schedule_name] is added to policy [$policy_name] with status code: ", $resp->code, "\n";
@@ -267,13 +309,20 @@ sub delete_client {
267309
my $policy_name = "vmware_test_policy";
268310
my $url = "$base_url/config/policies/$policy_name/clients/MEDIA_SERVER";
269311

312+
read_policy_extract_Generation_Number_From_Response();
313+
270314
my $req = HTTP::Request->new(DELETE => $url);
271315
$req->header('content-type' => $content_type_v2);
272316
$req->header('Authorization' => $token);
317+
# The audit reason for chaning the policy (this header is optional)
318+
$req->header('X-NetBackup-Audit-Reason' => "deleting client from the policy [$policy_name]");
319+
$req->header('If-Match' => $generation);
273320

274321
print "\n\n**************************************************************";
275322
print "\n\n Making DELETE Request to remove clients from the policy \n\n";
276323

324+
print "Using ETag : [", $generation, "] in the request If-Match to update the policy", "\n\n";
325+
277326
my $resp = $ua->request($req);
278327
if ($resp->is_success) {
279328
print "Client [MEDIA_SERVER] is deleted from policy [$policy_name] with status code: ", $resp->code, "\n";
@@ -290,13 +339,20 @@ sub delete_schedule {
290339
my $schedule_name = "schedule1";
291340
my $url = "$base_url/config/policies/$policy_name/schedules/$schedule_name";
292341

342+
read_policy_extract_Generation_Number_From_Response();
343+
293344
my $req = HTTP::Request->new(DELETE => $url);
294345
$req->header('content-type' => $content_type_v2);
295346
$req->header('Authorization' => $token);
347+
# The audit reason for chaning the policy (this header is optional)
348+
$req->header('X-NetBackup-Audit-Reason' => "deleting schedule [$schedule_name] from the policy [$policy_name]");
349+
$req->header('If-Match' => $generation);
296350

297351
print "\n\n**************************************************************";
298352
print "\n\n Making DELETE Request to remove schedule from the policy \n\n";
299353

354+
print "Using ETag : [", $generation, "] in the request If-Match to update the policy", "\n\n";
355+
300356
my $resp = $ua->request($req);
301357
if ($resp->is_success) {
302358
print "Schedule [$schedule_name] is deleted from policy [$policy_name] with status code: ", $resp->code, "\n";

0 commit comments

Comments
 (0)