forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_summary.go
125 lines (105 loc) · 3.27 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
package api
import (
"fmt"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
"strings"
)
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
RunningInstances int `json:"running_instances"`
Memory int64
Instances int
DiskQuota int64 `json:"disk_quota"`
Urls []string
State string
SpaceGuid string `json:"space_guid"`
}
func (resource ApplicationFromSummary) ToFields() (app models.ApplicationFields) {
app = models.ApplicationFields{}
app.Guid = resource.Guid
app.Name = resource.Name
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
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
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
}
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
}