sdk.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package sdk contains Nebius contains logic to create Nebius sdk for interaction with any Nebius service.
  14. package sdk
  15. import (
  16. "context"
  17. "crypto/tls"
  18. "crypto/x509"
  19. "errors"
  20. "time"
  21. "github.com/nebius/gosdk"
  22. "google.golang.org/grpc"
  23. "google.golang.org/grpc/credentials"
  24. "google.golang.org/grpc/keepalive"
  25. )
  26. // NewSDK initializes a new gosdk.SDK instance using the provided context, API domain, and CA certificate.
  27. // It sets up TLS configuration, including support for custom CA certificates, and gRPC dial options.
  28. // Returns the initialized SDK instance or an error if the setup fails.
  29. func NewSDK(ctx context.Context, apiDomain string, caCertificate []byte) (*gosdk.SDK, error) {
  30. tlsCfg := &tls.Config{MinVersion: tls.VersionTLS12}
  31. if len(caCertificate) > 0 {
  32. certPool := x509.NewCertPool()
  33. if !certPool.AppendCertsFromPEM(caCertificate) {
  34. return nil, errors.New("failed to append CA certificate. PEM parse error")
  35. }
  36. tlsCfg.RootCAs = certPool
  37. }
  38. sdk, err := gosdk.New(
  39. ctx,
  40. gosdk.WithDomain(apiDomain),
  41. gosdk.WithDialOptions(
  42. grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)),
  43. grpc.WithKeepaliveParams(keepalive.ClientParameters{
  44. Time: time.Second * 30,
  45. Timeout: time.Second * 5,
  46. PermitWithoutStream: false,
  47. }),
  48. ),
  49. )
  50. return sdk, err
  51. }