commit 82fc97ff8eb537c28bce19af5ffb697773bd200c
parent 6d1ce85e966615f3886d8a2431b3e859d3008db6
Author: Oliver Lowe <o@olowe.co>
Date: Wed, 12 Jan 2022 10:34:07 +1100
Merge Hosts and FilterHosts
Getting all Hosts is the same as calling FilterHosts with an empty
filter anyway.
Diffstat:
3 files changed, 8 insertions(+), 25 deletions(-)
diff --git a/host.go b/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) {
diff --git a/host_test.go b/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)
}
diff --git a/object.go b/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
}