frostfs-sdk-go/client/container_get.go
2023-07-12 19:08:37 +03:00

114 lines
3.2 KiB
Go

package client
import (
"context"
"errors"
"fmt"
containergrpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container/grpc"
refsgrpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs/grpc"
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
sessiongrpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
)
// PrmContainerGet groups parameters of ContainerGet operation.
type PrmContainerGet struct {
prmCommonMeta
idSet bool
id cid.ID
}
// SetContainer sets identifier of the container to be read.
// Required parameter.
func (x *PrmContainerGet) SetContainer(id cid.ID) {
x.id = id
x.idSet = true
}
func (x *PrmContainerGet) buildRequest(c *Client) (*containergrpc.GetRequest, error) {
if !x.idSet {
return nil, errorMissingContainer
}
var cidV2 refsgrpc.ContainerID
x.id.WriteToV2(&cidV2)
reqBody := new(containergrpc.GetRequest_Body)
reqBody.SetContainerId(&cidV2)
var req containergrpc.GetRequest
req.SetBody(reqBody)
c.prepareRequest(&req, new(sessiongrpc.RequestMetaHeader))
return &req, nil
}
// ResContainerGet groups resulting values of ContainerGet operation.
type ResContainerGet struct {
statusRes
cnr *container.Container
}
func NewResContainerGet() *ResContainerGet {
return &ResContainerGet{
cnr: new(container.Container),
}
}
// Container returns structured information about the requested container.
//
// Client doesn't retain value so modification is safe.
func (x *ResContainerGet) Container() *container.Container {
return x.cnr
}
// ContainerGet reads FrostFS container by ID.
//
// Exactly one return value is non-nil. By default, server status is returned in res structure.
// Any client's internal or transport errors are returned as `error`.
// If PrmInit.ResolveFrostFSFailures has been called, unsuccessful
// FrostFS status codes are returned as `error`, otherwise, are included
// in the returned result structure.
//
// Returns an error if parameters are set incorrectly (see PrmContainerGet docs).
// Context is required and must not be nil. It is used for network communication.
//
// Return statuses:
// - global (see Client docs);
// - *apistatus.ContainerNotFound.
func (c *Client) ContainerGet(ctx context.Context, prm PrmContainerGet) (*ResContainerGet, error) {
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}
if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
return nil, fmt.Errorf("sign request: %w", err)
}
resp, err := rpcapi.GetContainer(&c.c, req, client.WithContext(ctx))
if err != nil {
return nil, err
}
res := NewResContainerGet()
res.st, err = c.processResponse(resp)
if err != nil || !apistatus.IsSuccessful(res.st) {
return nil, err
}
cnrV2 := resp.GetBody().GetContainer()
if cnrV2 == nil {
return nil, errors.New("missing container in response")
}
if err := res.cnr.ReadFromV2(cnrV2); err != nil {
return nil, fmt.Errorf("invalid container in response: %w", err)
}
return res, nil
}