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
57 50558780 2022-01-06 o // A Client represents a client connection to the Icinga2 HTTP API.
58 50558780 2022-01-06 o // It should be created using Dial.
59 7aa5b055 2022-01-13 o // Since Client wraps http.Client, exported methods such as Get and
60 50558780 2022-01-06 o // PostForm can be used to implement any functionality not provided by
61 50558780 2022-01-06 o // methods of Client.
62 2c4d16ae 2021-12-23 o type Client struct {
64 b74ab3b3 2021-12-30 o username string
65 b74ab3b3 2021-12-30 o password string
69 76669f9b 2022-01-11 o var ErrNotExist = errors.New("object does not exist")
70 76669f9b 2022-01-11 o var ErrExist = errors.New("object already exists")
71 76669f9b 2022-01-11 o var ErrNoMatch = errors.New("no object matches filter")
73 50558780 2022-01-06 o // Dial returns a new Client connected to the Icinga2 server at addr.
74 50558780 2022-01-06 o // The recommended value for client is http.DefaultClient.
75 50558780 2022-01-06 o // But it may also be a modified client which, for example,
76 50558780 2022-01-06 o // skips TLS certificate verification.
77 50558780 2022-01-06 o func Dial(addr, username, password string, client *http.Client) (*Client, error) {
78 50558780 2022-01-06 o c := &Client{addr, username, password, client}
79 1646fdeb 2022-01-18 o if _, err := Permissions(c); err != nil {
80 2c4d16ae 2021-12-23 o return nil, err
85 1646fdeb 2022-01-18 o // Permissions returns the permissions granted to the Client.
86 1646fdeb 2022-01-18 o func Permissions(c *Client) ([]string, error) {
87 6d1ce85e 2022-01-12 o resp, err := c.get("", "")
88 2c4d16ae 2021-12-23 o if err != nil {
89 1646fdeb 2022-01-18 o return nil, err
91 1646fdeb 2022-01-18 o if resp.StatusCode != http.StatusOK {
92 1646fdeb 2022-01-18 o return nil, errors.New(resp.Status)
94 1646fdeb 2022-01-18 o defer resp.Body.Close()
95 1646fdeb 2022-01-18 o apiresp, err := parseAPIResponse(resp.Body)
96 1646fdeb 2022-01-18 o if err != nil {
97 1646fdeb 2022-01-18 o return nil, err
99 1646fdeb 2022-01-18 o for i := range apiresp.Results {
100 1646fdeb 2022-01-18 o return apiresp.Results[i].Permissions, nil
102 1646fdeb 2022-01-18 o return nil, errors.New("no permissions")