forked from cloudfoundry/cli
/
resource.go
97 lines (79 loc) · 2.42 KB
/
resource.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
85
86
87
88
89
90
91
92
93
94
95
96
97
package ccv2
import (
"bytes"
"encoding/json"
"os"
"strconv"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Resource represents a Cloud Controller Resource.
type Resource struct {
// Filename is the name of the resource.
Filename string `json:"fn"`
// Mode is the operating system file mode (aka file permissions) of the
// resource.
Mode os.FileMode `json:"mode"`
// SHA1 represents the SHA-1 hash of the resource.
SHA1 string `json:"sha1"`
// Size represents the file size of the resource.
Size int64 `json:"size"`
}
// MarshalJSON converts a resource into a Cloud Controller Resource.
func (r Resource) MarshalJSON() ([]byte, error) {
var ccResource struct {
Filename string `json:"fn,omitempty"`
Mode string `json:"mode,omitempty"`
SHA1 string `json:"sha1"`
Size int64 `json:"size"`
}
ccResource.Filename = r.Filename
ccResource.Size = r.Size
ccResource.SHA1 = r.SHA1
ccResource.Mode = strconv.FormatUint(uint64(r.Mode), 8)
return json.Marshal(ccResource)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Resource response.
func (r *Resource) UnmarshalJSON(data []byte) error {
var ccResource struct {
Filename string `json:"fn,omitempty"`
Mode string `json:"mode,omitempty"`
SHA1 string `json:"sha1"`
Size int64 `json:"size"`
}
err := cloudcontroller.DecodeJSON(data, &ccResource)
if err != nil {
return err
}
r.Filename = ccResource.Filename
r.Size = ccResource.Size
r.SHA1 = ccResource.SHA1
mode, err := strconv.ParseUint(ccResource.Mode, 8, 32)
if err != nil {
return err
}
r.Mode = os.FileMode(mode)
return nil
}
// UpdateResourceMatch returns the resources that exist on the cloud foundry instance
// from the set of resources given.
func (client *Client) UpdateResourceMatch(resourcesToMatch []Resource) ([]Resource, Warnings, error) {
body, err := json.Marshal(resourcesToMatch)
if err != nil {
return nil, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutResourceMatchRequest,
Body: bytes.NewReader(body),
})
if err != nil {
return nil, nil, err
}
request.Header.Set("Content-Type", "application/json")
var matchedResources []Resource
response := cloudcontroller.Response{
DecodeJSONResponseInto: &matchedResources,
}
err = client.connection.Make(request, &response)
return matchedResources, response.Warnings, err
}