FORMAT: 1A

# webd API

the web management server (`webd`) provides a REST HTTP API to control
multiple `elektrad` instances and serves the `client`

as a result of the client being served at `/`, the api is prefixed with `/api` to avoid conflicts



# get versions [GET /api/version]

returns the current version of the API and elektra

The API version is increased whenever breaking changes
(i.e. changes that prevent backward compatibility) are made.
The Elektra version is directly taken from the Elektra library,
for further information and explanation see [doc/VERSION.md](https://github.com/ElektraInitiative/libelektra/blob/master/doc/VERSION.md).

+ Response 200 (application/json; charset=utf-8)
    + Attributes (object)
        + api: 1 (number) - The version of the API itself
        + elektra (object) - Detailed version information about the used Elektra library
            + version: 0.8.23 (string) - The currently used version in its complete format
            + major: 0 (number) - The currently used major version
            + minor: 8 (number) - The currently used minor version
            + patch: 23 (number) - The currently used patch version



# Group instances

## instances endpoint [/api/instances]

### list all instances [GET]

+ Response 200 (application/json; charset=utf-8)
    + Attributes (array[Instance])

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

### create a new instance [POST]

+ Request (application/json)
    + Attributes (Instance)

+ Response 201
    + Attributes (Instance)

+ Request (application/json)
    + Attributes (Minimal Instance)

+ Response 201
    + Attributes (Minimal Instance)

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

## single instance [/api/instances/{instance_id}]

+ Attributes (Instance)

+ Parameters
    + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance

### get information about a single instance [GET]

+ Response 200 (application/json; charset=utf-8)
    + Attributes (Instance)

+ Response 404

### edit a single instance [PUT]

+ Request update host of instance (application/json)
    + Attributes (Minimal Instance)

    + Body

            { "host": "http://localhost:33333" }

+ Response 200

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

+ Response 404

### delete a single instance [DELETE]

+ Request
    + Parameters
        + instance_id: `0d8b3c29-88a5-4681-ad7a-17f5689e31f3` (string) - id of an instance

+ Response 200

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

+ Response 404

### get version of a single instance [GET /api/instances/{instance_id}/version]

+ Response 200 (application/json; charset=utf-8)
    + Attributes (Version)

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

+ Response 404


## single instance key database [/api/instances/{instance_id}/kdb/{+path}]

you can access a single instances' configuration via `clusterd` the same way you would directly access it

if the `INSTANCE` env var is used, webd creates a virtual instance with the id `my`. It can be requested via: `/instances/my/kdb`

### get configuration [GET]

get the content and subkeys of the specified `path` in the elektra key database
this actually does `kdb get`, `kdb ls` and `kdb meta-ls`/`kdb meta-get` at once and is used to browse the kdb

+ Request
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + path: `user/hello` (string) - path to the elektra config

+ Response 200 (application/json; charset=utf-8)
    + Attributes (KDBResponse)

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

+ Response 404

### set configuration [PUT]

set the content of the specified `path` in the elektra key database - works like `kdb set`

+ Request (text/plain)
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + path: `user/hello` (string) - path to the elektra config

    + Body

            hello world

+ Response 200

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

+ Response 404

### delete configuration [DELETE]

delete the key (and all its subkeys) on the specified `path` from the elektra key database - works like `kdb rm -r`

+ Request
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + path: `user/hello` (string) - path to the elektra config

+ Response 200

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

+ Response 404


## find keys [GET /api/instances/{instance_id}/kdbFind/{+query}]

+ Request (application/json)
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + query: `hello` (string) - search query

+ Response 200 (application/json; charset=utf-8)
    + Body

            [
                "user/hello",
                "user/helloWorld"
            ]


## metakeys [/api/instances/{instance_id}/kdbMeta/{+path}]

### create metakey [POST]

+ Attributes (Metakey)

+ Request (application/json)
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + path: `user/hello` (string) - path to the elektra config

+ Response 204

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)

### delete metakey [DELETE]

+ Attributes (Metakey)

+ Request (application/json)
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + path: `user/hello` (string) - path to the elektra config

+ Response 204

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)


## move operation [POST /api/instances/{instance_id}/kdbMv/{+from}]

move a key (and all its subkeys) to a new path - works like `kdb mv -r`

+ Request (text/plain)
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + from: `user/hello` (string) - path to the elektra config

    + Body

            user/toPath

+ Response 204

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)


## copy operation [POST /api/instances/{instance_id}/kdbCp/{+from}]

copy a key (and all its subkeys) to a new path - works like `kdb cp -r`

+ Request (text/plain)
    + Parameters
        + instance_id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string) - id of an instance
        + from: `user/hello` (string) - path to the elektra config

    + Body

            user/toPath

+ Response 204

+ Response 400 (application/json; charset=utf-8)
    + Attributes (Error)



# Data Structures

## Base Version (object)
+ api (number) - The version of the API itself

## Version (Base Version)
+ elektra (object) - Detailed version information about the used Elektra library
    + version (string) - The currently used version in its complete format
    + major (number) - The currently used major version
    + minor (number) - The currently used minor version
    + patch (number) - The currently used patch version

## Instance (object)
+ id: `46a288ae-7475-4cdd-a04c-3826c9a4b5f5` (string, required)
+ name: test instance (string, required)
+ host: `http://localhost:33333` (string, required)
+ visibility: user (string, required)
+ description: longer text describing the instance (string)

## Minimal Instance (object)
+ id: `0d8b3c29-88a5-4681-ad7a-17f5689e31f3` (string, required)
+ name: minimal instance (string, required)
+ host: `http://127.0.0.1:33333` (string, required)
+ visibility: user (string, required)

## KDBResponse (object)
+ exists: true (boolean, required) - `true` if the key exists in the kdb
+ name: hello (string, required) - name of the requested key
+ path: user/hello (string, required) - full path of the requested key
+ ls: user/hello, user/hello/world (array[string], required) - subkeys of the requested path, similar to `kdb ls`
+ value: hello world (string) - value of the key. Note: a key can exist but not have a value!
+ meta (object) - metadata of the requested path

## Error (object)
+ name (string) - description of the error, e.g. KDBError
+ message (string) - detailed error information, e.g. hint about malformed request

## Metakey (object)
+ key: metaName (string, required)
+ value: meta value (string, required)
