Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CloudPak-Outcomes
GitHub Repository: CloudPak-Outcomes/Outcomes-Projects
Path: blob/main/L4assets/DSandMLOpsAssets/CLIandSDK/Notebooks/CPD-02b User mgmt with cpd-cli.ipynb
1928 views
Kernel: Python 3.10

Using the cpd-cli utility for user managment

This notebook shows how cpd-cli can be used in a notebook.

The cpd-cli requires an API key.

Note that cpd-cli cannot do everything that is available through the REST API. The available commands include:

  • add-ldap-group-to-cpd-group, add-ldap-user-to-cpd-group

  • bulk-upsert-ud-roles, bulk-upsert-users

  • config-ldap

  • get-group, get-user

  • list-groups, list-ud-roles, list-users

  • upsert-group, upsert-user

  • version

The functionalities demonstrated are:

  • Setup access to cpd-cli

  • List users

  • List groups

  • List user-defined roles

For more information, see:

# Download the utility import requests import os import json url = "https://github.com/IBM/cpd-cli/releases/download/v12.0.4/cpd-cli-linux-EE-12.0.4.tgz" filename = 'cpd-cli-linux-EE-12.0.4.tgz' r = requests.get(url) f = open(filename,'wb') nb_bytes = f.write(r.content) f.close()
!tar xzf cpd-cli-linux-EE-12.0.4.tgz !rm -rf cpd-cli-linux-EE-12.0.4.tgz !ln -s cpd-cli-linux-EE-12.0.4-57/cpd-cli cpd-cli !ln -s cpd-cli-linux-EE-12.0.4-57/plugins plugins !ln -s cpd-cli-linux-EE-12.0.4-57/LICENSES LICENSES !ls

Setup cpd-cli configuration

see: Creating cpd-cli profile

# no "/" at the end of the URL os.environ['CPD_PROFILE_URL']="https://cpd-cpd.ai-governance-94074a334e51addd457c5646c0f9a073-0000.us-east.containers.appdomain.cloud" os.environ['CPD_ADMIN_USER'] = "jacquesr" os.environ['CPD_PROFILE_NAME'] = "jacquesr" os.environ['CPD_API_KEY'] = "<YOUR API KEY HERE>"
!./cpd-cli config users set ${CPD_ADMIN_USER} --username admin --apikey ${CPD_API_KEY} !./cpd-cli config profiles set ${CPD_PROFILE_NAME} --user ${CPD_ADMIN_USER} --url ${CPD_PROFILE_URL} !./cpd-cli user-mgmt version

List all users

The output is different from the one from the REST API.

cpd-cli user-mgmt list-users \ --profile=<cpd-configuration-profile-name> \ [--cpdconfig=<cpd-configuration-location>] \ [--output=json|yaml|csv|text] \ [--verbose]
!./cpd-cli user-mgmt list-users --profile ${CPD_PROFILE_NAME} --output=text

List groups

cpd-cli user-mgmt list-groups \ --profile=<cpd-configuration-profile-name> \ [--cpdconfig=<cpd-configuration-location>] \ [--include-members] \ [--output=json|yaml] \ [--verbose]
!./cpd-cli user-mgmt list-groups --profile=${CPD_PROFILE_NAME} --output=json

List user-defined roles

cpd-cli user-mgmt list-ud-roles \ --profile=<cpd-configuration-profile-name> \ [--cpdconfig=<cpd-configuration-location>] \ [--output=json|yaml|csv|text] \ [--verbose]
!./cpd-cli user-mgmt list-ud-roles --profile=${CPD_PROFILE_NAME} --output=text

Create a user

cpd-cli user-mgmt upsert-user \ --data=<json-file-name> \ --profile=<cpd-configuration-profile-name> \ [--cpdconfig=<cpd-configuration-location>] \ [--replace-roles] \ [--verbose]
new_user = { "username":"user1", "displayName":"user1", "email":"[email protected]", "user_roles":[ "User" ], "password":"password" } f = open("new_user.json",'w') nb_bytes = f.write(json.dumps(new_user)) f.close() # cpd-cli does not have a delete user command so the command is commented out # !./cpd-cli user-mgmt upsert-user --data=new_user.json --profile=${CPD_PROFILE_NAME} !cat new_user.json

Delete user

cpd-cli does not have a delete user command. For more information, see User management command summary