Skip to content

Commit 7eaa44e

Browse files
committed
Merge branch 'feature/sample-api-policy-python' of https://github.com/VeritasOS/netbackup-api-code-samples into feature/sample-api-policy-python
2 parents 4da0a4e + dc6f97e commit 7eaa44e

File tree

6 files changed

+342
-64
lines changed

6 files changed

+342
-64
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Pre-requisites:
2424
Use the following commands to run the python samples.
2525
- `python -W ignore create_policy_step_by_step.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
2626
- `python -W ignore create_policy_in_one_step.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
27+
- `python -W ignore rbac_filtering_in_policy.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
2728

2829
#### Tools
2930
The `tools` folder contains utilities that have proven useful in the development of projects using

recpies/python/create_policy_in_one_step.py renamed to recipes/python/create_policy_in_one_step.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ def read_command_line_arguments():
7777

7878
jwt = policy_api_requests.perform_login(username, password, domainName, domainType, base_url)
7979

80-
createPolicy = policy_api_requests.post_netbackup_VMwarePolicy(jwt, base_url)
80+
policy_api_requests.post_netbackup_VMwarePolicy(jwt, base_url)
8181

82-
listPolicies = policy_api_requests.get_netbackup_policies(jwt, base_url)
82+
policy_api_requests.get_netbackup_policies(jwt, base_url)
8383

84-
readPolicy = policy_api_requests.get_netbackup_policy(jwt, base_url)
84+
policy_api_requests.get_netbackup_policy(jwt, base_url)
8585

86-
updatePolicy = policy_api_requests.put_netbackup_policy(jwt, base_url)
86+
policy_api_requests.put_netbackup_policy(jwt, base_url)
8787

88-
readPolicy = policy_api_requests.get_netbackup_policy(jwt, base_url)
88+
policy_api_requests.get_netbackup_policy(jwt, base_url)
8989

90-
deletePolicy = policy_api_requests.delete_netbackup_policy(jwt, base_url)
90+
policy_api_requests.delete_VMware_netbackup_policy(jwt, base_url)
9191

92-
listPolicies = policy_api_requests.get_netbackup_policies(jwt, base_url)
92+
policy_api_requests.get_netbackup_policies(jwt, base_url)

recpies/python/create_policy_step_by_step.py renamed to recipes/python/create_policy_step_by_step.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,26 @@ def read_command_line_arguments():
7777

7878
jwt = policy_api_requests.perform_login(username, password, domainName, domainType, base_url)
7979

80-
createPolicy = policy_api_requests.post_netbackup_VMwarePolicy_defaults(jwt, base_url)
80+
policy_api_requests.post_netbackup_VMwarePolicy_defaults(jwt, base_url)
8181

82-
listPolicies = policy_api_requests.get_netbackup_policies(jwt, base_url)
82+
policy_api_requests.get_netbackup_policies(jwt, base_url)
8383

84-
readPolicy = policy_api_requests.get_netbackup_policy(jwt, base_url)
84+
policy_api_requests.get_netbackup_policy(jwt, base_url)
8585

86-
updatePolicy = policy_api_requests.put_netbackup_policy(jwt, base_url)
86+
policy_api_requests.put_netbackup_policy(jwt, base_url)
8787

88-
addClient = policy_api_requests.put_netbackup_client(jwt, base_url)
88+
policy_api_requests.put_netbackup_client(jwt, base_url)
8989

90-
addBackupSelection = policy_api_requests.put_netbackup_backupselections(jwt, base_url)
90+
policy_api_requests.put_netbackup_backupselections(jwt, base_url)
9191

92-
addSchedule = policy_api_requests.put_netbackup_schedule(jwt, base_url)
92+
policy_api_requests.put_netbackup_schedule(jwt, base_url)
9393

94-
readPolicy = policy_api_requests.get_netbackup_policy(jwt, base_url)
94+
policy_api_requests.get_netbackup_policy(jwt, base_url)
9595

96-
deleteClient = policy_api_requests.delete_netbackup_client(jwt, base_url)
96+
policy_api_requests.delete_netbackup_client(jwt, base_url)
9797

98-
deleteSchedule = policy_api_requests.delete_netbackup_schedule(jwt, base_url)
98+
policy_api_requests.delete_netbackup_schedule(jwt, base_url)
9999

100-
deletePolicy = policy_api_requests.delete_netbackup_policy(jwt, base_url)
100+
policy_api_requests.delete_VMware_netbackup_policy(jwt, base_url)
101101

102-
listPolicies = policy_api_requests.get_netbackup_policies(jwt, base_url)
102+
policy_api_requests.get_netbackup_policies(jwt, base_url)
Lines changed: 90 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import json
33

44
content_type = "application/vnd.netbackup+json; version=2.0"
5-
testPolicyName = "VMware_test_policy"
5+
testVMwarePolicyName = "VMware_test_policy"
6+
testOraclePolicyName = "Oracle_test_policy"
67
testClientName = "MEDIA_SERVER"
78
testScheduleName = "VMware_test_schedule"
89
etag = ""
@@ -17,7 +18,7 @@ def perform_login(username, password, domainName, domainType, base_url):
1718
resp = requests.post(url, headers=headers, json=req_body, verify=False)
1819

1920
if resp.status_code != 201:
20-
raise Exception('Login API failed with status code {} and {}'.format(resp.status_code, resp.json()))
21+
print('Login API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
2122

2223
print("\nThe response code of the Login API: {}\n".format(resp.status_code))
2324

@@ -28,10 +29,10 @@ def post_netbackup_VMwarePolicy_defaults(jwt, base_url):
2829
req_body = {
2930
"data": {
3031
"type": "policy",
31-
"id": testPolicyName,
32+
"id": testVMwarePolicyName,
3233
"attributes": {
3334
"policy": {
34-
"policyName": testPolicyName,
35+
"policyName": testVMwarePolicyName,
3536
"policyType": "VMware",
3637
"policyAttributes": {},
3738
"clients": [],
@@ -50,19 +51,50 @@ def post_netbackup_VMwarePolicy_defaults(jwt, base_url):
5051
resp = requests.post(url, headers=headers, json=req_body, verify=False)
5152

5253
if resp.status_code != 204:
53-
raise Exception('Create Policy API with defaults failed with status code {} and {}'.format(resp.status_code, resp.json()))
54+
print('Create Policy API with defaults failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
5455

55-
print("\n {} with defaults is created with status code : {}\n".format(testPolicyName,resp.status_code))
56+
print("\n {} with defaults is created with status code : {}\n".format(testVMwarePolicyName,resp.status_code))
57+
58+
def post_netbackup_OraclePolicy_defaults(jwt, base_url):
59+
url = base_url + "/config/policies/"
60+
req_body = {
61+
"data": {
62+
"type": "policy",
63+
"id": testOraclePolicyName,
64+
"attributes": {
65+
"policy": {
66+
"policyName": testOraclePolicyName,
67+
"policyType": "Oracle",
68+
"policyAttributes": {},
69+
"clients": [],
70+
"schedules": [],
71+
"backupSelections": {
72+
"selections": []
73+
}
74+
}
75+
}
76+
}
77+
}
78+
headers = {'Content-Type': content_type, 'Authorization': jwt}
79+
80+
print("\nMaking POST Request to create Oracle Policy with defaults \n")
81+
82+
resp = requests.post(url, headers=headers, json=req_body, verify=False)
83+
84+
if resp.status_code != 204:
85+
print('Create Policy API with defaults failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
86+
87+
print("\n {} with defaults is created with status code : {}\n".format(testVMwarePolicyName,resp.status_code))
5688

5789
def post_netbackup_VMwarePolicy(jwt, base_url):
5890
url = base_url + "/config/policies/"
5991
req_body = {
6092
"data": {
6193
"type": "policy",
62-
"id": testPolicyName,
94+
"id": testVMwarePolicyName,
6395
"attributes": {
6496
"policy": {
65-
"policyName": testPolicyName,
97+
"policyName": testVMwarePolicyName,
6698
"policyType": "VMware",
6799
"policyAttributes": {
68100
"active": True,
@@ -190,9 +222,9 @@ def post_netbackup_VMwarePolicy(jwt, base_url):
190222
resp = requests.post(url, headers=headers, json=req_body, verify=False)
191223

192224
if resp.status_code != 204:
193-
raise Exception('Create Policy API failed with status code {} and {}'.format(resp.status_code, resp.json()))
225+
print('Create Policy API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
194226

195-
print("\n {} with out defaults is created with status code : {}\n".format(testPolicyName,resp.status_code))
227+
print("\n {} with out defaults is created with status code : {}\n".format(testVMwarePolicyName,resp.status_code))
196228

197229
def get_netbackup_policies(jwt, base_url):
198230
url = base_url + "/config/policies"
@@ -203,14 +235,14 @@ def get_netbackup_policies(jwt, base_url):
203235
resp = requests.get(url, headers=headers, verify=False)
204236

205237
if resp.status_code != 200:
206-
raise Exception('List Policies API failed with status code {} and {}'.format(resp.status_code, resp.json()))
238+
print('List Policies API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
207239

208240
print("\nList policy succeeded with status code: {}\n".format(resp.status_code))
209241
print("\n Json Response body for List policies : \n{}\n".format(json.loads(resp.content)))
210242

211243

212244
def get_netbackup_policy(jwt, base_url):
213-
url = base_url + "/config/policies/" + testPolicyName
245+
url = base_url + "/config/policies/" + testVMwarePolicyName
214246
global etag
215247
headers = {'Content-Type': content_type, 'Authorization': jwt}
216248

@@ -219,37 +251,50 @@ def get_netbackup_policy(jwt, base_url):
219251
resp = requests.get(url, headers=headers, verify=False)
220252

221253
if resp.status_code != 200:
222-
raise Exception('GET Policy API failed with status code {} and {}'.format(resp.status_code, resp.json()))
254+
print('GET Policy API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
223255

224-
print("\nGet policy details on {} succeeded with status code: {}\n".format(testPolicyName, resp.status_code))
256+
print("\nGet policy details on {} succeeded with status code: {}\n".format(testVMwarePolicyName, resp.status_code))
225257
print("\n The E-tag for the get policy : {}\n".format(resp.headers['ETag']))
226258
etag = resp.headers['ETag']
227259
print("\n Json Response body for get policy : \n{}\n".format(json.loads(resp.content)))
228260

229261

230-
def delete_netbackup_policy(jwt, base_url):
231-
url = base_url + "/config/policies/" + testPolicyName
262+
def delete_VMware_netbackup_policy(jwt, base_url):
263+
url = base_url + "/config/policies/" + testVMwarePolicyName
264+
headers = {'Content-Type': content_type, 'Authorization': jwt, 'If-Match': etag}
265+
266+
print("\n Making policy DELETE Request on {}".format(testVMwarePolicyName))
267+
268+
resp = requests.delete(url, headers=headers, verify=False)
269+
270+
if resp.status_code != 204:
271+
print('DELETE Policy API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
272+
273+
print("\nThe policy is deleted with status code: {}\n".format(resp.status_code))
274+
275+
def delete_Oracle_netbackup_policy(jwt, base_url):
276+
url = base_url + "/config/policies/" + testOraclePolicyName
232277
headers = {'Content-Type': content_type, 'Authorization': jwt, 'If-Match': etag}
233278

234-
print("\n Making policy DELETE Request on {}".format(testPolicyName))
279+
print("\n Making policy DELETE Request on {}".format(testOraclePolicyName))
235280

236281
resp = requests.delete(url, headers=headers, verify=False)
237282

238283
if resp.status_code != 204:
239-
raise Exception('DELETE Policy API failed with status code {} and {}'.format(resp.status_code, resp.json()))
284+
print('DELETE Policy API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
240285

241286
print("\nThe policy is deleted with status code: {}\n".format(resp.status_code))
242287

243288
def put_netbackup_policy(jwt, base_url):
244-
url = base_url + "/config/policies/" + testPolicyName
289+
url = base_url + "/config/policies/" + testVMwarePolicyName
245290
global etag
246291
req_body = {
247292
"data": {
248293
"type": "policy",
249-
"id": testPolicyName,
294+
"id": testVMwarePolicyName,
250295
"attributes": {
251296
"policy": {
252-
"policyName": testPolicyName,
297+
"policyName": testVMwarePolicyName,
253298
"policyType": "VMware",
254299
"policyAttributes": {
255300
"keyword": "test"
@@ -265,17 +310,17 @@ def put_netbackup_policy(jwt, base_url):
265310
}
266311
headers = {'Content-Type': content_type, 'Authorization': jwt ,'If-Match': etag}
267312

268-
print("\n Making Update Request on {} by changing few attributes of the policy".format(testPolicyName))
313+
print("\n Making Update Request on {} by changing few attributes of the policy".format(testVMwarePolicyName))
269314

270315
resp = requests.put(url, headers=headers, json=req_body, verify=False)
271316

272317
if resp.status_code != 204:
273-
raise Exception('PUT Policy API failed with status code {} and {}'.format(resp.status_code, resp.json()))
318+
print('PUT Policy API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
274319
etag = resp.headers['ETag']
275-
print("\n{} Updated with status code : {}\n".format(testPolicyName, resp.status_code))
320+
print("\n{} Updated with status code : {}\n".format(testVMwarePolicyName, resp.status_code))
276321

277322
def put_netbackup_client(jwt, base_url):
278-
url = base_url + "/config/policies/" + testPolicyName + "/clients/" + testClientName
323+
url = base_url + "/config/policies/" + testVMwarePolicyName + "/clients/" + testClientName
279324
global etag
280325
req_body = {
281326
"data": {
@@ -289,17 +334,17 @@ def put_netbackup_client(jwt, base_url):
289334
}
290335
headers = {'Content-Type': content_type, 'Authorization': jwt, 'If-Match': etag}
291336

292-
print("\n Making PUT Request to add client to {}".format(testPolicyName))
337+
print("\n Making PUT Request to add client to {}".format(testVMwarePolicyName))
293338

294339
resp = requests.put(url, headers=headers, json=req_body, verify=False)
295340

296341
if resp.status_code != 201:
297-
raise Exception('PUT Client API failed with status code {} and {}'.format(resp.status_code, resp.json()))
342+
print('PUT Client API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
298343
etag = resp.headers['ETag']
299-
print("\n{} is added to {} with status code : {}\n".format(testClientName, testPolicyName, resp.status_code))
344+
print("\n{} is added to {} with status code : {}\n".format(testClientName, testVMwarePolicyName, resp.status_code))
300345

301346
def delete_netbackup_client(jwt, base_url):
302-
url = base_url + "/config/policies/" + testPolicyName + "/clients/" + testClientName
347+
url = base_url + "/config/policies/" + testVMwarePolicyName + "/clients/" + testClientName
303348
global etag
304349
headers = {'Content-Type': content_type, 'Authorization': jwt, 'If-Match': etag}
305350

@@ -308,12 +353,12 @@ def delete_netbackup_client(jwt, base_url):
308353
resp = requests.delete(url, headers=headers, verify=False)
309354

310355
if resp.status_code != 204:
311-
raise Exception('DELETE Client API failed with status code {} and {}'.format(resp.status_code, resp.json()))
356+
print('DELETE Client API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
312357
etag = resp.headers['ETag']
313-
print("\nClient {} is deleted from {} with status code: {}\n".format(testClientName, testPolicyName, resp.status_code))
358+
print("\nClient {} is deleted from {} with status code: {}\n".format(testClientName, testVMwarePolicyName, resp.status_code))
314359

315360
def delete_netbackup_schedule(jwt, base_url):
316-
url = base_url + "/config/policies/" + testPolicyName + "/schedules/" + testScheduleName
361+
url = base_url + "/config/policies/" + testVMwarePolicyName + "/schedules/" + testScheduleName
317362
global etag
318363
headers = {'Content-Type': content_type, 'Authorization': jwt, 'If-Match': etag}
319364

@@ -322,25 +367,25 @@ def delete_netbackup_schedule(jwt, base_url):
322367
resp = requests.delete(url, headers=headers, verify=False)
323368

324369
if resp.status_code != 204:
325-
raise Exception('DELETE schedule API failed with status code {} and {}'.format(resp.status_code, resp.json()))
370+
print('DELETE schedule API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
326371
etag = resp.headers['ETag']
327-
print("\n {} is deleted from the {} with status code: {}\n".format(testScheduleName, testPolicyName, resp.status_code))
372+
print("\n {} is deleted from the {} with status code: {}\n".format(testScheduleName, testVMwarePolicyName, resp.status_code))
328373

329374
def delete_netbackup_backupselections(jwt, base_url):
330-
url = base_url + "/config/policies/" + testPolicyName + "/backupselections"
375+
url = base_url + "/config/policies/" + testVMwarePolicyName + "/backupselections"
331376
headers = {'Content-Type': content_type, 'Authorization': jwt}
332377

333378
print("Making DELETE Request to remove Backupselections from the policy\n")
334379

335380
resp = requests.delete(url, headers=headers, verify=False)
336381

337382
if resp.status_code != 204:
338-
raise Exception('DELETE Backupselections API failed with status code {} and {}'.format(resp.status_code, resp.json()))
383+
print('DELETE Backupselections API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
339384

340-
print("\n BackupSelections is deleted for the {} with status code : {}\n".format(testPolicyName, resp.status_code))
385+
print("\n BackupSelections is deleted for the {} with status code : {}\n".format(testVMwarePolicyName, resp.status_code))
341386

342387
def put_netbackup_backupselections(jwt, base_url):
343-
url = base_url + "/config/policies/" + testPolicyName + "/backupselections"
388+
url = base_url + "/config/policies/" + testVMwarePolicyName + "/backupselections"
344389
global etag
345390
req_body = {
346391
"data": {
@@ -354,17 +399,17 @@ def put_netbackup_backupselections(jwt, base_url):
354399
}
355400
headers = {'Content-Type': content_type, 'Authorization': jwt, 'If-Match': etag}
356401

357-
print("\nMaking PUT Request to add BackupSelections to {}\n".format(testPolicyName))
402+
print("\nMaking PUT Request to add BackupSelections to {}\n".format(testVMwarePolicyName))
358403

359404
resp = requests.put(url, headers=headers, json=req_body, verify=False)
360405

361406
if resp.status_code != 204:
362-
raise Exception('PUT Backupselections API failed with status code {} and {}'.format(resp.status_code, resp.json()))
407+
print('PUT Backupselections API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
363408
etag = resp.headers['ETag']
364-
print("\n Backupselections added to {} with status code: {}\n".format(testPolicyName, resp.status_code))
409+
print("\n Backupselections added to {} with status code: {}\n".format(testVMwarePolicyName, resp.status_code))
365410

366411
def put_netbackup_schedule(jwt, base_url):
367-
url = base_url + "/config/policies/" + testPolicyName + "/schedules/" + testScheduleName
412+
url = base_url + "/config/policies/" + testVMwarePolicyName + "/schedules/" + testScheduleName
368413
global etag
369414
req_body = {
370415
"data": {
@@ -451,11 +496,11 @@ def put_netbackup_schedule(jwt, base_url):
451496
}
452497
headers = {'Content-Type': content_type, 'Authorization': jwt, 'If-Match': etag}
453498

454-
print("\nMaking PUT Request to add schedule to {}\n".format(testPolicyName))
499+
print("\nMaking PUT Request to add schedule to {}\n".format(testVMwarePolicyName))
455500

456501
resp = requests.put(url, headers=headers, json=req_body, verify=False)
457502

458503
if resp.status_code != 201:
459-
raise Exception('PUT Schedule API failed with status code {} and {}'.format(resp.status_code, resp.json()))
504+
print('PUT Schedule API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
460505
etag = resp.headers['ETag']
461-
print("\n{} is added to {} with status code : {}\n".format(testScheduleName, testPolicyName, resp.status_code))
506+
print("\n{} is added to {} with status code : {}\n".format(testScheduleName, testVMwarePolicyName, resp.status_code))

0 commit comments

Comments
 (0)