github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/v3action/droplet.go (about) 1 package v3action 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 8 ) 9 10 // Droplet represents a Cloud Controller droplet. 11 type Droplet struct { 12 GUID string 13 State constant.DropletState 14 CreatedAt string 15 Stack string 16 Image string 17 Buildpacks []Buildpack 18 } 19 20 type Buildpack ccv3.DropletBuildpack 21 22 // SetApplicationDroplet sets the droplet for an application. 23 func (actor Actor) SetApplicationDroplet(appName string, spaceGUID string, dropletGUID string) (Warnings, error) { 24 allWarnings := Warnings{} 25 application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 26 allWarnings = append(allWarnings, warnings...) 27 if err != nil { 28 return allWarnings, err 29 } 30 _, apiWarnings, err := actor.CloudControllerClient.SetApplicationDroplet(application.GUID, dropletGUID) 31 actorWarnings := Warnings(apiWarnings) 32 allWarnings = append(allWarnings, actorWarnings...) 33 34 if newErr, ok := err.(ccerror.UnprocessableEntityError); ok { 35 return allWarnings, actionerror.AssignDropletError{Message: newErr.Message} 36 } 37 38 return allWarnings, err 39 } 40 41 // GetApplicationDroplets returns the list of droplets that belong to applicaiton. 42 func (actor Actor) GetApplicationDroplets(appName string, spaceGUID string) ([]Droplet, Warnings, error) { 43 allWarnings := Warnings{} 44 application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 45 allWarnings = append(allWarnings, warnings...) 46 if err != nil { 47 return nil, allWarnings, err 48 } 49 50 ccv3Droplets, apiWarnings, err := actor.CloudControllerClient.GetDroplets( 51 ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{application.GUID}}, 52 ) 53 actorWarnings := Warnings(apiWarnings) 54 allWarnings = append(allWarnings, actorWarnings...) 55 if err != nil { 56 return nil, allWarnings, err 57 } 58 59 var droplets []Droplet 60 for _, ccv3Droplet := range ccv3Droplets { 61 droplets = append(droplets, actor.convertCCToActorDroplet(ccv3Droplet)) 62 } 63 64 return droplets, allWarnings, err 65 } 66 67 func (actor Actor) GetCurrentDropletByApplication(appGUID string) (Droplet, Warnings, error) { 68 droplet, warnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(appGUID) 69 switch err.(type) { 70 case ccerror.ApplicationNotFoundError: 71 return Droplet{}, Warnings(warnings), actionerror.ApplicationNotFoundError{GUID: appGUID} 72 case ccerror.DropletNotFoundError: 73 return Droplet{}, Warnings(warnings), actionerror.DropletNotFoundError{AppGUID: appGUID} 74 } 75 return actor.convertCCToActorDroplet(droplet), Warnings(warnings), err 76 } 77 78 func (actor Actor) convertCCToActorDroplet(ccDroplet ccv3.Droplet) Droplet { 79 var buildpacks []Buildpack 80 for _, ccBuildpack := range ccDroplet.Buildpacks { 81 buildpacks = append(buildpacks, Buildpack(ccBuildpack)) 82 } 83 84 return Droplet{ 85 GUID: ccDroplet.GUID, 86 State: constant.DropletState(ccDroplet.State), 87 CreatedAt: ccDroplet.CreatedAt, 88 Stack: ccDroplet.Stack, 89 Buildpacks: buildpacks, 90 Image: ccDroplet.Image, 91 } 92 }