forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_instance.go
119 lines (101 loc) · 3.57 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
package ccv2
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ServiceInstanceType is the type of the Service Instance.
type ServiceInstanceType string
const (
// UserProvidedService is a Service Instance that is created by a user.
UserProvidedService ServiceInstanceType = "user_provided_service_instance"
// ManagedService is a Service Instance that is managed by a service broker.
ManagedService ServiceInstanceType = "managed_service_instance"
)
// ServiceInstance represents a Cloud Controller Service Instance.
type ServiceInstance struct {
GUID string
Name string
Type ServiceInstanceType
}
// 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
Type string
}
}
err := json.Unmarshal(data, &ccServiceInstance)
if err != nil {
return err
}
serviceInstance.GUID = ccServiceInstance.Metadata.GUID
serviceInstance.Name = ccServiceInstance.Entity.Name
serviceInstance.Type = ServiceInstanceType(ccServiceInstance.Entity.Type)
return nil
}
// UserProvidedService returns true if the Service Instance is a user provided
// service.
func (serviceInstance ServiceInstance) UserProvided() bool {
return serviceInstance.Type == UserProvidedService
}
// Managed returns true if the Service Instance is a managed service.
func (serviceInstance ServiceInstance) Managed() bool {
return serviceInstance.Type == ManagedService
}
// GetServiceInstances returns back a list of *managed* Service Instances based
// off of the provided queries.
func (client *Client) GetServiceInstances(queries []Query) ([]ServiceInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceInstancesRequest,
Query: FormatQueryParameters(queries),
})
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 queries provided. User provided services will be included
// if includeUserProvidedServices is set to true.
func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, queries []Query) ([]ServiceInstance, Warnings, error) {
query := FormatQueryParameters(queries)
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
}