-
Notifications
You must be signed in to change notification settings - Fork 51
/
client.go
145 lines (122 loc) · 3.12 KB
/
client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gokong
import (
"net/url"
"os"
"reflect"
"strconv"
"strings"
"github.com/google/go-querystring/query"
)
const EnvKongAdminHostAddress = "KONG_ADMIN_ADDR"
const EnvKongAdminUsername = "KONG_ADMIN_USERNAME"
const EnvKongAdminPassword = "KONG_ADMIN_PASSWORD"
const EnvKongTLSSkipVerify = "TLS_SKIP_VERIFY"
const EnvKongApiKey = "KONG_API_KEY"
const EnvKongAdminToken = "KONG_ADMIN_TOKEN"
type KongAdminClient struct {
config *Config
}
type Config struct {
HostAddress string
Username string
Password string
InsecureSkipVerify bool
ApiKey string
AdminToken string
}
func addQueryString(currentUrl string, filter interface{}) (string, error) {
v := reflect.ValueOf(filter)
if v.Kind() == reflect.Ptr && v.IsNil() {
return currentUrl, nil
}
u, err := url.Parse(currentUrl)
if err != nil {
return currentUrl, err
}
qs, err := query.Values(filter)
if err != nil {
return currentUrl, err
}
u.RawQuery = qs.Encode()
return u.String(), nil
}
func NewDefaultConfig() *Config {
config := &Config{
HostAddress: "http://localhost:8001",
Username: "",
Password: "",
InsecureSkipVerify: false,
}
if os.Getenv(EnvKongAdminHostAddress) != "" {
config.HostAddress = strings.TrimRight(os.Getenv(EnvKongAdminHostAddress), "/")
}
if os.Getenv(EnvKongAdminHostAddress) != "" {
config.Username = os.Getenv(EnvKongAdminUsername)
}
if os.Getenv(EnvKongAdminPassword) != "" {
config.Password = os.Getenv(EnvKongAdminPassword)
}
if os.Getenv(EnvKongTLSSkipVerify) != "" {
skip, err := strconv.ParseBool(os.Getenv(EnvKongTLSSkipVerify))
if err == nil {
config.InsecureSkipVerify = skip
}
}
if os.Getenv(EnvKongApiKey) != "" {
config.ApiKey = os.Getenv(EnvKongApiKey)
}
if os.Getenv(EnvKongAdminToken) != "" {
config.AdminToken = os.Getenv(EnvKongAdminToken)
}
return config
}
func NewClient(config *Config) *KongAdminClient {
return &KongAdminClient{
config: config,
}
}
func (kongAdminClient *KongAdminClient) Status() *StatusClient {
return &StatusClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Consumers() *ConsumerClient {
return &ConsumerClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Plugins() *PluginClient {
return &PluginClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Certificates() *CertificateClient {
return &CertificateClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Snis() *SnisClient {
return &SnisClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Upstreams() *UpstreamClient {
return &UpstreamClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Routes() *RouteClient {
return &RouteClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Services() *ServiceClient {
return &ServiceClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Targets() *TargetClient {
return &TargetClient{
config: kongAdminClient.config,
}
}