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