Skip to content

Commit 2ccd2f8

Browse files
Merge pull request #4 from VeritasOS/add-python-samples
Added python samples.
2 parents b527c32 + f49bf70 commit 2ccd2f8

File tree

6 files changed

+239
-2
lines changed

6 files changed

+239
-2
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ Contains code samples to invoke NetBackup REST API using different programming l
55
#### Disclaimer
66
These scripts are only meant to be used as a reference. Please do not use these in production.
77

8-
#### Executing the scripts in PowerShell
8+
#### Executing the snippets in PowerShell
99
Pre-requisites:
10+
- NetBackup 8.1.1 or higher
1011
- Powershell 5.0 or higher
1112

12-
Use the following commands to run the sample powershell scripts.
13+
Use the following commands to run the powershell samples.
1314
- `.\Get-NB-Images.ps1 -nbmaster <masterServer> -username <username> -password <password>`
1415
- `.\Get-NB-Jobs.ps1 -nbmaster <masterServer> -username <username> -password <password>`
16+
17+
#### Executing the snippets in Python
18+
Pre-requisites:
19+
- NetBackup 8.1.1 or higher
20+
- python 3.5 or higher
21+
- python modules: `requests, texttable`
22+
23+
Use the following commands to run the python samples.
24+
- `python -W ignore get_nb_images.py -nbmaster <masterServer> -username <username> -password <password>`
25+
- `python -W ignore get_nb_jobs.py -nbmaster <masterServer> -username <username> -password <password>`

snippets/python/api_requests.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import requests
2+
3+
content_type = "application/vnd.netbackup+json; version=1.0"
4+
5+
def perform_login(username, password, base_url):
6+
url = base_url + "/login"
7+
req_body = {"userName": username, "password": password}
8+
headers = {'Content-Type': content_type}
9+
10+
print("performing POST on {} for user '{}'\n".format(url, req_body['userName']))
11+
12+
resp = requests.post(url, headers=headers, json=req_body, verify=False)
13+
14+
if resp.status_code != 201:
15+
raise Exception('Login API failed with status code {} and {}'.format(resp.status_code, resp.json()))
16+
17+
return resp.json()['token']
18+
19+
20+
def get_netbackup_images(jwt, base_url):
21+
url = base_url + "/catalog/images"
22+
headers = {'Content-Type': content_type, 'Authorization': jwt}
23+
query_params = {
24+
# "page[limit]": 100, #This changes the default page size to 100
25+
# "filter": "policyType eq 'VMware'" #This adds a filter to only show VMware backup Images
26+
}
27+
28+
print("performing GET on {}\n".format(url))
29+
30+
resp = requests.get(url, headers=headers, params=query_params, verify=False)
31+
32+
if resp.status_code != 200:
33+
raise Exception('Images API failed with status code {} and {}'.format(resp.status_code, resp.json()))
34+
35+
return resp.json()
36+
37+
38+
def get_netbackup_jobs(jwt, base_url):
39+
url = base_url + "/admin/jobs"
40+
headers = {'Content-Type': content_type, 'Authorization': jwt}
41+
query_params = {
42+
# "page[limit]": 100, #This changes the default page size to 100
43+
# "filter": "jobType eq 'RESTORE'" #This adds a filter to only show RESTORE Jobs
44+
}
45+
46+
print("performing GET on {}\n".format(url))
47+
48+
resp = requests.get(url, headers=headers, params=query_params, verify=False)
49+
50+
if resp.status_code != 200:
51+
raise Exception('Jobs API failed with status code {} and {}'.format(resp.status_code, resp.json()))
52+
53+
return resp.json()

