12 type object interface {
17 //go:generate ./crud.sh -o crud.go
19 func (c *Client) lookupObject(objpath string) (object, error) {
20 resp, err := c.get(objpath, "")
24 defer resp.Body.Close()
25 if resp.StatusCode == http.StatusNotFound {
26 return nil, ErrNotExist
28 iresp, err := parseResponse(resp.Body)
30 return nil, fmt.Errorf("parse response: %v", err)
31 } else if iresp.Error != nil {
32 return nil, iresp.Error
33 } else if resp.StatusCode != http.StatusOK {
34 return nil, errors.New(resp.Status)
36 return objectFromLookup(iresp)
39 func (c *Client) filterObjects(objpath, expr string) ([]object, error) {
40 resp, err := c.get(objpath, expr)
44 defer resp.Body.Close()
45 if expr != "" && resp.StatusCode == http.StatusNotFound {
46 return nil, ErrNoMatch
49 iresp, err := parseResponse(resp.Body)
51 return nil, fmt.Errorf("parse response: %v", err)
52 } else if iresp.Error != nil {
53 return nil, iresp.Error
54 } else if resp.StatusCode != http.StatusOK {
55 return nil, errors.New(resp.Status)
57 return iresp.Results, nil
60 func (c *Client) createObject(obj object) error {
61 buf := &bytes.Buffer{}
62 switch v := obj.(type) {
63 case Host, Service, User, HostGroup:
64 if err := json.NewEncoder(buf).Encode(v); err != nil {
68 return fmt.Errorf("create type %T unsupported", v)
70 resp, err := c.put(obj.path(), buf)
74 if resp.StatusCode == http.StatusOK {
77 defer resp.Body.Close()
78 iresp, err := parseResponse(resp.Body)
80 return fmt.Errorf("parse response: %v", err)
82 if strings.Contains(iresp.Error.Error(), "already exists") {
88 func (c *Client) deleteObject(objpath string, cascade bool) error {
89 resp, err := c.delete(objpath, cascade)
93 defer resp.Body.Close()
94 if resp.StatusCode == http.StatusOK {
96 } else if resp.StatusCode == http.StatusNotFound {
99 iresp, err := parseResponse(resp.Body)
101 return fmt.Errorf("parse response: %v", err)