Chapter 2. Basics of IdM API
You can use the IdM API to automate access to your IdM environment with custom scripts. This reduces manual effort and ensures consistent identity management across your infrastructure.
2.1. Initializing IdM API Copy linkLink copied to clipboard!
To use the IdM API, first initialize it in your environment. Proper initialization establishes the connection to IdM services and enables your scripts to execute API commands.
Prerequisites
- The IdM server or IdM client package is installed.
- A valid Kerberos ticket is issued.
Procedure
To initialize the IdM API, include the following code in the beginning of your script:
from ipalib import api api.bootstrap(context="server") api.finalize()
from ipalib import api api.bootstrap(context="server") api.finalize()Copy to Clipboard Copied! Toggle word wrap Toggle overflow To establish a connection with the LDAP server, add the following logic to your script after API initialization:
if api.env.in_server: api.Backend.ldap2.connect() else: api.Backend.rpcclient.connect()if api.env.in_server: api.Backend.ldap2.connect() else: api.Backend.rpcclient.connect()Copy to Clipboard Copied! Toggle word wrap Toggle overflow - If you run your script on the IdM server, this logic allows your script to connect directly to LDAP server.
- If you run your script on the IdM client, the script uses the Remote Procedure Call (RPC) client.
2.2. Running IdM API commands Copy linkLink copied to clipboard!
You can run IdM API commands within your script to automate identity management tasks. Use the api.Command structure to execute commands and retrieve information about users, groups, and other IdM objects.
Prerequisites
- The IdM API is initialized. For more information, see Initializing IdM API.
Procedure
To list the information about a user, include the following code in your script:
api.Command.user_show("user_name", no_members=True, all=True)api.Command.user_show("user_name", no_members=True, all=True)Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this example, you also pass arguments and options to the command
user_show.
2.3. IdM API commands output structure Copy linkLink copied to clipboard!
Each IdM API command returns output in four sections. Understanding this structure helps you parse command results and handle responses in your automation scripts.
IdM API output structure
result- This section provides the result of the command. It contains various details about the command operation, such as options and arguments which were passed to the command.
values- This section indicates the argument for the command.
messages-
This section shows various information which
ipatool provides after the execution of the command. summary- This section shows the summary for the operation.
In this example, your script executes the add_user command:
api.Command.user_add("test", givenname="a", sn="b")
api.Command.user_add("test", givenname="a", sn="b")
The output structure of that command is below:
2.4. Listing the IdM API commands and parameters Copy linkLink copied to clipboard!
You can list information about IdM API commands and their parameters by using command_show and param_show. This helps you discover available commands and understand the required inputs for your scripts.
Prerequisites
- The IdM API is initialized. For more information, see Initializing IdM API.
Procedure
To display information about
user_addcommand, execute the following code:api.Command.command_show("user_add")api.Command.command_show("user_add")Copy to Clipboard Copied! Toggle word wrap Toggle overflow The result for this command is as follows:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To display information about the
givennameparameter for theuser_addcommand, execute the following code:api.Command.param_show("user_add", name="givenname")api.Command.param_show("user_add", name="givenname")Copy to Clipboard Copied! Toggle word wrap Toggle overflow The result for this command is as follows:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.5. Using batches for executing IdM API commands Copy linkLink copied to clipboard!
You can execute multiple IdM API commands with a single call by using the batch command. Batching improves performance and reduces network overhead when performing bulk operations such as creating multiple users.
Prerequisites
- The IdM API is initialized. For more information, see Initializing IdM API.
Procedure
To create 100 IdM users in one batch, include the following code in your script:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.6. IdM API context Copy linkLink copied to clipboard!
IdM API context determines which plug-ins the API uses. Selecting the correct context ensures your scripts use the appropriate validation and execution methods for your environment.
IdM API context
server- Set of plug-ins which validate arguments and options that are passed to IdM API commands for execution.
client- Set of plug-ins which validate arguments and options that are forwarded to the IdM server for execution.
installer- Set of plug-ins which are specific to the installation process.
updates- Set of plug-ins which are specific to the updating process.