github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  	AppPorts                []int
    77  }
    78  
    79  const (
    80  	ApplicationStateStopped  = "stopped"
    81  	ApplicationStateStarted  = "started"
    82  	ApplicationStateRunning  = "running"
    83  	ApplicationStateCrashed  = "crashed"
    84  	ApplicationStateFlapping = "flapping"
    85  	ApplicationStateDown     = "down"
    86  	ApplicationStateStarting = "starting"
    87  )
    88  
    89  type AppParams struct {
    90  	BuildpackURL            *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  	Diego                   *bool
   103  	EnableSSH               *bool
   104  	Hosts                   []string
   105  	RoutePath               *string
   106  	InstanceCount           *int
   107  	Memory                  *int64
   108  	Name                    *string
   109  	NoHostname              *bool
   110  	NoRoute                 bool
   111  	UseRandomRoute          bool
   112  	UseRandomPort           bool
   113  	Path                    *string
   114  	ServicesToBind          []string
   115  	SpaceGUID               *string
   116  	StackGUID               *string
   117  	StackName               *string
   118  	State                   *string
   119  	PackageUpdatedAt        *time.Time
   120  	AppPorts                *[]int
   121  	Routes                  []ManifestRoute
   122  }
   123  
   124  func (app *AppParams) Merge(flagContext *AppParams) {
   125  	if flagContext.AppPorts != nil {
   126  		app.AppPorts = flagContext.AppPorts
   127  	}
   128  	if flagContext.BuildpackURL != nil {
   129  		app.BuildpackURL = flagContext.BuildpackURL
   130  	}
   131  	if flagContext.Command != nil {
   132  		app.Command = flagContext.Command
   133  	}
   134  	if flagContext.DiskQuota != nil {
   135  		app.DiskQuota = flagContext.DiskQuota
   136  	}
   137  	if flagContext.DockerImage != nil {
   138  		app.DockerImage = flagContext.DockerImage
   139  	}
   140  
   141  	switch {
   142  	case flagContext.DockerUsername != nil:
   143  		app.DockerUsername = flagContext.DockerUsername
   144  		// the password is always non-nil after we parse the flag context
   145  		app.DockerPassword = flagContext.DockerPassword
   146  	case app.DockerUsername != nil:
   147  		password := os.Getenv("CF_DOCKER_PASSWORD")
   148  		// if the password is empty, we will get a CC error
   149  		app.DockerPassword = &password
   150  	}
   151  
   152  	if flagContext.Domains != nil {
   153  		app.Domains = flagContext.Domains
   154  	}
   155  	if flagContext.EnableSSH != nil {
   156  		app.EnableSSH = flagContext.EnableSSH
   157  	}
   158  	if flagContext.EnvironmentVars != nil {
   159  		app.EnvironmentVars = flagContext.EnvironmentVars
   160  	}
   161  	if flagContext.GUID != nil {
   162  		app.GUID = flagContext.GUID
   163  	}
   164  	if flagContext.HealthCheckType != nil {
   165  		app.HealthCheckType = flagContext.HealthCheckType
   166  	}
   167  	if flagContext.HealthCheckHTTPEndpoint != nil {
   168  		app.HealthCheckHTTPEndpoint = flagContext.HealthCheckHTTPEndpoint
   169  	}
   170  	if flagContext.HealthCheckTimeout != nil {
   171  		app.HealthCheckTimeout = flagContext.HealthCheckTimeout
   172  	}
   173  	if flagContext.Hosts != nil {
   174  		app.Hosts = flagContext.Hosts
   175  	}
   176  	if flagContext.InstanceCount != nil {
   177  		app.InstanceCount = flagContext.InstanceCount
   178  	}
   179  	if flagContext.Memory != nil {
   180  		app.Memory = flagContext.Memory
   181  	}
   182  	if flagContext.Name != nil {
   183  		app.Name = flagContext.Name
   184  	}
   185  	if flagContext.Path != nil {
   186  		app.Path = flagContext.Path
   187  	}
   188  	if flagContext.RoutePath != nil {
   189  		app.RoutePath = flagContext.RoutePath
   190  	}
   191  	if flagContext.ServicesToBind != nil {
   192  		app.ServicesToBind = flagContext.ServicesToBind
   193  	}
   194  	if flagContext.SpaceGUID != nil {
   195  		app.SpaceGUID = flagContext.SpaceGUID
   196  	}
   197  	if flagContext.StackGUID != nil {
   198  		app.StackGUID = flagContext.StackGUID
   199  	}
   200  	if flagContext.StackName != nil {
   201  		app.StackName = flagContext.StackName
   202  	}
   203  	if flagContext.State != nil {
   204  		app.State = flagContext.State
   205  	}
   206  
   207  	app.NoRoute = app.NoRoute || flagContext.NoRoute
   208  	noHostBool := app.IsNoHostnameTrue() || flagContext.IsNoHostnameTrue()
   209  	app.NoHostname = &noHostBool
   210  	app.UseRandomRoute = app.UseRandomRoute || flagContext.UseRandomRoute
   211  }
   212  
   213  func (app *AppParams) IsEmpty() bool {
   214  	noHostBool := false
   215  	return reflect.DeepEqual(*app, AppParams{NoHostname: &noHostBool})
   216  }
   217  
   218  func (app *AppParams) IsHostEmpty() bool {
   219  	return app.Hosts == nil || len(app.Hosts) == 0
   220  }
   221  
   222  func (app *AppParams) IsNoHostnameTrue() bool {
   223  	if app.NoHostname == nil {
   224  		return false
   225  	}
   226  	return *app.NoHostname
   227  }