-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
71 lines (63 loc) · 1.8 KB
/
config.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
package monitor
import (
"github.com/pkg/errors"
pksNet "github.com/pupimvictor/pks-monitor/net"
"github.com/pupimvictor/pks-monitor/uaa"
"net/http"
"net/url"
)
// Config represents the configuration for the PKS CLI. This includes things
// like auth tokens, flags, and the PKS API host.
type Config struct {
API string `yaml:"api"`
CACert string `yaml:"ca_cert"`
Username string `yaml:"username"`
SkipSSLVerification bool `yaml:"skip_ssl_verification"`
AccessToken string `yaml:"access_token"`
RefreshToken string `yaml:"refresh_token"`
UaaCliId string
UaaCliSecret string
}
var (
// APIPort is the port used to communicate with the PKS API.
APIPort = "9021"
// UAAPort is the port used to communicate with the PKS UAA.
UAAPort = "8443"
)
// GetAccessToken returns the access token.
func (c *Config) GetAccessToken() string {
return c.AccessToken
}
// SetAccessToken sets the access token.
func (c *Config) SetAccessToken(at string) {
c.AccessToken = at
}
func CreateHttpClient(c *Config) (*http.Client, error) {
apiHTTPClient, err := pksNet.HTTPClient(c.SkipSSLVerification, []byte(c.CACert))
if err != nil {
return nil, errors.Wrap(err, "monitor: could not create HTTPClient")
}
apiHTTPClient.Transport = pksNet.NewAuthTransport(
apiHTTPClient.Transport,
c,
c.UaaCliId,
c.UaaCliSecret,
)
return apiHTTPClient, nil
}
func CreateUaaClient(c *Config) (*uaa.Client, error) {
uaaHTTPClient, err := pksNet.HTTPClient(c.SkipSSLVerification, []byte(c.CACert))
if err != nil {
return nil, errors.Wrap(err, "monitor: could not create HTTPClient")
}
u, err := url.Parse(c.API)
if err != nil {
return nil, err
}
u.Host = u.Host + ":" + UAAPort
uaaClient := &uaa.Client{
AuthURL: *u,
Client: uaaHTTPClient,
}
return uaaClient, nil
}