github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/resource.go (about) 1 package ccv3 2 3 import ( 4 "encoding/json" 5 "os" 6 "strconv" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 10 ) 11 12 type Checksum struct { 13 Value string `json:"value"` 14 } 15 16 type Resource struct { 17 // FilePath is the path of the resource. 18 FilePath string `json:"path"` 19 20 // Mode is the operating system file mode (aka file permissions) of the 21 // resource. 22 Mode os.FileMode `json:"mode"` 23 24 // SHA1 represents the SHA-1 hash of the resource. 25 Checksum Checksum `json:"checksum"` 26 27 // Size represents the file size of the resource. 28 SizeInBytes int64 `json:"size_in_bytes"` 29 } 30 31 // MarshalJSON converts a resource into a Cloud Controller Resource. 32 func (r Resource) MarshalJSON() ([]byte, error) { 33 var ccResource struct { 34 FilePath string `json:"path,omitempty"` 35 Mode string `json:"mode,omitempty"` 36 Checksum Checksum `json:"checksum"` 37 SizeInBytes int64 `json:"size_in_bytes"` 38 } 39 40 ccResource.FilePath = r.FilePath 41 ccResource.SizeInBytes = r.SizeInBytes 42 ccResource.Checksum = r.Checksum 43 ccResource.Mode = strconv.FormatUint(uint64(r.Mode), 8) 44 return json.Marshal(ccResource) 45 } 46 47 func (r Resource) ToV2FormattedResource() V2FormattedResource { 48 return V2FormattedResource{ 49 Filename: r.FilePath, 50 Mode: r.Mode, 51 SHA1: r.Checksum.Value, 52 Size: r.SizeInBytes, 53 } 54 } 55 56 // UnmarshalJSON helps unmarshal a Cloud Controller Resource response. 57 func (r *Resource) UnmarshalJSON(data []byte) error { 58 var ccResource struct { 59 FilePath string `json:"path,omitempty"` 60 Mode string `json:"mode,omitempty"` 61 Checksum Checksum `json:"checksum"` 62 SizeInBytes int64 `json:"size_in_bytes"` 63 } 64 65 err := cloudcontroller.DecodeJSON(data, &ccResource) 66 if err != nil { 67 return err 68 } 69 70 r.FilePath = ccResource.FilePath 71 r.SizeInBytes = ccResource.SizeInBytes 72 r.Checksum = ccResource.Checksum 73 mode, err := strconv.ParseUint(ccResource.Mode, 8, 32) 74 if err != nil { 75 return err 76 } 77 78 r.Mode = os.FileMode(mode) 79 return nil 80 } 81 82 func (client Client) ResourceMatch(resources []Resource) ([]Resource, Warnings, error) { 83 var responseBody map[string][]Resource 84 85 _, warnings, err := client.MakeRequest(RequestParams{ 86 RequestName: internal.PostResourceMatchesRequest, 87 RequestBody: map[string][]Resource{"resources": resources}, 88 ResponseBody: &responseBody, 89 }) 90 91 return responseBody["resources"], warnings, err 92 }