github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv2/service_binding.go (about) 1 package ccv2 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "net/url" 8 9 "code.cloudfoundry.org/cli/api/cloudcontroller" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 12 ) 13 14 // ServiceBinding represents a Cloud Controller Service Binding. 15 type ServiceBinding struct { 16 // AppGUID is the associated application GUID. 17 AppGUID string 18 // GUID is the unique Service Binding identifier. 19 GUID string 20 // LastOperation 21 LastOperation LastOperation 22 // Name is the name of the service binding 23 Name string 24 // ServiceInstanceGUID is the associated service GUID. 25 ServiceInstanceGUID string 26 27 // Parameters of the service binding 28 Parameters map[string]interface{} 29 } 30 31 // UnmarshalJSON helps unmarshal a Cloud Controller Service Binding response. 32 func (serviceBinding *ServiceBinding) UnmarshalJSON(data []byte) error { 33 var ccServiceBinding struct { 34 Metadata internal.Metadata 35 Entity struct { 36 AppGUID string `json:"app_guid"` 37 ServiceInstanceGUID string `json:"service_instance_guid"` 38 Name string `json:"name"` 39 LastOperation LastOperation `json:"last_operation"` 40 Parameters map[string]interface{} `json:"parameters"` 41 } `json:"entity"` 42 } 43 err := cloudcontroller.DecodeJSON(data, &ccServiceBinding) 44 if err != nil { 45 return err 46 } 47 48 serviceBinding.AppGUID = ccServiceBinding.Entity.AppGUID 49 serviceBinding.GUID = ccServiceBinding.Metadata.GUID 50 serviceBinding.ServiceInstanceGUID = ccServiceBinding.Entity.ServiceInstanceGUID 51 serviceBinding.Name = ccServiceBinding.Entity.Name 52 serviceBinding.LastOperation = ccServiceBinding.Entity.LastOperation 53 serviceBinding.Parameters = ccServiceBinding.Entity.Parameters 54 return nil 55 } 56 57 // serviceBindingRequestBody represents the body of the service binding create 58 // request. 59 type serviceBindingRequestBody struct { 60 ServiceInstanceGUID string `json:"service_instance_guid"` 61 AppGUID string `json:"app_guid"` 62 Name string `json:"name,omitempty"` 63 Parameters map[string]interface{} `json:"parameters"` 64 } 65 66 func (client *Client) CreateServiceBinding(appGUID string, serviceInstanceGUID string, bindingName string, acceptsIncomplete bool, parameters map[string]interface{}) (ServiceBinding, Warnings, error) { 67 requestBody := serviceBindingRequestBody{ 68 ServiceInstanceGUID: serviceInstanceGUID, 69 AppGUID: appGUID, 70 Name: bindingName, 71 Parameters: parameters, 72 } 73 74 bodyBytes, err := json.Marshal(requestBody) 75 if err != nil { 76 return ServiceBinding{}, nil, err 77 } 78 79 request, err := client.newHTTPRequest(requestOptions{ 80 RequestName: internal.PostServiceBindingRequest, 81 Body: bytes.NewReader(bodyBytes), 82 Query: url.Values{"accepts_incomplete": {fmt.Sprint(acceptsIncomplete)}}, 83 }) 84 if err != nil { 85 return ServiceBinding{}, nil, err 86 } 87 88 var serviceBinding ServiceBinding 89 response := cloudcontroller.Response{ 90 DecodeJSONResponseInto: &serviceBinding, 91 } 92 93 err = client.connection.Make(request, &response) 94 if err != nil { 95 return ServiceBinding{}, response.Warnings, err 96 } 97 98 return serviceBinding, response.Warnings, nil 99 } 100 101 // DeleteServiceBinding deletes the specified Service Binding. An updated 102 // service binding is returned only if acceptsIncomplete is true. 103 func (client *Client) DeleteServiceBinding(serviceBindingGUID string, acceptsIncomplete bool) (ServiceBinding, Warnings, error) { 104 request, err := client.newHTTPRequest(requestOptions{ 105 RequestName: internal.DeleteServiceBindingRequest, 106 URIParams: map[string]string{"service_binding_guid": serviceBindingGUID}, 107 Query: url.Values{"accepts_incomplete": {fmt.Sprint(acceptsIncomplete)}}, 108 }) 109 if err != nil { 110 return ServiceBinding{}, nil, err 111 } 112 113 var response cloudcontroller.Response 114 var serviceBinding ServiceBinding 115 if acceptsIncomplete { 116 response = cloudcontroller.Response{ 117 DecodeJSONResponseInto: &serviceBinding, 118 } 119 } 120 121 err = client.connection.Make(request, &response) 122 return serviceBinding, response.Warnings, err 123 } 124 125 // GetServiceBinding returns back a service binding with the provided GUID. 126 func (client *Client) GetServiceBinding(guid string) (ServiceBinding, Warnings, error) { 127 request, err := client.newHTTPRequest(requestOptions{ 128 RequestName: internal.GetServiceBindingRequest, 129 URIParams: Params{"service_binding_guid": guid}, 130 }) 131 if err != nil { 132 return ServiceBinding{}, nil, err 133 } 134 135 var serviceBinding ServiceBinding 136 response := cloudcontroller.Response{ 137 DecodeJSONResponseInto: &serviceBinding, 138 } 139 140 err = client.connection.Make(request, &response) 141 return serviceBinding, response.Warnings, err 142 } 143 144 // GetServiceBindings returns back a list of Service Bindings based off of the 145 // provided filters. 146 func (client *Client) GetServiceBindings(filters ...Filter) ([]ServiceBinding, Warnings, error) { 147 request, err := client.newHTTPRequest(requestOptions{ 148 RequestName: internal.GetServiceBindingsRequest, 149 Query: ConvertFilterParameters(filters), 150 }) 151 if err != nil { 152 return nil, nil, err 153 } 154 155 var fullBindingsList []ServiceBinding 156 warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error { 157 if binding, ok := item.(ServiceBinding); ok { 158 fullBindingsList = append(fullBindingsList, binding) 159 } else { 160 return ccerror.UnknownObjectInListError{ 161 Expected: ServiceBinding{}, 162 Unexpected: item, 163 } 164 } 165 return nil 166 }) 167 168 return fullBindingsList, warnings, err 169 } 170 171 // GetServiceInstanceServiceBindings returns back a list of Service Bindings for the provided service instance GUID. 172 func (client *Client) GetServiceInstanceServiceBindings(serviceInstanceGUID string) ([]ServiceBinding, Warnings, error) { 173 request, err := client.newHTTPRequest(requestOptions{ 174 RequestName: internal.GetServiceInstanceServiceBindingsRequest, 175 URIParams: map[string]string{"service_instance_guid": serviceInstanceGUID}, 176 }) 177 if err != nil { 178 return nil, nil, err 179 } 180 181 var fullBindingsList []ServiceBinding 182 warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error { 183 if binding, ok := item.(ServiceBinding); ok { 184 fullBindingsList = append(fullBindingsList, binding) 185 } else { 186 return ccerror.UnknownObjectInListError{ 187 Expected: ServiceBinding{}, 188 Unexpected: item, 189 } 190 } 191 return nil 192 }) 193 194 return fullBindingsList, warnings, err 195 } 196 197 // GetUserProvidedServiceInstanceServiceBindings returns back a list of Service Bindings for the provided user provided service instance GUID. 198 func (client *Client) GetUserProvidedServiceInstanceServiceBindings(userProvidedServiceInstanceGUID string) ([]ServiceBinding, Warnings, error) { 199 request, err := client.newHTTPRequest(requestOptions{ 200 RequestName: internal.GetUserProvidedServiceInstanceServiceBindingsRequest, 201 URIParams: map[string]string{"user_provided_service_instance_guid": userProvidedServiceInstanceGUID}, 202 }) 203 if err != nil { 204 return nil, nil, err 205 } 206 207 var fullBindingsList []ServiceBinding 208 warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error { 209 if binding, ok := item.(ServiceBinding); ok { 210 fullBindingsList = append(fullBindingsList, binding) 211 } else { 212 return ccerror.UnknownObjectInListError{ 213 Expected: ServiceBinding{}, 214 Unexpected: item, 215 } 216 } 217 return nil 218 }) 219 220 return fullBindingsList, warnings, err 221 }