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

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/resources"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/net"
    13  )
    14  
    15  //go:generate counterfeiter . ServiceAuthTokenRepository
    16  
    17  type ServiceAuthTokenRepository interface {
    18  	FindAll() (authTokens []models.ServiceAuthTokenFields, apiErr error)
    19  	FindByLabelAndProvider(label, provider string) (authToken models.ServiceAuthTokenFields, apiErr error)
    20  	Create(authToken models.ServiceAuthTokenFields) (apiErr error)
    21  	Update(authToken models.ServiceAuthTokenFields) (apiErr error)
    22  	Delete(authToken models.ServiceAuthTokenFields) (apiErr error)
    23  }
    24  
    25  type CloudControllerServiceAuthTokenRepository struct {
    26  	gateway net.Gateway
    27  	config  coreconfig.Reader
    28  }
    29  
    30  func NewCloudControllerServiceAuthTokenRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerServiceAuthTokenRepository) {
    31  	repo.gateway = gateway
    32  	repo.config = config
    33  	return
    34  }
    35  
    36  func (repo CloudControllerServiceAuthTokenRepository) FindAll() (authTokens []models.ServiceAuthTokenFields, apiErr error) {
    37  	return repo.findAllWithPath("/v2/service_auth_tokens")
    38  }
    39  
    40  func (repo CloudControllerServiceAuthTokenRepository) FindByLabelAndProvider(label, provider string) (authToken models.ServiceAuthTokenFields, apiErr error) {
    41  	path := fmt.Sprintf("/v2/service_auth_tokens?q=%s", url.QueryEscape("label:"+label+";provider:"+provider))
    42  	authTokens, apiErr := repo.findAllWithPath(path)
    43  	if apiErr != nil {
    44  		return
    45  	}
    46  
    47  	if len(authTokens) == 0 {
    48  		apiErr = errors.NewModelNotFoundError("Service Auth Token", label+" "+provider)
    49  		return
    50  	}
    51  
    52  	authToken = authTokens[0]
    53  	return
    54  }
    55  
    56  func (repo CloudControllerServiceAuthTokenRepository) findAllWithPath(path string) ([]models.ServiceAuthTokenFields, error) {
    57  	var authTokens []models.ServiceAuthTokenFields
    58  	apiErr := repo.gateway.ListPaginatedResources(
    59  		repo.config.APIEndpoint(),
    60  		path,
    61  		resources.AuthTokenResource{},
    62  		func(resource interface{}) bool {
    63  			if at, ok := resource.(resources.AuthTokenResource); ok {
    64  				authTokens = append(authTokens, at.ToFields())
    65  			}
    66  			return true
    67  		})
    68  
    69  	return authTokens, apiErr
    70  }
    71  
    72  func (repo CloudControllerServiceAuthTokenRepository) Create(authToken models.ServiceAuthTokenFields) (apiErr error) {
    73  	body := fmt.Sprintf(`{"label":"%s","provider":"%s","token":"%s"}`, authToken.Label, authToken.Provider, authToken.Token)
    74  	path := "/v2/service_auth_tokens"
    75  	return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, strings.NewReader(body))
    76  }
    77  
    78  func (repo CloudControllerServiceAuthTokenRepository) Delete(authToken models.ServiceAuthTokenFields) (apiErr error) {
    79  	path := fmt.Sprintf("/v2/service_auth_tokens/%s", authToken.GUID)
    80  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
    81  }
    82  
    83  func (repo CloudControllerServiceAuthTokenRepository) Update(authToken models.ServiceAuthTokenFields) (apiErr error) {
    84  	body := fmt.Sprintf(`{"token":"%s"}`, authToken.Token)
    85  	path := fmt.Sprintf("/v2/service_auth_tokens/%s", authToken.GUID)
    86  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(body))
    87  }