github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+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/ccerror" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 9 ) 10 11 // Service represents a Cloud Controller Service. 12 type Service struct { 13 // GUID is the unique Service identifier. 14 GUID string 15 // Label is the name of the service. 16 Label string 17 // Description is a short blurb describing the service. 18 Description string 19 // DocumentationURL is a url that points to a documentation page for the 20 // service. 21 DocumentationURL string 22 // Extra is a field with extra data pertaining to the service. 23 Extra ServiceExtra 24 } 25 26 // UnmarshalJSON helps unmarshal a Cloud Controller Service response. 27 func (service *Service) UnmarshalJSON(data []byte) error { 28 var ccService struct { 29 Metadata internal.Metadata 30 Entity struct { 31 Label string `json:"label"` 32 Description string `json:"description"` 33 DocumentationURL string `json:"documentation_url"` 34 Extra string `json:"extra"` 35 } 36 } 37 38 err := cloudcontroller.DecodeJSON(data, &ccService) 39 if err != nil { 40 return err 41 } 42 43 service.GUID = ccService.Metadata.GUID 44 service.Label = ccService.Entity.Label 45 service.Description = ccService.Entity.Description 46 service.DocumentationURL = ccService.Entity.DocumentationURL 47 48 // We explicitly unmarshal the Extra field to type string because CC returns 49 // a stringified JSON object ONLY for the 'extra' key (see test stub JSON 50 // response). This unmarshal strips escaped quotes, at which time we can then 51 // unmarshal into the ServiceExtra object. 52 // If 'extra' is null or not provided, this means sharing is NOT enabled 53 if len(ccService.Entity.Extra) != 0 { 54 extra := ServiceExtra{} 55 err = json.Unmarshal([]byte(ccService.Entity.Extra), &extra) 56 if err != nil { 57 return err 58 } 59 service.Extra.Shareable = extra.Shareable 60 } 61 62 return nil 63 } 64 65 // GetService returns the service with the given GUID. 66 func (client *Client) GetService(serviceGUID string) (Service, Warnings, error) { 67 request, err := client.newHTTPRequest(requestOptions{ 68 RequestName: internal.GetServiceRequest, 69 URIParams: Params{"service_guid": serviceGUID}, 70 }) 71 if err != nil { 72 return Service{}, nil, err 73 } 74 75 var service Service 76 response := cloudcontroller.Response{ 77 Result: &service, 78 } 79 80 err = client.connection.Make(request, &response) 81 return service, response.Warnings, err 82 } 83 84 // GetServices returns a list of Services given the provided filters. 85 func (client *Client) GetServices(filters ...Filter) ([]Service, Warnings, error) { 86 request, err := client.newHTTPRequest(requestOptions{ 87 RequestName: internal.GetServicesRequest, 88 Query: ConvertFilterParameters(filters), 89 }) 90 91 if err != nil { 92 return nil, nil, err 93 } 94 95 var fullServicesList []Service 96 warnings, err := client.paginate(request, Service{}, func(item interface{}) error { 97 if service, ok := item.(Service); ok { 98 fullServicesList = append(fullServicesList, service) 99 } else { 100 return ccerror.UnknownObjectInListError{ 101 Expected: Service{}, 102 Unexpected: item, 103 } 104 } 105 return nil 106 }) 107 return fullServicesList, warnings, err 108 }