-
Notifications
You must be signed in to change notification settings - Fork 72
/
default_client.go
271 lines (225 loc) · 8.56 KB
/
default_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package defaultapi
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
connectormgmt "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
kafkamgmt "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1"
"github.com/redhat-developer/app-services-cli/pkg/shared/kafkautil"
"github.com/redhat-developer/app-services-cli/internal/build"
"github.com/redhat-developer/app-services-cli/pkg/api/generic"
"github.com/redhat-developer/app-services-cli/pkg/api/rbac"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection/api"
"github.com/redhat-developer/app-services-cli/pkg/shared/svcstatus"
amsclient "github.com/redhat-developer/app-services-sdk-go/accountmgmt/apiv1/client"
kafkainstance "github.com/redhat-developer/app-services-sdk-go/kafkainstance/apiv1internal"
kafkainstanceclient "github.com/redhat-developer/app-services-sdk-go/kafkainstance/apiv1internal/client"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
kafkamgmtv1errors "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/error"
registryinstance "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal"
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
registrymgmt "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1"
registrymgmtclient "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client"
"golang.org/x/oauth2"
)
// defaultAPI is a type which defines a number of API creator functions
type defaultAPI struct {
api.Config
}
// New creates a new default API client wrapper
func New(cfg *api.Config) api.API {
return &defaultAPI{
Config: *cfg,
}
}
func (a *defaultAPI) GetConfig() api.Config {
return a.Config
}
// KafkaMgmt returns a new Kafka Management API client instance
func (a *defaultAPI) KafkaMgmt() kafkamgmtclient.DefaultApi {
tc := a.CreateOAuthTransport(a.AccessToken)
client := kafkamgmt.NewAPIClient(&kafkamgmt.Config{
BaseURL: a.ApiURL.String(),
Debug: a.Logger.DebugEnabled(),
HTTPClient: tc,
UserAgent: a.UserAgent,
})
return client.DefaultApi
}
// ServiceRegistryMgmt return a new Service Registry Management API client instance
func (a *defaultAPI) ServiceRegistryMgmt() registrymgmtclient.RegistriesApi {
tc := a.CreateOAuthTransport(a.AccessToken)
client := registrymgmt.NewAPIClient(®istrymgmt.Config{
BaseURL: a.ApiURL.String(),
Debug: a.Logger.DebugEnabled(),
HTTPClient: tc,
UserAgent: build.DefaultUserAgentPrefix + build.Version,
})
return client.RegistriesApi
}
// ServiceAccountMgmt return a new Service Account Management API client instance
func (a *defaultAPI) ServiceAccountMgmt() kafkamgmtclient.SecurityApi {
tc := a.CreateOAuthTransport(a.AccessToken)
client := kafkamgmt.NewAPIClient(&kafkamgmt.Config{
BaseURL: a.ApiURL.String(),
Debug: a.Logger.DebugEnabled(),
HTTPClient: tc,
UserAgent: a.UserAgent,
})
return client.SecurityApi
}
// KafkaAdmin returns a new Kafka Admin API client instance, with the Kafka configuration object
func (a *defaultAPI) KafkaAdmin(instanceID string) (*kafkainstanceclient.APIClient, *kafkamgmtclient.KafkaRequest, error) {
kafkaAPI := a.KafkaMgmt()
kafkaInstance, resp, err := kafkaAPI.GetKafkaById(context.Background(), instanceID).Execute()
if resp != nil {
defer resp.Body.Close()
}
if kafkamgmtv1errors.IsAPIError(err, kafkamgmtv1errors.ERROR_7) {
return nil, nil, kafkautil.NotFoundByIDError(instanceID)
} else if err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
kafkaStatus := kafkaInstance.GetStatus()
switch kafkaStatus {
case svcstatus.StatusProvisioning, svcstatus.StatusAccepted:
err = fmt.Errorf(`Kafka instance "%v" is not ready yet`, kafkaInstance.GetName())
return nil, nil, err
case svcstatus.StatusFailed:
err = fmt.Errorf(`Kafka instance "%v" has failed`, kafkaInstance.GetName())
return nil, nil, err
case svcstatus.StatusDeprovision:
err = fmt.Errorf(`Kafka instance "%v" is being deprovisioned`, kafkaInstance.GetName())
return nil, nil, err
case svcstatus.StatusDeleting:
err = fmt.Errorf(`Kafka instance "%v" is being deleted`, kafkaInstance.GetName())
return nil, nil, err
}
bootstrapURL := kafkaInstance.GetBootstrapServerHost()
if bootstrapURL == "" {
err = fmt.Errorf(`bootstrap URL is missing for Kafka instance "%v"`, kafkaInstance.GetName())
return nil, nil, err
}
apiURL := kafkaInstance.GetAdminApiServerUrl()
a.Logger.Debugf("Making request to %v", apiURL)
token := a.AccessToken
client := kafkainstance.NewAPIClient(&kafkainstance.Config{
BaseURL: apiURL,
Debug: a.Logger.DebugEnabled(),
HTTPClient: a.CreateOAuthTransport(token),
UserAgent: a.UserAgent,
})
return client, &kafkaInstance, nil
}
// ServiceRegistryInstance returns a new Service Registry API client instance, with the Registry configuration object
func (a *defaultAPI) ServiceRegistryInstance(instanceID string) (*registryinstanceclient.APIClient, *registrymgmtclient.Registry, error) {
registryAPI := a.ServiceRegistryMgmt()
instance, resp, err := registryAPI.GetRegistry(context.Background(), instanceID).Execute()
defer resp.Body.Close()
if err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
status := svcstatus.ServiceStatus(instance.GetStatus())
// nolint
switch status {
case svcstatus.StatusProvisioning, svcstatus.StatusAccepted:
err = fmt.Errorf(`service registry instance "%v" is not ready yet`, instance.GetName())
return nil, nil, err
case svcstatus.StatusFailed:
err = fmt.Errorf(`service registry instance "%v" has failed`, instance.GetName())
return nil, nil, err
case svcstatus.StatusDeprovision:
err = fmt.Errorf(`service registry instance "%v" is being deprovisioned`, instance.GetName())
return nil, nil, err
case svcstatus.StatusDeleting:
err = fmt.Errorf(`service registry instance "%v" is being deleted`, instance.GetName())
return nil, nil, err
}
registryUrl := instance.GetRegistryUrl()
if registryUrl == "" {
err = fmt.Errorf(`URL is missing for Service Registry instance "%v"`, instance.GetName())
return nil, nil, err
}
host, port, _ := net.SplitHostPort(registryUrl)
var baseURL string
if host == "localhost" {
var apiURL = &url.URL{
Scheme: "http",
Host: fmt.Sprintf("localhost:%v", port),
}
apiURL.Scheme = "http"
apiURL.Path = "/data/registry"
baseURL = apiURL.String()
} else {
baseURL = registryUrl + "/apis/registry/v2"
}
a.Logger.Debugf("Making request to %v", baseURL)
token := a.AccessToken
client := registryinstance.NewAPIClient(®istryinstance.Config{
BaseURL: baseURL,
Debug: a.Logger.DebugEnabled(),
HTTPClient: a.CreateOAuthTransport(token),
UserAgent: build.DefaultUserAgentPrefix + build.Version,
})
return client, &instance, nil
}
func (a *defaultAPI) GenericAPI() generic.GenericAPI {
tc := a.CreateOAuthTransport(a.AccessToken)
client := generic.NewGenericAPIClient(&generic.Config{
BaseURL: a.ApiURL.String(),
Debug: a.Logger.DebugEnabled(),
HTTPClient: tc,
})
return client
}
func (a *defaultAPI) ConnectorsMgmt() connectormgmtclient.APIClient {
tc := a.CreateOAuthTransport(a.AccessToken)
client := connectormgmt.NewAPIClient(&connectormgmt.Config{
BaseURL: a.ApiURL.String(),
Debug: a.Logger.DebugEnabled(),
HTTPClient: tc,
})
return *client
}
// AccountMgmt returns a new Account Management API client instance
func (a *defaultAPI) AccountMgmt() amsclient.AppServicesApi {
cfg := amsclient.NewConfiguration()
cfg.Scheme = a.ApiURL.Scheme
cfg.Host = a.ApiURL.Host
cfg.UserAgent = a.UserAgent
cfg.HTTPClient = a.CreateOAuthTransport(a.AccessToken)
apiClient := amsclient.NewAPIClient(cfg)
return apiClient.AppServicesApi
}
// RBAC returns a new RBAC API client instance
func (a *defaultAPI) RBAC() rbac.RbacAPI {
rbacAPI := rbac.RbacAPI{
PrincipalAPI: func() rbac.PrincipalAPI {
cl := a.CreateOAuthTransport(a.AccessToken)
cfg := rbac.Config{
HTTPClient: cl,
Debug: a.Logger.DebugEnabled(),
BaseURL: a.ConsoleURL,
}
return rbac.NewPrincipalAPIClient(&cfg)
},
}
return rbacAPI
}
// wraps the HTTP client with an OAuth2 Transport layer to provide automatic token refreshing
func (a *defaultAPI) CreateOAuthTransport(accessToken string) *http.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: accessToken,
},
)
return &http.Client{
Transport: &oauth2.Transport{
Base: a.HTTPClient.Transport,
Source: oauth2.ReuseTokenSource(nil, ts),
},
}
}