github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/app_summary.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/models" 10 "code.cloudfoundry.org/cli/cf/net" 11 ) 12 13 type ApplicationSummaries struct { 14 Apps []ApplicationFromSummary 15 } 16 17 func (resource ApplicationSummaries) ToModels() (apps []models.ApplicationFields) { 18 for _, application := range resource.Apps { 19 apps = append(apps, application.ToFields()) 20 } 21 return 22 } 23 24 type ApplicationFromSummary struct { 25 GUID string 26 Name string 27 Routes []RouteSummary 28 Services []ServicePlanSummary 29 Diego bool `json:"diego,omitempty"` 30 RunningInstances int `json:"running_instances"` 31 Memory int64 32 Instances int 33 DiskQuota int64 `json:"disk_quota"` 34 AppPorts []int `json:"ports"` 35 URLs []string 36 EnvironmentVars map[string]interface{} `json:"environment_json,omitempty"` 37 HealthCheckTimeout int `json:"health_check_timeout"` 38 HealthCheckType string `json:"health_check_type"` 39 HealthCheckHTTPEndpoint string `json:"health_check_http_endpoint"` 40 State string 41 DetectedStartCommand string `json:"detected_start_command"` 42 SpaceGUID string `json:"space_guid"` 43 StackGUID string `json:"stack_guid"` 44 Command string `json:"command"` 45 PackageState string `json:"package_state"` 46 PackageUpdatedAt *time.Time `json:"package_updated_at"` 47 Buildpack string 48 } 49 50 func (resource ApplicationFromSummary) ToFields() (app models.ApplicationFields) { 51 app = models.ApplicationFields{} 52 app.GUID = resource.GUID 53 app.Name = resource.Name 54 app.Diego = resource.Diego 55 app.State = strings.ToLower(resource.State) 56 app.InstanceCount = resource.Instances 57 app.DiskQuota = resource.DiskQuota 58 app.RunningInstances = resource.RunningInstances 59 app.Memory = resource.Memory 60 app.SpaceGUID = resource.SpaceGUID 61 app.StackGUID = resource.StackGUID 62 app.PackageUpdatedAt = resource.PackageUpdatedAt 63 app.PackageState = resource.PackageState 64 app.DetectedStartCommand = resource.DetectedStartCommand 65 app.HealthCheckTimeout = resource.HealthCheckTimeout 66 app.HealthCheckType = resource.HealthCheckType 67 app.HealthCheckHTTPEndpoint = resource.HealthCheckHTTPEndpoint 68 app.BuildpackURL = resource.Buildpack 69 app.Command = resource.Command 70 app.AppPorts = resource.AppPorts 71 app.EnvironmentVars = resource.EnvironmentVars 72 73 return 74 } 75 76 func (resource ApplicationFromSummary) ToModel() models.Application { 77 var app models.Application 78 79 app.ApplicationFields = resource.ToFields() 80 81 routes := []models.RouteSummary{} 82 for _, route := range resource.Routes { 83 routes = append(routes, route.ToModel()) 84 } 85 app.Routes = routes 86 87 services := []models.ServicePlanSummary{} 88 for _, service := range resource.Services { 89 services = append(services, service.ToModel()) 90 } 91 92 app.Routes = routes 93 app.Services = services 94 95 return app 96 } 97 98 type RouteSummary struct { 99 GUID string 100 Host string 101 Path string 102 Port int 103 Domain DomainSummary 104 } 105 106 func (resource RouteSummary) ToModel() (route models.RouteSummary) { 107 domain := models.DomainFields{} 108 domain.GUID = resource.Domain.GUID 109 domain.Name = resource.Domain.Name 110 domain.Shared = resource.Domain.OwningOrganizationGUID != "" 111 112 route.GUID = resource.GUID 113 route.Host = resource.Host 114 route.Path = resource.Path 115 route.Port = resource.Port 116 route.Domain = domain 117 return 118 } 119 120 func (resource ServicePlanSummary) ToModel() (route models.ServicePlanSummary) { 121 route.GUID = resource.GUID 122 route.Name = resource.Name 123 return 124 } 125 126 type DomainSummary struct { 127 GUID string 128 Name string 129 OwningOrganizationGUID string 130 } 131 132 //go:generate counterfeiter . AppSummaryRepository 133 134 type AppSummaryRepository interface { 135 GetSummariesInCurrentSpace() (apps []models.Application, apiErr error) 136 GetSummary(appGUID string) (summary models.Application, apiErr error) 137 } 138 139 type CloudControllerAppSummaryRepository struct { 140 config coreconfig.Reader 141 gateway net.Gateway 142 } 143 144 func NewCloudControllerAppSummaryRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerAppSummaryRepository) { 145 repo.config = config 146 repo.gateway = gateway 147 return 148 } 149 150 func (repo CloudControllerAppSummaryRepository) GetSummariesInCurrentSpace() ([]models.Application, error) { 151 resources := new(ApplicationSummaries) 152 153 path := fmt.Sprintf("%s/v2/spaces/%s/summary", repo.config.APIEndpoint(), repo.config.SpaceFields().GUID) 154 err := repo.gateway.GetResource(path, resources) 155 if err != nil { 156 return []models.Application{}, err 157 } 158 159 apps := make([]models.Application, len(resources.Apps)) 160 for i, resource := range resources.Apps { 161 apps[i] = resource.ToModel() 162 } 163 164 return apps, nil 165 } 166 167 func (repo CloudControllerAppSummaryRepository) GetSummary(appGUID string) (summary models.Application, apiErr error) { 168 path := fmt.Sprintf("%s/v2/apps/%s/summary", repo.config.APIEndpoint(), appGUID) 169 summaryResponse := new(ApplicationFromSummary) 170 apiErr = repo.gateway.GetResource(path, summaryResponse) 171 if apiErr != nil { 172 return 173 } 174 175 summary = summaryResponse.ToModel() 176 177 return 178 }