github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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{}, tags []string) (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{}, tags []string) (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  		Tags:            tags,
    42  	})
    43  
    44  	if err != nil {
    45  		apiErr = fmt.Errorf("%s: %s", "Error parsing response", err.Error())
    46  		return
    47  	}
    48  
    49  	return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
    50  }
    51  
    52  func (repo CCUserProvidedServiceInstanceRepository) Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error) {
    53  	path := fmt.Sprintf("/v2/user_provided_service_instances/%s", serviceInstanceFields.GUID)
    54  
    55  	reqBody := models.UserProvidedService{
    56  		Credentials:     serviceInstanceFields.Params,
    57  		SysLogDrainURL:  serviceInstanceFields.SysLogDrainURL,
    58  		RouteServiceURL: serviceInstanceFields.RouteServiceURL,
    59  		Tags:            serviceInstanceFields.Tags,
    60  	}
    61  	jsonBytes, err := json.Marshal(reqBody)
    62  	if err != nil {
    63  		apiErr = fmt.Errorf("%s: %s", "Error parsing response", err.Error())
    64  		return
    65  	}
    66  
    67  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
    68  }
    69  
    70  func (repo CCUserProvidedServiceInstanceRepository) GetSummaries() (models.UserProvidedServiceSummary, error) {
    71  	path := fmt.Sprintf("%s/v2/user_provided_service_instances", repo.config.APIEndpoint())
    72  
    73  	model := models.UserProvidedServiceSummary{}
    74  
    75  	apiErr := repo.gateway.GetResource(path, &model)
    76  	if apiErr != nil {
    77  		return models.UserProvidedServiceSummary{}, apiErr
    78  	}
    79  
    80  	return model, nil
    81  }