github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/droplet.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"io"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/uploads"
     9  	"code.cloudfoundry.org/cli/resources"
    10  )
    11  
    12  type DropletCreateRequest struct {
    13  	Relationships resources.Relationships `json:"relationships"`
    14  }
    15  
    16  // CreateDroplet creates a new droplet without a package for the app with
    17  // the given guid.
    18  func (client *Client) CreateDroplet(appGUID string) (resources.Droplet, Warnings, error) {
    19  	requestBody := DropletCreateRequest{
    20  		Relationships: resources.Relationships{
    21  			constant.RelationshipTypeApplication: resources.Relationship{GUID: appGUID},
    22  		},
    23  	}
    24  
    25  	var responseBody resources.Droplet
    26  
    27  	_, warnings, err := client.MakeRequest(RequestParams{
    28  		RequestName:  internal.PostDropletRequest,
    29  		RequestBody:  requestBody,
    30  		ResponseBody: &responseBody,
    31  	})
    32  
    33  	return responseBody, warnings, err
    34  }
    35  
    36  // GetApplicationDropletCurrent returns the current droplet for a given
    37  // application.
    38  func (client *Client) GetApplicationDropletCurrent(appGUID string) (resources.Droplet, Warnings, error) {
    39  	var responseBody resources.Droplet
    40  
    41  	_, warnings, err := client.MakeRequest(RequestParams{
    42  		RequestName:  internal.GetApplicationDropletCurrentRequest,
    43  		URIParams:    internal.Params{"app_guid": appGUID},
    44  		ResponseBody: &responseBody,
    45  	})
    46  
    47  	return responseBody, warnings, err
    48  }
    49  
    50  // GetDroplet returns a droplet with the given GUID.
    51  func (client *Client) GetDroplet(dropletGUID string) (resources.Droplet, Warnings, error) {
    52  	var responseBody resources.Droplet
    53  
    54  	_, warnings, err := client.MakeRequest(RequestParams{
    55  		RequestName:  internal.GetDropletRequest,
    56  		URIParams:    internal.Params{"droplet_guid": dropletGUID},
    57  		ResponseBody: &responseBody,
    58  	})
    59  
    60  	return responseBody, warnings, err
    61  }
    62  
    63  // GetDroplets lists droplets with optional filters.
    64  func (client *Client) GetDroplets(query ...Query) ([]resources.Droplet, Warnings, error) {
    65  	var droplets []resources.Droplet
    66  
    67  	_, warnings, err := client.MakeListRequest(RequestParams{
    68  		RequestName:  internal.GetDropletsRequest,
    69  		Query:        query,
    70  		ResponseBody: resources.Droplet{},
    71  		AppendToList: func(item interface{}) error {
    72  			droplets = append(droplets, item.(resources.Droplet))
    73  			return nil
    74  		},
    75  	})
    76  
    77  	return droplets, warnings, err
    78  }
    79  
    80  // GetPackageDroplets returns the droplets that run the specified packages
    81  func (client *Client) GetPackageDroplets(packageGUID string, query ...Query) ([]resources.Droplet, Warnings, error) {
    82  	var droplets []resources.Droplet
    83  
    84  	_, warnings, err := client.MakeListRequest(RequestParams{
    85  		RequestName:  internal.GetPackageDropletsRequest,
    86  		URIParams:    internal.Params{"package_guid": packageGUID},
    87  		Query:        query,
    88  		ResponseBody: resources.Droplet{},
    89  		AppendToList: func(item interface{}) error {
    90  			droplets = append(droplets, item.(resources.Droplet))
    91  			return nil
    92  		},
    93  	})
    94  
    95  	return droplets, warnings, err
    96  }
    97  
    98  // UploadDropletBits asynchronously uploads bits from a .tgz file located at dropletPath to the
    99  // droplet with guid dropletGUID. It returns a job URL pointing to the asynchronous upload job.
   100  func (client *Client) UploadDropletBits(dropletGUID string, dropletPath string, droplet io.Reader, dropletLength int64) (JobURL, Warnings, error) {
   101  	contentLength, err := uploads.CalculateRequestSize(dropletLength, dropletPath, "bits")
   102  	if err != nil {
   103  		return "", nil, err
   104  	}
   105  
   106  	contentType, body, writeErrors := uploads.CreateMultipartBodyAndHeader(droplet, dropletPath, "bits")
   107  
   108  	responseLocation, warnings, err := client.MakeRequestUploadAsync(
   109  		internal.PostDropletBitsRequest,
   110  		internal.Params{"droplet_guid": dropletGUID},
   111  		contentType,
   112  		body,
   113  		contentLength,
   114  		nil,
   115  		writeErrors,
   116  	)
   117  
   118  	return JobURL(responseLocation), warnings, err
   119  }
   120  
   121  func (client *Client) DownloadDroplet(dropletGUID string) ([]byte, Warnings, error) {
   122  	bytes, warnings, err := client.MakeRequestReceiveRaw(
   123  		internal.GetDropletBitsRequest,
   124  		internal.Params{"droplet_guid": dropletGUID},
   125  		"application/json",
   126  	)
   127  	return bytes, warnings, err
   128  }