-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
executable file
·86 lines (69 loc) · 2.3 KB
/
bind.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
package broker
import (
"context"
"fmt"
"github.com/cloudfoundry-community/go-uaa"
brokerapi "github.com/pivotal-cf/brokerapi/domain"
)
func (broker *ConfigServerBroker) Bind(ctx context.Context, instanceID, bindingID string, details brokerapi.BindDetails, asyncAllowed bool) (brokerapi.Binding, error) {
binding := brokerapi.Binding{}
kind, err := getKind(details)
if err != nil {
return binding, err
}
broker.Logger.Info("Bind: GetUAAClient")
api, err := broker.getUaaClient()
if err != nil {
broker.Logger.Info("Bind: Error in getting client")
return binding, err
}
clientId := makeClientIdForBinding(kind, bindingID)
password := genClientPassword()
client := uaa.Client{
ClientID: clientId,
AuthorizedGrantTypes: []string{"client_credentials"},
Authorities: []string{fmt.Sprintf("%s.%v.read", kind, instanceID)},
DisplayName: clientId,
ClientSecret: password,
}
broker.Logger.Info("Bind: got client info")
broker.Logger.Info("Bind: Create Client")
_, err = api.CreateClient(client)
if err != nil {
broker.Logger.Info("Bind: Error in CreateClient")
return binding, err
}
broker.Logger.Info("Bind: GetClient")
cfClient, err := broker.getClient()
if err != nil {
broker.Logger.Info("Bind: Error in GetClient")
return binding, err
}
broker.Logger.Info("Bind: Get Info")
info, _, _, err := cfClient.GetInfo()
if err != nil {
broker.Logger.Info("Bind: Error in Get Info")
return binding, err
}
broker.Logger.Info("Bind: GetApplicationByNameAndSpace")
app, _, err := cfClient.GetApplicationByNameAndSpace(makeAppName(kind, instanceID), broker.Config.InstanceSpaceGUID)
if err != nil {
broker.Logger.Info("Bind: Error in GetApplicationByNameAndSpace")
return binding, err
}
broker.Logger.Info("Bind: GetApplicationRoutes")
routes, _, err := cfClient.GetApplicationRoutes(app.GUID)
if err != nil {
broker.Logger.Info("Bind: Error in GetApplicationRoutes")
return binding, err
}
broker.Logger.Info("Bind: Building binding Credentials")
binding.Credentials = map[string]string{
"uri": fmt.Sprintf("https://%v", routes[0].URL),
"access_token_uri": fmt.Sprintf("%v/oauth/token", info.UAA()),
"client_id": clientId,
"client_secret": password,
}
broker.Logger.Info("Bind: Return")
return binding, nil
}