github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/cf/models/application.go (about)

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