freeipa/ipatests/test_integration/example_cli.py
Christian Heimes 6aebfe74fb Add context manager to ipalib.API
`ipalib.API` instances like `ipalib.api` now provide a context manager
that connects and disconnects the API object. Users no longer have to
deal with different types of backends or finalize the API correctly.

```python
import ipalib

with ipalib.api as api:
    api.Commands.ping()
```

See: https://pagure.io/freeipa/issue/9443
Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
2023-09-29 12:57:06 +02:00

20 lines
397 B
Python

import os
import ipalib
from ipaplatform.paths import paths
# authenticate with host keytab and custom ccache
os.environ.update(
KRB5_CLIENT_KTNAME=paths.KRB5_KEYTAB,
)
# custom options
overrides = {"context": "example_cli"}
ipalib.api.bootstrap(**overrides)
with ipalib.api as api:
user = api.Command.user_show("admin")
print(user)
assert not api.Backend.rpcclient.isconnected()