operation

package
v2.49.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 15 Imported by: 80

Documentation

Overview

Package operation provides implementation for handling operations in Yandex Cloud Go SDK.

An operation represents a long-running task in Yandex Cloud that can be monitored and managed through this package. Operations are typically created by service methods and can be polled for completion status.

Usage Example:

op, err := service.CreateResource(ctx, request)
if err != nil {
    // handle error
}

// Wait for operation completion
result, err := op.Wait(ctx)
if err != nil {
    // handle error
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(op YCOperation) error

Error extracts and returns the error from the provided YCOperation if it exists, otherwise returns nil.

func Response

func Response(op YCOperation) (proto.Message, error)

Response processes the response from a YCOperation and unmarshals it into a proto.Message.

Types

type AbstractOperation

type AbstractOperation interface {
	ID() string
	Description() string
	CreatedBy() string
	CreatedAt() time.Time
	Metadata() proto.Message
	ResourceID() string
	Done() bool
	PollOnce(ctx context.Context, opts ...grpc.CallOption) error
	Wait(ctx context.Context, opts ...grpc.CallOption) (proto.Message, error)
	Error() error
	Response() proto.Message
	Result() OperationResult
}

type Concretization

type Concretization struct {
	Poll PollFunc

	MetadataType proto.Message
	ResponseType proto.Message

	GetResourceID func(metadata proto.Message) string
}

Concretization is a type that defines the operation handling behavior for polling and response management. Poll is a function for retrieving operation information from an operation service. MetadataType specifies the protobuf message type for the operation's metadata. ResponseType specifies the protobuf message type for the operation's response. GetResourceID is a function to extract a resource ID from the metadata.

type Operation

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

Operation represents an operation instance with associated metadata, response, and error handling functionality.

func NewOperation

func NewOperation(pb YCOperation, concretization *Concretization) (*Operation, error)

NewOperation creates a new Operation instance based on the provided YCOperation and Concretization parameters. Returns an Operation instance and an error if the metadata type does not match or other issues arise.

func (*Operation) Abstract

func (o *Operation) Abstract() AbstractOperation

Abstract returns the current operation as an AbstractOperation, providing access to its abstract interface.

func (*Operation) CreatedAt

func (o *Operation) CreatedAt() time.Time

CreatedAt returns the creation timestamp of the operation as a time.Time object.

func (*Operation) CreatedBy

func (o *Operation) CreatedBy() string

CreatedBy returns the identifier of the entity that created the operation.

func (*Operation) Description

func (o *Operation) Description() string

Description returns a string that provides the description of the operation based on the underlying proto definition.

func (*Operation) Done

func (o *Operation) Done() bool

Done checks if the operation has been completed and returns true if it is done, otherwise false.

func (*Operation) Error

func (o *Operation) Error() error

Error returns the error encountered during the operation, prioritizing the responseError if available.

func (*Operation) ID

func (o *Operation) ID() string

ID returns the identifier of the Operation as a string.

func (*Operation) Metadata

func (o *Operation) Metadata() proto.Message

Metadata retrieves the metadata associated with the Operation. It returns a proto.Message representing the metadata.

func (*Operation) PollOnce

func (o *Operation) PollOnce(ctx context.Context, opts ...grpc.CallOption) error

PollOnce performs a single polling attempt to update the operation's state and metadata in place. Returns an error if polling fails.

func (*Operation) ResourceID

func (o *Operation) ResourceID() string

ResourceID retrieves the resource ID associated with the operation's metadata or panics if not defined.

func (*Operation) Response

func (o *Operation) Response() proto.Message

Response returns the response of the completed operation. Panics if the operation is not completed or has errors.

func (*Operation) Result

func (o *Operation) Result() OperationResult

Result returns the result of the operation if it has completed. Panics if the operation is not yet done.

func (*Operation) Wait

func (o *Operation) Wait(ctx context.Context, opts ...grpc.CallOption) (proto.Message, error)

Wait blocks until the operation is completed or the context is canceled, returning the operation's response or an error.

func (*Operation) WaitInterval

func (o *Operation) WaitInterval(
	ctx context.Context,
	pollInterval PollIntervalFunc,
	opts ...grpc.CallOption,
) (proto.Message, error)

WaitInterval polls the operation periodically until it is complete or the context is canceled, using a custom interval.

type OperationResult

type OperationResult interface {
	Response() proto.Message
	Metadata() proto.Message
	Error() error
}

type PollFunc

type PollFunc func(ctx context.Context, operationId string, opts ...grpc.CallOption) (YCOperation, error)

PollFunc defines a function type used to poll the status of an operation using its ID and return a YCOperation or an error.

type PollIntervalFunc

type PollIntervalFunc func(attempt int) time.Duration

PollIntervalFunc defines a function type that calculates the delay between polling attempts based on the attempt number.

type YCOperation

type YCOperation interface {
	GetId() string
	GetDescription() string
	GetMetadata() *anypb.Any
	GetCreatedAt() *timestamppb.Timestamp
	GetError() *status.Status
	GetCreatedBy() string
	GetResponse() *anypb.Any
	GetDone() bool
	ProtoReflect() protoreflect.Message
}

func PollUntilDone

func PollUntilDone(
	ctx context.Context,
	operationId string,
	poll PollFunc,
	pollInterval PollIntervalFunc,
	opts ...grpc.CallOption,
) (YCOperation, error)

PollUntilDone repeatedly polls the status of a long-running operation until it is marked as done or the context is canceled. ctx is the context for managing the polling lifecycle and cancellation. operationId identifies the specific operation to be polled. poll is a function used to fetch the current state of the operation. pollInterval determines the duration to wait between polling attempts based on the attempt count. opts allows for additional gRPC call options to be passed during polling. Returns the completed YCOperation or an error if polling fails or the context is canceled.

Jump to

Keyboard shortcuts

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