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