Skip to content

Commit a7225b0

Browse files
committed
adding initial rbac changes to python script
1 parent 239c0b6 commit a7225b0

File tree

6 files changed

+253
-34
lines changed

6 files changed

+253
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Pre-requisites:
3333
Use the following commands to run the python samples.
3434
- `python -W ignore create_policy_step_by_step.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
3535
- `python -W ignore create_policy_in_one_step.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
36+
- `python -W ignore rbac_filtering_in_policy.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
3637

3738
#### Executing the snippets in Perl
3839
Pre-requisites:

recipes/python/create_policy_in_one_step.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ def read_command_line_arguments():
8787

8888
readPolicy = policy_api_requests.get_netbackup_policy(jwt, base_url)
8989

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

9292
listPolicies = policy_api_requests.get_netbackup_policies(jwt, base_url)

recipes/python/create_policy_step_by_step.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ def read_command_line_arguments():
9797

9898
deleteSchedule = policy_api_requests.delete_netbackup_schedule(jwt, base_url)
9999

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

102102
listPolicies = policy_api_requests.get_netbackup_policies(jwt, base_url)

recipes/python/policy_api_requests.py

Lines changed: 77 additions & 32 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 = ""
@@ -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": [],
@@ -52,17 +53,48 @@ def post_netbackup_VMwarePolicy_defaults(jwt, base_url):
5253
if resp.status_code != 204:
5354
raise Exception('Create Policy API with defaults failed with status code {} and {}'.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 VMware 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+
raise Exception('Create Policy API with defaults failed with status code {} and {}'.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,
@@ -192,7 +224,7 @@ def post_netbackup_VMwarePolicy(jwt, base_url):
192224
if resp.status_code != 204:
193225
raise Exception('Create Policy API failed with status code {} and {}'.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"
@@ -210,7 +242,7 @@ def get_netbackup_policies(jwt, base_url):
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

@@ -221,17 +253,30 @@ def get_netbackup_policy(jwt, base_url):
221253
if resp.status_code != 200:
222254
raise Exception('GET Policy API failed with status code {} and {}'.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+
raise Exception('DELETE Policy API failed with status code {} and {}'.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

@@ -241,15 +286,15 @@ def delete_netbackup_policy(jwt, base_url):
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:
273318
raise Exception('PUT Policy API failed with status code {} and {}'.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:
297342
raise Exception('PUT Client API failed with status code {} and {}'.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

@@ -310,10 +355,10 @@ def delete_netbackup_client(jwt, base_url):
310355
if resp.status_code != 204:
311356
raise Exception('DELETE Client API failed with status code {} and {}'.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

@@ -324,10 +369,10 @@ def delete_netbackup_schedule(jwt, base_url):
324369
if resp.status_code != 204:
325370
raise Exception('DELETE schedule API failed with status code {} and {}'.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")
@@ -337,10 +382,10 @@ def delete_netbackup_backupselections(jwt, base_url):
337382
if resp.status_code != 204:
338383
raise Exception('DELETE Backupselections API failed with status code {} and {}'.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:
362407
raise Exception('PUT Backupselections API failed with status code {} and {}'.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:
459504
raise Exception('PUT Schedule API failed with status code {} and {}'.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))
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import sys
2+
import policy_api_requests
3+
import rbac_policy_api_requests
4+
import json
5+
6+
protocol = "https"
7+
port = 1556
8+
new_rbac_user = "testuser"
9+
new_rbac_domain = "rmnus"
10+
new_rbac_pass = "testpass"
11+
new_rbac_domainType = "vx"
12+
13+
def print_disclaimer():
14+
print("-------------------------------------------------------------------------------------------------")
15+
print("-- This script requires Python3.5 or higher. --")
16+
print("-- If your current system does not have Python3.5 or higher installed, this will not work. --")
17+
print("-------------------------------------------------------------------------------------------------\n")
18+
print("Executing this library requires some additional python3.5 libraries like \n\t'requests'.\n\n")
19+
print("You will, however, require 'requests' library to make the API calls.\n")
20+
print("You can install the dependent libraries using the following commands: ")
21+
print("pip install requests ")
22+
print("-------------------------------------------------------------------------------------------------\n\n\n")
23+
print("You can specify the 'nbmaster', 'username', 'password', 'domainName' and 'domainType' as command-line parameters\n")
24+
print_usage()
25+
26+
def print_usage():
27+
print("Example:")
28+
print("python -W ignore rbac_filtering_in_policy.py -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]\n\n\n")
29+
30+
def read_command_line_arguments():
31+
if len(sys.argv)%2 == 0:
32+
print_usage()
33+
exit()
34+
35+
global nbmaster
36+
global username
37+
global password
38+
global domainName
39+
global domainType
40+
41+
for i in range(1, len(sys.argv), 2):
42+
if sys.argv[i] == "-nbmaster":
43+
nbmaster = sys.argv[i + 1]
44+
elif sys.argv[i] == "-username":
45+
username = sys.argv[i + 1]
46+
elif sys.argv[i] == "-password":
47+
password = sys.argv[i + 1]
48+
elif sys.argv[i] == "-domainName":
49+
domainName = sys.argv[i + 1]
50+
elif sys.argv[i] == "-domainType":
51+
domainType = sys.argv[i + 1]
52+
else:
53+
print_usage()
54+
exit()
55+
56+
if nbmaster == "":
57+
print("Please provide the value for 'nbmaster'")
58+
exit()
59+
elif username == "":
60+
print("Please provide the value for 'username'")
61+
exit()
62+
elif password == "":
63+
print("Please provide the value for 'password'")
64+
exit()
65+
elif domainName == "":
66+
print("Please provide the value for 'domainName'")
67+
exit()
68+
elif domainType == "":
69+
print("Please provide the value for 'domainType'")
70+
exit()
71+
72+
print_disclaimer()
73+
74+
read_command_line_arguments()
75+
76+
base_url = protocol + "://" + nbmaster + ":" + str(port) + "/netbackup"
77+
78+
jwt = policy_api_requests.perform_login(username, password, domainName, domainType, base_url)
79+
80+
rbac_policy_api_requests.post_rbac_object_group_for_VMware_policy(jwt, base_url)
81+
rbac_policy_api_requests.create_bpnbat_user(new_rbac_user, new_rbac_domain, new_rbac_pass)
82+
83+
policy_api_requests.post_netbackup_VMwarePolicy_defaults(jwt, base_url)
84+
policy_api_requests.post_netbackup_OraclePolicy_defaults(jwt, base_url)
85+
86+
new_rbac_jwt = policy_api_requests.perform_login(new_rbac_user, new_rbac_pass, new_rbac_domain, new_rbac_domainType, base_url)
87+
88+
policy_api_requests.delete_VMware_netbackup_policy(jwt, base_url)
89+
policy_api_requests.post_netbackup_VMwarePolicy_defaults(new_rbac_jwt, base_url)
90+
policy_api_requests.post_netbackup_OraclePolicy_defaults(new_rbac_jwt, base_url)
91+
92+
policy_api_requests.delete_VMware_netbackup_policy(new_rbac_jwt, base_url)
93+
policy_api_requests.delete_Oracle_netbackup_policy(jwt, base_url)
94+
95+
rbac_policy_api_requests.delete_rbac_object_group_for_VMware_policy(jwt, base_url)
96+
97+
policy_api_requests.get_netbackup_policies(jwt, base_url)

0 commit comments

Comments
 (0)