Skip to content

Commit

Permalink
Merge pull request #259 from blackducksoftware/bulk-modify-users
Browse files Browse the repository at this point in the history
Create API token example
  • Loading branch information
nichollsdave authored Jan 4, 2024
2 parents b52ebc5 + f3b7b88 commit fdf4e25
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions examples/create_api_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'''
Created on January 1, 2024
@author: dnichol
Create a new user
To use this script. Firstly create a .restconfig.json file with either API token or basic auth (username/password) as per the examples :
https://github.com/blackducksoftware/hub-rest-api-python/blob/master/restconfig.json.example
https://github.com/blackducksoftware/hub-rest-api-python/blob/master/restconfig.json.api_token.example
Then to run:
python create_api_token.py MyToken "My Token Description"
It will output the token that is generated. If you would like the token to be read only add the -r flag to the command line.
'''
import argparse
import json
import logging
from pprint import pprint
import sys

from blackduck.HubRestApi import HubInstance


parser = argparse.ArgumentParser("Create an API token")
parser.add_argument("name")
parser.add_argument("description")
parser.add_argument("-r", "--readonly", action='store_true')


args = parser.parse_args()

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)

hub = HubInstance()

scope = ["read"]

if not args.readonly :
scope = ["read", "write"]

post_data = {
"name" : args.name,
"description" : args.description,
"scopes" : scope
}

current_user = hub.get_current_user()
add_token_url = hub.get_link(current_user, "api-tokens")

response = hub.execute_post(add_token_url, data=post_data)
if response.status_code == 201:
token_obj = response.json()
token=token_obj['token']
logging.info("Added API token {} = {}".format(args.name, token))
else:
logging.error("Failed to add API token {}, status code was {}".format(
args.name, response.status_code))





2 changes: 2 additions & 0 deletions examples/update_usergroups_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def update_user_group(hub_client, existing_name, new_name, user_group):

# Update the name.
user_group['name'] = new_name
#if user_group.get('externalGroupName') and user['externalGroupName'] == name:
user_group['externalName'] = new_name

logging.info(f"Updating user group {existing_name} to {user_group['name']} for user group {user_group_url}")
hub_client.session.put(user_group_url, json=user_group)
Expand Down

0 comments on commit fdf4e25

Please sign in to comment.