Blob


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