github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv2/service_binding.go (about) 1 package ccv2 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 10 ) 11 12 // ServiceBinding represents a Cloud Controller Service Binding. 13 type ServiceBinding struct { 14 GUID string 15 } 16 17 // UnmarshalJSON helps unmarshal a Cloud Controller Service Binding response. 18 func (serviceBinding *ServiceBinding) UnmarshalJSON(data []byte) error { 19 var ccServiceBinding struct { 20 Metadata internal.Metadata 21 } 22 err := json.Unmarshal(data, &ccServiceBinding) 23 if err != nil { 24 return err 25 } 26 27 serviceBinding.GUID = ccServiceBinding.Metadata.GUID 28 return nil 29 } 30 31 // serviceBindingRequestBody represents the body of the service binding create 32 // request. 33 type serviceBindingRequestBody struct { 34 ServiceInstanceGUID string `json:"service_instance_guid"` 35 AppGUID string `json:"app_guid"` 36 Parameters map[string]interface{} `json:"parameters"` 37 } 38 39 // CreateServiceBinding creates a service binding 40 func (client *Client) CreateServiceBinding(appGUID string, serviceInstanceGUID string, parameters map[string]interface{}) (ServiceBinding, Warnings, error) { 41 requestBody := serviceBindingRequestBody{ 42 ServiceInstanceGUID: serviceInstanceGUID, 43 AppGUID: appGUID, 44 Parameters: parameters, 45 } 46 47 bodyBytes, err := json.Marshal(requestBody) 48 if err != nil { 49 return ServiceBinding{}, nil, err 50 } 51 52 request, err := client.newHTTPRequest(requestOptions{ 53 RequestName: internal.PostServiceBindingRequest, 54 Body: bytes.NewReader(bodyBytes), 55 }) 56 if err != nil { 57 return ServiceBinding{}, nil, err 58 } 59 60 var serviceBinding ServiceBinding 61 response := cloudcontroller.Response{ 62 Result: &serviceBinding, 63 } 64 65 err = client.connection.Make(request, &response) 66 if err != nil { 67 return ServiceBinding{}, response.Warnings, err 68 } 69 70 return serviceBinding, response.Warnings, nil 71 } 72 73 // GetServiceBindings returns back a list of Service Bindings based off of the 74 // provided queries. 75 func (client *Client) GetServiceBindings(queries []Query) ([]ServiceBinding, Warnings, error) { 76 request, err := client.newHTTPRequest(requestOptions{ 77 RequestName: internal.GetServiceBindingsRequest, 78 Query: FormatQueryParameters(queries), 79 }) 80 if err != nil { 81 return nil, nil, err 82 } 83 84 var fullBindingsList []ServiceBinding 85 warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error { 86 if binding, ok := item.(ServiceBinding); ok { 87 fullBindingsList = append(fullBindingsList, binding) 88 } else { 89 return ccerror.UnknownObjectInListError{ 90 Expected: ServiceBinding{}, 91 Unexpected: item, 92 } 93 } 94 return nil 95 }) 96 97 return fullBindingsList, warnings, err 98 } 99 100 // DeleteServiceBinding will destroy the requested Service Binding. 101 func (client *Client) DeleteServiceBinding(serviceBindingGUID string) (Warnings, error) { 102 request, err := client.newHTTPRequest(requestOptions{ 103 RequestName: internal.DeleteServiceBindingRequest, 104 URIParams: map[string]string{"service_binding_guid": serviceBindingGUID}, 105 }) 106 if err != nil { 107 return nil, err 108 } 109 110 var response cloudcontroller.Response 111 err = client.connection.Make(request, &response) 112 return response.Warnings, err 113 }