forked from cloudfoundry/cli
/
service_auth_tokens.go
100 lines (83 loc) · 3.44 KB
/
service_auth_tokens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package api
import (
"cf/configuration"
"cf/errors"
"cf/models"
"cf/net"
"fmt"
"net/url"
"strings"
)
type PaginatedAuthTokenResources struct {
Resources []AuthTokenResource
}
type AuthTokenResource struct {
Resource
Entity AuthTokenEntity
}
type AuthTokenEntity struct {
Label string
Provider string
}
type ServiceAuthTokenRepository interface {
FindAll() (authTokens []models.ServiceAuthTokenFields, apiErr errors.Error)
FindByLabelAndProvider(label, provider string) (authToken models.ServiceAuthTokenFields, apiErr errors.Error)
Create(authToken models.ServiceAuthTokenFields) (apiErr errors.Error)
Update(authToken models.ServiceAuthTokenFields) (apiErr errors.Error)
Delete(authToken models.ServiceAuthTokenFields) (apiErr errors.Error)
}
type CloudControllerServiceAuthTokenRepository struct {
gateway net.Gateway
config configuration.Reader
}
func NewCloudControllerServiceAuthTokenRepository(config configuration.Reader, gateway net.Gateway) (repo CloudControllerServiceAuthTokenRepository) {
repo.gateway = gateway
repo.config = config
return
}
func (repo CloudControllerServiceAuthTokenRepository) FindAll() (authTokens []models.ServiceAuthTokenFields, apiErr errors.Error) {
path := fmt.Sprintf("%s/v2/service_auth_tokens", repo.config.ApiEndpoint())
return repo.findAllWithPath(path)
}
func (repo CloudControllerServiceAuthTokenRepository) FindByLabelAndProvider(label, provider string) (authToken models.ServiceAuthTokenFields, apiErr errors.Error) {
path := fmt.Sprintf("%s/v2/service_auth_tokens?q=%s", repo.config.ApiEndpoint(), url.QueryEscape("label:"+label+";provider:"+provider))
authTokens, apiErr := repo.findAllWithPath(path)
if apiErr != nil {
return
}
if len(authTokens) == 0 {
apiErr = errors.NewModelNotFoundError("Service Auth Token", label+" "+provider)
return
}
authToken = authTokens[0]
return
}
func (repo CloudControllerServiceAuthTokenRepository) findAllWithPath(path string) (authTokens []models.ServiceAuthTokenFields, apiErr errors.Error) {
resources := new(PaginatedAuthTokenResources)
apiErr = repo.gateway.GetResource(path, repo.config.AccessToken(), resources)
if apiErr != nil {
return
}
for _, resource := range resources.Resources {
authTokens = append(authTokens, models.ServiceAuthTokenFields{
Guid: resource.Metadata.Guid,
Label: resource.Entity.Label,
Provider: resource.Entity.Provider,
})
}
return
}
func (repo CloudControllerServiceAuthTokenRepository) Create(authToken models.ServiceAuthTokenFields) (apiErr errors.Error) {
body := fmt.Sprintf(`{"label":"%s","provider":"%s","token":"%s"}`, authToken.Label, authToken.Provider, authToken.Token)
path := fmt.Sprintf("%s/v2/service_auth_tokens", repo.config.ApiEndpoint())
return repo.gateway.CreateResource(path, repo.config.AccessToken(), strings.NewReader(body))
}
func (repo CloudControllerServiceAuthTokenRepository) Delete(authToken models.ServiceAuthTokenFields) (apiErr errors.Error) {
path := fmt.Sprintf("%s/v2/service_auth_tokens/%s", repo.config.ApiEndpoint(), authToken.Guid)
return repo.gateway.DeleteResource(path, repo.config.AccessToken())
}
func (repo CloudControllerServiceAuthTokenRepository) Update(authToken models.ServiceAuthTokenFields) (apiErr errors.Error) {
body := fmt.Sprintf(`{"token":"%s"}`, authToken.Token)
path := fmt.Sprintf("%s/v2/service_auth_tokens/%s", repo.config.ApiEndpoint(), authToken.Guid)
return repo.gateway.UpdateResource(path, repo.config.AccessToken(), strings.NewReader(body))
}