ycsdk

package module
v2.51.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 21 Imported by: 8

README

Yandex Cloud GO SDK

GoDoc CircleCI

Go SDK for Yandex Cloud services.

NOTE: SDK is under development, and may make backwards-incompatible changes.

Table of Contents

Installation

go get github.com/yandex-cloud/go-sdk/v2

Example usages

Initializing SDK
sdk, err := ycsdk.Build(ctx,
    options..WithCredentials(credentials.IAMToken(os.Getenv("YC_IAM_TOKEN"))),
)
if err != nil {
    log.Fatal(err)
}
SDK Authorization

https://yandex.cloud/ru/docs/iam/concepts/authorization/

IAM Token (Prefered)

https://yandex.cloud/ru/docs/iam/concepts/authorization/iam-token

sdk, err := ycsdk.Build(ctx,
    options..WithCredentials(credentials.IAMToken(os.Getenv("YC_IAM_TOKEN"))),
)
if err != nil {
    log.Fatal(err)
}
Inside Yandex Cloud Virtual Machine (Prefered)

https://yandex.cloud/ru/docs/compute/operations/vm-connect/auth-inside-vm#auth-inside-vm

sdk, err := ycsdk.Build(ctx,
    options.WithCredentials(credentials.InstanceServiceAccount()),
)
if err != nil {
	log.Fatal(err)
}
OAuth-token

https://yandex.cloud/ru/docs/iam/concepts/authorization/oauth-token

sdk, err := ycsdk.Build(ctx,
    options..WithCredentials(credentials.OAuthToken(os.Getenv("YC_IAM_OAUTH"))),
)
if err != nil {
    log.Fatal(err)
}
IAM Authorized Key (Service Account)

https://yandex.cloud/ru/docs/iam/concepts/authorization/key

keyPath := "your-authorized-key-path"
creds, err := credentials.ServiceAccountKeyFile(keyPath)
if err != nil {
    log.Fatalf("failed to load credentials: %w", err)
}
sdk, err := ycsdk.Build(ctx,
    options.WithCredentials(creds),
)
if err != nil {
	log.Fatal(err)
}
Service Usage
import (
    "context"
    "fmt"
    "log"
    "os"
    
    "github.com/yandex-cloud/go-sdk/v2"
    "github.com/yandex-cloud/go-sdk/v2/credentials"
    "github.com/yandex-cloud/go-sdk/v2/pkg/options"
    
    computeapi "github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
    computesdk "github.com/yandex-cloud/go-sdk/services/compute/v1"
)

func main() {
    // Create background context
    ctx := context.Background()

    // Initialize SDK with IAM token credentials from environment variable
    sdk, err := ycsdk.Build(ctx,
        options.WithCredentials(credentials.IAMToken(os.Getenv("YC_IAM_TOKEN")).
    ))
    
    if err != nil {
        log.Fatalf("failed to build SDK: %v", err)
    }

    // Create image client instance
    imageClient := computesdk.NewImageClient(sdk)

    // Request latest image from Debian 9
    resp, err := imageClient.GetLatestByFamily(ctx, &computeapi.GetImageLatestByFamilyRequest{
        FolderId: os.Getenv("YC_FOLDER_ID"), // Get folder ID from env variable
        Family:   "debian-9", // Specify Debian 9 image family
    })

    if err != nil {
        log.Fatalf("failed to get image: %v", err)
    }

    // Print response with image details
    fmt.Println(resp)
}

Retries

SDK provide built-in retry policy, that supports exponential backoff and jitter, and also retry budget. It's necessary to avoid retry amplification.

import (
    ...
    ycsdk "github.com/yandex-cloud/go-sdk/v2"
    "github.com/yandex-cloud/go-sdk/v2/credentials"
    "github.com/yandex-cloud/go-sdk/v2/pkg/options"
)

...

sdk, err := ycsdk.Build(
    ctx,
    options.WithCredentials(credentials.IAMToken(os.Getenv("YC_IAM_TOKEN"))),
    options.WithDefaultRetryOptions(),
)

SDK provide different modes for retry throttling policy:

  • persistent is suitable when you use SDK in any long-lived application, when SDK instance will live long enough for manage budget;
  • temporary is suitable when you use SDK in any short-lived application, e.g. scripts or CI/CD.

By default, SDK will use temporary mode, but you can change it through functional option.

SDK Resolvers

SDK resolvers allows you to find resource id by its name.


