github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/api/service_bindings.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/resources"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/net"
    12  )
    13  
    14  //go:generate counterfeiter . ServiceBindingRepository
    15  
    16  type ServiceBindingRepository interface {
    17  	Create(instanceGUID string, appGUID string, paramsMap map[string]interface{}) error
    18  	Delete(instance models.ServiceInstance, appGUID string) (bool, error)
    19  	ListAllForService(instanceGUID string) ([]models.ServiceBindingFields, error)
    20  }
    21  
    22  type CloudControllerServiceBindingRepository struct {
    23  	config  coreconfig.Reader
    24  	gateway net.Gateway
    25  }
    26  
    27  func NewCloudControllerServiceBindingRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerServiceBindingRepository) {
    28  	repo.config = config
    29  	repo.gateway = gateway
    30  	return
    31  }
    32  
    33  func (repo CloudControllerServiceBindingRepository) Create(instanceGUID, appGUID string, paramsMap map[string]interface{}) error {
    34  	path := "/v2/service_bindings"
    35  	request := models.ServiceBindingRequest{
    36  		AppGUID:             appGUID,
    37  		ServiceInstanceGUID: instanceGUID,
    38  		Params:              paramsMap,
    39  	}
    40  
    41  	jsonBytes, err := json.Marshal(request)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
    47  }
    48  
    49  func (repo CloudControllerServiceBindingRepository) Delete(instance models.ServiceInstance, appGUID string) (bool, error) {
    50  	var path string
    51  	for _, binding := range instance.ServiceBindings {
    52  		if binding.AppGUID == appGUID {
    53  			path = binding.URL
    54  			break
    55  		}
    56  	}
    57  
    58  	if path == "" {
    59  		return false, nil
    60  	}
    61  
    62  	return true, repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
    63  }
    64  
    65  func (repo CloudControllerServiceBindingRepository) ListAllForService(instanceGUID string) ([]models.ServiceBindingFields, error) {
    66  	serviceBindings := []models.ServiceBindingFields{}
    67  	err := repo.gateway.ListPaginatedResources(
    68  		repo.config.APIEndpoint(),
    69  		fmt.Sprintf("/v2/service_instances/%s/service_bindings", instanceGUID),
    70  		resources.ServiceBindingResource{},
    71  		func(resource interface{}) bool {
    72  			if serviceBindingResource, ok := resource.(resources.ServiceBindingResource); ok {
    73  				serviceBindings = append(serviceBindings, serviceBindingResource.ToFields())
    74  			}
    75  			return true
    76  		},
    77  	)
    78  	return serviceBindings, err
    79  }