2023-04-13 05:15:20 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
v2container "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/signature"
|
2023-04-13 05:55:01 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2023-04-13 05:15:20 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2024-03-01 12:16:16 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2023-04-13 05:15:20 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PrmContainerList groups parameters of ContainerList operation.
|
|
|
|
type PrmContainerList struct {
|
2023-10-23 14:25:46 +00:00
|
|
|
XHeaders []string
|
2023-04-13 05:15:20 +00:00
|
|
|
|
2023-11-21 08:35:10 +00:00
|
|
|
Account user.ID
|
2024-03-01 12:16:16 +00:00
|
|
|
|
|
|
|
Session *session.Container
|
2023-04-13 05:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAccount sets identifier of the FrostFS account to list the containers.
|
|
|
|
// Required parameter.
|
2023-10-23 14:25:46 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerList.Account instead.
|
2023-04-13 05:15:20 +00:00
|
|
|
func (x *PrmContainerList) SetAccount(id user.ID) {
|
2023-11-21 08:35:10 +00:00
|
|
|
x.Account = id
|
2023-04-13 05:15:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 07:31:07 +00:00
|
|
|
func (x *PrmContainerList) buildRequest(c *Client) (*v2container.ListRequest, error) {
|
2023-11-21 08:35:10 +00:00
|
|
|
if x.Account.IsEmpty() {
|
2023-04-13 05:33:17 +00:00
|
|
|
return nil, errorAccountNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
var ownerV2 refs.OwnerID
|
2023-10-23 14:25:46 +00:00
|
|
|
x.Account.WriteToV2(&ownerV2)
|
2023-04-13 05:33:17 +00:00
|
|
|
|
|
|
|
reqBody := new(v2container.ListRequestBody)
|
|
|
|
reqBody.SetOwnerID(&ownerV2)
|
|
|
|
|
2024-03-01 12:16:16 +00:00
|
|
|
var meta v2session.RequestMetaHeader
|
|
|
|
writeXHeadersToMeta(x.XHeaders, &meta)
|
|
|
|
|
|
|
|
if x.Session != nil {
|
|
|
|
var tokv2 v2session.Token
|
|
|
|
x.Session.WriteToV2(&tokv2)
|
|
|
|
|
|
|
|
meta.SetSessionToken(&tokv2)
|
|
|
|
}
|
|
|
|
|
2023-04-13 05:33:17 +00:00
|
|
|
var req v2container.ListRequest
|
|
|
|
req.SetBody(reqBody)
|
2024-03-01 12:16:16 +00:00
|
|
|
c.prepareRequest(&req, &meta)
|
2023-04-13 05:33:17 +00:00
|
|
|
return &req, nil
|
|
|
|
}
|
|
|
|
|
2023-04-13 05:15:20 +00:00
|
|
|
// ResContainerList groups resulting values of ContainerList operation.
|
|
|
|
type ResContainerList struct {
|
|
|
|
statusRes
|
|
|
|
|
|
|
|
ids []cid.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Containers returns list of identifiers of the account-owned containers.
|
|
|
|
//
|
|
|
|
// Client doesn't retain value so modification is safe.
|
|
|
|
func (x ResContainerList) Containers() []cid.ID {
|
|
|
|
return x.ids
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerList requests identifiers of the account-owned containers.
|
|
|
|
//
|
|
|
|
// 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`.
|
2023-09-15 15:52:08 +00:00
|
|
|
// If PrmInit.DisableFrostFSFailuresResolution has been called, unsuccessful
|
2023-09-12 15:57:04 +00:00
|
|
|
// FrostFS status codes are included in the returned result structure,
|
|
|
|
// otherwise, are also returned as `error`.
|
2023-04-13 05:15:20 +00:00
|
|
|
//
|
|
|
|
// Returns an error if parameters are set incorrectly (see PrmContainerList docs).
|
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
|
|
|
// Return statuses:
|
|
|
|
// - global (see Client docs).
|
|
|
|
func (c *Client) ContainerList(ctx context.Context, prm PrmContainerList) (*ResContainerList, error) {
|
2023-04-17 07:31:07 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
2023-04-13 05:33:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-04-13 05:15:20 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:18:07 +00:00
|
|
|
if err := signature.SignServiceMessage(&c.prm.Key, req); err != nil {
|
2023-04-13 05:55:01 +00:00
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
2023-04-13 05:15:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 05:55:01 +00:00
|
|
|
resp, err := rpcapi.ListContainers(&c.c, req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-13 05:15:20 +00:00
|
|
|
|
2023-04-13 05:55:01 +00:00
|
|
|
var res ResContainerList
|
|
|
|
res.st, err = c.processResponse(resp)
|
|
|
|
if err != nil || !apistatus.IsSuccessful(res.st) {
|
|
|
|
return &res, err
|
2023-04-13 05:15:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 05:55:01 +00:00
|
|
|
res.ids = make([]cid.ID, len(resp.GetBody().GetContainerIDs()))
|
|
|
|
for i, cidV2 := range resp.GetBody().GetContainerIDs() {
|
|
|
|
if err := res.ids[i].ReadFromV2(cidV2); err != nil {
|
|
|
|
return &res, fmt.Errorf("invalid ID in the response: %w", err)
|
|
|
|
}
|
2023-04-13 05:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|