forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_summary.go
152 lines (126 loc) · 4.1 KB
/
app_summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package api
import (
"fmt"
"strings"
"time"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type ApplicationSummaries struct {
Apps []ApplicationFromSummary
}
func (resource ApplicationSummaries) ToModels() (apps []models.ApplicationFields) {
for _, application := range resource.Apps {
apps = append(apps, application.ToFields())
}
return
}
type ApplicationFromSummary struct {
Guid string
Name string
Routes []RouteSummary
Services []ServicePlanSummary
Diego bool `json:"diego,omitempty"`
RunningInstances int `json:"running_instances"`
Memory int64
Instances int
DiskQuota int64 `json:"disk_quota"`
Urls []string
EnvironmentVars map[string]interface{} `json:"environment_json,omitempty"`
HealthCheckTimeout int `json:"health_check_timeout"`
State string
SpaceGuid string `json:"space_guid"`
PackageUpdatedAt *time.Time `json:"package_updated_at"`
}
func (resource ApplicationFromSummary) ToFields() (app models.ApplicationFields) {
app = models.ApplicationFields{}
app.Guid = resource.Guid
app.Name = resource.Name
app.Diego = resource.Diego
app.State = strings.ToLower(resource.State)
app.InstanceCount = resource.Instances
app.DiskQuota = resource.DiskQuota
app.RunningInstances = resource.RunningInstances
app.Memory = resource.Memory
app.SpaceGuid = resource.SpaceGuid
app.PackageUpdatedAt = resource.PackageUpdatedAt
app.HealthCheckTimeout = resource.HealthCheckTimeout
return
}
func (resource ApplicationFromSummary) ToModel() (app models.Application) {
app.ApplicationFields = resource.ToFields()
routes := []models.RouteSummary{}
for _, route := range resource.Routes {
routes = append(routes, route.ToModel())
}
app.Routes = routes
services := []models.ServicePlanSummary{}
for _, service := range resource.Services {
services = append(services, service.ToModel())
}
app.EnvironmentVars = resource.EnvironmentVars
app.Routes = routes
app.Services = services
return
}
type RouteSummary struct {
Guid string
Host string
Domain DomainSummary
}
func (resource RouteSummary) ToModel() (route models.RouteSummary) {
domain := models.DomainFields{}
domain.Guid = resource.Domain.Guid
domain.Name = resource.Domain.Name
domain.Shared = resource.Domain.OwningOrganizationGuid != ""
route.Guid = resource.Guid
route.Host = resource.Host
route.Domain = domain
return
}
func (resource ServicePlanSummary) ToModel() (route models.ServicePlanSummary) {
route.Guid = resource.Guid
route.Name = resource.Name
return
}
type DomainSummary struct {
Guid string
Name string
OwningOrganizationGuid string
}
type AppSummaryRepository interface {
GetSummariesInCurrentSpace() (apps []models.Application, apiErr error)
GetSummary(appGuid string) (summary models.Application, apiErr error)
}
type CloudControllerAppSummaryRepository struct {
config core_config.Reader
gateway net.Gateway
}
func NewCloudControllerAppSummaryRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerAppSummaryRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerAppSummaryRepository) GetSummariesInCurrentSpace() (apps []models.Application, apiErr error) {
resources := new(ApplicationSummaries)
path := fmt.Sprintf("%s/v2/spaces/%s/summary", repo.config.ApiEndpoint(), repo.config.SpaceFields().Guid)
apiErr = repo.gateway.GetResource(path, resources)
if apiErr != nil {
return
}
for _, resource := range resources.Apps {
apps = append(apps, resource.ToModel())
}
return
}
func (repo CloudControllerAppSummaryRepository) GetSummary(appGuid string) (summary models.Application, apiErr error) {
path := fmt.Sprintf("%s/v2/apps/%s/summary", repo.config.ApiEndpoint(), appGuid)
summaryResponse := new(ApplicationFromSummary)
apiErr = repo.gateway.GetResource(path, summaryResponse)
if apiErr != nil {
return
}
summary = summaryResponse.ToModel()
return
}