Skip to content

Commit ba58b1e

Browse files
author
Glenn Snyder
authored
Merge pull request #78 from ericyame/update-create-users
Add create_user_from_file.py to user creation from CSV file
2 parents f21425d + fa5bd67 commit ba58b1e

File tree

6 files changed

+112
-3
lines changed

6 files changed

+112
-3
lines changed

blackduck/HubRestApi.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ def delete_project_version_by_name(self, project_name, version_name, save_scans=
891891
else:
892892
logging.debug("Did not find project with name {}".format(project_name))
893893

894-
def delete_project_by_name(self, project_name, save_scans=False):
894+
def delete_project_by_name(self, project_name, save_scans=False, backup_scans=False):
895895
project = self.get_project_by_name(project_name)
896896
if project:
897897
# get project versions
@@ -905,6 +905,9 @@ def delete_project_by_name(self, project_name, save_scans=False):
905905
if delete_scans:
906906
# delete all code locations associated with each version
907907
for version in versions:
908+
if backup_scans:
909+
logging.debug("Backup code locations (aka scans) for version {}".format(version['versionName']))
910+
self.download_project_scans(project_name, version['versionName'])
908911
logging.debug("Deleting code locations (aka scans) for version {}".format(version['versionName']))
909912
self.delete_project_version_codelocations(version)
910913

@@ -1285,7 +1288,9 @@ def download_project_scans(self, project_name,version_name, output_folder=None):
12851288
if output_folder:
12861289
pathname = os.path.join(output_folder, filename)
12871290
else:
1288-
pathname = filename
1291+
if not os.path.exists(project_name):
1292+
os.mkdir(project_name)
1293+
pathname = os.path.join(project_name, filename)
12891294
responce = requests.get(url, headers=self.get_headers(), stream=True, verify=False)
12901295
with open(pathname, "wb") as f:
12911296
for data in responce.iter_content():

examples/clean_project.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"clean_project" : [
3+
"proftpd",
4+
"libiec"
5+
]
6+
}

examples/create_user_from_file.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Created on Jan 8, 2020
3+
4+
@author: ylei
5+
6+
Create new users from file (user_list.csv)
7+
"python create_user_from_file.py -f user_list.csv"
8+
9+
CSV File requires 4 columns titled 'username', 'first', 'last', and 'email'
10+
A sample user_list.csv is like below:
11+
username,first,last,email
12+
flast1,first1,last1,[email protected]
13+
flast2,first2,last2,[email protected]
14+
flast3,first3,last3,[email protected]
15+
16+
'''
17+
import argparse
18+
import logging
19+
import sys
20+
import csv
21+
22+
from blackduck.HubRestApi import HubInstance
23+
24+
25+
parser = argparse.ArgumentParser("Create bunch of new users (For example, LDAP users) from file")
26+
parser.add_argument("-f", "--filename", default="user_list.csv", help="File to store user list")
27+
28+
args = parser.parse_args()
29+
30+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
31+
logging.getLogger("requests").setLevel(logging.WARNING)
32+
logging.getLogger("urllib3").setLevel(logging.WARNING)
33+
34+
hub = HubInstance()
35+
36+
filename = args.filename
37+
with open(filename, 'r') as f:
38+
rows = csv.DictReader(f)
39+
for row in rows:
40+
print("Create user: ", row['username'])
41+
data = {
42+
"userName" : row['username'],
43+
"externalUserName" : row['username'],
44+
"firstName" : row['first'],
45+
"lastName" : row['last'],
46+
"email" : row['email'],
47+
"active" : True,
48+
"type": "EXTERNAL"
49+
}
50+
hub.create_user(data)

examples/delete_project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
parser = argparse.ArgumentParser("A program that will delete a project along with its scans")
1717
parser.add_argument("project", help="Project name")
1818
parser.add_argument("-k", "--keep_scans", action = 'store_true', default=False, help="Use this option if you want to keep scans associated with the project-versions. Default is False, scans will be deleted.")
19+
parser.add_argument("-b", "--backup_scans", action = 'store_true', default=False, help="Use this option if you want to backup scans associated with the project-versions. Default is False, scans will not be backuped.")
1920
args = parser.parse_args()
2021

2122
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
@@ -24,4 +25,4 @@
2425

2526
hub = HubInstance()
2627

27-
hub.delete_project_by_name(args.project, save_scans=args.keep_scans)
28+
hub.delete_project_by_name(args.project, save_scans=args.keep_scans, backup_scans=args.backup_scans)

examples/delete_projects.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Created on Nov 8, 2019
3+
4+
@author: ylei
5+
6+
Delete multiple projects and their scans. The project names to be deleted are kept in clean_project.json
7+
'''
8+
9+
import argparse
10+
import csv
11+
import logging
12+
import sys
13+
import json
14+
import arrow
15+
16+
from blackduck.HubRestApi import HubInstance
17+
18+
parser = argparse.ArgumentParser("A program that will delete projects along with their scans")
19+
#parser.add_argument("project", help="Project name")
20+
parser.add_argument("-f", "--filename", default="clean_project.json", help="File to keep Project names")
21+
parser.add_argument("-k", "--keep_scans", action = 'store_true', default=False, help="Use this option if you want to keep scans associated with the project-versions. Default is False, scans will be deleted.")
22+
parser.add_argument("-b", "--backup_scans", action = 'store_true', default=False, help="Use this option if you want to backup scans associated with the project-versions. Default is False, scans will not be backuped.")
23+
parser.add_argument("-a", "--age", default=3, help="The age, in days. If a project is older than this age it will be deleted.")
24+
args = parser.parse_args()
25+
26+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
27+
logging.getLogger("requests").setLevel(logging.WARNING)
28+
logging.getLogger("urllib3").setLevel(logging.WARNING)
29+
30+
hub = HubInstance()
31+
32+
filename = args.filename
33+
with open(filename, 'r+') as f:
34+
d = json.load(f)
35+
print("Projects in maintain json file: ", d['clean_project'])
36+
for proj in d['clean_project']:
37+
now = arrow.now()
38+
clean_time = now.to('local').shift(days=-args.age)
39+
project = hub.get_project_by_name(proj)
40+
41+
if clean_time > arrow.get(project['createdAt']).to('local'):
42+
print("Project ", project['name'], ' last for more than 3 days. Will be backup abd deleted.')
43+
hub.delete_project_by_name(proj, save_scans=args.keep_scans, backup_scans=args.backup_scans)

examples/user_list.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
username,first,last,email
2+
flast9,first9,last9,[email protected]
3+
flast7,first7,last7,[email protected]
4+
flast8,first8,last8,[email protected]

0 commit comments

Comments
 (0)