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

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/url"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/resources"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/errors"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/net"
    14  )
    15  
    16  //go:generate counterfeiter . ServiceKeyRepository
    17  
    18  type ServiceKeyRepository interface {
    19  	CreateServiceKey(serviceKeyGUID string, keyName string, params map[string]interface{}) error
    20  	ListServiceKeys(serviceKeyGUID string) ([]models.ServiceKey, error)
    21  	GetServiceKey(serviceKeyGUID string, keyName string) (models.ServiceKey, error)
    22  	DeleteServiceKey(serviceKeyGUID string) error
    23  }
    24  
    25  type CloudControllerServiceKeyRepository struct {
    26  	config  coreconfig.Reader
    27  	gateway net.Gateway
    28  }
    29  
    30  func NewCloudControllerServiceKeyRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerServiceKeyRepository) {
    31  	return CloudControllerServiceKeyRepository{
    32  		config:  config,
    33  		gateway: gateway,
    34  	}
    35  }
    36  
    37  func (c CloudControllerServiceKeyRepository) CreateServiceKey(instanceGUID string, keyName string, params map[string]interface{}) error {
    38  	path := "/v2/service_keys"
    39  
    40  	request := models.ServiceKeyRequest{
    41  		Name:                keyName,
    42  		ServiceInstanceGUID: instanceGUID,
    43  		Params:              params,
    44  	}
    45  	jsonBytes, err := json.Marshal(request)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	err = c.gateway.CreateResource(c.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
    51  
    52  	if httpErr, ok := err.(errors.HTTPError); ok {
    53  		switch httpErr.ErrorCode() {
    54  		case errors.ServiceKeyNameTaken:
    55  			return errors.NewModelAlreadyExistsError("Service key", keyName)
    56  		case errors.UnbindableService:
    57  			return errors.NewUnbindableServiceError()
    58  		default:
    59  			return errors.New(httpErr.Error())
    60  		}
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func (c CloudControllerServiceKeyRepository) ListServiceKeys(instanceGUID string) ([]models.ServiceKey, error) {
    67  	path := fmt.Sprintf("/v2/service_instances/%s/service_keys", instanceGUID)
    68  
    69  	return c.listServiceKeys(path)
    70  }
    71  
    72  func (c CloudControllerServiceKeyRepository) GetServiceKey(instanceGUID string, keyName string) (models.ServiceKey, error) {
    73  	path := fmt.Sprintf("/v2/service_instances/%s/service_keys?q=%s", instanceGUID, url.QueryEscape("name:"+keyName))
    74  
    75  	serviceKeys, err := c.listServiceKeys(path)
    76  	if err != nil || len(serviceKeys) == 0 {
    77  		return models.ServiceKey{}, err
    78  	}
    79  
    80  	return serviceKeys[0], nil
    81  }
    82  
    83  func (c CloudControllerServiceKeyRepository) listServiceKeys(path string) ([]models.ServiceKey, error) {
    84  	serviceKeys := []models.ServiceKey{}
    85  	err := c.gateway.ListPaginatedResources(
    86  		c.config.APIEndpoint(),
    87  		path,
    88  		resources.ServiceKeyResource{},
    89  		func(resource interface{}) bool {
    90  			serviceKey := resource.(resources.ServiceKeyResource).ToModel()
    91  			serviceKeys = append(serviceKeys, serviceKey)
    92  			return true
    93  		})
    94  
    95  	if err != nil {
    96  		if httpErr, ok := err.(errors.HTTPError); ok && httpErr.ErrorCode() == errors.NotAuthorized {
    97  			return []models.ServiceKey{}, errors.NewNotAuthorizedError()
    98  		}
    99  		return []models.ServiceKey{}, err
   100  	}
   101  
   102  	return serviceKeys, nil
   103  }
   104  
   105  func (c CloudControllerServiceKeyRepository) DeleteServiceKey(serviceKeyGUID string) error {
   106  	path := fmt.Sprintf("/v2/service_keys/%s", serviceKeyGUID)
   107  	return c.gateway.DeleteResource(c.config.APIEndpoint(), path)
   108  }