-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_v0.go
217 lines (182 loc) · 7.87 KB
/
client_v0.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package fireboltgosdk
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
)
type ClientImplV0 struct {
AccountID string
BaseClient
}
func MakeClientV0(settings *fireboltSettings, apiEndpoint string) (*ClientImplV0, error) {
client := &ClientImplV0{
BaseClient: BaseClient{
ClientID: settings.clientID,
ClientSecret: settings.clientSecret,
ApiEndpoint: apiEndpoint,
UserAgent: ConstructUserAgentString(),
},
}
client.parameterGetter = client.getQueryParams
client.accessTokenGetter = client.getAccessToken
var err error
client.AccountID, err = client.getAccountID(context.Background(), settings.accountName)
if err != nil {
return nil, ConstructNestedError("error during getting account id", err)
}
return client, nil
}
// getAccountIDByName returns account ID based on account name
func (c *ClientImplV0) getAccountIDByName(ctx context.Context, accountName string) (string, error) {
infolog.Printf("get account id by name: %s", accountName)
type AccountIdByNameResponse struct {
AccountId string `json:"account_id"`
}
params := map[string]string{"account_name": accountName}
resp := c.request(ctx, "GET", c.ApiEndpoint+AccountIdByNameURL, params, "")
if resp.err != nil {
return "", ConstructNestedError("error during getting account id by name request", resp.err)
}
var accountIdByNameResponse AccountIdByNameResponse
if err := json.Unmarshal(resp.data, &accountIdByNameResponse); err != nil {
return "", ConstructNestedError("error during unmarshalling account id by name response", errors.New(string(resp.data)))
}
return accountIdByNameResponse.AccountId, nil
}
// getDefaultAccountID returns an id of the default account
func (c *ClientImplV0) getDefaultAccountID(ctx context.Context) (string, error) {
type AccountResponse struct {
Id string `json:"id"`
Name string `json:"name"`
}
type DefaultAccountResponse struct {
Account AccountResponse `json:"account"`
}
resp := c.request(ctx, "GET", c.ApiEndpoint+DefaultAccountURL, make(map[string]string), "")
if resp.err != nil {
return "", ConstructNestedError("error during getting default account id request", resp.err)
}
var defaultAccountResponse DefaultAccountResponse
if err := json.Unmarshal(resp.data, &defaultAccountResponse); err != nil {
return "", ConstructNestedError("error during unmarshalling default account response", errors.New(string(resp.data)))
}
return defaultAccountResponse.Account.Id, nil
}
func (c *ClientImplV0) getAccountID(ctx context.Context, accountName string) (string, error) {
var accountId string
var err error
if accountName == "" {
infolog.Println("account name not specified, trying to get a default account id")
accountId, err = c.getDefaultAccountID(context.TODO())
} else {
accountId, err = c.getAccountIDByName(context.TODO(), accountName)
}
if err != nil {
return "", ConstructNestedError("error during getting account id", err)
}
return accountId, nil
}
// getEngineIdByName returns engineId based on engineName and accountId
func (c *ClientImplV0) getEngineIdByName(ctx context.Context, engineName string, accountId string) (string, error) {
infolog.Printf("get engine id by name '%s' and account id '%s'", engineName, accountId)
type EngineIdByNameInnerResponse struct {
AccountId string `json:"account_id"`
EngineId string `json:"engine_id"`
}
type EngineIdByNameResponse struct {
EngineId EngineIdByNameInnerResponse `json:"engine_id"`
}
params := map[string]string{"engine_name": engineName}
resp := c.request(ctx, "GET", fmt.Sprintf(c.ApiEndpoint+EngineIdByNameURL, accountId), params, "")
if resp.err != nil {
return "", ConstructNestedError("error during getting engine id by name request", resp.err)
}
var engineIdByNameResponse EngineIdByNameResponse
if err := json.Unmarshal(resp.data, &engineIdByNameResponse); err != nil {
return "", ConstructNestedError("error during unmarshalling engine id by name response", errors.New(string(resp.data)))
}
return engineIdByNameResponse.EngineId.EngineId, nil
}
// getEngineUrlById returns engine url based on engineId and accountId
func (c *ClientImplV0) getEngineUrlById(ctx context.Context, engineId string, accountId string) (string, error) {
infolog.Printf("get engine url by id '%s' and account id '%s'", engineId, accountId)
type EngineResponse struct {
Endpoint string `json:"endpoint"`
}
type EngineByIdResponse struct {
Engine EngineResponse `json:"engine"`
}
resp := c.request(ctx, "GET", fmt.Sprintf(c.ApiEndpoint+EngineByIdURL, accountId, engineId), make(map[string]string), "")
if resp.err != nil {
return "", ConstructNestedError("error during getting engine url by id request", resp.err)
}
var engineByIdResponse EngineByIdResponse
if err := json.Unmarshal(resp.data, &engineByIdResponse); err != nil {
return "", ConstructNestedError("error during unmarshalling engine url by id response", errors.New(string(resp.data)))
}
return makeCanonicalUrl(engineByIdResponse.Engine.Endpoint), nil
}
// getEngineUrlByName return engine URL based on engineName and accountName
func (c *ClientImplV0) getEngineUrlByName(ctx context.Context, engineName string, accountId string) (string, error) {
infolog.Printf("get engine url by name '%s' and account id '%s'", engineName, accountId)
engineId, err := c.getEngineIdByName(ctx, engineName, accountId)
if err != nil {
return "", ConstructNestedError("error during getting engine id by name", err)
}
engineUrl, err := c.getEngineUrlById(ctx, engineId, accountId)
if err != nil {
return "", ConstructNestedError("error during getting engine url by id", err)
}
return engineUrl, nil
}
// getEngineUrlByDatabase return URL of the default engine based on databaseName and accountName
func (c *ClientImplV0) getEngineUrlByDatabase(ctx context.Context, databaseName string, accountId string) (string, error) {
infolog.Printf("get engine url by database name '%s' and account name '%s'", databaseName, accountId)
type EngineUrlByDatabaseResponse struct {
EngineUrl string `json:"engine_url"`
}
params := map[string]string{"database_name": databaseName}
resp := c.request(ctx, "GET", fmt.Sprintf(c.ApiEndpoint+EngineUrlByDatabaseNameURL, accountId), params, "")
if resp.err != nil {
return "", ConstructNestedError("error during getting engine url by database request", resp.err)
}
var engineUrlByDatabaseResponse EngineUrlByDatabaseResponse
if err := json.Unmarshal(resp.data, &engineUrlByDatabaseResponse); err != nil {
return "", ConstructNestedError("error during unmarshalling engine url by database response", errors.New(string(resp.data)))
}
return engineUrlByDatabaseResponse.EngineUrl, nil
}
// GetConnectionParameters returns engine URL and engine name based on engineName and accountId
func (c *ClientImplV0) GetConnectionParameters(ctx context.Context, engineName, databaseName string) (string, map[string]string, error) {
// getting engineUrl either by using engineName if available,
// if not using default engine for the database
var engineUrl string
var err error
params := map[string]string{"database": databaseName}
if engineName != "" {
if strings.Contains(engineName, ".") {
engineUrl, err = makeCanonicalUrl(engineName), nil
} else {
engineUrl, err = c.getEngineUrlByName(ctx, engineName, c.AccountID)
}
} else {
infolog.Println("engine name not set, trying to get a default engine")
engineUrl, err = c.getEngineUrlByDatabase(ctx, databaseName, c.AccountID)
}
if err != nil {
return "", params, ConstructNestedError("error during getting engine url", err)
}
return engineUrl, params, nil
}
func (c *ClientImplV0) getQueryParams(setStatements map[string]string) (map[string]string, error) {
params := map[string]string{"output_format": outputFormat}
for setKey, setValue := range setStatements {
params[setKey] = setValue
}
return params, nil
}
func (c *ClientImplV0) getAccessToken() (string, error) {
return getAccessTokenUsernamePassword(c.ClientID, c.ClientSecret, c.ApiEndpoint, c.UserAgent)
}