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