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