commit 82fc97ff8eb537c28bce19af5ffb697773bd200c from: Oliver Lowe date: Wed Jan 12 01:22:30 2022 UTC Merge Hosts and FilterHosts Getting all Hosts is the same as calling FilterHosts with an empty filter anyway. commit - 6d1ce85e966615f3886d8a2431b3e859d3008db6 commit + 82fc97ff8eb537c28bce19af5ffb697773bd200c blob - 4cf94afd9784ee9e129e44fc674ca65474ae77d8 blob + d65e59b24fb1e7f2a481c9205a661f34ad299e46 --- host.go +++ host.go @@ -69,11 +69,13 @@ func (h Host) MarshalJSON() ([]byte, error) { return json.Marshal(jhost) } -// Hosts returns all Hosts in the Icinga2 configuration. -func (c *Client) Hosts() ([]Host, error) { - objects, err := c.filterObjects("/objects/hosts", "") +// Hosts returns Hosts matching the filter expression filter. +// If no hosts match, error wraps ErrNoMatch. +// To fetch all hosts, set filter to the empty string (""). +func (c *Client) Hosts(filter string) ([]Host, error) { + objects, err := c.filterObjects("/objects/hosts", filter) if err != nil { - return nil, fmt.Errorf("get all hosts: %w", err) + return nil, fmt.Errorf("get hosts filter %q: %w", filter, err) } var hosts []Host for _, o := range objects { @@ -86,25 +88,6 @@ func (c *Client) Hosts() ([]Host, error) { return hosts, nil } -// FilterHosts returns any matching hosts after applying the filter -// expression expr. If no hosts match, an empty slice and an error wrapping -// ErrNoMatch is returned. -func (c *Client) FilterHosts(expr string) ([]Host, error) { - objects, err := c.filterObjects("/objects/hosts", expr) - if err != nil { - return nil, fmt.Errorf("filter hosts %q: %w", expr, err) - } - var hosts []Host - for _, o := range objects { - v, ok := o.(Host) - if !ok { - return nil, fmt.Errorf("filter hosts %q: %T in response", expr, v) - } - hosts = append(hosts, v) - } - return hosts, nil -} - // LookupHost returns the Host identified by name. If no Host is found, // error wraps ErrNotExist. func (c *Client) LookupHost(name string) (Host, error) { blob - c306c079c0887aeabf9a9a65c029f758d2555e5c blob + e8308305a7cde15ca9c8c2ca193ebd48305c0adf --- host_test.go +++ host_test.go @@ -52,7 +52,7 @@ func TestFilter(t *testing.T) { } t.Logf("created host %s", h.Name) } - hosts, err := client.FilterHosts("match(\"*example.org\", host.name)") + hosts, err := client.Hosts("match(\"*example.org\", host.name)") if err != nil { t.Fatal(err) } blob - 43a5c780fceabdefc8a7482d3f3f92244511a925 blob + a09a08269b2f988f03f4099e057e222fde2177ac --- object.go +++ object.go @@ -16,7 +16,7 @@ type object interface { } func (c *Client) lookupObject(objpath string) (object, error) { - resp, err := c.get(objpath) + resp, err := c.get(objpath, "") if err != nil { return nil, err }