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