import (
    "context"
    "flag"
    "fmt"
    "log"
    "os"
    
    ycsdk "bb.yandex-team.ru/cloud/cloud-go/sdk-v2"
    "bb.yandex-team.ru/cloud/cloud-go/sdk-v2/credentials"
    "bb.yandex-team.ru/cloud/cloud-go/sdk-v2/pkg/options"
    "bb.yandex-team.ru/cloud/cloud-go/sdk-v2/pkg/sdkresolvers"
    computesdk "bb.yandex-team.ru/cloud/cloud-go/sdk-v2/sdkresolvers/compute/v1"
)

func main() {
	iamToken := flag.String("token", os.Getenv("YC_IAM_TOKEN"), "IAM token for Yandex.Cloud (env YC_IAM_TOKEN)")
	folderID := flag.String("folder-id", os.Getenv("YC_FOLDER_ID"), "Yandex.Cloud Folder ID (env YC_FOLDER_ID)")

	ctx := context.Background()
	sdk, err := ycsdk.Build(ctx,
		options.WithCredentials(credentials.IAMToken(*iamToken)),
	)
	if err != nil {
		log.Fatal(err)
	}

	name := "test_name"
	r := computesdk.DiskResolver(name, sdkresolvers.FolderID(*folderID))
	if err = r.Run(ctx, sdk); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("id of object %s is %s", name, r.ID())
}

More examples

More examples can be found in examples dir.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IamTokenCreateEndpoint = protoreflect.FullName("yandex.cloud.iam.v1.IamTokenService.Create")

Functions

This section is empty.

Types

type SDK

type SDK struct {
	// contains filtered or unexported fields
}

SDK provides a client connection wrapper managing connection pooling and endpoint resolution for gRPC services.

func Build

func Build(ctx context.Context, opts ...options.Option) (*SDK, error)

Build initializes and configures an SDK instance with the provided options and context. It applies default configurations and validates necessary parameters like credentials and endpoints. Returns an SDK instance or an error if initialization fails.

func (*SDK) CreateIAMToken added in v2.11.0

func (sdk *SDK) CreateIAMToken(ctx context.Context) (authentication.IamToken, error)

func (*SDK) GetConnection

func (sdk *SDK) GetConnection(ctx context.Context, method protoreflect.FullName, opts ...grpc.CallOption) (grpc.ClientConnInterface, error)

GetConnection retrieves a gRPC client connection for the specified method with optional call options.

func (*SDK) GetEndpoint added in v2.31.0

func (sdk *SDK) GetEndpoint(method protoreflect.FullName) (*endpoints.Endpoint, error)

func (*SDK) Shutdown

func (sdk *SDK) Shutdown(ctx context.Context) error

Shutdown gracefully terminates the SDK by closing all active gRPC connections in the connection pool.

func (*SDK) WithEndpoints added in v2.39.0

func (sdk *SDK) WithEndpoints(endpoints *endpoints.PrefixEndpointsResolver) *SDK

func (*SDK) WithEnpointsResolver added in v2.39.0

func (sdk *SDK) WithEnpointsResolver(endpointsResolver endpoints.EndpointsResolver) *SDK

Directories

Path Synopsis
Package credentials provides functionality for managing authentication and authorization credentials used in SDK operations.
Package credentials provides functionality for managing authentication and authorization credentials used in SDK operations.
pkg
authentication
Package authentication provides a interface for IAM (Identity and Access Management) token operations in sdk services.
Package authentication provides a interface for IAM (Identity and Access Management) token operations in sdk services.
endpoints
Package endpoints provides functionality for managing service endpoints and endpoint resolution in the Yandex Cloud Go SDK.
Package endpoints provides functionality for managing service endpoints and endpoint resolution in the Yandex Cloud Go SDK.
log
operation
Package operation provides implementation for handling operations in Yandex Cloud Go SDK.
Package operation provides implementation for handling operations in Yandex Cloud Go SDK.
transport
Package transport provides functionality for managing gRPC connections and communication in the Yandex Cloud Go SDK.
Package transport provides functionality for managing gRPC connections and communication in the Yandex Cloud Go SDK.
services
endpoint
Code generated by sdkgen-v2.
Code generated by sdkgen-v2.
endpoints
Code generated by sdkgen-v2.
Code generated by sdkgen-v2.
iam/v1
Code generated by sdkgen-v2.
Code generated by sdkgen-v2.
iam/v1/awscompatibility
Code generated by sdkgen-v2.
Code generated by sdkgen-v2.
iam/v1/workload
Code generated by sdkgen-v2.
Code generated by sdkgen-v2.
iam/v1/workload/oidc
Code generated by sdkgen-v2.
Code generated by sdkgen-v2.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL