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

     1  package manifest
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/types"
     8  )
     9  
    10  type Application struct {
    11  	Buildpack      types.FilteredString
    12  	Command        types.FilteredString
    13  	DiskQuota      types.NullByteSizeInMb
    14  	DockerImage    string
    15  	DockerPassword string
    16  	DockerUsername string
    17  	Domain         string
    18  	// EnvironmentVariables can be any valid json type (ie, strings not
    19  	// guaranteed, although CLI only ships strings).
    20  	EnvironmentVariables    map[string]string
    21  	HealthCheckHTTPEndpoint string
    22  	HealthCheckTimeout      int
    23  	// HealthCheckType attribute defines the number of seconds that is allocated
    24  	// for starting an application.
    25  	HealthCheckType string
    26  	Hostname        string
    27  	Instances       types.NullInt
    28  	Memory          types.NullByteSizeInMb
    29  	Name            string
    30  	NoHostname      bool
    31  	NoRoute         bool
    32  	Path            string
    33  	RandomRoute     bool
    34  	Routes          []string
    35  	RoutePath       string
    36  	Services        []string
    37  	StackName       string
    38  
    39  	DeprecatedDomain     interface{}
    40  	DeprecatedDomains    interface{}
    41  	DeprecatedHost       interface{}
    42  	DeprecatedHosts      interface{}
    43  	DeprecatedNoHostname interface{}
    44  }
    45  
    46  func (app Application) String() string {
    47  	return fmt.Sprintf(
    48  		"App Name: '%s', Buildpack IsSet: %t, Buildpack: '%s', Command IsSet: %t, Command: '%s', Disk Quota: '%s', Docker Image: '%s', Health Check HTTP Endpoint: '%s', Health Check Timeout: '%d', Health Check Type: '%s', Hostname: '%s', Instances IsSet: %t, Instances: '%d', Memory: '%s', No-Hostname: %t, No-Route: %t, Path: '%s', RandomRoute: %t, RoutePath: '%s', Routes: [%s], Services: [%s], Stack Name: '%s'",
    49  		app.Name,
    50  		app.Buildpack.IsSet,
    51  		app.Buildpack.Value,
    52  		app.Command.IsSet,
    53  		app.Command.Value,
    54  		app.DiskQuota,
    55  		app.DockerImage,
    56  		app.HealthCheckHTTPEndpoint,
    57  		app.HealthCheckTimeout,
    58  		app.HealthCheckType,
    59  		app.Hostname,
    60  		app.Instances.IsSet,
    61  		app.Instances.Value,
    62  		app.Memory,
    63  		app.NoHostname,
    64  		app.NoRoute,
    65  		app.Path,
    66  		app.RandomRoute,
    67  		app.RoutePath,
    68  		strings.Join(app.Routes, ", "),
    69  		strings.Join(app.Services, ", "),
    70  		app.StackName,
    71  	)
    72  }
    73  
    74  func (app Application) MarshalYAML() (interface{}, error) {
    75  	var m = rawManifestApplication{
    76  		Buildpack:               app.Buildpack.Value,
    77  		Command:                 app.Command.Value,
    78  		Docker:                  rawDockerInfo{Image: app.DockerImage, Username: app.DockerUsername},
    79  		EnvironmentVariables:    app.EnvironmentVariables,
    80  		HealthCheckHTTPEndpoint: app.HealthCheckHTTPEndpoint,
    81  		HealthCheckType:         app.HealthCheckType,
    82  		Name:                    app.Name,
    83  		NoRoute:                 app.NoRoute,
    84  		Path:                    app.Path,
    85  		Services:                app.Services,
    86  		StackName:               app.StackName,
    87  		Timeout:                 app.HealthCheckTimeout,
    88  	}
    89  	m.DiskQuota = app.DiskQuota.String()
    90  	m.Memory = app.Memory.String()
    91  
    92  	if app.Instances.IsSet {
    93  		m.Instances = &app.Instances.Value
    94  	}
    95  
    96  	for _, route := range app.Routes {
    97  		m.Routes = append(m.Routes, rawManifestRoute{Route: route})
    98  	}
    99  
   100  	return m, nil
   101  }
   102  
   103  func (app *Application) UnmarshalYAML(unmarshaller func(interface{}) error) error {
   104  	var m rawManifestApplication
   105  
   106  	err := unmarshaller(&m)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	app.DeprecatedDomain = m.DeprecatedDomain
   111  	app.DeprecatedDomains = m.DeprecatedDomains
   112  	app.DeprecatedHost = m.DeprecatedHost
   113  	app.DeprecatedHosts = m.DeprecatedHosts
   114  	app.DeprecatedNoHostname = m.DeprecatedNoHostname
   115  	app.DockerImage = m.Docker.Image
   116  	app.DockerUsername = m.Docker.Username
   117  	app.HealthCheckHTTPEndpoint = m.HealthCheckHTTPEndpoint
   118  	app.HealthCheckType = m.HealthCheckType
   119  	app.Name = m.Name
   120  	app.NoRoute = m.NoRoute
   121  	app.Path = m.Path
   122  	app.RandomRoute = m.RandomRoute
   123  	app.Services = m.Services
   124  	app.StackName = m.StackName
   125  	app.HealthCheckTimeout = m.Timeout
   126  	app.EnvironmentVariables = m.EnvironmentVariables
   127  
   128  	app.Instances.ParseIntValue(m.Instances)
   129  
   130  	if fmtErr := app.DiskQuota.ParseStringValue(m.DiskQuota); fmtErr != nil {
   131  		return fmtErr
   132  	}
   133  
   134  	if fmtErr := app.Memory.ParseStringValue(m.Memory); fmtErr != nil {
   135  		return fmtErr
   136  	}
   137  
   138  	for _, route := range m.Routes {
   139  		app.Routes = append(app.Routes, route.Route)
   140  	}
   141  
   142  	// "null" values are identical to non-existant values in YAML. In order to
   143  	// detect if an explicit null is given, a manual existance check is required.
   144  	exists := map[string]interface{}{}
   145  	err = unmarshaller(&exists)
   146  	if err != nil {
   147  		return err
   148  	}
   149  
   150  	if _, ok := exists["buildpack"]; ok {
   151  		app.Buildpack.ParseValue(m.Buildpack)
   152  		app.Buildpack.IsSet = true
   153  	}
   154  
   155  	if _, ok := exists["command"]; ok {
   156  		app.Command.ParseValue(m.Command)
   157  		app.Command.IsSet = true
   158  	}
   159  
   160  	return nil
   161  }