github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+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  	EnvironmentJson      *map[string]interface{} `json:"environment_json,omitempty"`
    53  	HealthCheckTimeout   *int                    `json:"health_check_timeout,omitempty"`
    54  }
    55  
    56  func (resource AppRouteResource) ToFields() (route models.RouteSummary) {
    57  	route.Guid = resource.Metadata.Guid
    58  	route.Host = resource.Entity.Host
    59  	return
    60  }
    61  
    62  func (resource AppRouteResource) ToModel() (route models.RouteSummary) {
    63  	route.Guid = resource.Metadata.Guid
    64  	route.Host = resource.Entity.Host
    65  	route.Domain.Guid = resource.Entity.Domain.Metadata.Guid
    66  	route.Domain.Name = resource.Entity.Domain.Entity.Name
    67  	return
    68  }
    69  
    70  func NewApplicationEntityFromAppParams(app models.AppParams) ApplicationEntity {
    71  	entity := ApplicationEntity{
    72  		Buildpack:          app.BuildpackUrl,
    73  		Name:               app.Name,
    74  		SpaceGuid:          app.SpaceGuid,
    75  		Instances:          app.InstanceCount,
    76  		Memory:             app.Memory,
    77  		DiskQuota:          app.DiskQuota,
    78  		StackGuid:          app.StackGuid,
    79  		Command:            app.Command,
    80  		HealthCheckTimeout: app.HealthCheckTimeout,
    81  	}
    82  	if app.State != nil {
    83  		state := strings.ToUpper(*app.State)
    84  		entity.State = &state
    85  	}
    86  	if app.EnvironmentVars != nil && *app.EnvironmentVars != nil {
    87  		entity.EnvironmentJson = app.EnvironmentVars
    88  	}
    89  	return entity
    90  }
    91  
    92  func (resource ApplicationResource) ToFields() (app models.ApplicationFields) {
    93  	entity := resource.Entity
    94  	app.Guid = resource.Metadata.Guid
    95  
    96  	if entity.Name != nil {
    97  		app.Name = *entity.Name
    98  	}
    99  	if entity.Memory != nil {
   100  		app.Memory = *entity.Memory
   101  	}
   102  	if entity.DiskQuota != nil {
   103  		app.DiskQuota = *entity.DiskQuota
   104  	}
   105  	if entity.Instances != nil {
   106  		app.InstanceCount = *entity.Instances
   107  	}
   108  	if entity.State != nil {
   109  		app.State = strings.ToLower(*entity.State)
   110  	}
   111  	if entity.EnvironmentJson != nil {
   112  		app.EnvironmentVars = *entity.EnvironmentJson
   113  	}
   114  	if entity.SpaceGuid != nil {
   115  		app.SpaceGuid = *entity.SpaceGuid
   116  	}
   117  	if entity.DetectedStartCommand != nil {
   118  		app.DetectedStartCommand = *entity.DetectedStartCommand
   119  	}
   120  	if entity.Command != nil {
   121  		app.Command = *entity.Command
   122  	}
   123  	return
   124  }
   125  
   126  func (resource ApplicationResource) ToModel() (app models.Application) {
   127  	app.ApplicationFields = resource.ToFields()
   128  
   129  	entity := resource.Entity
   130  	if entity.Stack != nil {
   131  		app.Stack = entity.Stack.ToFields()
   132  	}
   133  
   134  	if entity.Routes != nil {
   135  		for _, routeResource := range *entity.Routes {
   136  			app.Routes = append(app.Routes, routeResource.ToModel())
   137  		}
   138  	}
   139  
   140  	return
   141  }