forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
154 lines (132 loc) · 4.56 KB
/
service.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
package ccv2
import (
"encoding/json"
"net/url"
"strconv"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Service represents a Cloud Controller Service.
type Service struct {
// GUID is the unique Service identifier.
GUID string
// Label is the name of the service.
Label string
// Description is a short blurb describing the service.
Description string
// DocumentationURL is a url that points to a documentation page for the
// service.
DocumentationURL string
// ServiceBrokerName is name of the service broker associated with the service
ServiceBrokerName string
// Extra is a field with extra data pertaining to the service.
Extra ServiceExtra
}
// DeleteService deletes the service with the given GUID, and returns any errors and warnings.
func (client *Client) DeleteService(serviceGUID string, purge bool) (Warnings, error) {
queryParams := url.Values{}
queryParams.Set("purge", strconv.FormatBool(purge))
queryParams.Set("async", "true")
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteServiceRequest,
Query: queryParams,
URIParams: Params{"service_guid": serviceGUID},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// UnmarshalJSON helps unmarshal a Cloud Controller Service response.
func (service *Service) UnmarshalJSON(data []byte) error {
var ccService struct {
Metadata internal.Metadata
Entity struct {
Label string `json:"label"`
Description string `json:"description"`
DocumentationURL string `json:"documentation_url"`
ServiceBrokerName string `json:"service_broker_name"`
Extra string `json:"extra"`
}
}
err := cloudcontroller.DecodeJSON(data, &ccService)
if err != nil {
return err
}
service.GUID = ccService.Metadata.GUID
service.Label = ccService.Entity.Label
service.Description = ccService.Entity.Description
service.DocumentationURL = ccService.Entity.DocumentationURL
service.ServiceBrokerName = ccService.Entity.ServiceBrokerName
// We explicitly unmarshal the Extra field to type string because CC returns
// a stringified JSON object ONLY for the 'extra' key (see test stub JSON
// response). This unmarshal strips escaped quotes, at which time we can then
// unmarshal into the ServiceExtra object.
// If 'extra' is null or not provided, this means sharing is NOT enabled
if len(ccService.Entity.Extra) != 0 {
extra := ServiceExtra{}
err = json.Unmarshal([]byte(ccService.Entity.Extra), &extra)
if err != nil {
return err
}
service.Extra.Shareable = extra.Shareable
if service.DocumentationURL == "" {
service.DocumentationURL = extra.DocumentationURL
}
}
return nil
}
// GetService returns the service with the given GUID.
func (client *Client) GetService(serviceGUID string) (Service, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceRequest,
URIParams: Params{"service_guid": serviceGUID},
})
if err != nil {
return Service{}, nil, err
}
var service Service
response := cloudcontroller.Response{
DecodeJSONResponseInto: &service,
}
err = client.connection.Make(request, &response)
return service, response.Warnings, err
}
// GetServices returns a list of Services given the provided filters.
func (client *Client) GetServices(filters ...Filter) ([]Service, Warnings, error) {
opts := requestOptions{
RequestName: internal.GetServicesRequest,
Query: ConvertFilterParameters(filters),
}
return client.makeServicesRequest(opts)
}
func (client *Client) GetSpaceServices(spaceGUID string, filters ...Filter) ([]Service, Warnings, error) {
opts := requestOptions{
RequestName: internal.GetSpaceServicesRequest,
Query: ConvertFilterParameters(filters),
URIParams: Params{"space_guid": spaceGUID},
}
return client.makeServicesRequest(opts)
}
func (client *Client) makeServicesRequest(opts requestOptions) ([]Service, Warnings, error) {
request, err := client.newHTTPRequest(opts)
if err != nil {
return nil, nil, err
}
var fullServicesList []Service
warnings, err := client.paginate(request, Service{}, func(item interface{}) error {
if service, ok := item.(Service); ok {
fullServicesList = append(fullServicesList, service)
} else {
return ccerror.UnknownObjectInListError{
Expected: Service{},
Unexpected: item,
}
}
return nil
})
return fullServicesList, warnings, err
}