github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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  type Resource struct {
    14  	Filename string      `json:"fn"`
    15  	Mode     os.FileMode `json:"mode"`
    16  	SHA1     string      `json:"sha1"`
    17  	Size     int64       `json:"size"`
    18  }
    19  
    20  func (r *Resource) UnmarshalJSON(rawJSON []byte) error {
    21  	var ccResource struct {
    22  		Filename string `json:"fn,omitempty"`
    23  		Mode     string `json:"mode,omitempty"`
    24  		SHA1     string `json:"sha1"`
    25  		Size     int64  `json:"size"`
    26  	}
    27  
    28  	err := json.Unmarshal(rawJSON, &ccResource)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	r.Filename = ccResource.Filename
    34  	r.Size = ccResource.Size
    35  	r.SHA1 = ccResource.SHA1
    36  	mode, err := strconv.ParseUint(ccResource.Mode, 8, 32)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	r.Mode = os.FileMode(mode)
    42  	return nil
    43  }
    44  
    45  func (r Resource) MarshalJSON() ([]byte, error) {
    46  	var ccResource struct {
    47  		Filename string `json:"fn,omitempty"`
    48  		Mode     string `json:"mode,omitempty"`
    49  		SHA1     string `json:"sha1"`
    50  		Size     int64  `json:"size"`
    51  	}
    52  
    53  	ccResource.Filename = r.Filename
    54  	ccResource.Size = r.Size
    55  	ccResource.SHA1 = r.SHA1
    56  	ccResource.Mode = strconv.FormatUint(uint64(r.Mode), 8)
    57  	return json.Marshal(ccResource)
    58  }
    59  
    60  // ResourceMatch returns the resources that exist on the cloud foundry instance
    61  // from the set of resources given.
    62  func (client *Client) ResourceMatch(resourcesToMatch []Resource) ([]Resource, Warnings, error) {
    63  	body, err := json.Marshal(resourcesToMatch)
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  
    68  	request, err := client.newHTTPRequest(requestOptions{
    69  		RequestName: internal.PutResourceMatch,
    70  		Body:        bytes.NewReader(body),
    71  	})
    72  	if err != nil {
    73  		return nil, nil, err
    74  	}
    75  
    76  	request.Header.Set("Content-Type", "application/json")
    77  
    78  	var matchedResources []Resource
    79  	response := cloudcontroller.Response{
    80  		Result: &matchedResources,
    81  	}
    82  
    83  	err = client.connection.Make(request, &response)
    84  	return matchedResources, response.Warnings, err
    85  }