11 50558780 2022-01-06 o // Host represents a Host object.
12 6e415953 2022-01-06 o type Host struct {
13 50558780 2022-01-06 o Name string `json:"name"`
14 50558780 2022-01-06 o Address string `json:"address"`
15 50558780 2022-01-06 o Address6 string `json:"address6"`
16 50558780 2022-01-06 o Groups []string `json:"groups"`
17 50558780 2022-01-06 o State int `json:"state"`
18 50558780 2022-01-06 o CheckCommand string `json:"check_command"`
19 50558780 2022-01-06 o DisplayName string `json:"display_name"`
22 50558780 2022-01-06 o type hostresults struct {
23 50558780 2022-01-06 o Results []hostresult `json:"results"`
27 50558780 2022-01-06 o type hostresult struct {
28 50558780 2022-01-06 o Host Host `json:"attrs"`
32 6e415953 2022-01-06 o var ErrNoHost = errors.New("no such host")
34 50558780 2022-01-06 o func (h Host) MarshalJSON() ([]byte, error) {
35 50558780 2022-01-06 o type Attrs struct {
36 50558780 2022-01-06 o Address string `json:"address"`
37 50558780 2022-01-06 o CheckCommand string `json:"check_command"`
38 50558780 2022-01-06 o DisplayName string `json:"display_name"`
40 50558780 2022-01-06 o type host struct {
41 50558780 2022-01-06 o Attrs Attrs `json:"attrs"`
43 50558780 2022-01-06 o jhost := &host{
45 50558780 2022-01-06 o Address: h.Address,
46 50558780 2022-01-06 o CheckCommand: h.CheckCommand,
47 50558780 2022-01-06 o DisplayName: h.DisplayName,
50 50558780 2022-01-06 o return json.Marshal(jhost)
53 50558780 2022-01-06 o // Hosts returns all Hosts in the Icinga2 configuration.
54 6e415953 2022-01-06 o func (c *Client) Hosts() ([]Host, error) {
55 50558780 2022-01-06 o resp, err := c.get("/objects/hosts")
56 6e415953 2022-01-06 o if err != nil {
57 6e415953 2022-01-06 o return nil, err
59 50558780 2022-01-06 o defer resp.Body.Close()
60 50558780 2022-01-06 o var res hostresults
61 50558780 2022-01-06 o if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
62 50558780 2022-01-06 o return nil, err
64 50558780 2022-01-06 o if res.Err() != nil {
65 50558780 2022-01-06 o return nil, res.Err()
67 50558780 2022-01-06 o var hosts []Host
68 50558780 2022-01-06 o for _, r := range res.Results {
69 50558780 2022-01-06 o hosts = append(hosts, r.Host)
71 50558780 2022-01-06 o return hosts, nil
74 50558780 2022-01-06 o // LookupHost returns the Host identified by name.
75 50558780 2022-01-06 o // If no Host is found, error wraps ErrNoHost.
76 6e415953 2022-01-06 o func (c *Client) LookupHost(name string) (Host, error) {
77 6e415953 2022-01-06 o resp, err := c.get("/objects/hosts/" + name)
78 6e415953 2022-01-06 o if err != nil {
79 6e415953 2022-01-06 o return Host{}, err
81 6e415953 2022-01-06 o if resp.StatusCode == http.StatusNotFound {
82 6e415953 2022-01-06 o return Host{}, fmt.Errorf("lookup %s: %w", name, ErrNoHost)
84 6e415953 2022-01-06 o return Host{}, err
87 50558780 2022-01-06 o // CreateHost creates the Host host.
88 50558780 2022-01-06 o // The Name and CheckCommand fields of host must be non-zero.
89 50558780 2022-01-06 o func (c *Client) CreateHost(host Host) error {
90 50558780 2022-01-06 o buf := &bytes.Buffer{}
91 50558780 2022-01-06 o if err := json.NewEncoder(buf).Encode(host); err != nil {
94 50558780 2022-01-06 o if err := c.put("/objects/hosts/"+host.Name, buf); err != nil {
95 50558780 2022-01-06 o return fmt.Errorf("create host %s: %w", host.Name, err)
100 50558780 2022-01-06 o // DeleteHost deletes the Host identified by name.
101 50558780 2022-01-06 o // If no Host is found, error wraps ErrNoObject.
102 6e415953 2022-01-06 o func (c *Client) DeleteHost(name string) error {
103 6e415953 2022-01-06 o if err := c.delete("/objects/hosts/" + name); err != nil {
104 6e415953 2022-01-06 o return fmt.Errorf("delete host %s: %w", name, err)