commit - d762d1d1aeb9ddb5a796ccfe1fefa3edea7e242f
commit + bd3b8a33c54037e66233db80fd1120eb0b6215f1
blob - 36b12a496a825ccc2aef75a719a123d76ce3879a
blob + 25a41af9798781b7bafb1cb5293b17f6399ce79d
--- crud.go
+++ crud.go
// 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)
}
// 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)
}
// 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
// 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
// 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)
}
// 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)
}
// 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
// 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
// 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)
}
// 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)
}
// 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
// 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
// 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)
}
// 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)
}
// 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
// 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
// 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)
}
// 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)
}
// 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
// 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
"encoding/json"
"errors"
"fmt"
+ "io"
"net/http"
"strings"
)
//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
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
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:
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