8 // Host represents a Host object.
10 Name string `json:"name"`
11 Address string `json:"address"`
12 Address6 string `json:"address6"`
13 Groups []string `json:"groups"`
14 State HostState `json:"state"`
15 CheckCommand string `json:"check_command"`
16 DisplayName string `json:"display_name"`
22 HostUp HostState = 0 + iota
27 func (s HostState) String() string {
34 return "HostUnreachable"
36 return "unhandled host state"
39 func (h Host) name() string {
43 func (h Host) path() string {
44 return "/objects/hosts/" + h.Name
47 func (h Host) attrs() map[string]interface{} {
48 m := make(map[string]interface{})
49 m["display_name"] = h.DisplayName
53 func (h Host) MarshalJSON() ([]byte, error) {
55 Address string `json:"address"`
56 CheckCommand string `json:"check_command"`
57 DisplayName string `json:"display_name"`
60 Attrs Attrs `json:"attrs"`
65 CheckCommand: h.CheckCommand,
66 DisplayName: h.DisplayName,
69 return json.Marshal(jhost)
72 // Hosts returns all Hosts in the Icinga2 configuration.
73 func (c *Client) Hosts() ([]Host, error) {
74 objects, err := c.filterObjects("/objects/hosts", "")
76 return nil, fmt.Errorf("get all hosts: %w", err)
79 for _, o := range objects {
82 return nil, fmt.Errorf("get all hosts: %T in response", v)
84 hosts = append(hosts, v)
89 // FilterHosts returns any matching hosts after applying the filter
90 // expression expr. If no hosts match, an empty slice and an error wrapping
91 // ErrNoMatch is returned.
92 func (c *Client) FilterHosts(expr string) ([]Host, error) {
93 objects, err := c.filterObjects("/objects/hosts", expr)
95 return nil, fmt.Errorf("filter hosts %q: %w", expr, err)
98 for _, o := range objects {
101 return nil, fmt.Errorf("filter hosts %q: %T in response", expr, v)
103 hosts = append(hosts, v)
108 // LookupHost returns the Host identified by name. If no Host is found,
109 // error wraps ErrNotExist.
110 func (c *Client) LookupHost(name string) (Host, error) {
111 obj, err := c.lookupObject("/objects/hosts/" + name)
113 return Host{}, fmt.Errorf("lookup %s: %w", name, err)
117 return Host{}, fmt.Errorf("lookup %s: result type %T is not host", name, obj)
122 // CreateHost creates the Host host.
123 // The Name and CheckCommand fields of host must be non-zero.
124 func (c *Client) CreateHost(host Host) error {
125 if err := c.createObject(host); err != nil {
126 return fmt.Errorf("create host %s: %w", host.Name, err)
131 // DeleteHost deletes the Host identified by name.
132 // If no Host is found, error wraps ErrNotExist.
133 func (c *Client) DeleteHost(name string) error {
134 if err := c.deleteObject("/objects/hosts/" + name); err != nil {
135 return fmt.Errorf("delete host %s: %w", name, err)