12 type object interface {
14 attrs() map[string]interface{}
18 func (c *Client) lookupObject(objpath string) (object, error) {
19 resp, err := c.get(objpath)
23 defer resp.Body.Close()
24 if resp.StatusCode == http.StatusNotFound {
25 return nil, ErrNotExist
27 iresp, err := parseResponse(resp.Body)
29 return nil, fmt.Errorf("parse response: %v", err)
30 } else if iresp.Error != nil {
31 return nil, iresp.Error
32 } else if resp.StatusCode != http.StatusOK {
33 return nil, errors.New(resp.Status)
35 return objectFromLookup(iresp)
38 func (c *Client) filterObjects(objpath, expr string) ([]object, error) {
39 resp, err := c.get(objpath, expr)
43 defer resp.Body.Close()
44 if expr != "" && resp.StatusCode == http.StatusNotFound {
45 return nil, ErrNoMatch
48 iresp, err := parseResponse(resp.Body)
50 return nil, fmt.Errorf("parse response: %v", err)
51 } else if iresp.Error != nil {
52 return nil, iresp.Error
53 } else if resp.StatusCode != http.StatusOK {
54 return nil, errors.New(resp.Status)
56 return iresp.Results, nil
59 func (c *Client) createObject(obj object) error {
60 buf := &bytes.Buffer{}
61 switch v := obj.(type) {
62 case Host, Service, User:
63 if err := json.NewEncoder(buf).Encode(v); err != nil {
67 return fmt.Errorf("create type %T unsupported", v)
69 resp, err := c.put(obj.path(), buf)
73 if resp.StatusCode == http.StatusOK {
76 defer resp.Body.Close()
77 iresp, err := parseResponse(resp.Body)
79 return fmt.Errorf("parse response: %v", err)
81 if strings.Contains(iresp.Error.Error(), "already exists") {
87 func (c *Client) deleteObject(objpath string) error {
88 resp, err := c.delete(objpath)
92 defer resp.Body.Close()
93 if resp.StatusCode == http.StatusOK {
95 } else if resp.StatusCode == http.StatusNotFound {
98 iresp, err := parseResponse(resp.Body)
100 return fmt.Errorf("parse response: %v", err)