Blame


1 2c4d16ae 2021-12-23 o package icinga
2 2c4d16ae 2021-12-23 o
3 2c4d16ae 2021-12-23 o import (
4 6e415953 2022-01-06 o "fmt"
5 2c4d16ae 2021-12-23 o "io"
6 2c4d16ae 2021-12-23 o "net/http"
7 76669f9b 2022-01-11 o "net/url"
8 9ffce56b 2022-01-31 o "strings"
9 2c4d16ae 2021-12-23 o )
10 2c4d16ae 2021-12-23 o
11 2c4d16ae 2021-12-23 o const versionPrefix = "/v1"
12 2c4d16ae 2021-12-23 o
13 514f2cd1 2022-01-07 o // NewRequest returns an authenticated HTTP request with appropriate header
14 514f2cd1 2022-01-07 o // for sending to an Icinga2 server.
15 514f2cd1 2022-01-07 o func NewRequest(method, url, username, password string, body io.Reader) (*http.Request, error) {
16 2c4d16ae 2021-12-23 o req, err := http.NewRequest(method, url, body)
17 2c4d16ae 2021-12-23 o if err != nil {
18 2c4d16ae 2021-12-23 o return nil, err
19 2c4d16ae 2021-12-23 o }
20 2c4d16ae 2021-12-23 o switch req.Method {
21 76669f9b 2022-01-11 o case http.MethodGet, http.MethodDelete:
22 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
23 2c4d16ae 2021-12-23 o case http.MethodPost, http.MethodPut:
24 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
25 2c4d16ae 2021-12-23 o req.Header.Set("Content-Type", "application/json")
26 dad28b1a 2021-12-30 o default:
27 dad28b1a 2021-12-30 o return nil, fmt.Errorf("new request: unsupported method %s", req.Method)
28 2c4d16ae 2021-12-23 o }
29 514f2cd1 2022-01-07 o req.SetBasicAuth(username, password)
30 2c4d16ae 2021-12-23 o return req, nil
31 2c4d16ae 2021-12-23 o }
32 2c4d16ae 2021-12-23 o
33 9ffce56b 2022-01-31 o // filterEncode url-encodes the filter expression expr in the required
34 9ffce56b 2022-01-31 o // format to be included in a request. Notably, spaces need to be encoded
35 9ffce56b 2022-01-31 o // as "%20", not "+" as returned by the url package.
36 9ffce56b 2022-01-31 o func filterEncode(expr string) string {
37 9ffce56b 2022-01-31 o v := url.Values{}
38 9ffce56b 2022-01-31 o v.Set("filter", expr)
39 9ffce56b 2022-01-31 o return strings.ReplaceAll(v.Encode(), "+", "%20")
40 9ffce56b 2022-01-31 o }
41 9ffce56b 2022-01-31 o
42 6d1ce85e 2022-01-12 o func (c *Client) get(path, filter string) (*http.Response, error) {
43 76669f9b 2022-01-11 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
44 76669f9b 2022-01-11 o if err != nil {
45 76669f9b 2022-01-11 o return nil, err
46 76669f9b 2022-01-11 o }
47 6d1ce85e 2022-01-12 o if filter != "" {
48 9ffce56b 2022-01-31 o u.RawQuery = filterEncode(filter)
49 6d1ce85e 2022-01-12 o }
50 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodGet, u.String(), c.username, c.password, nil)
51 76669f9b 2022-01-11 o if err != nil {
52 76669f9b 2022-01-11 o return nil, err
53 76669f9b 2022-01-11 o }
54 76669f9b 2022-01-11 o return c.Do(req)
55 76669f9b 2022-01-11 o }
56 76669f9b 2022-01-11 o
57 2c4d16ae 2021-12-23 o func (c *Client) post(path string, body io.Reader) (*http.Response, error) {
58 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
59 514f2cd1 2022-01-07 o req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
60 2c4d16ae 2021-12-23 o if err != nil {
61 2c4d16ae 2021-12-23 o return nil, err
62 2c4d16ae 2021-12-23 o }
63 514f2cd1 2022-01-07 o return c.Do(req)
64 2c4d16ae 2021-12-23 o }
65 2c4d16ae 2021-12-23 o
66 76669f9b 2022-01-11 o func (c *Client) put(path string, body io.Reader) (*http.Response, error) {
67 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
68 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodPut, url, c.username, c.password, body)
69 2c4d16ae 2021-12-23 o if err != nil {
70 76669f9b 2022-01-11 o return nil, err
71 2c4d16ae 2021-12-23 o }
72 76669f9b 2022-01-11 o return c.Do(req)
73 2c4d16ae 2021-12-23 o }
74 2c4d16ae 2021-12-23 o
75 02a1a100 2022-01-18 o func (c *Client) delete(path string, cascade bool) (*http.Response, error) {
76 02a1a100 2022-01-18 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
77 2c4d16ae 2021-12-23 o if err != nil {
78 76669f9b 2022-01-11 o return nil, err
79 2c4d16ae 2021-12-23 o }
80 02a1a100 2022-01-18 o if cascade {
81 02a1a100 2022-01-18 o v := url.Values{}
82 02a1a100 2022-01-18 o v.Set("cascade", "1")
83 02a1a100 2022-01-18 o u.RawQuery = v.Encode()
84 02a1a100 2022-01-18 o }
85 02a1a100 2022-01-18 o req, err := NewRequest(http.MethodDelete, u.String(), c.username, c.password, nil)
86 02a1a100 2022-01-18 o if err != nil {
87 02a1a100 2022-01-18 o return nil, err
88 02a1a100 2022-01-18 o }
89 76669f9b 2022-01-11 o return c.Do(req)
90 2c4d16ae 2021-12-23 o }