github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/resource.go (about) 1 package ccv2 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "os" 7 "strconv" 8 9 "code.cloudfoundry.org/cli/api/cloudcontroller" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 11 ) 12 13 // Resource represents a Cloud Controller Resource. 14 type Resource struct { 15 16 // Filename is the name of the resource. 17 Filename string `json:"fn"` 18 19 // Mode is the operating system file mode (aka file permissions) of the 20 // resource. 21 Mode os.FileMode `json:"mode"` 22 23 // SHA1 represents the SHA-1 hash of the resource. 24 SHA1 string `json:"sha1"` 25 26 // Size represents the file size of the resource. 27 Size int64 `json:"size"` 28 } 29 30 // MarshalJSON converts a resource into a Cloud Controller Resource. 31 func (r Resource) MarshalJSON() ([]byte, error) { 32 var ccResource struct { 33 Filename string `json:"fn,omitempty"` 34 Mode string `json:"mode,omitempty"` 35 SHA1 string `json:"sha1"` 36 Size int64 `json:"size"` 37 } 38 39 ccResource.Filename = r.Filename 40 ccResource.Size = r.Size 41 ccResource.SHA1 = r.SHA1 42 ccResource.Mode = strconv.FormatUint(uint64(r.Mode), 8) 43 return json.Marshal(ccResource) 44 } 45 46 // UnmarshalJSON helps unmarshal a Cloud Controller Resource response. 47 func (r *Resource) UnmarshalJSON(data []byte) error { 48 var ccResource struct { 49 Filename string `json:"fn,omitempty"` 50 Mode string `json:"mode,omitempty"` 51 SHA1 string `json:"sha1"` 52 Size int64 `json:"size"` 53 } 54 55 err := cloudcontroller.DecodeJSON(data, &ccResource) 56 if err != nil { 57 return err 58 } 59 60 r.Filename = ccResource.Filename 61 r.Size = ccResource.Size 62 r.SHA1 = ccResource.SHA1 63 mode, err := strconv.ParseUint(ccResource.Mode, 8, 32) 64 if err != nil { 65 return err 66 } 67 68 r.Mode = os.FileMode(mode) 69 return nil 70 } 71 72 // UpdateResourceMatch returns the resources that exist on the cloud foundry instance 73 // from the set of resources given. 74 func (client *Client) UpdateResourceMatch(resourcesToMatch []Resource) ([]Resource, Warnings, error) { 75 body, err := json.Marshal(resourcesToMatch) 76 if err != nil { 77 return nil, nil, err 78 } 79 80 request, err := client.newHTTPRequest(requestOptions{ 81 RequestName: internal.PutResourceMatchRequest, 82 Body: bytes.NewReader(body), 83 }) 84 if err != nil { 85 return nil, nil, err 86 } 87 88 request.Header.Set("Content-Type", "application/json") 89 90 var matchedResources []Resource 91 response := cloudcontroller.Response{ 92 DecodeJSONResponseInto: &matchedResources, 93 } 94 95 err = client.connection.Make(request, &response) 96 return matchedResources, response.Warnings, err 97 }