forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droplet.go
98 lines (81 loc) · 2.8 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
package v3action
import (
"net/url"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
)
type DropletState string
const (
DropletStateStaged DropletState = "STAGED"
DropletStateFailed DropletState = "FAILED"
DropletStateCopying DropletState = "COPYING"
DropletStateExpired DropletState = "EXPIRED"
)
// Droplet represents a Cloud Controller droplet.
type Droplet struct {
GUID string
State DropletState
CreatedAt string
Stack string
Image string
Buildpacks []Buildpack
}
type Buildpack ccv3.DropletBuildpack
// AssignDropletError is returned when assigning the current droplet of an app
// fails
type AssignDropletError struct {
Message string
}
func (a AssignDropletError) Error() string {
return a.Message
}
// SetApplicationDroplet sets the droplet for an application.
func (actor Actor) SetApplicationDroplet(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, AssignDropletError{Message: newErr.Message}
}
return allWarnings, err
}
// GetApplicationDroplets returns the list of droplets that belong to applicaiton.
func (actor Actor) GetApplicationDroplets(appName string, spaceGUID string) ([]Droplet, Warnings, error) {
allWarnings := Warnings{}
application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
ccv3Droplets, apiWarnings, err := actor.CloudControllerClient.GetApplicationDroplets(application.GUID, url.Values{})
actorWarnings := Warnings(apiWarnings)
allWarnings = append(allWarnings, actorWarnings...)
if err != nil {
return nil, allWarnings, err
}
var droplets []Droplet
for _, ccv3Droplet := range ccv3Droplets {
droplets = append(droplets, actor.convertCCToActorDroplet(ccv3Droplet))
}
return droplets, allWarnings, err
}
func (actor Actor) convertCCToActorDroplet(ccv3Droplet ccv3.Droplet) Droplet {
var buildpacks []Buildpack
for _, ccv3Buildpack := range ccv3Droplet.Buildpacks {
buildpacks = append(buildpacks, Buildpack(ccv3Buildpack))
}
return Droplet{
GUID: ccv3Droplet.GUID,
State: DropletState(ccv3Droplet.State),
CreatedAt: ccv3Droplet.CreatedAt,
Stack: ccv3Droplet.Stack,
Buildpacks: buildpacks,
Image: ccv3Droplet.Image,
}
}