forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droplet.go
103 lines (87 loc) · 3.53 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
package v3action
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/resources"
)
// Droplet represents a Cloud Controller droplet.
type Droplet struct {
GUID string
State constant.DropletState
CreatedAt string
Stack string
Image string
Buildpacks []Buildpack
}
type Buildpack resources.DropletBuildpack
// 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 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.GetDroplets(
ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{application.GUID}},
)
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) GetCurrentDropletByApplication(appGUID string) (Droplet, Warnings, error) {
droplet, warnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(appGUID)
switch err.(type) {
case ccerror.ApplicationNotFoundError:
return Droplet{}, Warnings(warnings), actionerror.ApplicationNotFoundError{GUID: appGUID}
case ccerror.DropletNotFoundError:
return Droplet{}, Warnings(warnings), actionerror.DropletNotFoundError{AppGUID: appGUID}
}
return actor.convertCCToActorDroplet(droplet), Warnings(warnings), err
}
func (actor Actor) convertCCToActorDroplet(ccDroplet resources.Droplet) Droplet {
var buildpacks []Buildpack
for _, ccBuildpack := range ccDroplet.Buildpacks {
buildpacks = append(buildpacks, Buildpack(ccBuildpack))
}
return Droplet{
GUID: ccDroplet.GUID,
State: constant.DropletState(ccDroplet.State),
CreatedAt: ccDroplet.CreatedAt,
Stack: ccDroplet.Stack,
Buildpacks: buildpacks,
Image: ccDroplet.Image,
}
}