Blame


1 50558780 2022-01-06 o // package icinga provides a client to the Icinga2 HTTP API.
2 50558780 2022-01-06 o //
3 50558780 2022-01-06 o // A Client manages interaction with an Icinga2 server.
4 50558780 2022-01-06 o // It is created using Dial:
5 50558780 2022-01-06 o //
6 50558780 2022-01-06 o // client, err := icinga.Dial("icinga.example.com:5665", "icinga", "secret", http.DefaultClient)
7 50558780 2022-01-06 o // if err != nil {
8 50558780 2022-01-06 o // // handle error
9 50558780 2022-01-06 o // }
10 6456042b 2022-01-06 o // host, err := client.LookupHost("myserver.example.com")
11 50558780 2022-01-06 o // if err != nil {
12 50558780 2022-01-06 o // // handle error
13 50558780 2022-01-06 o // }
14 50558780 2022-01-06 o //
15 50558780 2022-01-06 o // Since Client wraps http.Client, exported methods of http.Client such
16 50558780 2022-01-06 o // as Get and PostForm can be used to implement any extra functionality
17 50558780 2022-01-06 o // not provided by this package. For example:
18 50558780 2022-01-06 o //
19 50558780 2022-01-06 o // resp, err := client.PostForm("https://icinga.example.com:5665", data)
20 50558780 2022-01-06 o // if err != nil {
21 50558780 2022-01-06 o // // handle error
22 50558780 2022-01-06 o // }
23 50558780 2022-01-06 o //
24 2c4d16ae 2021-12-23 o package icinga
25 2c4d16ae 2021-12-23 o
26 2c4d16ae 2021-12-23 o import (
27 76669f9b 2022-01-11 o "errors"
28 2c4d16ae 2021-12-23 o "net/http"
29 2c4d16ae 2021-12-23 o )
30 2c4d16ae 2021-12-23 o
31 50558780 2022-01-06 o // A Client represents a client connection to the Icinga2 HTTP API.
32 50558780 2022-01-06 o // It should be created using Dial.
33 50558780 2022-01-06 o // Since Client wraps http.Client, standard methods such as Get and
34 50558780 2022-01-06 o // PostForm can be used to implement any functionality not provided by
35 50558780 2022-01-06 o // methods of Client.
36 2c4d16ae 2021-12-23 o type Client struct {
37 50558780 2022-01-06 o addr string
38 b74ab3b3 2021-12-30 o username string
39 b74ab3b3 2021-12-30 o password string
40 007a3d09 2021-12-27 o *http.Client
41 2c4d16ae 2021-12-23 o }
42 2c4d16ae 2021-12-23 o
43 76669f9b 2022-01-11 o var ErrNotExist = errors.New("object does not exist")
44 76669f9b 2022-01-11 o var ErrExist = errors.New("object already exists")
45 76669f9b 2022-01-11 o var ErrNoMatch = errors.New("no object matches filter")
46 76669f9b 2022-01-11 o
47 50558780 2022-01-06 o // Dial returns a new Client connected to the Icinga2 server at addr.
48 50558780 2022-01-06 o // The recommended value for client is http.DefaultClient.
49 50558780 2022-01-06 o // But it may also be a modified client which, for example,
50 50558780 2022-01-06 o // skips TLS certificate verification.
51 50558780 2022-01-06 o func Dial(addr, username, password string, client *http.Client) (*Client, error) {
52 50558780 2022-01-06 o c := &Client{addr, username, password, client}
53 76669f9b 2022-01-11 o if _, err := c.Permissions(); err != nil {
54 2c4d16ae 2021-12-23 o return nil, err
55 2c4d16ae 2021-12-23 o }
56 2c4d16ae 2021-12-23 o return c, nil
57 2c4d16ae 2021-12-23 o }
58 2c4d16ae 2021-12-23 o
59 76669f9b 2022-01-11 o func (c *Client) Permissions() (response, error) {
60 6d1ce85e 2022-01-12 o resp, err := c.get("", "")
61 2c4d16ae 2021-12-23 o if err != nil {
62 76669f9b 2022-01-11 o return response{}, err
63 2c4d16ae 2021-12-23 o }
64 76669f9b 2022-01-11 o if resp.StatusCode == http.StatusOK {
65 76669f9b 2022-01-11 o return response{}, nil
66 2c4d16ae 2021-12-23 o }
67 76669f9b 2022-01-11 o return response{}, errors.New(resp.Status)
68 2c4d16ae 2021-12-23 o }