forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_summary.go
120 lines (96 loc) · 3.39 KB
/
service_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
package api
import (
"fmt"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type ServiceInstancesSummaries struct {
Apps []ServiceInstanceSummaryApp
ServiceInstances []ServiceInstanceSummary `json:"services"`
}
func (resource ServiceInstancesSummaries) ToModels() []models.ServiceInstance {
var instances []models.ServiceInstance
for _, instanceSummary := range resource.ServiceInstances {
applicationNames := resource.findApplicationNamesForInstance(instanceSummary.Name)
planSummary := instanceSummary.ServicePlan
servicePlan := models.ServicePlanFields{}
servicePlan.Name = planSummary.Name
servicePlan.GUID = planSummary.GUID
offeringSummary := planSummary.ServiceOffering
serviceOffering := models.ServiceOfferingFields{}
serviceOffering.Label = offeringSummary.Label
serviceOffering.Provider = offeringSummary.Provider
serviceOffering.Version = offeringSummary.Version
instance := models.ServiceInstance{}
instance.Name = instanceSummary.Name
instance.LastOperation.Type = instanceSummary.LastOperation.Type
instance.LastOperation.State = instanceSummary.LastOperation.State
instance.LastOperation.Description = instanceSummary.LastOperation.Description
instance.ApplicationNames = applicationNames
instance.ServicePlan = servicePlan
instance.ServiceOffering = serviceOffering
instances = append(instances, instance)
}
return instances
}
func (resource ServiceInstancesSummaries) findApplicationNamesForInstance(instanceName string) []string {
var applicationNames []string
for _, app := range resource.Apps {
for _, name := range app.ServiceNames {
if name == instanceName {
applicationNames = append(applicationNames, app.Name)
}
}
}
return applicationNames
}
type ServiceInstanceSummaryApp struct {
Name string
ServiceNames []string `json:"service_names"`
}
type LastOperationSummary struct {
Type string `json:"type"`
State string `json:"state"`
Description string `json:"description"`
}
type ServiceInstanceSummary struct {
Name string
LastOperation LastOperationSummary `json:"last_operation"`
ServicePlan ServicePlanSummary `json:"service_plan"`
}
type ServicePlanSummary struct {
Name string
GUID string
ServiceOffering ServiceOfferingSummary `json:"service"`
}
type ServiceOfferingSummary struct {
Label string
Provider string
Version string
}
//go:generate counterfeiter . ServiceSummaryRepository
type ServiceSummaryRepository interface {
GetSummariesInCurrentSpace() ([]models.ServiceInstance, error)
}
type CloudControllerServiceSummaryRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerServiceSummaryRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerServiceSummaryRepository {
return CloudControllerServiceSummaryRepository{
config: config,
gateway: gateway,
}
}
func (repo CloudControllerServiceSummaryRepository) GetSummariesInCurrentSpace() ([]models.ServiceInstance, error) {
var instances []models.ServiceInstance
path := fmt.Sprintf("%s/v2/spaces/%s/summary", repo.config.APIEndpoint(), repo.config.SpaceFields().GUID)
resource := new(ServiceInstancesSummaries)
err := repo.gateway.GetResource(path, resource)
if err != nil {
return nil, err
}
instances = resource.ToModels()
return instances, nil
}