github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  // UnmarshalJSON helps unmarshal a Cloud Controller Resource response.
    31  func (r *Resource) UnmarshalJSON(rawJSON []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  	err := json.Unmarshal(rawJSON, &ccResource)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	r.Filename = ccResource.Filename
    45  	r.Size = ccResource.Size
    46  	r.SHA1 = ccResource.SHA1
    47  	mode, err := strconv.ParseUint(ccResource.Mode, 8, 32)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	r.Mode = os.FileMode(mode)
    53  	return nil
    54  }
    55  
    56  // MarshalJSON converts a resource into a Cloud Controller Resource.
    57  func (r Resource) MarshalJSON() ([]byte, error) {
    58  	var ccResource struct {
    59  		Filename string `json:"fn,omitempty"`
    60  		Mode     string `json:"mode,omitempty"`
    61  		SHA1     string `json:"sha1"`
    62  		Size     int64  `json:"size"`
    63  	}
    64  
    65  	ccResource.Filename = r.Filename
    66  	ccResource.Size = r.Size
    67  	ccResource.SHA1 = r.SHA1
    68  	ccResource.Mode = strconv.FormatUint(uint64(r.Mode), 8)
    69  	return json.Marshal(ccResource)
    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  		Result: &matchedResources,
    93  	}
    94  
    95  	err = client.connection.Make(request, &response)
    96  	return matchedResources, response.Warnings, err
    97  }