github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+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 ApplicationEntity struct {
    47  	Name                 *string                 `json:"name,omitempty"`
    48  	Command              *string                 `json:"command,omitempty"`
    49  	DetectedStartCommand *string                 `json:"detected_start_command,omitempty"`
    50  	State                *string                 `json:"state,omitempty"`
    51  	SpaceGUID            *string                 `json:"space_guid,omitempty"`
    52  	Instances            *int                    `json:"instances,omitempty"`
    53  	Memory               *int64                  `json:"memory,omitempty"`
    54  	DiskQuota            *int64                  `json:"disk_quota,omitempty"`
    55  	StackGUID            *string                 `json:"stack_guid,omitempty"`
    56  	Stack                *StackResource          `json:"stack,omitempty"`
    57  	Routes               *[]AppRouteResource     `json:"routes,omitempty"`
    58  	Buildpack            *string                 `json:"buildpack,omitempty"`
    59  	DetectedBuildpack    *string                 `json:"detected_buildpack,omitempty"`
    60  	EnvironmentJSON      *map[string]interface{} `json:"environment_json,omitempty"`
    61  	HealthCheckType      *string                 `json:"health_check_type,omitempty"`
    62  	HealthCheckTimeout   *int                    `json:"health_check_timeout,omitempty"`
    63  	PackageState         *string                 `json:"package_state,omitempty"`
    64  	StagingFailedReason  *string                 `json:"staging_failed_reason,omitempty"`
    65  	Diego                *bool                   `json:"diego,omitempty"`
    66  	DockerImage          *string                 `json:"docker_image,omitempty"`
    67  	EnableSSH            *bool                   `json:"enable_ssh,omitempty"`
    68  	PackageUpdatedAt     *time.Time              `json:"package_updated_at,omitempty"`
    69  	AppPorts             *[]int                  `json:"ports,omitempty"`
    70  }
    71  
    72  func (resource AppRouteResource) ToFields() (route models.RouteSummary) {
    73  	route.GUID = resource.Metadata.GUID
    74  	route.Host = resource.Entity.Host
    75  	return
    76  }
    77  
    78  func (resource AppRouteResource) ToModel() (route models.RouteSummary) {
    79  	route.GUID = resource.Metadata.GUID
    80  	route.Host = resource.Entity.Host
    81  	route.Domain.GUID = resource.Entity.Domain.Metadata.GUID
    82  	route.Domain.Name = resource.Entity.Domain.Entity.Name
    83  	return
    84  }
    85  
    86  func (resource AppFileResource) ToIntegrityFields() IntegrityFields {
    87  	return IntegrityFields{
    88  		Sha1: resource.Sha1,
    89  		Size: resource.Size,
    90  	}
    91  }
    92  
    93  func NewApplicationEntityFromAppParams(app models.AppParams) ApplicationEntity {
    94  	entity := ApplicationEntity{
    95  		Buildpack:          app.BuildpackURL,
    96  		Name:               app.Name,
    97  		SpaceGUID:          app.SpaceGUID,
    98  		Instances:          app.InstanceCount,
    99  		Memory:             app.Memory,
   100  		DiskQuota:          app.DiskQuota,
   101  		StackGUID:          app.StackGUID,
   102  		Command:            app.Command,
   103  		HealthCheckType:    app.HealthCheckType,
   104  		HealthCheckTimeout: app.HealthCheckTimeout,
   105  		DockerImage:        app.DockerImage,
   106  		Diego:              app.Diego,
   107  		EnableSSH:          app.EnableSSH,
   108  		PackageUpdatedAt:   app.PackageUpdatedAt,
   109  		AppPorts:           app.AppPorts,
   110  	}
   111  
   112  	if app.State != nil {
   113  		state := strings.ToUpper(*app.State)
   114  		entity.State = &state
   115  	}
   116  
   117  	if app.EnvironmentVars != nil && *app.EnvironmentVars != nil {
   118  		entity.EnvironmentJSON = app.EnvironmentVars
   119  	}
   120  
   121  	return entity
   122  }
   123  
   124  func (resource ApplicationResource) ToFields() (app models.ApplicationFields) {
   125  	entity := resource.Entity
   126  	app.GUID = resource.Metadata.GUID
   127  
   128  	if entity.Name != nil {
   129  		app.Name = *entity.Name
   130  	}
   131  	if entity.Memory != nil {
   132  		app.Memory = *entity.Memory
   133  	}
   134  	if entity.DiskQuota != nil {
   135  		app.DiskQuota = *entity.DiskQuota
   136  	}
   137  	if entity.Instances != nil {
   138  		app.InstanceCount = *entity.Instances
   139  	}
   140  	if entity.State != nil {
   141  		app.State = strings.ToLower(*entity.State)
   142  	}
   143  	if entity.EnvironmentJSON != nil {
   144  		app.EnvironmentVars = *entity.EnvironmentJSON
   145  	}
   146  	if entity.SpaceGUID != nil {
   147  		app.SpaceGUID = *entity.SpaceGUID
   148  	}
   149  	if entity.DetectedStartCommand != nil {
   150  		app.DetectedStartCommand = *entity.DetectedStartCommand
   151  	}
   152  	if entity.Command != nil {
   153  		app.Command = *entity.Command
   154  	}
   155  	if entity.PackageState != nil {
   156  		app.PackageState = *entity.PackageState
   157  	}
   158  	if entity.StagingFailedReason != nil {
   159  		app.StagingFailedReason = *entity.StagingFailedReason
   160  	}
   161  	if entity.DockerImage != nil {
   162  		app.DockerImage = *entity.DockerImage
   163  	}
   164  	if entity.Buildpack != nil {
   165  		app.Buildpack = *entity.Buildpack
   166  	}
   167  	if entity.DetectedBuildpack != nil {
   168  		app.DetectedBuildpack = *entity.DetectedBuildpack
   169  	}
   170  	if entity.HealthCheckType != nil {
   171  		app.HealthCheckType = *entity.HealthCheckType
   172  	}
   173  	if entity.Diego != nil {
   174  		app.Diego = *entity.Diego
   175  	}
   176  	if entity.EnableSSH != nil {
   177  		app.EnableSSH = *entity.EnableSSH
   178  	}
   179  	if entity.PackageUpdatedAt != nil {
   180  		app.PackageUpdatedAt = entity.PackageUpdatedAt
   181  	}
   182  
   183  	return
   184  }
   185  
   186  func (resource ApplicationResource) ToModel() (app models.Application) {
   187  	app.ApplicationFields = resource.ToFields()
   188  
   189  	entity := resource.Entity
   190  	if entity.Stack != nil {
   191  		app.Stack = entity.Stack.ToFields()
   192  	}
   193  
   194  	if entity.Routes != nil {
   195  		for _, routeResource := range *entity.Routes {
   196  			app.Routes = append(app.Routes, routeResource.ToModel())
   197  		}
   198  	}
   199  
   200  	return
   201  }