Skip to content

truenas/api_client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Join Discord Join Forums File Issue

TrueNAS Websocket Client

Found an issue? Please report it on our Jira bugtracker.

About

The TrueNAS websocket client provides the command line tool midclt and the means to easily communicate with middleware using Python by making calls through the websocket API. The client can connect to a local TrueNAS instance by default or to a specified remote socket. This offers an alternative to going through the web UI or connecting via ssh.

By default, communication facilitated by the API between the client and middleware now uses the JSON-RPC 2.0 protocol. However, it is still possible to use the legacy client by passing a legacy uri, e.g. 'ws://some.truenas.address/websocket' as opposed to 'ws://some.truenas.address/api/current'.

Getting Started

TrueNAS comes with this client preinstalled, but it is also possible to use the TrueNAS websocket client from a non-TrueNAS host.

On a non-TrueNAS host, ensure that Git is installed and run pip install git+https://github.com/truenas/api_client.git to automatically install dependencies. You may alternatively clone this repository and run python setup.py install. Using a Python venv is recommended.

Usage

The midclt command (not to be confused with the TrueNAS CLI) provides a way to make direct API calls through the client. To view its syntax, enter midclt -h. The -h option can also be used with any of midclt's subcommands.

The client's default behavior is to connect to the localhost's middlewared socket. For a remote connection, e.g. from a Windows host, you must specify the --uri option and authenticate with either user credentials or an API key. For example: midclt --uri ws://<TRUENAS_IP>/api/current -K key ...

Make local API calls

root@my_truenas[~]# midclt call user.create '{"full_name": "John Doe", "username": "user", "password": "pass", "group_create": true}'

Login to a remote TrueNAS

root@my_truenas[~]# midclt --uri ws://some.other.truenas/api/current -U user -P password call system.info

Start a job

root@my_truenas[~]# midclt call -j pool.dataset.lock mypool/mydataset

Development

The TrueNAS API client can also be used in Python scripts.

Make local API calls

from truenas_api_client import Client

with Client() as c:  # Local IPC
      print(c.ping())  # pong
      user = {"full_name": "John Doe", "username": "user", "password": "pass", "group_create": True}
      entry_id = c.call("user.create", user)
      user = c.call("user.get_instance", entry_id)
      print(user["full_name"])  # John Doe

Login with a user account or an API key

# User account
with Client(uri="ws://some.other.truenas/api/current") as c:
      c.call("auth.login", username, password)

# API key
with Client(uri="ws://some.other.truenas/api/current") as c:
      c.call("auth.login_with_api_key", key)

Start a job

with Client() as c:
      is_locked = c.call("pool.dataset.lock", "mypool/mydataset", job=True)
      if is_locked:
            args = {"datasets": [{"name": "mypool/mydataset", "passphrase": "passphrase"}]}
            c.call("pool.dataset.unlock", "mypool/mydataset", args, job=True)

Helpful Links

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages