summaryrefslogtreecommitdiffstats
path: root/doc/70-REST-API.md
blob: dd5d26697b5ea8035bae6041acd3568fb5cacf5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<a id="REST-API"></a>The Icinga Director REST API
=================================================

Introduction
------------

Icinga Director has been designed with a REST API in mind. Most URLs you
can access with your browser will also act as valid REST url endpoints.

Base Headers
------------
All your requests MUST have a valid accept header. The only acceptable
variant right now is `application/json`, so please always append a header
as follows to your requests:

    Accept: application/json


Authentication
--------------
Please use HTTP authentication and any valid Icinga Web 2 user, granted
enough permissions to accomplish the desired actions. The restrictions
and permissions that have been assigned to web users will also be enforced
for API users. In addition, the permission `director/api` is required for
any API access.

Versioning
----------

There are no version strings so far in the Director URLs. We will try hard
to not break compatibility with future versions. Sure, sooner or later we
also might be forced to introduce some kind of versioning. But who knows?

As a developer you can trust us to not remove any existing REST url or any
provided property. However, you must always be ready to accept new properties.

URL scheme and supported methods
--------------------------------

We support GET, POST, PUT and DELETE.

| Method | Meaning                                                             |
|--------|---------------------------------------------------------------------|
| GET    | Read / fetch data. Not allowed to run operations with the potential |
|        | to cause any harm                                                   |
| POST   | Trigger actions, create or modify objects. Can also be used to      |
|        | partially modify objects                                            |
| PUT    | Creates or replaces objects, cannot be used to modify single object |
|        | properties                                                          |
| DELETE | Remove a specific object                                            |

TODO: more examples showing the difference between POST and PUT

POST director/host
 gives 201 on success
GET director/host?name=hostname.example.com
PUT director/host?name=hostname.example.com
 gives 200 ok on success and 304 not modified on no change
DELETE director/host?name=hostname.example.com
 gives 200 on success


First example request with CURL
-------------------------------

```sh
curl -H 'Accept: application/json' \
     -u 'username:password' \
     'https://icinga.example.com/icingaweb2/director/host?name=hostname.example.com'
```

### CURL helper script

A script like the following makes it easy to play around with curl:

```sh
METHOD=$1
URL=$2
BODY="$3"
USERNAME="demo"
PASSWORD="***"
test -z "$PASSWORD" || USERNAME="$USERNAME:$PASSWORD"

test -z "$BODY" && curl -u "$USERNAME" \
  -i https://icingaweb/icingaweb/$URL \
  -H 'Accept: application/json' \
  -X $METHOD

test -z "$BODY" || curl -u "$USERNAME" \
  -i https://icingaweb/icingaweb/$URL \
  -H 'Accept: application/json' \
  -X $METHOD \
  -d "$BODY"

echo
```

It can be used as follows:

```sh
director-curl GET director/host?name=localhost

director-curl POST director/host '{"object_name": "host2", "... }'
```


Should I use HTTPS?
-------------------

Sure, absolutely, no doubt. There is no, absolutely no reason to NOT use
HTTPS these days. Especially not for a configuration tool allowing you to
configure check commands that are going to be executed on all your servers.

Icinga Objects
--------------

### Special parameters

| Parameter      | Description                                                 |
|----------------|-------------------------------------------------------------|
| resolved       | Resolve all inherited properties and show a flat object     |
| withNull       | Retrieve default (null) properties also                     |
| withServices   | Show services attached to a host. `resolved` and `withNull` |
|                | are applied for services too                                |
| allowOverrides | Set variable overrides for virtual Services                 |
| showStacktrace | Returns the related stack trace, in case an error occurs    |

#### Resolve object properties

In case you add the `resolved` parameter to your URL, all inherited object
properties will be resolved. Such a URL could look as follows:

    director/host?name=hostname.example.com&resolved


#### Retrieve default (null) properties also

Per default properties with `null` value are skipped when shipping a result.
You can influence this behavior with the `properties` parameter. Just append
`&withNull` to your URL:

    director/host?name=hostname.example.com&withNull


#### Fetch host with it's services

This is what the `withServices` parameter exists:

    director/host?name=hostname.example.com&withServices


#### Retrieve only specific properties

The `properties` parameter also allows you to specify a list of specific
properties. In that case, only the given properties will be returned, even
when they have no (`null`) value:

    director/host?name=hostname.example.com&properties=object_name,address,vars


#### Override vars for inherited/applied Services

Enabling `allowOverrides` allows you to let Director figure out, whether your
modified Custom Variables need to be applied to a specific individual Service,
or whether setting Overrides at Host level is the way to go.

     POST director/service?name=Uptime&host=hostname.example.com&allowOverrices

```json
{ "vars.uptime_warning": 300 }
```

In case `Uptime` is an Apply Rule, calling this without `allowOverrides` will
trigger a 404 response. Please note that when modifying the Host object, the
body for response 200 will show the Host object, as that's the one that has
been modified.

### Example

GET director/host?name=pe2015.example.com
```json
{
  "address": "127.0.0.3",
  "check_command": null,
  "check_interval": null,
  "display_name": "pe2015 (example.com)",
  "enable_active_checks": null,
  "flapping_threshold": null,
  "groups": [ ],
  "imports": [
    "generic-host"
  ],
  "retry_interval": null,
  "vars": {
    "facts": {
      "aio_agent_build": "1.2.5",
      "aio_agent_version": "1.2.5",
      "architecture": "amd64",
      "augeas": {
        "version": "1.4.0"
      },
   ...
}
```

director/host?name=pe2015.example.com&resolved
```json
{
    "address": "127.0.0.3",
    "check_command": "tom_ping",
    "check_interval": "60",
    "display_name": "pe2015 (example.com)",
    "enable_active_checks": true,
    "groups": [ ],
    "imports": [
      "generic-host"
    ],
    "retry_interval": "10",
    "vars": {
      "facts": {
        "aio_agent_build": "1.2.5",
        "aio_agent_version": "1.2.5",
        "architecture": "amd64",
        "augeas": {
          "version": "1.4.0"
        },
     ...
}
```

JSON is pretty-printed per default, at least for PHP >= 5.4

Error handling
--------------

Director tries hard to return meaningful output and error codes:
```
HTTP/1.1 400 Bad Request
Server: Apache
Content-Length: 46
Connection: close
Content-Type: application/json
```

```json
{
    "error": "Invalid JSON: Syntax error"
}
```

Trigger actions
---------------
You can of course also use the API to trigger specific actions. Deploying the configuration is as simple as issueing:

    POST director/config/deploy

More
----

Currently we do not handle Last-Modified und ETag headers. This would involve some work, but could be a cool feature. Let us know your ideas!


Sample scenario
---------------

Let's show you how the REST API works with a couple of practical examples:

### Create a new host

```
POST director/host
```

```json
{
  "object_name": "apitest",
  "object_type": "object",
  "address": "127.0.0.1",
  "vars": {
    "location": "Berlin"
  }
}
```
#### Response
```
HTTP/1.1 201 Created
Date: Tue, 01 Mar 2016 04:43:55 GMT
Server: Apache
Content-Length: 140
Content-Type: application/json
```

```json
{
    "address": "127.0.0.1",
    "object_name": "apitest",
    "object_type": "object",
    "vars": {
        "location": "Berlin"
    }
}
```

The most important part of the response is the response code: `201`, a resource has been created. Just for fun, let's fire the same request again. The answer obviously changes:

```
HTTP/1.1 500 Internal Server Error
Date: Tue, 01 Mar 2016 04:45:04 GMT
Server: Apache
Content-Length: 60
Connection: close
Content-Type: application/json
```

```json
{
    "error": "Trying to recreate icinga_host (apitest)"
}
```

So, let's update this host. To work with existing objects, you must ship their `name` in the URL:

    POST director/host?name=apitest

```json
{
  "object_name": "apitest",
  "object_type": "object",
  "address": "127.0.0.1",
  "vars": {
    "location": "Berlin"
  }
}
```

Same body, so no change:
```
HTTP/1.1 304 Not Modified
Date: Tue, 01 Mar 2016 04:45:33 GMT
Server: Apache
```

So let's now try to really change something:

    POST director/host?name=apitest

```json
{"address": "127.0.0.2", "vars.event": "Icinga CAMP" }
```

We get status `200`, changes have been applied:

```
HTTP/1.1 200 OK
Date: Tue, 01 Mar 2016 04:46:25 GMT
Server: Apache
Content-Length: 172
Content-Type: application/json
```

```json
{
    "address": "127.0.0.2",
    "object_name": "apitest",
    "object_type": "object",
    "vars": {
        "location": "Berlin",
        "event": "Icinga CAMP"
    }
}
```

The response always returns the full object on modification. This way you can immediately investigate the merged result. As you can see, `POST` requests only touch the parameters you passed - the rest remains untouched.

One more example to prove this:

```
POST director/host?name=apitest
```

```json
{"address": "127.0.0.2", "vars.event": "Icinga CAMP" }
```

No modification, you get a `304`. HTTP standards strongly discourage shipping a body in this case:
```
HTTP/1.1 304 Not Modified
Date: Tue, 01 Mar 2016 04:52:05 GMT
Server: Apache
```

As you might have noted, we only changed single properties in the vars dictionary. Now lets override the whole dictionary:

```
POST director/host?name=apitest
```

```json
{"address": "127.0.0.2", "vars": { "event": [ "Icinga", "Camp" ] } }
```

The response shows that this works as expected:

```
HTTP/1.1 200 OK
Date: Tue, 01 Mar 2016 04:52:33 GMT
Server: Apache
Content-Length: 181
Content-Type: application/json
```

```json
{
    "address": "127.0.0.2",
    "object_name": "apitest",
    "object_type": "object",
    "vars": {
        "event": [
            "Icinga",
            "Camp"
        ]
    }
}
```

If merging properties is not what you want, `PUT` comes to the rescue:

    PUT director/host?name=apitest

```
{ "vars": { "event": [ "Icinga", "Camp" ] }
```

All other properties vanished, all but name and type:
```
HTTP/1.1 200 OK
Date: Tue, 01 Mar 2016 04:54:33 GMT
Server: Apache
Content-Length: 153
Content-Type: application/json
```

```json
{
    "object_name": "apitest",
    "object_type": "object",
    "vars": {
        "event": [
            "Icinga",
            "Camp"
        ]
    }
}
```

Let's put "nothing":

    PUT director/host?name=apitest

```json
{}
```

Works as expected:

```
HTTP/1.1 200 OK
Date: Tue, 01 Mar 2016 04:57:35 GMT
Server: Apache
Content-Length: 62
Content-Type: application/json
```

```json
{
    "object_name": "apitest",
    "object_type": "object"
}
```

Of course, `PUT` also supports `304`, you can check this by sending the same request again.

Now let's try to cheat:

    KILL director/host?name=apitest

```
HTTP/1.1 400 Bad Request
Date: Tue, 01 Mar 2016 04:54:07 GMT
Server: Apache
Content-Length: 43
Connection: close
Content-Type: application/json
```

```json
{
    "error": "Unsupported method KILL"
}
```

Ok, no way. So let's use the correct method:

    DELETE director/host?name=apitest

```
HTTP/1.1 200 OK
Date: Tue, 01 Mar 2016 05:59:22 GMT
Server: Apache
Content-Length: 109
Content-Type: application/json
```

```json
{
    "imports": [
        "generic-host"
    ],
    "object_name": "apitest",
    "object_type": "object"
}
```

### Service Apply Rules

Please note that Service Apply Rule names are not unique in Icinga 2. They are
not real objects, they are creating other objects in a loop. This makes it
impossible to distinct them by name. Therefore, a dedicated REST API endpoint
`director/serviceapplyrules` ships all Service Apply Rules combined with their
internal ID. This ID can then be used to modify or delete a Rule via
`director/service`.

### Deployment Status
In case you want to fetch the information about the deployments status, 
you can call the following API:

    GET director/config/deployment-status

```
HTTP/1.1 200 OK
Date: Wed, 07 Oct 2020 13:14:33 GMT
Server: Apache
Content-Type: application/json
```

```json
{
    "active_configuration": {
        "stage_name": "b191211d-05cb-4679-842b-c45170b96421",
        "config": "617b9cbad9e141cfc3f4cb636ec684bd60073be1",
        "activity": "028b3a19ca7457f5fc9dbb5e4ea527eaf61616a2"
    }
}
```
This throws a 500 in case Icinga isn't reachable. 
In case there is no active stage name related to the Director, active_configuration 
is set to null.

Another possibility is to pass a list of checksums to fetch the status of 
specific deployments and (activity log) activities.
Following, you can see an example of how to do it:

    GET director/config/deployment-status?config_checksums=617b9cbad9e141cfc3f4cb636ec684bd60073be2,
    617b9cbad9e141cfc3f4cb636ec684bd60073be1&activity_log_checksums=617b9cbad9e141cfc3f4cb636ec684bd60073be1,
    028b3a19ca7457f5fc9dbb5e4ea527eaf61616a2
    
```json
{
    "active_configuration": {
        "stage_name": "b191211d-05cb-4679-842b-c45170b96421",
        "config": "617b9cbad9e141cfc3f4cb636ec684bd60073be1",
        "activity": "028b3a19ca7457f5fc9dbb5e4ea527eaf61616a2"
    },
    "configs": {
        "617b9cbad9e141cfc3f4cb636ec684bd60073be2": "deployed",
        "617b9cbad9e141cfc3f4cb636ec684bd60073be1": "active"
    },
    "activities": {
        "617b9cbad9e141cfc3f4cb636ec684bd60073be1": "undeployed",
        "028b3a19ca7457f5fc9dbb5e4ea527eaf61616a2": "active"
    }
}
```
The list of possible status is: 
* `active`: whether this configuration is currently active
* `deployed`: whether this configuration has ever been deployed
* `failed`: whether the deployment of this configuration has failed
* `undeployed`: whether this configuration has been rendered, but not yet deployed
* `unknown`: whether no configurations have been found for this checksum

### Agent Tickets

The Director is very helpful when it goes to manage your Icinga Agents. In
case you want to fetch tickets through the API, please do as follows:

    GET director/host/ticket?name=apitest

```
HTTP/1.1 200 OK
Date: Thu, 07 Apr 2016 22:19:24 GMT
Server: Apache
Content-Length: 43
Content-Type: application/json
```

```json
"5de9883080e03278039bce57e4fbdbe8fd262c40"
```

Please expect an error in case the host does not exist or has not been
configured to be an Icinga Agent.

### Self Service API

#### Theory of operation

Icinga Director offers a Self Service API, allowing new Icinga nodes to register
themselves. No credentials are required, authentication is based on API keys.
There are two types of such keys:

* Host Template API keys
* Host Object API keys

Template keys basically grant the permission to:

* Create a new host based on that template
* Specify name and address properties for that host

This is a one-time operation and allows one to claim ownership of a specific host.
Now, there are two possible scenarios:

* The host already exists
* The host is not known to Icinga Director

In case the host already exists, Director will check whether it's API key matches
the given one. [..]

#### Request processing for Host registration

A new node will `POST` to `self-service/register-host`, with two parameters in
the URL:

* `name`: it's desired object name, usually the FQDN
* `key`: a valid Host Template API key

In it's body it is allowed to specify a specific set of properties. At the time
of this writing, these are:

* `display_name`
* `address`
* `address6`

Director will validate the `key` and load the corresponding *Host Template*. In
case no such is found, the request is rejected. Then it checks whether a *Host*
with the given `name` exists. In case it does, the request is rejected unless:

* It inherits the loaded *Host Template*
* It already has an API key

If these conditions match, the request is processed. The following sketch roughly shows the decision tree (AFTER the key has been
validated):

```
                               +-----------------------------+
    +--------------+           | * Validate given properties |
    | Host exists? | -- NO --> | * Create new host object    |-----------+
    +--------------+           | * Return new Host API key   |           |
           |                   +-----------------------------+           |
          YES                                                            |
           |                                                             |
           v                          +-----------------------------+    |
   +----------------------+           | * Validate given properties |    |
   | Host has an API key? | -- NO --> | * Apply eventual changes    |----+
   +----------------------+           | * Return new Host API key   |    |
           |                          +-----------------------------+    |
          YES                                                            |
           |                                         +-------------------+
           v                                         |
   +--------------------+                            v
   | Reject the request |                +---------------------+
   +--------------------+                | Client persists the |
                                         | new Host API key    |
                                         +---------------------+
```