github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/resources/applications.go (about)

     1  package resources
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  )
     9  
    10  type PaginatedApplicationResources struct {
    11  	Resources []ApplicationResource
    12  }
    13  
    14  type AppRouteEntity struct {
    15  	Host   string
    16  	Domain struct {
    17  		Resource
    18  		Entity struct {
    19  			Name string
    20  		}
    21  	}
    22  }
    23  
    24  type AppRouteResource struct {
    25  	Resource
    26  	Entity AppRouteEntity
    27  }
    28  
    29  type IntegrityFields struct {
    30  	Sha1 string `json:"sha1"`
    31  	Size int64  `json:"size"`
    32  }
    33  
    34  type AppFileResource struct {
    35  	Sha1 string `json:"sha1"`
    36  	Size int64  `json:"size"`
    37  	Path string `json:"fn"`
    38  	Mode string `json:"mode"`
    39  }
    40  
    41  type ApplicationResource struct {
    42  	Resource
    43  	Entity ApplicationEntity
    44  }
    45  
    46  type DockerCredentials struct {
    47  	Username string `json:"username"`
    48  	Password string `json:"password"`
    49  }
    50  
    51  type ApplicationEntity struct {
    52  	Name                    *string                 `json:"name,omitempty"`
    53  	Command                 *string                 `json:"command,omitempty"`
    54  	DetectedStartCommand    *string                 `json:"detected_start_command,omitempty"`
    55  	State                   *string                 `json:"state,omitempty"`
    56  	SpaceGUID               *string                 `json:"space_guid,omitempty"`
    57  	Instances               *int                    `json:"instances,omitempty"`
    58  	Memory                  *int64                  `json:"memory,omitempty"`
    59  	DiskQuota               *int64                  `json:"disk_quota,omitempty"`
    60  	StackGUID               *string                 `json:"stack_guid,omitempty"`
    61  	Stack                   *StackResource          `json:"stack,omitempty"`
    62  	Routes                  *[]AppRouteResource     `json:"routes,omitempty"`
    63  	Buildpack               *string                 `json:"buildpack,omitempty"`
    64  	DetectedBuildpack       *string                 `json:"detected_buildpack,omitempty"`
    65  	EnvironmentJSON         *map[string]interface{} `json:"environment_json,omitempty"`
    66  	HealthCheckType         *string                 `json:"health_check_type,omitempty"`
    67  	HealthCheckHTTPEndpoint *string                 `json:"health_check_http_endpoint,omitempty"`
    68  	HealthCheckTimeout      *int                    `json:"health_check_timeout,omitempty"`
    69  	PackageState            *string                 `json:"package_state,omitempty"`
    70  	StagingFailedReason     *string                 `json:"staging_failed_reason,omitempty"`
    71  	Diego                   *bool                   `json:"diego,omitempty"`
    72  	DockerImage             *string                 `json:"docker_image,omitempty"`
    73  	DockerCredentials       *DockerCredentials      `json:"docker_credentials,omitempty"`
    74  	EnableSSH               *bool                   `json:"enable_ssh,omitempty"`
    75  	PackageUpdatedAt        *time.Time              `json:"package_updated_at,omitempty"`
    76  	AppPorts                *[]int                  `json:"ports,omitempty"`
    77  }
    78  
    79  func (resource AppRouteResource) ToFields() (route models.RouteSummary) {
    80  	route.GUID = resource.Metadata.GUID
    81  	route.Host = resource.Entity.Host
    82  	return
    83  }
    84  
    85  func (resource AppRouteResource) ToModel() (route models.RouteSummary) {
    86  	route.GUID = resource.Metadata.GUID
    87  	route.Host = resource.Entity.Host
    88  	route.Domain.GUID = resource.Entity.Domain.Metadata.GUID
    89  	route.Domain.Name = resource.Entity.Domain.Entity.Name
    90  	return
    91  }
    92  
    93  func (resource AppFileResource) ToIntegrityFields() IntegrityFields {
    94  	return IntegrityFields{
    95  		Sha1: resource.Sha1,
    96  		Size: resource.Size,
    97  	}
    98  }
    99  
   100  func NewApplicationEntityFromAppParams(app models.AppParams) ApplicationEntity {
   101  	entity := ApplicationEntity{
   102  		Buildpack:               app.BuildpackURL,
   103  		Name:                    app.Name,
   104  		SpaceGUID:               app.SpaceGUID,
   105  		Instances:               app.InstanceCount,
   106  		Memory:                  app.Memory,
   107  		DiskQuota:               app.DiskQuota,
   108  		StackGUID:               app.StackGUID,
   109  		Command:                 app.Command,
   110  		HealthCheckType:         app.HealthCheckType,
   111  		HealthCheckTimeout:      app.HealthCheckTimeout,
   112  		HealthCheckHTTPEndpoint: app.HealthCheckHTTPEndpoint,
   113  		DockerImage:             app.DockerImage,
   114  		Diego:                   app.Diego,
   115  		EnableSSH:               app.EnableSSH,
   116  		PackageUpdatedAt:        app.PackageUpdatedAt,
   117  		AppPorts:                app.AppPorts,
   118  	}
   119  
   120  	if app.State != nil {
   121  		state := strings.ToUpper(*app.State)
   122  		entity.State = &state
   123  	}
   124  
   125  	if app.EnvironmentVars != nil && *app.EnvironmentVars != nil {
   126  		entity.EnvironmentJSON = app.EnvironmentVars
   127  	}
   128  
   129  	if app.DockerUsername != nil {
   130  		creds := DockerCredentials{
   131  			Username: *app.DockerUsername,
   132  			Password: *app.DockerPassword,
   133  		}
   134  		entity.DockerCredentials = &creds
   135  	}
   136  
   137  	return entity
   138  }
   139  
   140  func (resource ApplicationResource) ToFields() (app models.ApplicationFields) {
   141  	entity := resource.Entity
   142  	app.GUID = resource.Metadata.GUID
   143  
   144  	if entity.Name != nil {
   145  		app.Name = *entity.Name
   146  	}
   147  	if entity.Memory != nil {
   148  		app.Memory = *entity.Memory
   149  	}
   150  	if entity.DiskQuota != nil {
   151  		app.DiskQuota = *entity.DiskQuota
   152  	}
   153  	if entity.Instances != nil {
   154  		app.InstanceCount = *entity.Instances
   155  	}
   156  	if entity.State != nil {
   157  		app.State = strings.ToLower(*entity.State)
   158  	}
   159  	if entity.EnvironmentJSON != nil {
   160  		app.EnvironmentVars = *entity.EnvironmentJSON
   161  	}
   162  	if entity.SpaceGUID != nil {
   163  		app.SpaceGUID = *entity.SpaceGUID
   164  	}
   165  	if entity.DetectedStartCommand != nil {
   166  		app.DetectedStartCommand = *entity.DetectedStartCommand
   167  	}
   168  	if entity.Command != nil {
   169  		app.Command = *entity.Command
   170  	}
   171  	if entity.PackageState != nil {
   172  		app.PackageState = *entity.PackageState
   173  	}
   174  	if entity.StagingFailedReason != nil {
   175  		app.StagingFailedReason = *entity.StagingFailedReason
   176  	}
   177  	if entity.DockerImage != nil {
   178  		app.DockerImage = *entity.DockerImage
   179  	}
   180  	if entity.Buildpack != nil {
   181  		app.Buildpack = *entity.Buildpack
   182  	}
   183  	if entity.DetectedBuildpack != nil {
   184  		app.DetectedBuildpack = *entity.DetectedBuildpack
   185  	}
   186  	if entity.HealthCheckType != nil {
   187  		app.HealthCheckType = *entity.HealthCheckType
   188  	}
   189  	if entity.HealthCheckHTTPEndpoint != nil {
   190  		app.HealthCheckHTTPEndpoint = *entity.HealthCheckHTTPEndpoint
   191  	}
   192  	if entity.Diego != nil {
   193  		app.Diego = *entity.Diego
   194  	}
   195  	if entity.EnableSSH != nil {
   196  		app.EnableSSH = *entity.EnableSSH
   197  	}
   198  	if entity.PackageUpdatedAt != nil {
   199  		app.PackageUpdatedAt = entity.PackageUpdatedAt
   200  	}
   201  
   202  	return
   203  }
   204  
   205  func (resource ApplicationResource) ToModel() (app models.Application) {
   206  	app.ApplicationFields = resource.ToFields()
   207  
   208  	entity := resource.Entity
   209  	if entity.Stack != nil {
   210  		app.Stack = entity.Stack.ToFields()
   211  	}
   212  
   213  	if entity.Routes != nil {
   214  		for _, routeResource := range *entity.Routes {
   215  			app.Routes = append(app.Routes, routeResource.ToModel())
   216  		}
   217  	}
   218  
   219  	return
   220  }