github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/pushaction/application.go (about)

     1  package pushaction
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	log "github.com/sirupsen/logrus"
     9  )
    10  
    11  type Application struct {
    12  	v2action.Application
    13  	Stack v2action.Stack
    14  }
    15  
    16  func (app Application) String() string {
    17  	return fmt.Sprintf("%s, Stack Name: '%s'", app.Application, app.Stack.Name)
    18  }
    19  
    20  func (app *Application) SetStack(stack v2action.Stack) {
    21  	app.Stack = stack
    22  	app.StackGUID = stack.GUID
    23  }
    24  
    25  func (actor Actor) CreateOrUpdateApp(config ApplicationConfig) (ApplicationConfig, Event, Warnings, error) {
    26  	log.Debugf("creating or updating application")
    27  	if config.UpdatingApplication() {
    28  		app := config.DesiredApplication.Application
    29  
    30  		// Apps updates with both docker image and stack guids fail. So do not send
    31  		// StackGUID unless it is necessary.
    32  		if config.CurrentApplication.StackGUID == config.DesiredApplication.StackGUID {
    33  			app.StackGUID = ""
    34  		}
    35  
    36  		// For some versions of CC, sending state will always result in CC
    37  		// attempting to do perform that request (i.e. started -> start/restart).
    38  		// In order to prevent repeated unintended restarts in the middle of a
    39  		// push, don't send state. This will be fixed in capi-release 1.48.0.
    40  		if config.CurrentApplication.State == config.DesiredApplication.State {
    41  			app.State = ""
    42  		}
    43  
    44  		log.Debugf("updating application: %#v", app)
    45  		app, warnings, err := actor.V2Actor.UpdateApplication(app)
    46  		if err != nil {
    47  			log.Errorln("updating application:", err)
    48  			return ApplicationConfig{}, "", Warnings(warnings), err
    49  		}
    50  
    51  		config.DesiredApplication.Application = app
    52  		config.CurrentApplication = config.DesiredApplication
    53  		return config, UpdatedApplication, Warnings(warnings), err
    54  	} else {
    55  		log.Debugf("creating application: %#v", config.DesiredApplication)
    56  		app, warnings, err := actor.V2Actor.CreateApplication(config.DesiredApplication.Application)
    57  		if err != nil {
    58  			log.Errorln("creating application:", err)
    59  			return ApplicationConfig{}, "", Warnings(warnings), err
    60  		}
    61  
    62  		config.DesiredApplication.Application = app
    63  		config.CurrentApplication = config.DesiredApplication
    64  		return config, CreatedApplication, Warnings(warnings), err
    65  	}
    66  }
    67  
    68  func (actor Actor) FindOrReturnPartialApp(appName string, spaceGUID string) (bool, Application, v2action.Warnings, error) {
    69  	foundApp, appWarnings, err := actor.V2Actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    70  	if _, ok := err.(actionerror.ApplicationNotFoundError); ok {
    71  		log.Warnf("unable to find app %s in current space (GUID: %s)", appName, spaceGUID)
    72  		return false, Application{
    73  			Application: v2action.Application{
    74  				Name:      appName,
    75  				SpaceGUID: spaceGUID,
    76  			},
    77  		}, appWarnings, nil
    78  	} else if err != nil {
    79  		log.WithField("appName", appName).Error("error retrieving app")
    80  		return false, Application{}, appWarnings, err
    81  	}
    82  
    83  	stack, stackWarnings, err := actor.V2Actor.GetStack(foundApp.StackGUID)
    84  	warnings := append(appWarnings, stackWarnings...)
    85  	if err != nil {
    86  		log.Warnf("unable to find app's stack (GUID: %s)", foundApp.StackGUID)
    87  		return false, Application{}, warnings, err
    88  	}
    89  
    90  	app := Application{
    91  		Application: foundApp,
    92  		Stack:       stack,
    93  	}
    94  	return true, app, warnings, err
    95  }