forked from cloudfoundry/cli
/
service_instance.go
201 lines (173 loc) · 6.67 KB
/
service_instance.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package ccv2
import (
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ServiceInstance represents a Cloud Controller Service Instance.
type ServiceInstance struct {
// GUID is the unique service instance identifier.
GUID string
// Name is the name given to the service instance.
Name string
// SpaceGUID is the unique identifier of the space that this service instance
// belongs to.
SpaceGUID string
// ServiceGUID is the unique identifier of the service that this service
// instance belongs to.
ServiceGUID string
// ServicePlanGUID is the unique identifier of the service plan that this
// service instance belongs to.
ServicePlanGUID string
// Type is the type of service instance.
Type constant.ServiceInstanceType
// Tags is a list of all tags for the service instance.
Tags []string
// DashboardURL is the service-broker provided URL to access administrative
// features of the service instance.
DashboardURL string
// LastOperation is the status of the last operation requested on the service
// instance.
LastOperation LastOperation
}
// Managed returns true if the Service Instance is a managed service.
func (serviceInstance ServiceInstance) Managed() bool {
return serviceInstance.Type == constant.ServiceInstanceTypeManagedService
}
// UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response.
func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error {
var ccServiceInstance struct {
Metadata internal.Metadata
Entity struct {
Name string `json:"name"`
SpaceGUID string `json:"space_guid"`
ServiceGUID string `json:"service_guid"`
ServicePlanGUID string `json:"service_plan_guid"`
Type string `json:"type"`
Tags []string `json:"tags"`
DashboardURL string `json:"dashboard_url"`
LastOperation struct {
Type string `json:"type"`
State string `json:"state"`
Description string `json:"description"`
UpdatedAt string `json:"updated_at"`
CreatedAt string `json:"created_at"`
} `json:"last_operation"`
}
}
err := cloudcontroller.DecodeJSON(data, &ccServiceInstance)
if err != nil {
return err
}
serviceInstance.GUID = ccServiceInstance.Metadata.GUID
serviceInstance.Name = ccServiceInstance.Entity.Name
serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID
serviceInstance.ServiceGUID = ccServiceInstance.Entity.ServiceGUID
serviceInstance.ServicePlanGUID = ccServiceInstance.Entity.ServicePlanGUID
serviceInstance.Type = constant.ServiceInstanceType(ccServiceInstance.Entity.Type)
serviceInstance.Tags = ccServiceInstance.Entity.Tags
serviceInstance.DashboardURL = ccServiceInstance.Entity.DashboardURL
serviceInstance.LastOperation = LastOperation(ccServiceInstance.Entity.LastOperation)
return nil
}
// UserProvided returns true if the Service Instance is a user provided
// service.
func (serviceInstance ServiceInstance) UserProvided() bool {
return serviceInstance.Type == constant.ServiceInstanceTypeUserProvidedService
}
// GetServiceInstance returns the service instance with the given GUID. This
// service can be either a managed or user provided.
func (client *Client) GetServiceInstance(serviceInstanceGUID string) (ServiceInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceInstanceRequest,
URIParams: Params{"service_instance_guid": serviceInstanceGUID},
})
if err != nil {
return ServiceInstance{}, nil, err
}
var serviceInstance ServiceInstance
response := cloudcontroller.Response{
Result: &serviceInstance,
}
err = client.connection.Make(request, &response)
return serviceInstance, response.Warnings, err
}
// GetServiceInstances returns back a list of *managed* Service Instances based
// off of the provided filters.
func (client *Client) GetServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceInstancesRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullInstancesList []ServiceInstance
warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
if instance, ok := item.(ServiceInstance); ok {
fullInstancesList = append(fullInstancesList, instance)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServiceInstance{},
Unexpected: item,
}
}
return nil
})
return fullInstancesList, warnings, err
}
// GetSpaceServiceInstances returns back a list of Service Instances based off
// of the space and filters provided. User provided services will be included
// if includeUserProvidedServices is set to true.
func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, filters ...Filter) ([]ServiceInstance, Warnings, error) {
query := ConvertFilterParameters(filters)
if includeUserProvidedServices {
query.Add("return_user_provided_service_instances", "true")
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSpaceServiceInstancesRequest,
URIParams: map[string]string{"guid": spaceGUID},
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullInstancesList []ServiceInstance
warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
if instance, ok := item.(ServiceInstance); ok {
fullInstancesList = append(fullInstancesList, instance)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServiceInstance{},
Unexpected: item,
}
}
return nil
})
return fullInstancesList, warnings, err
}
// GetUserProvidedServiceInstances returns back a list of *user provided* Service Instances based
// off the provided queries.
func (client *Client) GetUserProvidedServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetUserProvidedServiceInstancesRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullInstancesList []ServiceInstance
warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
if instance, ok := item.(ServiceInstance); ok {
fullInstancesList = append(fullInstancesList, instance)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServiceInstance{},
Unexpected: item,
}
}
return nil
})
return fullInstancesList, warnings, err
}