icinga

Icinga2 API client in Go
git clone https://git.olowe.co/icinga
Log | Files | Refs | README | LICENSE

commit 60c115fb1c7683cea9553380eddaeb0033dd17bf
parent fc8956331f70b23ffe89c8307de77bbc0ecccb39
Author: Oliver Lowe <o@olowe.co>
Date:   Wed, 12 Jan 2022 08:16:37 +1100

Merge filter and all objects methods

Getting all objects is the same as getting objects with a zero filter.

Diffstat:
Mhost.go | 2+-
Mobject.go | 33+++++++++++----------------------
Muser.go | 2+-
3 files changed, 13 insertions(+), 24 deletions(-)

diff --git a/host.go b/host.go @@ -71,7 +71,7 @@ func (h Host) MarshalJSON() ([]byte, error) { // Hosts returns all Hosts in the Icinga2 configuration. func (c *Client) Hosts() ([]Host, error) { - objects, err := c.allObjects("/objects/hosts") + objects, err := c.filterObjects("/objects/hosts", "") if err != nil { return nil, fmt.Errorf("get all hosts: %w", err) } diff --git a/object.go b/object.go @@ -35,35 +35,24 @@ func (c *Client) lookupObject(objpath string) (object, error) { return objectFromLookup(iresp) } -func (c *Client) allObjects(objpath string) ([]object, error) { - resp, err := c.get(objpath) - if err != nil { - return nil, err - } - defer resp.Body.Close() - iresp, err := parseResponse(resp.Body) - if err != nil { - return nil, err - } else if iresp.Error != nil { - return nil, iresp.Error - } else if resp.StatusCode != http.StatusOK { - return nil, errors.New(resp.Status) - } - return iresp.Results, nil -} - func (c *Client) filterObjects(objpath, expr string) ([]object, error) { - resp, err := c.getFilter(objpath, expr) + var resp *http.Response + var err error + if expr == "" { + resp, err = c.get(objpath) + } else { + resp, err = c.getFilter(objpath, expr) + if resp.StatusCode == http.StatusNotFound { + return nil, ErrNoMatch + } + } if err != nil { return nil, err } defer resp.Body.Close() - if resp.StatusCode == http.StatusNotFound { - return nil, ErrNoMatch - } iresp, err := parseResponse(resp.Body) if err != nil { - return nil, err + return nil, fmt.Errorf("parse response: %v", err) } else if iresp.Error != nil { return nil, iresp.Error } else if resp.StatusCode != http.StatusOK { diff --git a/user.go b/user.go @@ -49,7 +49,7 @@ func (u User) attrs() map[string]interface{} { } func (c *Client) Users() ([]User, error) { - objects, err := c.allObjects("/objects/users") + objects, err := c.filterObjects("/objects/users", "") if err != nil { return nil, fmt.Errorf("get all users: %w", err) }