github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/v7action/droplet.go (about)

     1  package v7action
     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  // SetApplicationDropletByApplicationNameAndSpace sets the droplet for an application.
    23  func (actor Actor) SetApplicationDropletByApplicationNameAndSpace(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  func (actor Actor) SetApplicationDroplet(appGUID string, dropletGUID string) (Warnings, error) {
    42  	_, warnings, err := actor.CloudControllerClient.SetApplicationDroplet(appGUID, dropletGUID)
    43  
    44  	if newErr, ok := err.(ccerror.UnprocessableEntityError); ok {
    45  		return Warnings(warnings), actionerror.AssignDropletError{Message: newErr.Message}
    46  	}
    47  
    48  	return Warnings(warnings), err
    49  }
    50  
    51  // GetApplicationDroplets returns the list of droplets that belong to applicaiton.
    52  func (actor Actor) GetApplicationDroplets(appName string, spaceGUID string) ([]Droplet, Warnings, error) {
    53  	allWarnings := Warnings{}
    54  	application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    55  	allWarnings = append(allWarnings, warnings...)
    56  	if err != nil {
    57  		return nil, allWarnings, err
    58  	}
    59  
    60  	ccv3Droplets, apiWarnings, err := actor.CloudControllerClient.GetDroplets(
    61  		ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{application.GUID}},
    62  	)
    63  	actorWarnings := Warnings(apiWarnings)
    64  	allWarnings = append(allWarnings, actorWarnings...)
    65  	if err != nil {
    66  		return nil, allWarnings, err
    67  	}
    68  
    69  	var droplets []Droplet
    70  	for _, ccv3Droplet := range ccv3Droplets {
    71  		droplets = append(droplets, actor.convertCCToActorDroplet(ccv3Droplet))
    72  	}
    73  
    74  	return droplets, allWarnings, err
    75  }
    76  
    77  func (actor Actor) GetCurrentDropletByApplication(appGUID string) (Droplet, Warnings, error) {
    78  	droplet, warnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(appGUID)
    79  	switch err.(type) {
    80  	case ccerror.ApplicationNotFoundError:
    81  		return Droplet{}, Warnings(warnings), actionerror.ApplicationNotFoundError{GUID: appGUID}
    82  	case ccerror.DropletNotFoundError:
    83  		return Droplet{}, Warnings(warnings), actionerror.DropletNotFoundError{AppGUID: appGUID}
    84  	}
    85  	return actor.convertCCToActorDroplet(droplet), Warnings(warnings), err
    86  }
    87  
    88  func (actor Actor) convertCCToActorDroplet(ccDroplet ccv3.Droplet) Droplet {
    89  	var buildpacks []Buildpack
    90  	for _, ccBuildpack := range ccDroplet.Buildpacks {
    91  		buildpacks = append(buildpacks, Buildpack(ccBuildpack))
    92  	}
    93  
    94  	return Droplet{
    95  		GUID:       ccDroplet.GUID,
    96  		State:      constant.DropletState(ccDroplet.State),
    97  		CreatedAt:  ccDroplet.CreatedAt,
    98  		Stack:      ccDroplet.Stack,
    99  		Buildpacks: buildpacks,
   100  		Image:      ccDroplet.Image,
   101  	}
   102  }