github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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 is the unique Service Binding identifier.
    15  	GUID string
    16  	// AppGUID is the associated application GUID.
    17  	AppGUID string
    18  	// ServiceInstanceGUID is the associated service GUID.
    19  	ServiceInstanceGUID string
    20  }
    21  
    22  // UnmarshalJSON helps unmarshal a Cloud Controller Service Binding response.
    23  func (serviceBinding *ServiceBinding) UnmarshalJSON(data []byte) error {
    24  	var ccServiceBinding struct {
    25  		Metadata internal.Metadata
    26  		Entity   struct {
    27  			AppGUID             string `json:"app_guid"`
    28  			ServiceInstanceGUID string `json:"service_instance_guid"`
    29  		} `json:"entity"`
    30  	}
    31  	err := json.Unmarshal(data, &ccServiceBinding)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	serviceBinding.AppGUID = ccServiceBinding.Entity.AppGUID
    37  	serviceBinding.GUID = ccServiceBinding.Metadata.GUID
    38  	serviceBinding.ServiceInstanceGUID = ccServiceBinding.Entity.ServiceInstanceGUID
    39  	return nil
    40  }
    41  
    42  // serviceBindingRequestBody represents the body of the service binding create
    43  // request.
    44  type serviceBindingRequestBody struct {
    45  	ServiceInstanceGUID string                 `json:"service_instance_guid"`
    46  	AppGUID             string                 `json:"app_guid"`
    47  	Parameters          map[string]interface{} `json:"parameters"`
    48  }
    49  
    50  // CreateServiceBinding creates a link between an application and a service
    51  // instance, also known as a Service Binding.
    52  func (client *Client) CreateServiceBinding(appGUID string, serviceInstanceGUID string, parameters map[string]interface{}) (ServiceBinding, Warnings, error) {
    53  	requestBody := serviceBindingRequestBody{
    54  		ServiceInstanceGUID: serviceInstanceGUID,
    55  		AppGUID:             appGUID,
    56  		Parameters:          parameters,
    57  	}
    58  
    59  	bodyBytes, err := json.Marshal(requestBody)
    60  	if err != nil {
    61  		return ServiceBinding{}, nil, err
    62  	}
    63  
    64  	request, err := client.newHTTPRequest(requestOptions{
    65  		RequestName: internal.PostServiceBindingRequest,
    66  		Body:        bytes.NewReader(bodyBytes),
    67  	})
    68  	if err != nil {
    69  		return ServiceBinding{}, nil, err
    70  	}
    71  
    72  	var serviceBinding ServiceBinding
    73  	response := cloudcontroller.Response{
    74  		Result: &serviceBinding,
    75  	}
    76  
    77  	err = client.connection.Make(request, &response)
    78  	if err != nil {
    79  		return ServiceBinding{}, response.Warnings, err
    80  	}
    81  
    82  	return serviceBinding, response.Warnings, nil
    83  }
    84  
    85  // GetServiceBindings returns back a list of Service Bindings based off of the
    86  // provided filters.
    87  func (client *Client) GetServiceBindings(filters ...Filter) ([]ServiceBinding, Warnings, error) {
    88  	request, err := client.newHTTPRequest(requestOptions{
    89  		RequestName: internal.GetServiceBindingsRequest,
    90  		Query:       ConvertFilterParameters(filters),
    91  	})
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  
    96  	var fullBindingsList []ServiceBinding
    97  	warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error {
    98  		if binding, ok := item.(ServiceBinding); ok {
    99  			fullBindingsList = append(fullBindingsList, binding)
   100  		} else {
   101  			return ccerror.UnknownObjectInListError{
   102  				Expected:   ServiceBinding{},
   103  				Unexpected: item,
   104  			}
   105  		}
   106  		return nil
   107  	})
   108  
   109  	return fullBindingsList, warnings, err
   110  }
   111  
   112  // DeleteServiceBinding will destroy the requested Service Binding.
   113  func (client *Client) DeleteServiceBinding(serviceBindingGUID string) (Warnings, error) {
   114  	request, err := client.newHTTPRequest(requestOptions{
   115  		RequestName: internal.DeleteServiceBindingRequest,
   116  		URIParams:   map[string]string{"service_binding_guid": serviceBindingGUID},
   117  	})
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	var response cloudcontroller.Response
   123  	err = client.connection.Make(request, &response)
   124  	return response.Warnings, err
   125  }
   126  
   127  // GetServiceInstanceServiceBindings returns back a list of Service Bindings for the provided service instance GUID.
   128  func (client *Client) GetServiceInstanceServiceBindings(serviceInstanceGUID string) ([]ServiceBinding, Warnings, error) {
   129  	request, err := client.newHTTPRequest(requestOptions{
   130  		RequestName: internal.GetServiceInstanceServiceBindingsRequest,
   131  		URIParams:   map[string]string{"service_instance_guid": serviceInstanceGUID},
   132  	})
   133  	if err != nil {
   134  		return nil, nil, err
   135  	}
   136  
   137  	var fullBindingsList []ServiceBinding
   138  	warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error {
   139  		if binding, ok := item.(ServiceBinding); ok {
   140  			fullBindingsList = append(fullBindingsList, binding)
   141  		} else {
   142  			return ccerror.UnknownObjectInListError{
   143  				Expected:   ServiceBinding{},
   144  				Unexpected: item,
   145  			}
   146  		}
   147  		return nil
   148  	})
   149  
   150  	return fullBindingsList, warnings, err
   151  }
   152  
   153  // GetUserProvidedServiceInstanceServiceBindings returns back a list of Service Bindings for the provided user provided service instance GUID.
   154  func (client *Client) GetUserProvidedServiceInstanceServiceBindings(userProvidedServiceInstanceGUID string) ([]ServiceBinding, Warnings, error) {
   155  	request, err := client.newHTTPRequest(requestOptions{
   156  		RequestName: internal.GetUserProvidedServiceInstanceServiceBindingsRequest,
   157  		URIParams:   map[string]string{"user_provided_service_instance_guid": userProvidedServiceInstanceGUID},
   158  	})
   159  	if err != nil {
   160  		return nil, nil, err
   161  	}
   162  
   163  	var fullBindingsList []ServiceBinding
   164  	warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error {
   165  		if binding, ok := item.(ServiceBinding); ok {
   166  			fullBindingsList = append(fullBindingsList, binding)
   167  		} else {
   168  			return ccerror.UnknownObjectInListError{
   169  				Expected:   ServiceBinding{},
   170  				Unexpected: item,
   171  			}
   172  		}
   173  		return nil
   174  	})
   175  
   176  	return fullBindingsList, warnings, err
   177  }