Skip to content

Commit ad155c3

Browse files
committed
Add iterate_endpoints example
1 parent 1b61ced commit ad155c3

File tree

2 files changed

+58
-64
lines changed

2 files changed

+58
-64
lines changed

examples/client/create_get_and_delete_project.py

Lines changed: 0 additions & 64 deletions
This file was deleted.

examples/client/iterate_endpoints.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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("Recursively traverse resource endpoints")
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(
22+
base_url=args.base_url,
23+
token=access_token,
24+
verify=args.verify
25+
)
26+
27+
28+
def iterate_sub_resources(parent, breadcrumb=""):
29+
try:
30+
resources = bd.list_resources(parent)
31+
except TypeError:
32+
return
33+
34+
for res in resources:
35+
print(f"{breadcrumb}->{res}", end='')
36+
if res in ['scan-data', # 200; but .bdio output is not json
37+
'apiDocumentation', # 200; but output is just a huge amount of html
38+
'detectUri', # 204; no content
39+
]:
40+
print("...SKIPPING")
41+
continue
42+
print()
43+
44+
try:
45+
obj = bd.get_resource(res, parent, items=False)
46+
except requests.HTTPError:
47+
continue
48+
if isinstance(obj, dict):
49+
print(f"dict with {len(obj.keys())} keys; keys:{obj.keys()}")
50+
else:
51+
raise RuntimeError("200 result should always return a dict: " + obj)
52+
if 'items' in obj:
53+
items = obj['items']
54+
if len(items) > 0:
55+
iterate_sub_resources(items[0], f"{breadcrumb}->{res}")
56+
57+
58+
iterate_sub_resources(None)

0 commit comments

Comments
 (0)