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