Chapter 2. Basics of IdM API
You can use the IdM API to automate the access to IdM environment with your custom scripts.
2.1. Initializing IdM API Copy linkLink copied to clipboard!
To use the IdM API, first initialize it in your environment.
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 run an IdM API command, use the api.Command
structure in your script.
Prerequisites
- The IdM API is initialized. For more information, see Initializing IdM API.
Procedure
For example, to list the information about 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 has four sections for its output. These sections contain various information about the command execution.
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
ipa
tool 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 the IdM API command and its parameters by using the commands command_show
and param_show
.
Prerequisites
- The IdM API is initialized. For more information, see Initializing IdM API.
Procedure
To display information about
user_add
command, 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
givenname
parameter for theuser_add
command, 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. The following example shows how to create multiple IdM 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 into 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. Specify the context during API initialization. For example on how to use the IdM API context, see Initializing IdM API.
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.