github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+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  	DockerImage             *string                 `json:"docker_image,omitempty"`
    72  	DockerCredentials       *DockerCredentials      `json:"docker_credentials,omitempty"`
    73  	EnableSSH               *bool                   `json:"enable_ssh,omitempty"`
    74  	PackageUpdatedAt        *time.Time              `json:"package_updated_at,omitempty"`
    75  }
    76  
    77  func (resource AppRouteResource) ToFields() (route models.RouteSummary) {
    78  	route.GUID = resource.Metadata.GUID
    79  	route.Host = resource.Entity.Host
    80  	return
    81  }
    82  
    83  func (resource AppRouteResource) ToModel() (route models.RouteSummary) {
    84  	route.GUID = resource.Metadata.GUID
    85  	route.Host = resource.Entity.Host
    86  	route.Domain.GUID = resource.Entity.Domain.Metadata.GUID
    87  	route.Domain.Name = resource.Entity.Domain.Entity.Name
    88  	return
    89  }
    90  
    91  func (resource AppFileResource) ToIntegrityFields() IntegrityFields {
    92  	return IntegrityFields{
    93  		Sha1: resource.Sha1,
    94  		Size: resource.Size,
    95  	}
    96  }
    97  
    98  func NewApplicationEntityFromAppParams(app models.AppParams) ApplicationEntity {
    99  	entity := ApplicationEntity{
   100  		Buildpack:               app.BuildpackURL,
   101  		Name:                    app.Name,
   102  		SpaceGUID:               app.SpaceGUID,
   103  		Instances:               app.InstanceCount,
   104  		Memory:                  app.Memory,
   105  		DiskQuota:               app.DiskQuota,
   106  		StackGUID:               app.StackGUID,
   107  		Command:                 app.Command,
   108  		HealthCheckType:         app.HealthCheckType,
   109  		HealthCheckTimeout:      app.HealthCheckTimeout,
   110  		HealthCheckHTTPEndpoint: app.HealthCheckHTTPEndpoint,
   111  		DockerImage:             app.DockerImage,
   112  		EnableSSH:               app.EnableSSH,
   113  		PackageUpdatedAt:        app.PackageUpdatedAt,
   114  	}
   115  
   116  	if app.State != nil {
   117  		state := strings.ToUpper(*app.State)
   118  		entity.State = &state
   119  	}
   120  
   121  	if app.EnvironmentVars != nil && *app.EnvironmentVars != nil {
   122  		entity.EnvironmentJSON = app.EnvironmentVars
   123  	}
   124  
   125  	if app.DockerUsername != nil {
   126  		creds := DockerCredentials{
   127  			Username: *app.DockerUsername,
   128  			Password: *app.DockerPassword,
   129  		}
   130  		entity.DockerCredentials = &creds
   131  	}
   132  
   133  	return entity
   134  }
   135  
   136  func (resource ApplicationResource) ToFields() (app models.ApplicationFields) {
   137  	entity := resource.Entity
   138  	app.GUID = resource.Metadata.GUID
   139  
   140  	if entity.Name != nil {
   141  		app.Name = *entity.Name
   142  	}
   143  	if entity.Memory != nil {
   144  		app.Memory = *entity.Memory
   145  	}
   146  	if entity.DiskQuota != nil {
   147  		app.DiskQuota = *entity.DiskQuota
   148  	}
   149  	if entity.Instances != nil {
   150  		app.InstanceCount = *entity.Instances
   151  	}
   152  	if entity.State != nil {
   153  		app.State = strings.ToLower(*entity.State)
   154  	}
   155  	if entity.EnvironmentJSON != nil {
   156  		app.EnvironmentVars = *entity.EnvironmentJSON
   157  	}
   158  	if entity.SpaceGUID != nil {
   159  		app.SpaceGUID = *entity.SpaceGUID
   160  	}
   161  	if entity.DetectedStartCommand != nil {
   162  		app.DetectedStartCommand = *entity.DetectedStartCommand
   163  	}
   164  	if entity.Command != nil {
   165  		app.Command = *entity.Command
   166  	}
   167  	if entity.PackageState != nil {
   168  		app.PackageState = *entity.PackageState
   169  	}
   170  	if entity.StagingFailedReason != nil {
   171  		app.StagingFailedReason = *entity.StagingFailedReason
   172  	}
   173  	if entity.DockerImage != nil {
   174  		app.DockerImage = *entity.DockerImage
   175  	}
   176  	if entity.Buildpack != nil {
   177  		app.Buildpack = *entity.Buildpack
   178  	}
   179  	if entity.DetectedBuildpack != nil {
   180  		app.DetectedBuildpack = *entity.DetectedBuildpack
   181  	}
   182  	if entity.HealthCheckType != nil {
   183  		app.HealthCheckType = *entity.HealthCheckType
   184  	}
   185  	if entity.HealthCheckHTTPEndpoint != nil {
   186  		app.HealthCheckHTTPEndpoint = *entity.HealthCheckHTTPEndpoint
   187  	}
   188  	if entity.EnableSSH != nil {
   189  		app.EnableSSH = *entity.EnableSSH
   190  	}
   191  	if entity.PackageUpdatedAt != nil {
   192  		app.PackageUpdatedAt = entity.PackageUpdatedAt
   193  	}
   194  
   195  	return
   196  }
   197  
   198  func (resource ApplicationResource) ToModel() (app models.Application) {
   199  	app.ApplicationFields = resource.ToFields()
   200  
   201  	entity := resource.Entity
   202  	if entity.Stack != nil {
   203  		app.Stack = entity.Stack.ToFields()
   204  	}
   205  
   206  	if entity.Routes != nil {
   207  		for _, routeResource := range *entity.Routes {
   208  			app.Routes = append(app.Routes, routeResource.ToModel())
   209  		}
   210  	}
   211  
   212  	return
   213  }