-
Notifications
You must be signed in to change notification settings - Fork 929
/
clients.go
62 lines (52 loc) · 1.63 KB
/
clients.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
package api
import (
"fmt"
"net/http"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/net"
)
//go:generate counterfeiter . ClientRepository
type ClientRepository interface {
ClientExists(clientID string) (exists bool, apiErr error)
}
type CloudControllerClientRepository struct {
config coreconfig.Reader
uaaGateway net.Gateway
}
func NewCloudControllerClientRepository(config coreconfig.Reader, uaaGateway net.Gateway) (repo CloudControllerClientRepository) {
repo.config = config
repo.uaaGateway = uaaGateway
return
}
func (repo CloudControllerClientRepository) ClientExists(clientID string) (exists bool, apiErr error) {
exists = false
uaaEndpoint, apiErr := repo.getAuthEndpoint()
if apiErr != nil {
return exists, apiErr
}
path := fmt.Sprintf("%s/oauth/clients/%s", uaaEndpoint, clientID)
uaaResponse := new(resources.UAAUserResources)
apiErr = repo.uaaGateway.GetResource(path, uaaResponse)
if apiErr != nil {
if errType, ok := apiErr.(errors.HTTPError); ok {
switch errType.StatusCode() {
case http.StatusNotFound:
return false, errors.NewModelNotFoundError("Client", clientID)
case http.StatusForbidden:
return false, errors.NewAccessDeniedError()
}
}
return false, apiErr
}
return true, nil
}
func (repo CloudControllerClientRepository) getAuthEndpoint() (string, error) {
uaaEndpoint := repo.config.UaaEndpoint()
if uaaEndpoint == "" {
return "", errors.New(T("UAA endpoint missing from config file"))
}
return uaaEndpoint, nil
}