Skip to main content

Python Client

Python version

The Python client supports Python 2.7 or the current subversion of Python 3. We recommend that you use Python 3 for all new projects, Python 2 being on the way out.

Installing the client

There are two ways to install Python, either using the package python-bm-client available in the BlueMind repositories (Python 2.7 only), or via pip (Python 2.7 or Python 3).

We recommend that you use the pip method, in a virtual environment (virtualenv or venv) if possible so that you don't interfere with the system. Implementing this environment being outside this documentation's scope, the following instructions are restricted to the system's Python distribution.

aptitude install python-pip 
pip install --upgrade setuptools
pip install netbluemind4

Using the client

Authentication

The client uses the BMClient class to start:

from netbluemind.python.client import BMClient

# BEGIN CONF
URL="http://localhost:8090/api"
# END CONF

f = open('/etc/bm/bm-core.tok', 'r')
KEY = f.readline()
f.close()

client = BMClient(URL, KEY)

"URL" is the BlueMind server's URL. From outside, this URL will be https://bluemind.domain.net/api.

"KEY" is the admin0 password or token. In this example we are using the admin0 token available on the server in order to have extended rights on the API, but you can authenticate as a user with:

from netbluemind.python.client import BMClient

# BEGIN CONF
URL="http://localhost:8090/api"
# END CONF

client = BMClient(URL)
client.login('login@domain.net', 'password')

Please note that you can also use a user's API key instead of their password.

Use

BMClient

The BMClient is a helper used to access classes in order to manipulate users, groups, etc.

from netbluemind.python.client import BMClient

# BEGIN CONF
URL="http://localhost:8090/api"
# END CONF

client = BMClient(URL)
client.login("login@domain.net", "password")
print client.apiKey

iUser = client.users("domain.net")
user = iUser.byEmail("login@domain.net")
print user.value.login

Users

iUser (IUser instance) is used to access and edit all user information (password, photo, contact details, archived status, etc.). Methods return an instance for an ItemValue object which is used to access the object in question via the value. attribute.

Domains

You can create or modify domains using the IDomains interface. When you create a domain, it is important that the domain uid is the same as its name. As a result, to create a domain:

domain = Domain()
domain.name = "local.lan"
domain.label = "local.lan"
domain.description = "Domain of " + "local.lan"
domain.properties = {}
# global must always be set to False
domain.global_ = False
# defines domain aliases
domain.aliases = []

idomains.create("local.lan", domain)

You can define settings for the ImportLDAP and ImportAD plugins using the properties attribute:

properties = dict()
properties['import_ldap_hostname'] = 'ldap.local'
properties['import_ldap_password'] = 'ldap-password'
properties['import_ldap_user_filter'] = '(mail=\*)'
properties['import_ldap_accept_certificate'] = True
properties['import_ldap_ext_id_attribute'] = 'entryUUID'
properties['import_ldap_relay_mailbox_group'] = ''
properties['import_ldap_enabled'] = True
properties['import_ldap_group_filter'] = '(objectClass=posixGroup)'
properties['import_ldap_protocol'] = 'tls'
properties['import_ldap_base_dn'] = 'dc=lan,dc=local'
properties['import_ldap_login_dn'] = 'uid=admin,cn=users,cn=accounts,dc=lan,dc=local'
properties['lang'] = 'fr'
properties['im_public_auth'] = True # allow Instant messaging for all users
properties['domain_max_users'] = None
properties['mail_routing_relay'] = ''
properties['mailbox_max_user_quota'] = '0'
properties['mailbox_default_user_quota'] = '0'
properties['mailbox_max_publicfolder_quota'] = '0'
properties['mailbox_default_publicfolder_quota'] = '0'
properties['filehosting_max_filesize'] = '0'
properties['filehosting_retention'] = '365'
properties['mail_autoDetachmentLimit'] = '0'
properties['mail_forward_unknown_to_relay'] = False

For an Active Directory import, simply remplace ldap with ad.

Examples

You can find many examples of what you can do here: https://forge.bluemind.net/stash/projects/BA/repos/bluemind-samples/browse/python-api-examples

Starting a backup

client = BMClient(URL, KEY)
idataprotect = client.instance(IDataProtect)

taskRef = idataprotect.saveAll()
status = client.waitTask(taskRef)
print "\nBackup done with status [" + status.state.value + "]"