forked from Scalingo/go-scalingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
160 lines (140 loc) · 3.57 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
package scalingo
import (
"crypto/tls"
"time"
"github.com/Scalingo/go-scalingo/http"
errgo "gopkg.in/errgo.v1"
)
type API interface {
AddonsService
AddonProvidersService
AppsService
AlertsService
AutoscalersService
BackupsService
CollaboratorsService
DeploymentsService
DomainsService
VariablesService
EventsService
KeysService
LoginService
LogDrainsService
LogsArchivesService
LogsService
NotificationPlatformsService
NotifiersService
OperationsService
RegionsService
RegionMigrationsService
RunsService
SignUpService
SourcesService
TokensService
UsersService
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
AuthEndpoint string
DatabaseAPIEndpoint string
APIToken string
Region string
UserAgent string
// StaticTokenGenerator is present for retrocompatibility with legacy tokens
// DEPRECATED, Use standard APIToken field for normal operations
StaticTokenGenerator *StaticTokenGenerator
}
func New(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(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 len(c.config.APIToken) != 0 {
tokenGenerator = http.NewAPITokenGenerator(c, c.config.APIToken)
}
return http.NewClient(http.ScalingoAPI, http.ClientConfig{
UserAgent: c.config.UserAgent,
Timeout: c.config.Timeout,
TLSConfig: c.config.TLSConfig,
APIVersion: "1",
TokenGenerator: tokenGenerator,
Endpoint: c.config.APIEndpoint,
})
}
func (c *Client) DBAPI(app, addon string) http.Client {
if c.dbClient != nil {
return c.dbClient
}
return http.NewClient(http.DBAPI, http.ClientConfig{
Timeout: c.config.Timeout,
TLSConfig: c.config.TLSConfig,
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 len(c.config.APIToken) != 0 {
tokenGenerator = http.NewAPITokenGenerator(c, c.config.APIToken)
}
return http.NewClient(http.AuthAPI, http.ClientConfig{
Timeout: c.config.Timeout,
TLSConfig: c.config.TLSConfig,
APIVersion: "1",
TokenGenerator: tokenGenerator,
Endpoint: c.config.AuthEndpoint,
})
}