Skip to content

Commit 004d402

Browse files
committed
Add more examples
1 parent f7844a8 commit 004d402

File tree

5 files changed

+142
-11
lines changed

5 files changed

+142
-11
lines changed

examples/client/crud_project.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from blackduck import Client
2+
3+
import argparse
4+
import logging
5+
import requests
6+
7+
logging.basicConfig(
8+
level=logging.DEBUG,
9+
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
10+
)
11+
12+
parser = argparse.ArgumentParser("Create, read, update, and delete a project")
13+
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
14+
parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token")
15+
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
16+
parser.add_argument("--project", required=True, help="Project name")
17+
args = parser.parse_args()
18+
19+
with open(args.token_file, 'r') as tf:
20+
access_token = tf.readline().strip()
21+
22+
bd = Client(
23+
base_url=args.base_url,
24+
token=access_token,
25+
verify=args.verify
26+
)
27+
28+
project_name = args.project
29+
30+
# POST
31+
project_data = {
32+
'name': project_name,
33+
'description': "some description",
34+
'projectLevelAdjustments': True,
35+
}
36+
37+
try:
38+
r = bd.session.post("/api/projects", json=project_data)
39+
r.raise_for_status()
40+
print(f"created project {r.links['project']['url']}")
41+
except requests.HTTPError as err:
42+
# more fine grained error handling here; otherwise:
43+
bd.http_error_handler(err)
44+
45+
# GET
46+
params = {
47+
'q': [f"name:{project_name}"]
48+
}
49+
project_url = None
50+
for project in bd.get_items("/api/projects", params=params):
51+
if project['name'] == project_name:
52+
project_url = bd.list_resources(project)['href']
53+
print(f"project url: {project_url}")
54+
55+
# PUT
56+
project_data = {
57+
'name': project_name,
58+
'description': "a different description"
59+
}
60+
61+
try:
62+
r = bd.session.put(project_url, json=project_data)
63+
r.raise_for_status()
64+
print("updated project")
65+
except requests.HTTPError as err:
66+
bd.http_error_handler(err)
67+
68+
# DELETE
69+
try:
70+
r = bd.session.delete(project_url)
71+
r.raise_for_status()
72+
print("deleted project")
73+
except requests.HTTPError as err:
74+
if err.response.status_code == 404:
75+
print("not found")
76+
else:
77+
bd.http_error_handler(err)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from blackduck import Client
2+
3+
import argparse
4+
import logging
5+
from pprint import pprint
6+
7+
logging.basicConfig(
8+
level=logging.DEBUG,
9+
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
10+
)
11+
12+
parser = argparse.ArgumentParser("Get a specific component and list its vulnerabilities")
13+
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
14+
parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token")
15+
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
16+
args = parser.parse_args()
17+
18+
with open(args.token_file, 'r') as tf:
19+
access_token = tf.readline().strip()
20+
21+
bd = Client(base_url=args.base_url, token=access_token, verify=args.verify)
22+
23+
params = {
24+
'q': ["maven:commons-beanutils:commons-beanutils:1.9.3"]
25+
}
26+
search_results = bd.get_items("/api/components", params=params)
27+
for result in search_results:
28+
pprint(result)
29+
print(f"{result['componentName']} {result['versionName']}")
30+
url = result['version']
31+
component_version = bd.get_json(url)
32+
33+
for vulnerability in bd.get_resource('vulnerabilities', component_version):
34+
print(vulnerability['name'])

examples/client/get_vulnerability.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from blackduck import Client
2+
3+
import argparse
4+
import logging
5+
from pprint import pprint
6+
7+
logging.basicConfig(
8+
level=logging.DEBUG,
9+
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
10+
)
11+
12+
parser = argparse.ArgumentParser("Get a single vulnerability by id")
13+
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
14+
parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token")
15+
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
16+
parser.add_argument("--vulnerability", help="vulnerability id e.g. CVE-2016-4009 or BDSA-2014-0129")
17+
args = parser.parse_args()
18+
19+
with open(args.token_file, 'r') as tf:
20+
access_token = tf.readline().strip()
21+
22+
bd = Client(base_url=args.base_url, token=access_token, verify=args.verify)
23+
24+
vulnerability = bd.get_json(f"/api/vulnerabilities/{args.vulnerability}")
25+
print(f"Details about {vulnerability['name']}")
26+
pprint(vulnerability)

examples/client/password.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,17 @@
1111
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
1212
)
1313

14-
parser = argparse.ArgumentParser()
14+
parser = argparse.ArgumentParser("Authenticate using username/password credentials")
1515
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
1616
parser.add_argument("--username", required=True, help="Hub user")
1717
parser.add_argument("--password", required=True, help="Hub user")
1818
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
1919
args = parser.parse_args()
2020

21-
verify = args.verify
2221
base_url = args.base_url
23-
session = HubSession(base_url, timeout=15.0, retries=3, verify=verify)
24-
auth = CookieAuth(session, username=args.username, password=args.password)
22+
session = HubSession(base_url, timeout=15.0, retries=3, verify=args.verify)
23+
auth = CookieAuth(session, args.username, args.password)
2524

26-
bd = Client(
27-
base_url=base_url,
28-
session=session,
29-
auth=auth,
30-
verify=verify
31-
)
25+
bd = Client(base_url=base_url, session=session, auth=auth)
3226

3327
pprint(bd.list_resources())

examples/client/quickstart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
1010
)
1111

12-
parser = argparse.ArgumentParser()
12+
parser = argparse.ArgumentParser("Quickstart demonstration with Client")
1313
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
1414
parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token")
1515
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")

0 commit comments

Comments
 (0)