1 50558780 2022-01-06 o // package icinga provides a client to the Icinga2 HTTP API.
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:
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
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:
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
24 7aa5b055 2022-01-13 o // Methods on Client provide API actions like looking up users and creating
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
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"
37 7aa5b055 2022-01-13 o // if err := client.CreateHost(host); err != nil {
38 7aa5b055 2022-01-13 o // // handle error
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:
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
53 49e200e1 2022-02-03 o "encoding/json"
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 {
65 b74ab3b3 2021-12-30 o username string
66 b74ab3b3 2021-12-30 o password string
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")
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
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
92 1646fdeb 2022-01-18 o if resp.StatusCode != http.StatusOK {
93 1646fdeb 2022-01-18 o return nil, errors.New(resp.Status)
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
100 9feb16b3 2022-02-02 o return apiresp.Results[0].Permissions, nil