github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/user_provided_service_instances.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  )
    12  
    13  //go:generate counterfeiter . UserProvidedServiceInstanceRepository
    14  
    15  type UserProvidedServiceInstanceRepository interface {
    16  	Create(name, drainURL string, routeServiceURL string, params map[string]interface{}) (apiErr error)
    17  	Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error)
    18  	GetSummaries() (models.UserProvidedServiceSummary, error)
    19  }
    20  
    21  type CCUserProvidedServiceInstanceRepository struct {
    22  	config  coreconfig.Reader
    23  	gateway net.Gateway
    24  }
    25  
    26  func NewCCUserProvidedServiceInstanceRepository(config coreconfig.Reader, gateway net.Gateway) (repo CCUserProvidedServiceInstanceRepository) {
    27  	repo.config = config
    28  	repo.gateway = gateway
    29  	return
    30  }
    31  
    32  func (repo CCUserProvidedServiceInstanceRepository) Create(name, drainURL string, routeServiceURL string, params map[string]interface{}) (apiErr error) {
    33  	path := "/v2/user_provided_service_instances"
    34  
    35  	jsonBytes, err := json.Marshal(models.UserProvidedService{
    36  		Name:            name,
    37  		Credentials:     params,
    38  		SpaceGUID:       repo.config.SpaceFields().GUID,
    39  		SysLogDrainURL:  drainURL,
    40  		RouteServiceURL: routeServiceURL,
    41  	})
    42  
    43  	if err != nil {
    44  		apiErr = fmt.Errorf("%s: %s", "Error parsing response", err.Error())
    45  		return
    46  	}
    47  
    48  	return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
    49  }
    50  
    51  func (repo CCUserProvidedServiceInstanceRepository) Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error) {
    52  	path := fmt.Sprintf("/v2/user_provided_service_instances/%s", serviceInstanceFields.GUID)
    53  
    54  	reqBody := models.UserProvidedService{
    55  		Credentials:     serviceInstanceFields.Params,
    56  		SysLogDrainURL:  serviceInstanceFields.SysLogDrainURL,
    57  		RouteServiceURL: serviceInstanceFields.RouteServiceURL,
    58  	}
    59  	jsonBytes, err := json.Marshal(reqBody)
    60  	if err != nil {
    61  		apiErr = fmt.Errorf("%s: %s", "Error parsing response", err.Error())
    62  		return
    63  	}
    64  
    65  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
    66  }
    67  
    68  func (repo CCUserProvidedServiceInstanceRepository) GetSummaries() (models.UserProvidedServiceSummary, error) {
    69  	path := fmt.Sprintf("%s/v2/user_provided_service_instances", repo.config.APIEndpoint())
    70  
    71  	model := models.UserProvidedServiceSummary{}
    72  
    73  	apiErr := repo.gateway.GetResource(path, &model)
    74  	if apiErr != nil {
    75  		return models.UserProvidedServiceSummary{}, apiErr
    76  	}
    77  
    78  	return model, nil
    79  }