github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/resources/applications.go (about)

     1  package resources
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/cloudfoundry/cli/cf/models"
     7  )
     8  
     9  type PaginatedApplicationResources struct {
    10  	Resources []ApplicationResource
    11  }
    12  
    13  type AppRouteEntity struct {
    14  	Host   string
    15  	Domain struct {
    16  		Resource
    17  		Entity struct {
    18  			Name string
    19  		}
    20  	}
    21  }
    22  
    23  type AppRouteResource struct {
    24  	Resource
    25  	Entity AppRouteEntity
    26  }
    27  
    28  type AppFileResource struct {
    29  	Path string `json:"fn"`
    30  	Sha1 string `json:"sha1"`
    31  	Size int64  `json:"size"`
    32  }
    33  
    34  type ApplicationResource struct {
    35  	Resource
    36  	Entity ApplicationEntity
    37  }
    38  
    39  type ApplicationEntity struct {
    40  	Name                 *string                 `json:"name,omitempty"`
    41  	Command              *string                 `json:"command,omitempty"`
    42  	DetectedStartCommand *string                 `json:"detected_start_command,omitempty"`
    43  	State                *string                 `json:"state,omitempty"`
    44  	SpaceGuid            *string                 `json:"space_guid,omitempty"`
    45  	Instances            *int                    `json:"instances,omitempty"`
    46  	Memory               *int64                  `json:"memory,omitempty"`
    47  	DiskQuota            *int64                  `json:"disk_quota,omitempty"`
    48  	StackGuid            *string                 `json:"stack_guid,omitempty"`
    49  	Stack                *StackResource          `json:"stack,omitempty"`
    50  	Routes               *[]AppRouteResource     `json:"routes,omitempty"`
    51  	Buildpack            *string                 `json:"buildpack,omitempty"`
    52  	DetectedBuildpack    *string                 `json:"detected_buildpack,omitempty"`
    53  	EnvironmentJson      *map[string]interface{} `json:"environment_json,omitempty"`
    54  	HealthCheckTimeout   *int                    `json:"health_check_timeout,omitempty"`
    55  	PackageState         *string                 `json:"package_state,omitempty"`
    56  	StagingFailedReason  *string                 `json:"staging_failed_reason,omitempty"`
    57  	Diego                bool                    `json:"diego,omitempty"`
    58  }
    59  
    60  func (resource AppRouteResource) ToFields() (route models.RouteSummary) {
    61  	route.Guid = resource.Metadata.Guid
    62  	route.Host = resource.Entity.Host
    63  	return
    64  }
    65  
    66  func (resource AppRouteResource) ToModel() (route models.RouteSummary) {
    67  	route.Guid = resource.Metadata.Guid
    68  	route.Host = resource.Entity.Host
    69  	route.Domain.Guid = resource.Entity.Domain.Metadata.Guid
    70  	route.Domain.Name = resource.Entity.Domain.Entity.Name
    71  	return
    72  }
    73  
    74  func NewApplicationEntityFromAppParams(app models.AppParams) ApplicationEntity {
    75  	entity := ApplicationEntity{
    76  		Buildpack:          app.BuildpackUrl,
    77  		Name:               app.Name,
    78  		SpaceGuid:          app.SpaceGuid,
    79  		Instances:          app.InstanceCount,
    80  		Memory:             app.Memory,
    81  		DiskQuota:          app.DiskQuota,
    82  		StackGuid:          app.StackGuid,
    83  		Command:            app.Command,
    84  		HealthCheckTimeout: app.HealthCheckTimeout,
    85  	}
    86  	if app.State != nil {
    87  		state := strings.ToUpper(*app.State)
    88  		entity.State = &state
    89  	}
    90  	if app.EnvironmentVars != nil && *app.EnvironmentVars != nil {
    91  		entity.EnvironmentJson = app.EnvironmentVars
    92  	}
    93  	return entity
    94  }
    95  
    96  func (resource ApplicationResource) ToFields() (app models.ApplicationFields) {
    97  	entity := resource.Entity
    98  	app.Guid = resource.Metadata.Guid
    99  
   100  	if entity.Name != nil {
   101  		app.Name = *entity.Name
   102  	}
   103  	if entity.Memory != nil {
   104  		app.Memory = *entity.Memory
   105  	}
   106  	if entity.DiskQuota != nil {
   107  		app.DiskQuota = *entity.DiskQuota
   108  	}
   109  	if entity.Instances != nil {
   110  		app.InstanceCount = *entity.Instances
   111  	}
   112  	if entity.State != nil {
   113  		app.State = strings.ToLower(*entity.State)
   114  	}
   115  	if entity.EnvironmentJson != nil {
   116  		app.EnvironmentVars = *entity.EnvironmentJson
   117  	}
   118  	if entity.SpaceGuid != nil {
   119  		app.SpaceGuid = *entity.SpaceGuid
   120  	}
   121  	if entity.DetectedStartCommand != nil {
   122  		app.DetectedStartCommand = *entity.DetectedStartCommand
   123  	}
   124  	if entity.Command != nil {
   125  		app.Command = *entity.Command
   126  	}
   127  	if entity.PackageState != nil {
   128  		app.PackageState = *entity.PackageState
   129  	}
   130  	if entity.StagingFailedReason != nil {
   131  		app.StagingFailedReason = *entity.StagingFailedReason
   132  	}
   133  	if entity.Buildpack != nil {
   134  		app.Buildpack = *entity.Buildpack
   135  	}
   136  	if entity.DetectedBuildpack != nil {
   137  		app.DetectedBuildpack = *entity.DetectedBuildpack
   138  	}
   139  
   140  	app.Diego = entity.Diego
   141  
   142  	return
   143  }
   144  
   145  func (resource ApplicationResource) ToModel() (app models.Application) {
   146  	app.ApplicationFields = resource.ToFields()
   147  
   148  	entity := resource.Entity
   149  	if entity.Stack != nil {
   150  		app.Stack = entity.Stack.ToFields()
   151  	}
   152  
   153  	if entity.Routes != nil {
   154  		for _, routeResource := range *entity.Routes {
   155  			app.Routes = append(app.Routes, routeResource.ToModel())
   156  		}
   157  	}
   158  
   159  	return
   160  }