forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droplet.go
117 lines (96 loc) · 4.13 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
package v7action
import (
"io"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/resources"
)
// CreateApplicationDroplet creates a new droplet without a package for the app with
// guid appGUID.
func (actor Actor) CreateApplicationDroplet(appGUID string) (resources.Droplet, Warnings, error) {
ccDroplet, warnings, err := actor.CloudControllerClient.CreateDroplet(appGUID)
return ccDroplet, Warnings(warnings), err
}
// SetApplicationDropletByApplicationNameAndSpace sets the droplet for an application.
func (actor Actor) SetApplicationDropletByApplicationNameAndSpace(appName string, spaceGUID string, dropletGUID string) (Warnings, error) {
allWarnings := Warnings{}
application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
_, apiWarnings, err := actor.CloudControllerClient.SetApplicationDroplet(application.GUID, dropletGUID)
actorWarnings := Warnings(apiWarnings)
allWarnings = append(allWarnings, actorWarnings...)
if newErr, ok := err.(ccerror.UnprocessableEntityError); ok {
return allWarnings, actionerror.AssignDropletError{Message: newErr.Message}
}
return allWarnings, err
}
func (actor Actor) SetApplicationDroplet(appGUID string, dropletGUID string) (Warnings, error) {
_, warnings, err := actor.CloudControllerClient.SetApplicationDroplet(appGUID, dropletGUID)
if newErr, ok := err.(ccerror.UnprocessableEntityError); ok {
return Warnings(warnings), actionerror.AssignDropletError{Message: newErr.Message}
}
return Warnings(warnings), err
}
// GetApplicationDroplets returns the list of droplets that belong to application.
func (actor Actor) GetApplicationDroplets(appName string, spaceGUID string) ([]resources.Droplet, Warnings, error) {
allWarnings := Warnings{}
application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
droplets, apiWarnings, err := actor.CloudControllerClient.GetDroplets(
ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{application.GUID}},
ccv3.Query{Key: ccv3.OrderBy, Values: []string{ccv3.CreatedAtDescendingOrder}},
)
actorWarnings := Warnings(apiWarnings)
allWarnings = append(allWarnings, actorWarnings...)
if err != nil {
return nil, allWarnings, err
}
if len(droplets) == 0 {
return []resources.Droplet{}, allWarnings, nil
}
currentDroplet, apiWarnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(application.GUID)
allWarnings = append(allWarnings, apiWarnings...)
if err != nil {
if _, ok := err.(ccerror.DropletNotFoundError); ok {
return droplets, allWarnings, nil
}
return []resources.Droplet{}, allWarnings, err
}
for i, droplet := range droplets {
if droplet.GUID == currentDroplet.GUID {
droplets[i].IsCurrent = true
}
}
return droplets, allWarnings, err
}
func (actor Actor) GetCurrentDropletByApplication(appGUID string) (resources.Droplet, Warnings, error) {
droplet, warnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(appGUID)
switch err.(type) {
case ccerror.ApplicationNotFoundError:
return resources.Droplet{}, Warnings(warnings), actionerror.ApplicationNotFoundError{GUID: appGUID}
case ccerror.DropletNotFoundError:
return resources.Droplet{}, Warnings(warnings), actionerror.DropletNotFoundError{AppGUID: appGUID}
}
return droplet, Warnings(warnings), err
}
func (actor Actor) UploadDroplet(dropletGUID string, dropletPath string, progressReader io.Reader, size int64) (Warnings, error) {
var allWarnings Warnings
jobURL, uploadWarnings, err := actor.CloudControllerClient.UploadDropletBits(dropletGUID, dropletPath, progressReader, size)
allWarnings = append(allWarnings, uploadWarnings...)
if err != nil {
return allWarnings, err
}
jobWarnings, jobErr := actor.CloudControllerClient.PollJob(jobURL)
allWarnings = append(allWarnings, jobWarnings...)
if jobErr != nil {
return allWarnings, jobErr
}
return allWarnings, nil
}