Skip to main content

Client library to interact with the Univention Directory Manager (UDM) REST API.

Project description

UDM REST API Client

PyPI - Version PyPI - Python Version License - AGPLv3 linting - Ruff types - Mypy

Client library and CLI to interact with the Univention Directory Manager (UDM) REST API.

Features

  • Asynchronous and synchronous Python functions
  • Command line interface
  • Automatic handling of HTTP(S) sessions
  • Type annotations
  • Python 3.7+

Usage

Synchronous Python code

from univention.admin.rest.client import UDM

udm = UDM.http("https://10.20.30.40/univention/udm/", "Administrator", "s3cr3t")
module = udm.get("users/user")

# 1. create a user
obj = module.new()
obj.properties["username"] = "foo"
obj.properties["password"] = "univention"
obj.properties["lastname"] = "foo"
obj.save()

# 2. search for users
for result in module.search("uid=*"):
    obj = result.open()
    print(obj)
    print(obj.properties)
    print(obj.objects.groups)

# 3. get by dn
ldap_base = udm.get_ldap_base()
obj = module.get(f"uid=foo,cn=users,{ldap_base}")

# 4. get referenced objects e.g. groups
pg = obj.objects["primaryGroup"].open()
print(pg.dn, pg.properties)
print(obj.objects["groups"])

# 5. modify
obj.properties["description"] = "foo"
obj.save()

# 6. move to the ldap base
obj.move(ldap_base)

# 7. remove
obj.delete()

Asynchronous Python code

import asyncio
from univention.admin.rest.async_client import UDM

async def main():
    async with UDM.http("http://10.20.30.40./univention/udm/", "Administrator", "s3cr3t") as udm:
        module = await udm.get("users/user")

        # 1. create a user
        obj = await module.new()
        obj.properties["username"] = "foo"
        obj.properties["password"] = "univention"
        obj.properties["lastname"] = "foo"
        await obj.save()

        # 2. search for users
        async for result in module.search("uid=*"):
            obj = await result.open()
            print(obj)
            print(obj.properties)
            print(obj.objects.groups)

        # 3. get by dn
        ldap_base = await udm.get_ldap_base()
        obj = await module.get(f"uid=foo,cn=users,{ldap_base}")

        # 4. get referenced objects e.g. groups
        pg = await obj.objects["primaryGroup"].open()
        print(pg.dn, pg.properties)
        print(obj.objects["groups"])

        # 5. modify
        obj.properties["description"] = "foo"
        await obj.save()

        # 6. move to the ldap base
        await obj.move(ldap_base)

        # 7. remove
        await obj.delete()

asyncio.run(main())

Command line interface

PASS_FILE="$HOME/pw-$(date +%Y%m%d%H%M%S)"
echo "s3cr3t" > "$PASS_FILE"

udm \
  --username Administrator \
  --bindpwdfile "$PASS_FILE" \
  --uri http://10.20.30.40/univention/udm/ \
  users/user list \
  --filter uid=Administrator

rm -f "$PASS_FILE"

Instead of using udm, the CLI can also be called using python3 -m univention.admin.rest.client.

Error codes

Error codes and other details of the UDM REST API can be found in its documentation.

Installation

The dependencies for the CLI, synchronous and asynchronous clients differ. They are available separately as "extra" dependencies, to reduce your projects total dependencies. Without installing the "extra" requirements, only the common dependencies of the three interfaces will be installed, and none will be usable.

  • async: installs aiohttp
  • sync: installs requests
  • cli: installs python-ldap and requests

To install the library including the dependencies for the synchronous client via pip from PyPI run:

pip install udm-rest-api-client[sync]

To install the library including the dependencies for the asynchronous client via pip from PyPI run:

pip install udm-rest-api-client[async]

To install the CLI (incl. the required library and its dependencies):

pip install udm-rest-api-client[cli]

Multiple extras can be specified at the same time:

pip install udm-rest-api-client[async,cli,dev,sync]

CLI and pipx

If you wish to use the udm command line interface from anywhere in your system, without having to manually handle virtual environments, install it using pipx:

pipx install udm-rest-api-client[cli]
which udm
$HOME/.local/bin/udm

Building and publishing

There is a Makefile that offers all required commands. A help text is available, when started without arguments:

$ make

clean                remove all build and Python artifacts
clean-build          remove build artifacts
clean-pyc            remove Python file artifacts
build                builds source and wheel package
publish              package and upload a release to pypi
publish-test         package and upload a release to the pypi test site
install-hatch        install 'hatch' using 'pipx'

The build system uses hatch. To install it, run make install-hatch. Then execute make publish-test to build and publish the client to https://test.pypi.org/project/udm-rest-api-client/ or make publish to publish to https://pypi.org/project/udm-rest-api-client/ .

License

The UDM REST API Client is distributed under the terms of the GNU Affero General Public License v3.0 only (AGPLv3) license.

Changelog

udm-rest-api-client (0.0.2) unstable; urgency=medium

  • Initial release. (Bug #57204)
  • Split from univention-directory-manager-rest (10.0.8-5 / 12.0.7)

... see here for earlier changes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

udm_rest_api_client-0.0.2.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

udm_rest_api_client-0.0.2-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file udm_rest_api_client-0.0.2.tar.gz.

File metadata

  • Download URL: udm_rest_api_client-0.0.2.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for udm_rest_api_client-0.0.2.tar.gz
Algorithm Hash digest
SHA256 09a16390fa1fe3f84a6ca3c419a51899edeead61c4297402eb50057095b55246
MD5 17a8d4f19c4dcd26a44c445891a6ec44
BLAKE2b-256 a85fd5d27b0a6c22b03524e7c4a8716ea2fa7e361b888a5106c00e55dd323256

See more details on using hashes here.

File details

Details for the file udm_rest_api_client-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for udm_rest_api_client-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c515598a79398d41ef33360a85138efcd41f1797f2ee2ad8b1e9dbb5ba56f318
MD5 3469a1d1da51f4e133800a7c550386e5
BLAKE2b-256 28550f673dc3acb16fbc29f78337acaa39774ad91c9e8ff827f297bdeec563f2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page