-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk.go
280 lines (244 loc) · 7.69 KB
/
sdk.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
272
273
274
275
276
277
278
279
280
package gosdk
import (
"context"
"errors"
"fmt"
"log/slog"
"sync/atomic"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"google.golang.org/grpc"
grpc_creds "google.golang.org/grpc/credentials"
"github.com/nebius/gosdk/auth"
"github.com/nebius/gosdk/conn"
"github.com/nebius/gosdk/serviceerror"
"github.com/nebius/gosdk/services/nebius"
)
// SDK is the Nebius API client.
type SDK struct {
resolver conn.Resolver
dialer conn.Dialer
tokener auth.BearerTokener
inits []func(context.Context, *SDK) error
closes []func() error
}
// New creates a new [SDK] with the provided options.
// By default, it also performs any necessary I/O operations.
// To separate I/O operations from instantiation, use the [WithExplicitInit] option.
//
// Important: Ensure that the provided ctx is not closed before calling [SDK.Close].
func New(ctx context.Context, opts ...Option) (*SDK, error) { //nolint:funlen
domain := "api.eu.nebius.cloud:443"
credentials := NoCredentials()
handler := slog.Handler(NoopHandler{})
explicitInit := false
customSubstitutions := make(map[string]string)
var logOpts []logging.Option
var customDialOpts []grpc.DialOption
var customResolvers []conn.Resolver
var customInits []func(context.Context, *SDK) error
for _, opt := range opts {
switch o := opt.(type) {
case optionCredentials:
credentials = o.creds
case optionLogger:
handler = o.handler
case optionLoggingOptions:
logOpts = append(logOpts, o...)
case optionDialOpts:
customDialOpts = append(customDialOpts, o...)
case optionResolvers:
customResolvers = append(customResolvers, o...)
case optionDomain:
if o == "" {
return nil, errors.New("empty domain provided")
}
domain = string(o)
case optionAddressTemplate:
customSubstitutions[o.find] = o.replace
case optionExplicitInit:
explicitInit = bool(o)
case optionInit:
customInits = append(customInits, o)
}
}
logger := slog.New(handler)
substitutions := map[string]string{
"{domain}": domain,
}
for find, replace := range customSubstitutions {
substitutions[find] = replace
}
var inits []func(context.Context, *SDK) error
var closes []func() error
var tokener auth.BearerTokener
explicitSelector := false
credsMap := map[auth.Selector]Credentials{
auth.Select("default"): credentials,
}
switch c := credentials.(type) { //nolint:gocritic // complains about single case in public gosdk
case credsOneOf:
if len(c) == 0 {
return nil, errors.New("credentials map is empty")
}
explicitSelector = true
credsMap = c
}
auths := make(map[auth.Selector]auth.Authenticator, len(credsMap))
for selector, creds := range credsMap {
switch c := creds.(type) {
case credsNoCreds:
auths[selector] = nil
case credsAuthenticator:
auths[selector] = c.auth
case credsTokener:
tokener = c.tokener
auths[selector] = auth.NewAuthenticatorFromBearerTokener(tokener)
case credsServiceAccount:
t := auth.NewExchangeableBearerTokener(auth.NewServiceAccountExchangeTokenRequester(c.reader), nil)
inits = append(inits, func(_ context.Context, sdk *SDK) error {
t.SetClient(sdk.Services().IAM().V1().TokenExchange())
return nil
})
//nolint:mnd // 90%, 1s and 1m are recommended values by IAM team
cache := auth.NewCachedServiceTokener(logger, t, 0.9, 1*time.Second, 1.5, 1*time.Minute)
inits = append(inits, initCache(cache))
tokener = cache
auths[selector] = auth.NewAuthenticatorFromBearerTokener(tokener)
case credsPropagate:
auths[selector] = auth.NewPropagateAuthorizationHeader()
case credsOneOf:
return nil, errors.New("one of credentials can not be nested")
default:
return nil, fmt.Errorf("unknown credentials type: %T", creds)
}
}
dialOpts := []grpc.DialOption{}
dialOpts = append(dialOpts,
grpc.WithChainUnaryInterceptor(conn.IdempotencyKeyInterceptor),
grpc.WithTransportCredentials(grpc_creds.NewTLS(nil)), // tls enabled by default
)
dialOpts = append(dialOpts, customDialOpts...)
dialOpts = append(dialOpts,
grpc.WithChainUnaryInterceptor(
unaryLoggingInterceptor(logger, logOpts...),
serviceerror.UnaryClientInterceptor,
),
grpc.WithChainStreamInterceptor(
streamLoggingInterceptor(logger, logOpts...),
serviceerror.StreamClientInterceptor,
),
)
// do not add interceptors if no credentials provided
for _, a := range auths {
if a != nil {
dialOpts = append(dialOpts,
grpc.WithChainUnaryInterceptor(auth.UnaryClientInterceptor(logger, auths, explicitSelector)),
grpc.WithChainStreamInterceptor(auth.StreamClientInterceptor(logger, auths, explicitSelector)),
)
break
}
}
// apply timeout after retry and auth interceptors for each request
dialOpts = append(dialOpts, grpc.WithChainUnaryInterceptor(conn.UnaryClientTimeoutInterceptor(1*time.Minute)))
dialer := conn.NewDialer(logger, dialOpts...)
closes = append(closes, dialer.Close)
inits = append(inits, customInits...)
sdk := &SDK{
resolver: conn.NewContextResolver(
logger,
conn.NewCachedResolver(
logger,
conn.NewTemplateExpander(
substitutions,
conn.NewResolversChain(append(customResolvers, conn.NewConventionResolver())...),
),
),
),
dialer: dialer,
tokener: tokener,
inits: inits,
closes: closes,
}
if !explicitInit {
errI := sdk.Init(ctx)
if errI != nil {
return nil, errI
}
}
return sdk, nil
}
// Init finalizes the creation of the [SDK] by performing all required I/O operations.
// It is automatically called by the [New] constructor by default.
// This method should only be called manually if the [WithExplicitInit] option is used.
//
// Important: Ensure that the provided ctx is not closed before calling [SDK.Close].
func (s *SDK) Init(ctx context.Context) error {
for _, init := range s.inits {
err := init(ctx, s)
if err != nil {
return err
}
}
s.inits = nil // reset to prevent issues if Init() is called multiple times or called without [WithExplicitInit]
return nil
}
// Services is a fluent interface to get a Nebius service client.
func (s *SDK) Services() nebius.Services {
return nebius.New(s)
}
// Close closes connections and leans up resources.
func (s *SDK) Close() error {
var errs error
for _, c := range s.closes {
err := c()
if err != nil {
errs = errors.Join(errs, err)
}
}
return errs
}
// BearerToken returns the token used to authorize gRPC requests.
func (s *SDK) BearerToken(ctx context.Context) (auth.BearerToken, error) {
if s.tokener == nil {
return auth.BearerToken{}, errors.New("tokener not initialized")
}
return s.tokener.BearerToken(ctx)
}
// Resolve returns an address of a specific service.
func (s *SDK) Resolve(ctx context.Context, id conn.ServiceID) (conn.Address, error) {
return s.resolver.Resolve(ctx, id)
}
// Dial returns an underlying gRPC client connection for a specific address.
func (s *SDK) Dial(ctx context.Context, address conn.Address) (grpc.ClientConnInterface, error) {
return s.dialer.Dial(ctx, address)
}
func initCache(cache *auth.CachedServiceTokener) func(context.Context, *SDK) error {
const wait = 5 * time.Second
return func(ctx context.Context, sdk *SDK) error {
ctx, cancel := context.WithCancel(ctx)
errs := make(chan error, 1)
stopped := atomic.Bool{}
stopCacheAndWaitResult := func() error {
if !stopped.CompareAndSwap(false, true) {
return nil
}
cancel()
select {
case err := <-errs:
if err == context.Canceled { //nolint:errorlint // don't use errors.Is intentionally
return nil
}
return err
case <-time.After(wait):
return errors.New("timed out waiting for cache")
}
}
// Need to stop cache before closing dialer
sdk.closes = append([]func() error{stopCacheAndWaitResult}, sdk.closes...)
go func() {
errs <- cache.Run(ctx)
}()
return nil
}
}