-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
84 lines (65 loc) · 1.81 KB
/
asset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package client
import (
"encoding/json"
"fmt"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types"
)
var assetsPath = createNSBasePath(coreAPIGroup, coreAPIVersion, "assets")
// ListAssets fetches a list of asset resources from the backend
func (client *RestClient) ListAssets(namespace string, options *ListOptions) ([]corev2.Asset, error) {
var assets []corev2.Asset
if err := client.List(assetsPath(namespace), &assets, options); err != nil {
return assets, err
}
return assets, nil
}
// FetchAsset fetches an asset resource from the backend
func (client *RestClient) FetchAsset(name string) (*types.Asset, error) {
var asset types.Asset
path := assetsPath(client.config.Namespace(), name)
res, err := client.R().Get(path)
if err != nil {
return &asset, fmt.Errorf("GET %q: %s", path, err)
}
if res.StatusCode() >= 400 {
return &asset, UnmarshalError(res)
}
err = json.Unmarshal(res.Body(), &asset)
return &asset, err
}
// CreateAsset creates an asset resource from the backend
func (client *RestClient) CreateAsset(asset *types.Asset) error {
bytes, err := json.Marshal(asset)
if err != nil {
return err
}
path := assetsPath(asset.Namespace)
res, err := client.R().SetBody(bytes).Post(path)
if err != nil {
return err
}
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// UpdateAsset updates an asset resource from the backend
func (client *RestClient) UpdateAsset(asset *types.Asset) (err error) {
bytes, err := json.Marshal(asset)
if err != nil {
return err
}
path := assetsPath(asset.Namespace, asset.Name)
res, err := client.R().SetBody(bytes).Put(path)
if err != nil {
return fmt.Errorf("PUT %q: %s", path, err)
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}