snippets/python/get_nb_images.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import sys
2+
import api_requests
3+
import json
4+
import texttable as tt
5+
6+
protocol = "https"
7+
nbmaster = ""
8+
username = ""
9+
password = ""
10+
port = 1556
11+
12+
13+
def print_image_details(data):
14+
tab = tt.Texttable()
15+
headings = ['Image ID','Policy','Client','Backup Time']
16+
tab.header(headings)
17+
18+
for data_item in data:
19+
tuple_value = (data_item['id'], data_item['attributes']['policyName'], data_item['attributes']['clientName'], data_item['attributes']['backupTime'])
20+
tab.add_row(tuple_value)
21+
22+
print(tab.draw())
23+
24+
25+
def print_disclaimer():
26+
print("-------------------------------------------------------------------------------------------------")
27+
print("-- This script requires Python3.5 or higher. --")
28+
print("-- If your current system does not have Python3.5 or higher installed, this will not work. --")
29+
print("-------------------------------------------------------------------------------------------------\n")
30+
print("Executing this library requires some additional python3.5 libraries like \n\t'requests' \n\t'texttable'.\n\n")
31+
print("'texttable' library is just used to show the 'pretty' version of the API response,\nyou might not need it for your netbackup automation.\n")
32+
print("You will, however, require 'requests' library to make the API calls.\n")
33+
print("You can install the dependent libraries using the following commands: ")
34+
print("pip install requests texttable")
35+
print("-------------------------------------------------------------------------------------------------\n\n\n")
36+
print("You can specify the 'nbmaster', 'username' and 'password' as command-line parameters\n")
37+
print_usage()
38+
39+
def print_usage():
40+
print("Example:")
41+
print("python -W ignore get_nb_images.py -nbmaster <masterServer> -username <username> -password <password>\n\n\n")
42+
43+
def read_command_line_arguments():
44+
if len(sys.argv)%2 == 0:
45+
print_usage()
46+
exit()
47+
48+
global nbmaster
49+
global username
50+
global password
51+
52+
for i in range(1, len(sys.argv), 2):
53+
if sys.argv[i] == "-nbmaster":
54+
nbmaster = sys.argv[i + 1]
55+
elif sys.argv[i] == "-username":
56+
username = sys.argv[i + 1]
57+
elif sys.argv[i] == "-password":
58+
password = sys.argv[i + 1]
59+
else:
60+
print_usage()
61+
exit()
62+
63+
if nbmaster == "":
64+
print("Please provide the value for 'nbmaster'")
65+
exit()
66+
elif username == "":
67+
print("Please provide the value for 'username'")
68+
exit()
69+
elif password == "":
70+
print("Please provide the value for 'password'")
71+
exit()
72+
73+
print_disclaimer()
74+
75+
read_command_line_arguments()
76+
77+
base_url = protocol + "://" + nbmaster + ":" + str(port) + "/netbackup"
78+
79+
jwt = api_requests.perform_login(username, password, base_url)
80+
81+
images = api_requests.get_netbackup_images(jwt, base_url)
82+
83+
data = images['data']
84+
85+
if len(data) > 0:
86+
print_image_details(data)
87+

snippets/python/get_nb_jobs.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import sys
2+
import api_requests
3+
import json
4+
import texttable as tt
5+
6+
protocol = "https"
7+
nbmaster = ""
8+
username = ""
9+
password = ""
10+
port = 1556
11+
12+
def print_job_details(data):
13+
tab = tt.Texttable()
14+
headings = ['Job ID','Type','State','Status']
15+
tab.header(headings)
16+
17+
for data_item in data:
18+
tuple_value = (data_item['attributes']['jobId'], data_item['attributes']['jobType'], data_item['attributes']['state'], data_item['attributes']['status'])
19+
tab.add_row(tuple_value)
20+
21+
print(tab.draw())
22+
23+
24+
def print_disclaimer():
25+
print("-------------------------------------------------------------------------------------------------")
26+
print("-- This script requires Python3.5 or higher. --")
27+
print("-- If your current system does not have Python3.5 or higher installed, this will not work. --")
28+
print("-------------------------------------------------------------------------------------------------\n")
29+
print("Executing this library requires some additional python3.5 libraries like \n\t'requests' \n\t'texttable'.\n\n")
30+
print("'texttable' library is just used to show the 'pretty' version of the API response,\nyou might not need it for your netbackup automation.\n")
31+
print("You will, however, require 'requests' library to make the API calls.\n")
32+
print("You can install the dependent libraries using the following commands: ")
33+
print("pip install requests texttable")
34+
print("-------------------------------------------------------------------------------------------------\n\n\n")
35+
print("You can specify the 'nbmaster', 'username' and 'password' as command-line parameters\n")
36+
print_usage()
37+
38+
def print_usage():
39+
print("Example:")
40+
print("python -W ignore get_nb_jobs.py -nbmaster <masterServer> -username <username> -password <password>\n\n\n")
41+
42+
def read_command_line_arguments():
43+
if len(sys.argv)%2 == 0:
44+
print_usage()
45+
exit()
46+
47+
global nbmaster
48+
global username
49+
global password
50+
51+
for i in range(1, len(sys.argv), 2):
52+
if sys.argv[i] == "-nbmaster":
53+
nbmaster = sys.argv[i + 1]
54+
elif sys.argv[i] == "-username":
55+
username = sys.argv[i + 1]
56+
elif sys.argv[i] == "-password":
57+
password = sys.argv[i + 1]
58+
else:
59+
print_usage()
60+
exit()
61+
62+
if nbmaster == "":
63+
print("Please provide the value for 'nbmaster'")
64+
exit()
65+
elif username == "":
66+
print("Please provide the value for 'username'")
67+
exit()
68+
elif password == "":
69+
print("Please provide the value for 'password'")
70+
exit()
71+
72+
print_disclaimer()
73+
74+
read_command_line_arguments()
75+
76+
base_url = protocol + "://" + nbmaster + ":" + str(port) + "/netbackup"
77+
78+
jwt = api_requests.perform_login(username, password, base_url)
79+
80+
jobs = api_requests.get_netbackup_jobs(jwt, base_url)
81+
82+
data = jobs['data']
83+
84+
if len(data) > 0:
85+
print_job_details(data)
86+

0 commit comments

Comments
 (0)