github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv3/resource.go (about)

     1  package ccv3
     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/ccv3/internal"
    11  )
    12  
    13  type Checksum struct {
    14  	Value string `json:"value"`
    15  }
    16  
    17  type Resource struct {
    18  	// FilePath is the path of the resource.
    19  	FilePath string `json:"path"`
    20  
    21  	// Mode is the operating system file mode (aka file permissions) of the
    22  	// resource.
    23  	Mode os.FileMode `json:"mode"`
    24  
    25  	// SHA1 represents the SHA-1 hash of the resource.
    26  	Checksum Checksum `json:"checksum"`
    27  
    28  	// Size represents the file size of the resource.
    29  	SizeInBytes int64 `json:"size_in_bytes"`
    30  }
    31  
    32  // MarshalJSON converts a resource into a Cloud Controller Resource.
    33  func (r Resource) MarshalJSON() ([]byte, error) {
    34  	var ccResource struct {
    35  		FilePath    string   `json:"path,omitempty"`
    36  		Mode        string   `json:"mode,omitempty"`
    37  		Checksum    Checksum `json:"checksum"`
    38  		SizeInBytes int64    `json:"size_in_bytes"`
    39  	}
    40  
    41  	ccResource.FilePath = r.FilePath
    42  	ccResource.SizeInBytes = r.SizeInBytes
    43  	ccResource.Checksum = r.Checksum
    44  	ccResource.Mode = strconv.FormatUint(uint64(r.Mode), 8)
    45  	return json.Marshal(ccResource)
    46  }
    47  
    48  func (r Resource) ToV2FormattedResource() V2FormattedResource {
    49  	return V2FormattedResource{
    50  		Filename: r.FilePath,
    51  		Mode:     r.Mode,
    52  		SHA1:     r.Checksum.Value,
    53  		Size:     r.SizeInBytes,
    54  	}
    55  }
    56  
    57  // UnmarshalJSON helps unmarshal a Cloud Controller Resource response.
    58  func (r *Resource) UnmarshalJSON(data []byte) error {
    59  	var ccResource struct {
    60  		FilePath    string   `json:"path,omitempty"`
    61  		Mode        string   `json:"mode,omitempty"`
    62  		Checksum    Checksum `json:"checksum"`
    63  		SizeInBytes int64    `json:"size_in_bytes"`
    64  	}
    65  
    66  	err := cloudcontroller.DecodeJSON(data, &ccResource)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	r.FilePath = ccResource.FilePath
    72  	r.SizeInBytes = ccResource.SizeInBytes
    73  	r.Checksum = ccResource.Checksum
    74  	mode, err := strconv.ParseUint(ccResource.Mode, 8, 32)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	r.Mode = os.FileMode(mode)
    80  	return nil
    81  }
    82  
    83  func (client Client) ResourceMatch(resources []Resource) ([]Resource, Warnings, error) {
    84  	bodyBytes, err := json.Marshal(map[string][]Resource{"resources": resources})
    85  	if err != nil {
    86  		return []Resource{}, nil, err
    87  	}
    88  
    89  	request, err := client.newHTTPRequest(requestOptions{
    90  		RequestName: internal.PostResourceMatchesRequest,
    91  		Body:        bytes.NewReader(bodyBytes),
    92  	})
    93  	if err != nil {
    94  		return []Resource{}, nil, err
    95  	}
    96  
    97  	var matchedResources map[string][]Resource
    98  	response := cloudcontroller.Response{
    99  		DecodeJSONResponseInto: &matchedResources,
   100  	}
   101  	err = client.connection.Make(request, &response)
   102  
   103  	return matchedResources["resources"], response.Warnings, err
   104  }