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 2c4d16ae 2021-12-23 o )
9 2c4d16ae 2021-12-23 o
10 2c4d16ae 2021-12-23 o const versionPrefix = "/v1"
11 2c4d16ae 2021-12-23 o
12 514f2cd1 2022-01-07 o // NewRequest returns an authenticated HTTP request with appropriate header
13 514f2cd1 2022-01-07 o // for sending to an Icinga2 server.
14 514f2cd1 2022-01-07 o func NewRequest(method, url, username, password string, body io.Reader) (*http.Request, error) {
15 2c4d16ae 2021-12-23 o req, err := http.NewRequest(method, url, body)
16 2c4d16ae 2021-12-23 o if err != nil {
17 2c4d16ae 2021-12-23 o return nil, err
18 2c4d16ae 2021-12-23 o }
19 2c4d16ae 2021-12-23 o switch req.Method {
20 76669f9b 2022-01-11 o case http.MethodGet, http.MethodDelete:
21 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
22 2c4d16ae 2021-12-23 o case http.MethodPost, http.MethodPut:
23 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
24 2c4d16ae 2021-12-23 o req.Header.Set("Content-Type", "application/json")
25 dad28b1a 2021-12-30 o default:
26 dad28b1a 2021-12-30 o return nil, fmt.Errorf("new request: unsupported method %s", req.Method)
27 2c4d16ae 2021-12-23 o }
28 514f2cd1 2022-01-07 o req.SetBasicAuth(username, password)
29 2c4d16ae 2021-12-23 o return req, nil
30 2c4d16ae 2021-12-23 o }
31 2c4d16ae 2021-12-23 o
32 6d1ce85e 2022-01-12 o func (c *Client) get(path, filter string) (*http.Response, error) {
33 76669f9b 2022-01-11 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
34 76669f9b 2022-01-11 o if err != nil {
35 76669f9b 2022-01-11 o return nil, err
36 76669f9b 2022-01-11 o }
37 6d1ce85e 2022-01-12 o if filter != "" {
38 6d1ce85e 2022-01-12 o v := url.Values{}
39 6d1ce85e 2022-01-12 o v.Set("filter", filter)
40 6c703620 2022-01-12 o u.RawQuery = v.Encode()
41 6d1ce85e 2022-01-12 o }
42 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodGet, u.String(), c.username, c.password, nil)
43 76669f9b 2022-01-11 o if err != nil {
44 76669f9b 2022-01-11 o return nil, err
45 76669f9b 2022-01-11 o }
46 76669f9b 2022-01-11 o return c.Do(req)
47 76669f9b 2022-01-11 o }
48 76669f9b 2022-01-11 o
49 2c4d16ae 2021-12-23 o func (c *Client) post(path string, body io.Reader) (*http.Response, error) {
50 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
51 514f2cd1 2022-01-07 o req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
52 2c4d16ae 2021-12-23 o if err != nil {
53 2c4d16ae 2021-12-23 o return nil, err
54 2c4d16ae 2021-12-23 o }
55 514f2cd1 2022-01-07 o return c.Do(req)
56 2c4d16ae 2021-12-23 o }
57 2c4d16ae 2021-12-23 o
58 76669f9b 2022-01-11 o func (c *Client) put(path string, body io.Reader) (*http.Response, error) {
59 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
60 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodPut, url, c.username, c.password, body)
61 2c4d16ae 2021-12-23 o if err != nil {
62 76669f9b 2022-01-11 o return nil, err
63 2c4d16ae 2021-12-23 o }
64 76669f9b 2022-01-11 o return c.Do(req)
65 2c4d16ae 2021-12-23 o }
66 2c4d16ae 2021-12-23 o
67 02a1a100 2022-01-18 o func (c *Client) delete(path string, cascade bool) (*http.Response, error) {
68 02a1a100 2022-01-18 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
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 02a1a100 2022-01-18 o if cascade {
73 02a1a100 2022-01-18 o v := url.Values{}
74 02a1a100 2022-01-18 o v.Set("cascade", "1")
75 02a1a100 2022-01-18 o u.RawQuery = v.Encode()
76 02a1a100 2022-01-18 o }
77 02a1a100 2022-01-18 o req, err := NewRequest(http.MethodDelete, u.String(), c.username, c.password, nil)
78 02a1a100 2022-01-18 o if err != nil {
79 02a1a100 2022-01-18 o return nil, err
80 02a1a100 2022-01-18 o }
81 76669f9b 2022-01-11 o return c.Do(req)
82 2c4d16ae 2021-12-23 o }