forked from kevholditch/gokong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
133 lines (110 loc) · 2.77 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
package gokong
import (
"net/url"
"os"
"reflect"
"strings"
"github.com/google/go-querystring/query"
)
const EnvKongAdminHostAddress = "KONG_ADMIN_ADDR"
const EnvKongApiHostAddress = "KONG_API_ADDR"
const EnvKongAdminUsername = "KONG_ADMIN_USERNAME"
const EnvKongAdminPassword = "KONG_ADMIN_PASSWORD"
const EnvKongApiKey = "KONG_API_KEY"
type KongAdminClient struct {
config *Config
}
type Config struct {
HostAddress string
Username string
Password string
InsecureSkipVerify bool
ApiKey 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(EnvKongApiKey) != "" {
config.ApiKey = os.Getenv(EnvKongApiKey)
}
return config
}
func NewClient(config *Config) *KongAdminClient {
return &KongAdminClient{
config: config,
}
}
func (kongAdminClient *KongAdminClient) Status() *StatusClient {
return &StatusClient{
config: kongAdminClient.config,
}
}
func (kongAdminClient *KongAdminClient) Apis() *ApiClient {
return &ApiClient{
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,
}
}