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 7aa5b055 2022-01-13 o //
11 7aa5b055 2022-01-13 o // Icinga2 servers in the wild often serve self-signed certificates which fail
12 7aa5b055 2022-01-13 o // verification by Go's tls client. To ignore the errors, Dial the server with a
13 7aa5b055 2022-01-13 o // modified http.Client:
14 7aa5b055 2022-01-13 o //
15 7aa5b055 2022-01-13 o // t := http.DefaultTransport.(*http.Transport)
16 7aa5b055 2022-01-13 o // t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
17 7aa5b055 2022-01-13 o // c := http.DefaultClient
18 7aa5b055 2022-01-13 o // c.Transport = t
19 7aa5b055 2022-01-13 o // client, err := icinga.Dial(addr, user, pass, c)
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 7aa5b055 2022-01-13 o // Methods on Client provide API actions like looking up users and creating
25 7aa5b055 2022-01-13 o // hosts:
26 7aa5b055 2022-01-13 o //
27 7aa5b055 2022-01-13 o // user, err := client.LookupUser("oliver")
28 7aa5b055 2022-01-13 o // if err != nil {
29 7aa5b055 2022-01-13 o // // handle error
30 7aa5b055 2022-01-13 o // }
31 7aa5b055 2022-01-13 o // host := Host{
32 7aa5b055 2022-01-13 o // Name: "myserver.example.com",
33 7aa5b055 2022-01-13 o // CheckCommand: "hostalive"
34 7aa5b055 2022-01-13 o // Address: "192.0.2.1"
35 7aa5b055 2022-01-13 o // Address6: "2001:db8::1"
36 7aa5b055 2022-01-13 o // }
37 7aa5b055 2022-01-13 o // if err := client.CreateHost(host); err != nil {
38 7aa5b055 2022-01-13 o // // handle error
39 7aa5b055 2022-01-13 o // }
40 7aa5b055 2022-01-13 o //
41 50558780 2022-01-06 o // Since Client wraps http.Client, exported methods of http.Client such
42 50558780 2022-01-06 o // as Get and PostForm can be used to implement any extra functionality
43 50558780 2022-01-06 o // not provided by this package. For example:
44 50558780 2022-01-06 o //
45 50558780 2022-01-06 o // resp, err := client.PostForm("https://icinga.example.com:5665", data)
46 50558780 2022-01-06 o // if err != nil {
47 50558780 2022-01-06 o // // handle error
48 50558780 2022-01-06 o // }
49 50558780 2022-01-06 o //
50 2c4d16ae 2021-12-23 o package icinga
51 2c4d16ae 2021-12-23 o
52 2c4d16ae 2021-12-23 o import (
53 49e200e1 2022-02-03 o "encoding/json"
54 76669f9b 2022-01-11 o "errors"
55 2c4d16ae 2021-12-23 o "net/http"
56 2c4d16ae 2021-12-23 o )
57 2c4d16ae 2021-12-23 o
58 50558780 2022-01-06 o // A Client represents a client connection to the Icinga2 HTTP API.
59 50558780 2022-01-06 o // It should be created using Dial.
60 7aa5b055 2022-01-13 o // Since Client wraps http.Client, exported methods such as Get and
61 50558780 2022-01-06 o // PostForm can be used to implement any functionality not provided by
62 50558780 2022-01-06 o // methods of Client.
63 2c4d16ae 2021-12-23 o type Client struct {
64 50558780 2022-01-06 o addr string
65 b74ab3b3 2021-12-30 o username string
66 b74ab3b3 2021-12-30 o password string
67 007a3d09 2021-12-27 o *http.Client
68 2c4d16ae 2021-12-23 o }
69 2c4d16ae 2021-12-23 o
70 76669f9b 2022-01-11 o var ErrNotExist = errors.New("object does not exist")
71 76669f9b 2022-01-11 o var ErrExist = errors.New("object already exists")
72 76669f9b 2022-01-11 o var ErrNoMatch = errors.New("no object matches filter")
73 76669f9b 2022-01-11 o
74 50558780 2022-01-06 o // Dial returns a new Client connected to the Icinga2 server at addr.
75 50558780 2022-01-06 o // The recommended value for client is http.DefaultClient.
76 50558780 2022-01-06 o // But it may also be a modified client which, for example,
77 50558780 2022-01-06 o // skips TLS certificate verification.
78 50558780 2022-01-06 o func Dial(addr, username, password string, client *http.Client) (*Client, error) {
79 50558780 2022-01-06 o c := &Client{addr, username, password, client}
80 1646fdeb 2022-01-18 o if _, err := Permissions(c); err != nil {
81 2c4d16ae 2021-12-23 o return nil, err
82 2c4d16ae 2021-12-23 o }
83 2c4d16ae 2021-12-23 o return c, nil
84 2c4d16ae 2021-12-23 o }
85 2c4d16ae 2021-12-23 o
86 1646fdeb 2022-01-18 o // Permissions returns the permissions granted to the Client.
87 1646fdeb 2022-01-18 o func Permissions(c *Client) ([]string, error) {
88 6d1ce85e 2022-01-12 o resp, err := c.get("", "")
89 2c4d16ae 2021-12-23 o if err != nil {
90 1646fdeb 2022-01-18 o return nil, err
91 2c4d16ae 2021-12-23 o }
92 1646fdeb 2022-01-18 o if resp.StatusCode != http.StatusOK {
93 1646fdeb 2022-01-18 o return nil, errors.New(resp.Status)
94 2c4d16ae 2021-12-23 o }
95 1646fdeb 2022-01-18 o defer resp.Body.Close()
96 49e200e1 2022-02-03 o var apiresp apiResponse
97 49e200e1 2022-02-03 o if err := json.NewDecoder(resp.Body).Decode(&apiresp); err != nil {
98 1646fdeb 2022-01-18 o return nil, err
99 1646fdeb 2022-01-18 o }
100 9feb16b3 2022-02-02 o return apiresp.Results[0].Permissions, nil
101 2c4d16ae 2021-12-23 o }