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 2c4d16ae 2021-12-23 o func (c *Client) get(path string) (*http.Response, error) {
33 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
34 514f2cd1 2022-01-07 o req, err := NewRequest(http.MethodGet, url, c.username, c.password, nil)
35 2c4d16ae 2021-12-23 o if err != nil {
36 2c4d16ae 2021-12-23 o return nil, err
37 2c4d16ae 2021-12-23 o }
38 514f2cd1 2022-01-07 o return c.Do(req)
39 2c4d16ae 2021-12-23 o }
40 2c4d16ae 2021-12-23 o
41 76669f9b 2022-01-11 o func (c *Client) getFilter(path, filter string) (*http.Response, error) {
42 76669f9b 2022-01-11 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
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 v := url.Values{}
47 76669f9b 2022-01-11 o v.Set("filter", filter)
48 76669f9b 2022-01-11 o u.RawQuery = v.Encode()
49 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodGet, u.String(), c.username, c.password, nil)
50 76669f9b 2022-01-11 o if err != nil {
51 76669f9b 2022-01-11 o return nil, err
52 76669f9b 2022-01-11 o }
53 76669f9b 2022-01-11 o return c.Do(req)
54 76669f9b 2022-01-11 o }
55 76669f9b 2022-01-11 o
56 2c4d16ae 2021-12-23 o func (c *Client) post(path string, body io.Reader) (*http.Response, error) {
57 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
58 514f2cd1 2022-01-07 o req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
59 2c4d16ae 2021-12-23 o if err != nil {
60 2c4d16ae 2021-12-23 o return nil, err
61 2c4d16ae 2021-12-23 o }
62 514f2cd1 2022-01-07 o return c.Do(req)
63 2c4d16ae 2021-12-23 o }
64 2c4d16ae 2021-12-23 o
65 76669f9b 2022-01-11 o func (c *Client) put(path string, body io.Reader) (*http.Response, error) {
66 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
67 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodPut, url, c.username, c.password, body)
68 2c4d16ae 2021-12-23 o if err != nil {
69 76669f9b 2022-01-11 o return nil, err
70 2c4d16ae 2021-12-23 o }
71 76669f9b 2022-01-11 o return c.Do(req)
72 2c4d16ae 2021-12-23 o }
73 2c4d16ae 2021-12-23 o
74 76669f9b 2022-01-11 o func (c *Client) delete(path string) (*http.Response, error) {
75 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
76 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodDelete, url, c.username, c.password, nil)
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 76669f9b 2022-01-11 o return c.Do(req)
81 2c4d16ae 2021-12-23 o }