Skip to content

Commit 58b9b36

Browse files
committed
get app id example
1 parent 5458add commit 58b9b36

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

examples/client/get_application_id.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
'''
3+
4+
Copyright (C) 2023 Synopsys, Inc.
5+
http://www.blackducksoftware.com/
6+
7+
Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing,
18+
software distributed under the License is distributed on an
19+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
KIND, either express or implied. See the License for the
21+
specific language governing permissions and limitations
22+
under the License.
23+
24+
Extract application ID if present
25+
26+
usage: python3 get_application_id.py [-h] -u BASE_URL -t TOKEN_FILE [-nv] project_name
27+
28+
positional arguments:
29+
project_name
30+
31+
options:
32+
-h, --help show this help message and exit
33+
-u BASE_URL, --base-url BASE_URL
34+
Hub server URL e.g. https://your.blackduck.url
35+
-t TOKEN_FILE, --token-file TOKEN_FILE
36+
containing access token
37+
-nv, --no-verify disable TLS certificate verification
38+
39+
'''
40+
import argparse
41+
import json
42+
import logging
43+
import sys
44+
import arrow
45+
46+
from blackduck import Client
47+
from pprint import pprint
48+
49+
parser = argparse.ArgumentParser("python3 get_application_id.py")
50+
parser.add_argument("project_name")
51+
parser.add_argument("-u", "--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
52+
parser.add_argument("-t", "--token-file", dest='token_file', required=True, help="containing access token")
53+
parser.add_argument("-nv", "--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
54+
args = parser.parse_args()
55+
56+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
57+
logging.getLogger("requests").setLevel(logging.WARNING)
58+
logging.getLogger("urllib3").setLevel(logging.WARNING)
59+
logging.getLogger("blackduck").setLevel(logging.WARNING)
60+
61+
with open(args.token_file, 'r') as tf:
62+
access_token = tf.readline().strip()
63+
64+
bd = Client(
65+
base_url=args.base_url,
66+
token=access_token,
67+
verify=args.verify
68+
)
69+
70+
headers = {}
71+
params = { 'q': [f"name:{args.project_name}"] }
72+
projects = [p for p in bd.get_resource('projects', params=params) if p['name'] == args.project_name]
73+
74+
if not len(projects):
75+
print (f'No project found for {args.project_name}')
76+
77+
for project in projects:
78+
project_name = project['name']
79+
project_mappings = bd.get_resource('project-mappings', project)
80+
application_id = None
81+
for mapping in project_mappings:
82+
application_id = mapping.get('applicationId',None)
83+
print (f"Project: {project['name']:50} Application ID: {application_id}")
84+

0 commit comments

Comments
 (0)