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

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/url"
     8  	"strings"
     9  
    10  	"code.cloudfoundry.org/cli/cf/api/resources"
    11  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    12  	"code.cloudfoundry.org/cli/cf/errors"
    13  	"code.cloudfoundry.org/cli/cf/models"
    14  	"code.cloudfoundry.org/cli/cf/net"
    15  )
    16  
    17  //go:generate counterfeiter . ServiceBrokerRepository
    18  
    19  type ServiceBrokerRepository interface {
    20  	ListServiceBrokers(callback func(models.ServiceBroker) bool) error
    21  	FindByName(name string) (serviceBroker models.ServiceBroker, apiErr error)
    22  	FindByGUID(guid string) (serviceBroker models.ServiceBroker, apiErr error)
    23  	Create(name, url, username, password, spaceGUID string) (apiErr error)
    24  	Update(serviceBroker models.ServiceBroker) (apiErr error)
    25  	Rename(guid, name string) (apiErr error)
    26  	Delete(guid string) (apiErr error)
    27  }
    28  
    29  type CloudControllerServiceBrokerRepository struct {
    30  	config  coreconfig.Reader
    31  	gateway net.Gateway
    32  }
    33  
    34  func NewCloudControllerServiceBrokerRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerServiceBrokerRepository) {
    35  	repo.config = config
    36  	repo.gateway = gateway
    37  	return
    38  }
    39  
    40  func (repo CloudControllerServiceBrokerRepository) ListServiceBrokers(callback func(models.ServiceBroker) bool) error {
    41  	return repo.gateway.ListPaginatedResources(
    42  		repo.config.APIEndpoint(),
    43  		"/v2/service_brokers",
    44  		resources.ServiceBrokerResource{},
    45  		func(resource interface{}) bool {
    46  			callback(resource.(resources.ServiceBrokerResource).ToFields())
    47  			return true
    48  		})
    49  }
    50  
    51  func (repo CloudControllerServiceBrokerRepository) FindByName(name string) (serviceBroker models.ServiceBroker, apiErr error) {
    52  	foundBroker := false
    53  	apiErr = repo.gateway.ListPaginatedResources(
    54  		repo.config.APIEndpoint(),
    55  		fmt.Sprintf("/v2/service_brokers?q=%s", url.QueryEscape("name:"+name)),
    56  		resources.ServiceBrokerResource{},
    57  		func(resource interface{}) bool {
    58  			serviceBroker = resource.(resources.ServiceBrokerResource).ToFields()
    59  			foundBroker = true
    60  			return false
    61  		})
    62  
    63  	if !foundBroker && (apiErr == nil) {
    64  		apiErr = errors.NewModelNotFoundError("Service Broker", name)
    65  	}
    66  
    67  	return
    68  }
    69  func (repo CloudControllerServiceBrokerRepository) FindByGUID(guid string) (serviceBroker models.ServiceBroker, apiErr error) {
    70  	broker := new(resources.ServiceBrokerResource)
    71  	apiErr = repo.gateway.GetResource(repo.config.APIEndpoint()+fmt.Sprintf("/v2/service_brokers/%s", guid), broker)
    72  	serviceBroker = broker.ToFields()
    73  	return
    74  }
    75  
    76  func (repo CloudControllerServiceBrokerRepository) Create(name, url, username, password, spaceGUID string) error {
    77  	path := "/v2/service_brokers"
    78  	args := struct {
    79  		Name      string `json:"name"`
    80  		URL       string `json:"broker_url"`
    81  		Username  string `json:"auth_username"`
    82  		Password  string `json:"auth_password"`
    83  		SpaceGUID string `json:"space_guid,omitempty"`
    84  	}{
    85  		name,
    86  		url,
    87  		username,
    88  		password,
    89  		spaceGUID,
    90  	}
    91  	bs, err := json.Marshal(args)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(bs))
    96  }
    97  
    98  func (repo CloudControllerServiceBrokerRepository) Update(serviceBroker models.ServiceBroker) (apiErr error) {
    99  	path := fmt.Sprintf("/v2/service_brokers/%s", serviceBroker.GUID)
   100  	body := fmt.Sprintf(
   101  		`{"broker_url":"%s","auth_username":"%s","auth_password":"%s"}`,
   102  		serviceBroker.URL, serviceBroker.Username, serviceBroker.Password,
   103  	)
   104  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(body))
   105  }
   106  
   107  func (repo CloudControllerServiceBrokerRepository) Rename(guid, name string) (apiErr error) {
   108  	path := fmt.Sprintf("/v2/service_brokers/%s", guid)
   109  	body := fmt.Sprintf(`{"name":"%s"}`, name)
   110  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(body))
   111  }
   112  
   113  func (repo CloudControllerServiceBrokerRepository) Delete(guid string) (apiErr error) {
   114  	path := fmt.Sprintf("/v2/service_brokers/%s", guid)
   115  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
   116  }