-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
87 lines (70 loc) · 2.57 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*Package icinga2 provides Icinga2 API client functionality.
See the tests for basic usage examples. */
package icinga2
import (
"crypto/tls"
"errors"
"io"
"net/http"
"net/url"
"path/filepath"
"github.com/bytemine/go-icinga2/event"
)
const icingaAPI = "v1"
// Client is a Icinga2 client.
type Client struct {
url *url.URL
user string
password string
insecureSkipVerify bool
}
// NewClient prepares a Client for usage.
//
// The icingaURL should contain the path up to the API-version part, eg. http://example.org:5665/ when the API lives at
// http://example.org:5665/v1 . User and password are the credentials of the API user. Set insecureSkipVerify to true
// if you have an invalid certificate chain.
func NewClient(icingaURL string, user, password string, insecureSkipVerify bool) (*Client, error) {
x, err := url.Parse(icingaURL)
if err != nil {
return nil, err
}
return &Client{url: x, user: user, password: password, insecureSkipVerify: insecureSkipVerify}, nil
}
// EventStream opens an Icinga2 event stream.
//
// Queue is an "unique" queue name, though multiple clients can use the same name if they use the same parameters for filter and streamtype.
//
// Filter is an Icinga API filter, described at: http://docs.icinga.org/icinga2/snapshot/doc/module/icinga2/chapter/icinga2-api#icinga2-api-filters
//
// Streamtype selects the type of events the EventStreamer should listen for, see the package constants.
//
// The returned io.Reader can directly be used with a json.Decoder if only one StreamType is requested. Otherwise event.Mux can be used to
// split the stream into seperate steams for each type.
func (c *Client) EventStream(queue string, filter string, streamtype ...event.StreamType) (io.Reader, error) {
x := http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: c.insecureSkipVerify}}, Timeout: 0}
var q url.Values = make(map[string][]string)
types := []string{}
for _, v := range streamtype {
types = append(types, string(v))
}
q["types"] = types
if filter != "" {
q.Set("filter", filter)
}
if queue == "" {
return nil, errors.New("queue name can't have zero value")
}
q.Set("queue", queue)
u := url.URL{Scheme: "https", Host: c.url.Host, Path: filepath.Join(c.url.Path, icingaAPI, "events"), RawQuery: q.Encode()}
req, err := http.NewRequest("POST", u.String(), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.user, c.password)
req.Header.Add("Accept", "application/json")
res, err := x.Do(req)
if err != nil {
return nil, err
}
return res.Body, nil
}