github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/api/cloudcontroller/ccv2/service_binding.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  // ServiceBinding represents a Cloud Controller Service Binding.
    11  type ServiceBinding struct {
    12  	GUID string
    13  }
    14  
    15  // UnmarshalJSON helps unmarshal a Cloud Controller Service Binding response.
    16  func (serviceBinding *ServiceBinding) UnmarshalJSON(data []byte) error {
    17  	var ccServiceBinding struct {
    18  		Metadata internal.Metadata
    19  	}
    20  	err := json.Unmarshal(data, &ccServiceBinding)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	serviceBinding.GUID = ccServiceBinding.Metadata.GUID
    26  	return nil
    27  }
    28  
    29  // GetServiceBindings returns back a list of Service Bindings based off of the
    30  // provided queries.
    31  func (client *Client) GetServiceBindings(queries []Query) ([]ServiceBinding, Warnings, error) {
    32  	request, err := client.newHTTPRequest(requestOptions{
    33  		RequestName: internal.ServiceBindingsRequest,
    34  		Query:       FormatQueryParameters(queries),
    35  	})
    36  	if err != nil {
    37  		return nil, nil, err
    38  	}
    39  
    40  	var fullBindingsList []ServiceBinding
    41  	warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error {
    42  		if binding, ok := item.(ServiceBinding); ok {
    43  			fullBindingsList = append(fullBindingsList, binding)
    44  		} else {
    45  			return cloudcontroller.UnknownObjectInListError{
    46  				Expected:   ServiceBinding{},
    47  				Unexpected: item,
    48  			}
    49  		}
    50  		return nil
    51  	})
    52  
    53  	return fullBindingsList, warnings, err
    54  }
    55  
    56  // DeleteServiceBinding will destroy the requested Service Binding.
    57  func (client *Client) DeleteServiceBinding(serviceBindingGUID string) (Warnings, error) {
    58  	request, err := client.newHTTPRequest(requestOptions{
    59  		RequestName: internal.DeleteServiceBindingRequest,
    60  		URIParams:   map[string]string{"service_binding_guid": serviceBindingGUID},
    61  	})
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	var response cloudcontroller.Response
    67  	err = client.connection.Make(request, &response)
    68  	return response.Warnings, err
    69  }