Commit Diff


commit - d762d1d1aeb9ddb5a796ccfe1fefa3edea7e242f
commit + bd3b8a33c54037e66233db80fd1120eb0b6215f1
blob - 36b12a496a825ccc2aef75a719a123d76ce3879a
blob + 25a41af9798781b7bafb1cb5293b17f6399ce79d
--- crud.go
+++ crud.go
@@ -8,7 +8,7 @@ import "fmt"
 // If no hosts match, error wraps ErrNoMatch.
 // To fetch all host, set filter to the empty string ("").
 func (c *Client) Hosts(filter string) ([]Host, error) {
-	objects, err := c.filterObjects("/objects/hosts", filter)
+	objects, err := filter(c, "/objects/hosts", filter)
 	if err != nil {
 		return nil, fmt.Errorf("get hosts filter %s: %w", filter, err)
 	}
@@ -26,7 +26,7 @@ func (c *Client) Hosts(filter string) ([]Host, error) 
 // LookupHost returns the Host identified by name. If no Host is found, error
 // wraps ErrNotExist.
 func (c *Client) LookupHost(name string) (Host, error) {
-	obj, err := c.lookupObject("/objects/hosts/" + name)
+	obj, err := lookup(c, "/objects/hosts/"+name)
 	if err != nil {
 		return Host{}, fmt.Errorf("lookup host %s: %w", name, err)
 	}
@@ -40,7 +40,7 @@ func (c *Client) LookupHost(name string) (Host, error)
 // CreateHost creates host. Some fields of host must be set for successful
 // creation; see the type definition of Host for details.
 func (c *Client) CreateHost(host Host) error {
-	if err := c.createObject(host); err != nil {
+	if err := create(c, host); err != nil {
 		return fmt.Errorf("create host %s: %w", host.Name, err)
 	}
 	return nil
@@ -50,7 +50,7 @@ func (c *Client) CreateHost(host Host) error {
 // depending on the Host are also deleted. If no Host is found, error wraps
 // ErrNotExist.
 func (c *Client) DeleteHost(name string, cascade bool) error {
-	if err := c.deleteObject("/objects/hosts/"+name, cascade); err != nil {
+	if err := delete(c, "/objects/hosts/"+name, cascade); err != nil {
 		return fmt.Errorf("delete host %s: %w", name, err)
 	}
 	return nil
@@ -59,7 +59,7 @@ func (c *Client) DeleteHost(name string, cascade bool)
 // If no services match, error wraps ErrNoMatch.
 // To fetch all service, set filter to the empty string ("").
 func (c *Client) Services(filter string) ([]Service, error) {
-	objects, err := c.filterObjects("/objects/services", filter)
+	objects, err := filter(c, "/objects/services", filter)
 	if err != nil {
 		return nil, fmt.Errorf("get services filter %s: %w", filter, err)
 	}
@@ -77,7 +77,7 @@ func (c *Client) Services(filter string) ([]Service, e
 // LookupService returns the Service identified by name. If no Service is found, error
 // wraps ErrNotExist.
 func (c *Client) LookupService(name string) (Service, error) {
-	obj, err := c.lookupObject("/objects/services/" + name)
+	obj, err := lookup(c, "/objects/services/"+name)
 	if err != nil {
 		return Service{}, fmt.Errorf("lookup service %s: %w", name, err)
 	}
@@ -91,7 +91,7 @@ func (c *Client) LookupService(name string) (Service, 
 // CreateService creates service. Some fields of service must be set for successful
 // creation; see the type definition of Service for details.
 func (c *Client) CreateService(service Service) error {
-	if err := c.createObject(service); err != nil {
+	if err := create(c, service); err != nil {
 		return fmt.Errorf("create service %s: %w", service.Name, err)
 	}
 	return nil
@@ -101,7 +101,7 @@ func (c *Client) CreateService(service Service) error 
 // depending on the Service are also deleted. If no Service is found, error wraps
 // ErrNotExist.
 func (c *Client) DeleteService(name string, cascade bool) error {
-	if err := c.deleteObject("/objects/services/"+name, cascade); err != nil {
+	if err := delete(c, "/objects/services/"+name, cascade); err != nil {
 		return fmt.Errorf("delete service %s: %w", name, err)
 	}
 	return nil
@@ -110,7 +110,7 @@ func (c *Client) DeleteService(name string, cascade bo
 // If no users match, error wraps ErrNoMatch.
 // To fetch all user, set filter to the empty string ("").
 func (c *Client) Users(filter string) ([]User, error) {
-	objects, err := c.filterObjects("/objects/users", filter)
+	objects, err := filter(c, "/objects/users", filter)
 	if err != nil {
 		return nil, fmt.Errorf("get users filter %s: %w", filter, err)
 	}
@@ -128,7 +128,7 @@ func (c *Client) Users(filter string) ([]User, error) 
 // LookupUser returns the User identified by name. If no User is found, error
 // wraps ErrNotExist.
 func (c *Client) LookupUser(name string) (User, error) {
-	obj, err := c.lookupObject("/objects/users/" + name)
+	obj, err := lookup(c, "/objects/users/"+name)
 	if err != nil {
 		return User{}, fmt.Errorf("lookup user %s: %w", name, err)
 	}
@@ -142,7 +142,7 @@ func (c *Client) LookupUser(name string) (User, error)
 // CreateUser creates user. Some fields of user must be set for successful
 // creation; see the type definition of User for details.
 func (c *Client) CreateUser(user User) error {
-	if err := c.createObject(user); err != nil {
+	if err := create(c, user); err != nil {
 		return fmt.Errorf("create user %s: %w", user.Name, err)
 	}
 	return nil
@@ -152,7 +152,7 @@ func (c *Client) CreateUser(user User) error {
 // depending on the User are also deleted. If no User is found, error wraps
 // ErrNotExist.
 func (c *Client) DeleteUser(name string, cascade bool) error {
-	if err := c.deleteObject("/objects/users/"+name, cascade); err != nil {
+	if err := delete(c, "/objects/users/"+name, cascade); err != nil {
 		return fmt.Errorf("delete user %s: %w", name, err)
 	}
 	return nil
@@ -161,7 +161,7 @@ func (c *Client) DeleteUser(name string, cascade bool)
 // If no hostgroups match, error wraps ErrNoMatch.
 // To fetch all hostgroup, set filter to the empty string ("").
 func (c *Client) HostGroups(filter string) ([]HostGroup, error) {
-	objects, err := c.filterObjects("/objects/hostgroups", filter)
+	objects, err := filter(c, "/objects/hostgroups", filter)
 	if err != nil {
 		return nil, fmt.Errorf("get hostgroups filter %s: %w", filter, err)
 	}
@@ -179,7 +179,7 @@ func (c *Client) HostGroups(filter string) ([]HostGrou
 // LookupHostGroup returns the HostGroup identified by name. If no HostGroup is found, error
 // wraps ErrNotExist.
 func (c *Client) LookupHostGroup(name string) (HostGroup, error) {
-	obj, err := c.lookupObject("/objects/hostgroups/" + name)
+	obj, err := lookup(c, "/objects/hostgroups/"+name)
 	if err != nil {
 		return HostGroup{}, fmt.Errorf("lookup hostgroup %s: %w", name, err)
 	}
@@ -193,7 +193,7 @@ func (c *Client) LookupHostGroup(name string) (HostGro
 // CreateHostGroup creates hostgroup. Some fields of hostgroup must be set for successful
 // creation; see the type definition of HostGroup for details.
 func (c *Client) CreateHostGroup(hostgroup HostGroup) error {
-	if err := c.createObject(hostgroup); err != nil {
+	if err := create(c, hostgroup); err != nil {
 		return fmt.Errorf("create hostgroup %s: %w", hostgroup.Name, err)
 	}
 	return nil
@@ -203,7 +203,7 @@ func (c *Client) CreateHostGroup(hostgroup HostGroup) 
 // depending on the HostGroup are also deleted. If no HostGroup is found, error wraps
 // ErrNotExist.
 func (c *Client) DeleteHostGroup(name string, cascade bool) error {
-	if err := c.deleteObject("/objects/hostgroups/"+name, cascade); err != nil {
+	if err := delete(c, "/objects/hostgroups/"+name, cascade); err != nil {
 		return fmt.Errorf("delete hostgroup %s: %w", name, err)
 	}
 	return nil
blob - 98555ec48091ac0edc461dbba2a34659164d40b9
blob + 3ff1455a34b9b8026f314c5115eacfd98a2cf854
--- crud.skel
+++ crud.skel
@@ -2,7 +2,7 @@
 // If no PLURAL match, error wraps ErrNoMatch.
 // To fetch all LOWER, set filter to the empty string ("").
 func (c *Client) TYPEs(filter string) ([]TYPE, error) {
-	objects, err := c.filterObjects("/objects/PLURAL", filter)
+	objects, err := filter(c, "/objects/PLURAL", filter)
 	if err != nil {
 		return nil, fmt.Errorf("get PLURAL filter %s: %w", filter, err)
 	}
@@ -20,7 +20,7 @@ func (c *Client) TYPEs(filter string) ([]TYPE, error) 
 // LookupTYPE returns the TYPE identified by name. If no TYPE is found, error
 // wraps ErrNotExist.
 func (c *Client) LookupTYPE(name string) (TYPE, error) {
-	obj, err := c.lookupObject("/objects/PLURAL/" + name)
+	obj, err := lookup(c, "/objects/PLURAL/" + name)
 	if err != nil {
 		return TYPE{}, fmt.Errorf("lookup LOWER %s: %w", name, err)
 	}
@@ -34,7 +34,7 @@ func (c *Client) LookupTYPE(name string) (TYPE, error)
 // CreateTYPE creates LOWER. Some fields of LOWER must be set for successful
 // creation; see the type definition of TYPE for details.
 func (c *Client) CreateTYPE(LOWER TYPE) error {
-	if err := c.createObject(LOWER); err != nil {
+	if err := create(c, LOWER); err != nil {
 		return fmt.Errorf("create LOWER %s: %w", LOWER.Name, err)
 	}
 	return nil
@@ -44,7 +44,7 @@ func (c *Client) CreateTYPE(LOWER TYPE) error {
 // depending on the TYPE are also deleted. If no TYPE is found, error wraps
 // ErrNotExist.
 func (c *Client) DeleteTYPE(name string, cascade bool) error {
-	if err := c.deleteObject("/objects/PLURAL/"+name, cascade); err != nil {
+	if err := delete(c, "/objects/PLURAL/"+name, cascade); err != nil {
 		return fmt.Errorf("delete LOWER %s: %w", name, err)
 	}
 	return nil
blob - faef3a21b4d7a2dd7c852d98da51a75a11c30355
blob + 09f7c1e4bc305d093c2167ff5d0a457a2405509c
--- object.go
+++ object.go
@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"io"
 	"net/http"
 	"strings"
 )
@@ -17,6 +18,29 @@ type object interface {
 //go:generate ./crud.sh -o crud.go
 
 func (c *Client) lookupObject(objpath string) (object, error) {
+	return lookup(c, objpath)
+}
+
+func (c *Client) filterObjects(objpath, expr string) ([]object, error) {
+	return filter(c, objpath, expr)
+}
+
+func (c *Client) createObject(obj object) error {
+	return create(c, obj)
+}
+
+func (c *Client) deleteObject(objpath string, cascade bool) error {
+	return delete(c, objpath, cascade)
+}
+
+type client interface {
+	get(path, filter string) (*http.Response, error)
+	post(path string, body io.Reader) (*http.Response, error)
+	put(path string, body io.Reader) (*http.Response, error)
+	delete(path string, cascade bool) (*http.Response, error)
+}
+
+func lookup(c client, objpath string) (object, error) {
 	resp, err := c.get(objpath, "")
 	if err != nil {
 		return nil, err
@@ -36,7 +60,7 @@ func (c *Client) lookupObject(objpath string) (object,
 	return objectFromLookup(iresp)
 }
 
-func (c *Client) filterObjects(objpath, expr string) ([]object, error) {
+func filter(c client, objpath, expr string) ([]object, error) {
 	resp, err := c.get(objpath, expr)
 	if err != nil {
 		return nil, err
@@ -55,7 +79,7 @@ func (c *Client) filterObjects(objpath, expr string) (
 	return iresp.Results, nil
 }
 
-func (c *Client) createObject(obj object) error {
+func create(c client, obj object) error {
 	buf := &bytes.Buffer{}
 	switch v := obj.(type) {
 	case Host, Service, User, HostGroup:
@@ -83,7 +107,7 @@ func (c *Client) createObject(obj object) error {
 	return iresp.Error
 }
 
-func (c *Client) deleteObject(objpath string, cascade bool) error {
+func delete(c client, objpath string, cascade bool) error {
 	resp, err := c.delete(objpath, cascade)
 	if err != nil {
 		return err