-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
198 lines (172 loc) · 4.32 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package scalingo
import (
"context"
"crypto/tls"
"time"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/http"
)
type API interface {
AddonsService
AddonProvidersService
AppsService
AlertsService
AutoscalersService
BackupsService
CollaboratorsService
ContainersService
ContainerSizesService
CronTasksService
DatabasesService
DeploymentsService
DomainsService
EventsService
InvoicesService
KeysService
LogDrainsService
LogsArchivesService
LogsService
NotificationPlatformsService
NotifiersService
OperationsService
RegionsService
RegionMigrationsService
RunsService
SignUpService
SourcesService
StacksService
TokensService
UsersService
VariablesService
http.TokenGenerator
ScalingoAPI() http.Client
AuthAPI() http.Client
DBAPI(app, addon string) http.Client
}
var _ API = (*Client)(nil)
type Client struct {
config ClientConfig
apiClient http.Client
dbClient http.Client
authClient http.Client
}
type ClientConfig struct {
Timeout time.Duration
TLSConfig *tls.Config
APIEndpoint string
APIPrefix string
AuthEndpoint string
AuthPrefix string
DatabaseAPIEndpoint string
DatabaseAPIPrefix string
APIToken string
Region string
UserAgent string
DisableHTTPClientCache bool
// StaticTokenGenerator is present for Scalingo internal use only
StaticTokenGenerator *StaticTokenGenerator
}
func New(ctx context.Context, cfg ClientConfig) (*Client, error) {
// Apply defaults
if cfg.AuthEndpoint == "" {
cfg.AuthEndpoint = "https://auth.scalingo.com"
}
if cfg.UserAgent == "" {
cfg.UserAgent = "go-scalingo v" + Version
}
// If there's no region defined return the client as is
if cfg.Region == "" {
return &Client{
config: cfg,
}, nil
}
// if a region was defined, create a temp client to query the auth service for region list
// then create the real client
tmpClient := &Client{
config: cfg,
}
region, err := tmpClient.getRegion(ctx, cfg.Region)
if err == ErrRegionNotFound {
return nil, err
} else if err != nil {
return nil, errgo.Notef(err, "fail to get region informations")
}
cfg.APIEndpoint = region.API
cfg.DatabaseAPIEndpoint = region.DatabaseAPI
return &Client{
config: cfg,
}, nil
}
func (c *Client) ScalingoAPI() http.Client {
if c.apiClient != nil {
return c.apiClient
}
var tokenGenerator http.TokenGenerator
if c.config.StaticTokenGenerator != nil {
tokenGenerator = c.config.StaticTokenGenerator
}
if c.config.APIToken != "" {
tokenGenerator = http.NewAPITokenGenerator(c, c.config.APIToken)
}
prefix := "/v1"
if c.config.APIPrefix != "" {
prefix = c.config.APIPrefix
}
client := http.NewClient(http.ClientConfig{
UserAgent: c.config.UserAgent,
Timeout: c.config.Timeout,
TLSConfig: c.config.TLSConfig,
APIConfig: http.APIConfig{Prefix: prefix},
TokenGenerator: tokenGenerator,
Endpoint: c.config.APIEndpoint,
})
if !c.config.DisableHTTPClientCache {
c.apiClient = client
}
return client
}
func (c *Client) DBAPI(app, addon string) http.Client {
if c.dbClient != nil {
return c.dbClient
}
prefix := "/api"
if c.config.DatabaseAPIPrefix != "" {
prefix = c.config.DatabaseAPIPrefix
}
return http.NewClient(http.ClientConfig{
UserAgent: c.config.UserAgent,
Timeout: c.config.Timeout,
TLSConfig: c.config.TLSConfig,
APIConfig: http.APIConfig{Prefix: prefix},
TokenGenerator: http.NewAddonTokenGenerator(app, addon, c),
Endpoint: c.config.DatabaseAPIEndpoint,
})
}
func (c *Client) AuthAPI() http.Client {
if c.authClient != nil {
return c.authClient
}
var tokenGenerator http.TokenGenerator
if c.config.StaticTokenGenerator != nil {
tokenGenerator = c.config.StaticTokenGenerator
}
if c.config.APIToken != "" {
tokenGenerator = http.NewAPITokenGenerator(c, c.config.APIToken)
}
prefix := "/v1"
if c.config.AuthPrefix != "" {
prefix = c.config.AuthPrefix
}
client := http.NewClient(http.ClientConfig{
UserAgent: c.config.UserAgent,
Timeout: c.config.Timeout,
TLSConfig: c.config.TLSConfig,
APIConfig: http.APIConfig{Prefix: prefix},
TokenGenerator: tokenGenerator,
Endpoint: c.config.AuthEndpoint,
})
if !c.config.DisableHTTPClientCache {
c.authClient = client
}
return client
}