-
Notifications
You must be signed in to change notification settings - Fork 928
/
service.go
88 lines (76 loc) · 2.52 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
package ccv2
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"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
// Extra is a field with extra data pertaining to the service.
Extra ServiceExtra
}
// ServiceExtra contains extra service related properties.
type ServiceExtra struct {
// Shareable is true if the service is shareable across organizations and
// spaces.
Shareable bool
}
// 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"`
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
// 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
}
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{
Result: &service,
}
err = client.connection.Make(request, &response)
return service, response.Warnings, err
}