github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/service.go (about) 1 package ccv2 2 3 import ( 4 "encoding/json" 5 "net/url" 6 "strconv" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 11 ) 12 13 // Service represents a Cloud Controller Service. 14 type Service struct { 15 // GUID is the unique Service identifier. 16 GUID string 17 // Label is the name of the service. 18 Label string 19 // Description is a short blurb describing the service. 20 Description string 21 // DocumentationURL is a url that points to a documentation page for the 22 // service. 23 DocumentationURL string 24 // ServiceBrokerName is name of the service broker associated with the service 25 ServiceBrokerName string 26 // Extra is a field with extra data pertaining to the service. 27 Extra ServiceExtra 28 } 29 30 // DeleteService deletes the service with the given GUID, and returns any errors and warnings. 31 func (client *Client) DeleteService(serviceGUID string, purge bool) (Warnings, error) { 32 queryParams := url.Values{} 33 queryParams.Set("purge", strconv.FormatBool(purge)) 34 queryParams.Set("async", "true") 35 36 request, err := client.newHTTPRequest(requestOptions{ 37 RequestName: internal.DeleteServiceRequest, 38 Query: queryParams, 39 URIParams: Params{"service_guid": serviceGUID}, 40 }) 41 if err != nil { 42 return nil, err 43 } 44 45 response := cloudcontroller.Response{} 46 47 err = client.connection.Make(request, &response) 48 return response.Warnings, err 49 } 50 51 // UnmarshalJSON helps unmarshal a Cloud Controller Service response. 52 func (service *Service) UnmarshalJSON(data []byte) error { 53 var ccService struct { 54 Metadata internal.Metadata 55 Entity struct { 56 Label string `json:"label"` 57 Description string `json:"description"` 58 DocumentationURL string `json:"documentation_url"` 59 ServiceBrokerName string `json:"service_broker_name"` 60 Extra string `json:"extra"` 61 } 62 } 63 64 err := cloudcontroller.DecodeJSON(data, &ccService) 65 if err != nil { 66 return err 67 } 68 69 service.GUID = ccService.Metadata.GUID 70 service.Label = ccService.Entity.Label 71 service.Description = ccService.Entity.Description 72 service.DocumentationURL = ccService.Entity.DocumentationURL 73 service.ServiceBrokerName = ccService.Entity.ServiceBrokerName 74 75 // We explicitly unmarshal the Extra field to type string because CC returns 76 // a stringified JSON object ONLY for the 'extra' key (see test stub JSON 77 // response). This unmarshal strips escaped quotes, at which time we can then 78 // unmarshal into the ServiceExtra object. 79 // If 'extra' is null or not provided, this means sharing is NOT enabled 80 if len(ccService.Entity.Extra) != 0 { 81 extra := ServiceExtra{} 82 err = json.Unmarshal([]byte(ccService.Entity.Extra), &extra) 83 if err != nil { 84 return err 85 } 86 service.Extra.Shareable = extra.Shareable 87 if service.DocumentationURL == "" { 88 service.DocumentationURL = extra.DocumentationURL 89 } 90 } 91 92 return nil 93 } 94 95 // GetService returns the service with the given GUID. 96 func (client *Client) GetService(serviceGUID string) (Service, Warnings, error) { 97 request, err := client.newHTTPRequest(requestOptions{ 98 RequestName: internal.GetServiceRequest, 99 URIParams: Params{"service_guid": serviceGUID}, 100 }) 101 if err != nil { 102 return Service{}, nil, err 103 } 104 105 var service Service 106 response := cloudcontroller.Response{ 107 DecodeJSONResponseInto: &service, 108 } 109 110 err = client.connection.Make(request, &response) 111 return service, response.Warnings, err 112 } 113 114 // GetServices returns a list of Services given the provided filters. 115 func (client *Client) GetServices(filters ...Filter) ([]Service, Warnings, error) { 116 opts := requestOptions{ 117 RequestName: internal.GetServicesRequest, 118 Query: ConvertFilterParameters(filters), 119 } 120 121 return client.makeServicesRequest(opts) 122 } 123 124 func (client *Client) GetSpaceServices(spaceGUID string, filters ...Filter) ([]Service, Warnings, error) { 125 opts := requestOptions{ 126 RequestName: internal.GetSpaceServicesRequest, 127 Query: ConvertFilterParameters(filters), 128 URIParams: Params{"space_guid": spaceGUID}, 129 } 130 131 return client.makeServicesRequest(opts) 132 } 133 134 func (client *Client) makeServicesRequest(opts requestOptions) ([]Service, Warnings, error) { 135 request, err := client.newHTTPRequest(opts) 136 137 if err != nil { 138 return nil, nil, err 139 } 140 141 var fullServicesList []Service 142 warnings, err := client.paginate(request, Service{}, func(item interface{}) error { 143 if service, ok := item.(Service); ok { 144 fullServicesList = append(fullServicesList, service) 145 } else { 146 return ccerror.UnknownObjectInListError{ 147 Expected: Service{}, 148 Unexpected: item, 149 } 150 } 151 return nil 152 }) 153 return fullServicesList, warnings, err 154 }