2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-09 06:48:05 +00:00
|
|
|
"fmt"
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
v2accounting "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/accounting"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
2023-08-09 06:48:05 +00:00
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/accounting"
|
2023-08-09 06:48:05 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-11-09 08:07:49 +00:00
|
|
|
)
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// PrmBalanceGet groups parameters of BalanceGet operation.
|
|
|
|
type PrmBalanceGet struct {
|
2023-10-23 14:16:13 +00:00
|
|
|
XHeaders []string
|
2022-03-03 11:04:53 +00:00
|
|
|
|
2023-10-23 14:16:13 +00:00
|
|
|
Account *user.ID
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetAccount sets identifier of the FrostFS account for which the balance is requested.
|
2022-04-11 06:30:22 +00:00
|
|
|
// Required parameter.
|
2023-10-23 14:16:13 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmBalanceGet.Account instead.
|
2022-04-11 06:30:22 +00:00
|
|
|
func (x *PrmBalanceGet) SetAccount(id user.ID) {
|
2023-10-23 14:16:13 +00:00
|
|
|
x.Account = &id
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 06:48:05 +00:00
|
|
|
func (x *PrmBalanceGet) buildRequest(c *Client) (*v2accounting.BalanceRequest, error) {
|
2023-10-23 14:16:13 +00:00
|
|
|
if x.Account == nil {
|
2023-08-09 06:48:05 +00:00
|
|
|
return nil, errorAccountNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountV2 refs.OwnerID
|
2023-10-23 14:16:13 +00:00
|
|
|
x.Account.WriteToV2(&accountV2)
|
2023-08-09 06:48:05 +00:00
|
|
|
|
|
|
|
var body v2accounting.BalanceRequestBody
|
|
|
|
body.SetOwnerID(&accountV2)
|
|
|
|
|
|
|
|
var req v2accounting.BalanceRequest
|
|
|
|
req.SetBody(&body)
|
|
|
|
|
|
|
|
c.prepareRequest(&req, new(v2session.RequestMetaHeader))
|
|
|
|
return &req, nil
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// ResBalanceGet groups resulting values of BalanceGet operation.
|
|
|
|
type ResBalanceGet struct {
|
2021-11-16 18:17:25 +00:00
|
|
|
statusRes
|
|
|
|
|
2022-08-05 07:56:49 +00:00
|
|
|
amount accounting.Decimal
|
2021-11-16 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Amount returns current amount of funds on the FrostFS account as decimal number.
|
2022-08-05 07:56:49 +00:00
|
|
|
func (x ResBalanceGet) Amount() accounting.Decimal {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.amount
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// BalanceGet requests current balance of the FrostFS account.
|
2021-12-29 13:55:29 +00:00
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
2022-01-11 12:44:22 +00:00
|
|
|
// 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`.
|
2021-12-02 12:21:54 +00:00
|
|
|
//
|
2023-02-27 07:53:27 +00:00
|
|
|
// Returns an error if parameters are set incorrectly (see PrmBalanceGet docs).
|
2021-12-02 12:21:54 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Return statuses:
|
2022-08-24 14:17:40 +00:00
|
|
|
// - global (see Client docs).
|
2022-02-17 16:10:49 +00:00
|
|
|
func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (*ResBalanceGet, error) {
|
2023-08-09 06:48:05 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:18:07 +00:00
|
|
|
if err := signature.SignServiceMessage(&c.prm.Key, req); err != nil {
|
2023-08-09 06:48:05 +00:00
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
|
|
|
}
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2023-08-09 06:48:05 +00:00
|
|
|
resp, err := rpcapi.Balance(&c.c, req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
2022-03-12 16:15:26 +00:00
|
|
|
|
2023-08-09 06:48:05 +00:00
|
|
|
var res ResBalanceGet
|
|
|
|
res.st, err = c.processResponse(resp)
|
|
|
|
if err != nil || !apistatus.IsSuccessful(res.st) {
|
|
|
|
return &res, err
|
|
|
|
}
|
2022-08-05 07:56:49 +00:00
|
|
|
|
2023-08-09 06:48:05 +00:00
|
|
|
const fieldBalance = "balance"
|
2022-07-07 10:51:05 +00:00
|
|
|
|
2023-08-09 06:48:05 +00:00
|
|
|
bal := resp.GetBody().GetBalance()
|
|
|
|
if bal == nil {
|
|
|
|
return &res, newErrMissingResponseField(fieldBalance)
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 06:48:05 +00:00
|
|
|
if err := res.amount.ReadFromV2(*bal); err != nil {
|
|
|
|
return &res, newErrInvalidResponseField(fieldBalance, err)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
return &res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|