github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/api/cloudcontroller/ccv2/service.go (about) 1 package ccv2 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 8 ) 9 10 // Service represents a Cloud Controller Service. 11 type Service struct { 12 // GUID is the unique Service identifier. 13 GUID string 14 // Label is the name of the service. 15 Label string 16 // Description is a short blurb describing the service. 17 Description string 18 // DocumentationURL is a url that points to a documentation page for the 19 // service. 20 DocumentationURL string 21 // Extra is a field with extra data pertaining to the service. 22 Extra ServiceExtra 23 } 24 25 // UnmarshalJSON helps unmarshal a Cloud Controller Service response. 26 func (service *Service) UnmarshalJSON(data []byte) error { 27 var ccService struct { 28 Metadata internal.Metadata 29 Entity struct { 30 Label string `json:"label"` 31 Description string `json:"description"` 32 DocumentationURL string `json:"documentation_url"` 33 Extra string `json:"extra"` 34 } 35 } 36 37 err := cloudcontroller.DecodeJSON(data, &ccService) 38 if err != nil { 39 return err 40 } 41 42 service.GUID = ccService.Metadata.GUID 43 service.Label = ccService.Entity.Label 44 service.Description = ccService.Entity.Description 45 service.DocumentationURL = ccService.Entity.DocumentationURL 46 47 // We explicitly unmarshal the Extra field to type string because CC returns 48 // a stringified JSON object ONLY for the 'extra' key (see test stub JSON 49 // response). This unmarshal strips escaped quotes, at which time we can then 50 // unmarshal into the ServiceExtra object. 51 // If 'extra' is null or not provided, this means sharing is NOT enabled 52 if len(ccService.Entity.Extra) != 0 { 53 extra := ServiceExtra{} 54 err = json.Unmarshal([]byte(ccService.Entity.Extra), &extra) 55 if err != nil { 56 return err 57 } 58 service.Extra.Shareable = extra.Shareable 59 } 60 61 return nil 62 } 63 64 // GetService returns the service with the given GUID. 65 func (client *Client) GetService(serviceGUID string) (Service, Warnings, error) { 66 request, err := client.newHTTPRequest(requestOptions{ 67 RequestName: internal.GetServiceRequest, 68 URIParams: Params{"service_guid": serviceGUID}, 69 }) 70 if err != nil { 71 return Service{}, nil, err 72 } 73 74 var service Service 75 response := cloudcontroller.Response{ 76 Result: &service, 77 } 78 79 err = client.connection.Make(request, &response) 80 return service, response.Warnings, err 81 }