code.cloudfoundry.org/cli@v7.1.0+incompatible/cf/models/application.go (about)

     1  package models
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"strings"
     7  	"time"
     8  )
     9  
    10  type Application struct {
    11  	ApplicationFields
    12  	Stack    *Stack
    13  	Routes   []RouteSummary
    14  	Services []ServicePlanSummary
    15  }
    16  
    17  func (model Application) HasRoute(route Route) bool {
    18  	for _, boundRoute := range model.Routes {
    19  		if boundRoute.GUID == route.GUID {
    20  			return true
    21  		}
    22  	}
    23  	return false
    24  }
    25  
    26  func (model Application) ToParams() AppParams {
    27  	state := strings.ToUpper(model.State)
    28  	params := AppParams{
    29  		GUID:                    &model.GUID,
    30  		Name:                    &model.Name,
    31  		BuildpackURL:            &model.BuildpackURL,
    32  		Command:                 &model.Command,
    33  		DiskQuota:               &model.DiskQuota,
    34  		InstanceCount:           &model.InstanceCount,
    35  		HealthCheckType:         &model.HealthCheckType,
    36  		HealthCheckHTTPEndpoint: &model.HealthCheckHTTPEndpoint,
    37  		Memory:                  &model.Memory,
    38  		State:                   &state,
    39  		SpaceGUID:               &model.SpaceGUID,
    40  		EnvironmentVars:         &model.EnvironmentVars,
    41  		DockerImage:             &model.DockerImage,
    42  	}
    43  
    44  	if model.Stack != nil {
    45  		params.StackGUID = &model.Stack.GUID
    46  	}
    47  
    48  	return params
    49  }
    50  
    51  type ApplicationFields struct {
    52  	GUID                    string
    53  	Name                    string
    54  	BuildpackURL            string
    55  	Command                 string
    56  	Diego                   bool
    57  	DetectedStartCommand    string
    58  	DiskQuota               int64 // in Megabytes
    59  	EnvironmentVars         map[string]interface{}
    60  	InstanceCount           int
    61  	Memory                  int64 // in Megabytes
    62  	RunningInstances        int
    63  	HealthCheckType         string
    64  	HealthCheckHTTPEndpoint string
    65  	HealthCheckTimeout      int
    66  	State                   string
    67  	SpaceGUID               string
    68  	StackGUID               string
    69  	PackageUpdatedAt        *time.Time
    70  	PackageState            string
    71  	StagingFailedReason     string
    72  	Buildpack               string
    73  	DetectedBuildpack       string
    74  	DockerImage             string
    75  	EnableSSH               bool
    76  }
    77  
    78  const (
    79  	ApplicationStateStopped  = "stopped"
    80  	ApplicationStateStarted  = "started"
    81  	ApplicationStateRunning  = "running"
    82  	ApplicationStateCrashed  = "crashed"
    83  	ApplicationStateFlapping = "flapping"
    84  	ApplicationStateDown     = "down"
    85  	ApplicationStateStarting = "starting"
    86  )
    87  
    88  type AppParams struct {
    89  	BuildpackURL            *string
    90  	Buildpacks              []string
    91  	Command                 *string
    92  	DiskQuota               *int64
    93  	Domains                 []string
    94  	EnvironmentVars         *map[string]interface{}
    95  	GUID                    *string
    96  	HealthCheckType         *string
    97  	HealthCheckHTTPEndpoint *string
    98  	HealthCheckTimeout      *int
    99  	DockerImage             *string
   100  	DockerUsername          *string
   101  	DockerPassword          *string
   102  	EnableSSH               *bool
   103  	Hosts                   []string
   104  	RoutePath               *string
   105  	InstanceCount           *int
   106  	Memory                  *int64
   107  	Name                    *string
   108  	NoHostname              *bool
   109  	NoRoute                 bool
   110  	UseRandomRoute          bool
   111  	UseRandomPort           bool
   112  	Path                    *string
   113  	ServicesToBind          []string
   114  	SpaceGUID               *string
   115  	StackGUID               *string
   116  	StackName               *string
   117  	State                   *string
   118  	PackageUpdatedAt        *time.Time
   119  	Routes                  []ManifestRoute
   120  }
   121  
   122  func (app *AppParams) Merge(flagContext *AppParams) {
   123  	if flagContext.BuildpackURL != nil {
   124  		app.BuildpackURL = flagContext.BuildpackURL
   125  	}
   126  	if flagContext.Command != nil {
   127  		app.Command = flagContext.Command
   128  	}
   129  	if flagContext.DiskQuota != nil {
   130  		app.DiskQuota = flagContext.DiskQuota
   131  	}
   132  	if flagContext.DockerImage != nil {
   133  		app.DockerImage = flagContext.DockerImage
   134  	}
   135  
   136  	switch {
   137  	case flagContext.DockerUsername != nil:
   138  		app.DockerUsername = flagContext.DockerUsername
   139  		// the password is always non-nil after we parse the flag context
   140  		app.DockerPassword = flagContext.DockerPassword
   141  	case app.DockerUsername != nil:
   142  		password := os.Getenv("CF_DOCKER_PASSWORD")
   143  		// if the password is empty, we will get a CC error
   144  		app.DockerPassword = &password
   145  	}
   146  
   147  	if flagContext.Domains != nil {
   148  		app.Domains = flagContext.Domains
   149  	}
   150  	if flagContext.EnableSSH != nil {
   151  		app.EnableSSH = flagContext.EnableSSH
   152  	}
   153  	if flagContext.EnvironmentVars != nil {
   154  		app.EnvironmentVars = flagContext.EnvironmentVars
   155  	}
   156  	if flagContext.GUID != nil {
   157  		app.GUID = flagContext.GUID
   158  	}
   159  	if flagContext.HealthCheckType != nil {
   160  		app.HealthCheckType = flagContext.HealthCheckType
   161  	}
   162  	if flagContext.HealthCheckHTTPEndpoint != nil {
   163  		app.HealthCheckHTTPEndpoint = flagContext.HealthCheckHTTPEndpoint
   164  	}
   165  	if flagContext.HealthCheckTimeout != nil {
   166  		app.HealthCheckTimeout = flagContext.HealthCheckTimeout
   167  	}
   168  	if flagContext.Hosts != nil {
   169  		app.Hosts = flagContext.Hosts
   170  	}
   171  	if flagContext.InstanceCount != nil {
   172  		app.InstanceCount = flagContext.InstanceCount
   173  	}
   174  	if flagContext.Memory != nil {
   175  		app.Memory = flagContext.Memory
   176  	}
   177  	if flagContext.Name != nil {
   178  		app.Name = flagContext.Name
   179  	}
   180  	if flagContext.Path != nil {
   181  		app.Path = flagContext.Path
   182  	}
   183  	if flagContext.RoutePath != nil {
   184  		app.RoutePath = flagContext.RoutePath
   185  	}
   186  	if flagContext.ServicesToBind != nil {
   187  		app.ServicesToBind = flagContext.ServicesToBind
   188  	}
   189  	if flagContext.SpaceGUID != nil {
   190  		app.SpaceGUID = flagContext.SpaceGUID
   191  	}
   192  	if flagContext.StackGUID != nil {
   193  		app.StackGUID = flagContext.StackGUID
   194  	}
   195  	if flagContext.StackName != nil {
   196  		app.StackName = flagContext.StackName
   197  	}
   198  	if flagContext.State != nil {
   199  		app.State = flagContext.State
   200  	}
   201  
   202  	app.NoRoute = app.NoRoute || flagContext.NoRoute
   203  	noHostBool := app.IsNoHostnameTrue() || flagContext.IsNoHostnameTrue()
   204  	app.NoHostname = &noHostBool
   205  	app.UseRandomRoute = app.UseRandomRoute || flagContext.UseRandomRoute
   206  }
   207  
   208  func (app *AppParams) IsEmpty() bool {
   209  	noHostBool := false
   210  	return reflect.DeepEqual(*app, AppParams{NoHostname: &noHostBool})
   211  }
   212  
   213  func (app *AppParams) IsHostEmpty() bool {
   214  	return app.Hosts == nil || len(app.Hosts) == 0
   215  }
   216  
   217  func (app *AppParams) IsNoHostnameTrue() bool {
   218  	if app.NoHostname == nil {
   219  		return false
   220  	}
   221  	return *app.NoHostname
   222  }