2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-07 08:25:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-11-09 08:07:49 +00:00
|
|
|
|
|
|
|
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2021-11-16 18:15:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
2021-11-09 08:07:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
|
|
|
)
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// PrmEndpointInfo groups parameters of EndpointInfo operation.
|
2022-03-03 11:04:53 +00:00
|
|
|
type PrmEndpointInfo struct {
|
|
|
|
prmCommonMeta
|
|
|
|
}
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// ResEndpointInfo group resulting values of EndpointInfo operation.
|
|
|
|
type ResEndpointInfo struct {
|
2021-12-02 12:21:54 +00:00
|
|
|
statusRes
|
|
|
|
|
2021-11-09 08:07:49 +00:00
|
|
|
version *version.Version
|
|
|
|
|
|
|
|
ni *netmap.NodeInfo
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// LatestVersion returns latest NeoFS API protocol's version in use.
|
|
|
|
//
|
|
|
|
// Client doesn't retain value so modification is safe.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResEndpointInfo) LatestVersion() *version.Version {
|
2021-12-02 12:21:54 +00:00
|
|
|
return x.version
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *ResEndpointInfo) setLatestVersion(ver *version.Version) {
|
2021-12-02 12:21:54 +00:00
|
|
|
x.version = ver
|
2021-11-16 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// NodeInfo returns information about the NeoFS node served on the remote endpoint.
|
|
|
|
//
|
|
|
|
// Client doesn't retain value so modification is safe.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResEndpointInfo) NodeInfo() *netmap.NodeInfo {
|
2021-12-02 12:21:54 +00:00
|
|
|
return x.ni
|
2021-11-16 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *ResEndpointInfo) setNodeInfo(info *netmap.NodeInfo) {
|
2021-12-02 12:21:54 +00:00
|
|
|
x.ni = info
|
2021-11-16 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// EndpointInfo requests information about the storage node served on the remote endpoint.
|
|
|
|
//
|
|
|
|
// Method can be used as a health check to see if node is alive and responds to requests.
|
2021-12-29 13:55:29 +00:00
|
|
|
//
|
2022-01-11 12:44:22 +00:00
|
|
|
// Any client's internal or transport errors are returned as `error`.
|
2022-07-04 15:33:33 +00:00
|
|
|
// If PrmInit.ResolveNeoFSFailures has been called, unsuccessful
|
2022-01-11 12:44:22 +00:00
|
|
|
// NeoFS status codes are returned as `error`, otherwise, are included
|
|
|
|
// in the returned result structure.
|
2021-12-02 12:21:54 +00:00
|
|
|
//
|
2022-02-17 16:10:49 +00:00
|
|
|
// Immediately panics if parameters are set incorrectly (see PrmEndpointInfo docs).
|
2021-12-02 12:21:54 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
2022-02-17 16:10:49 +00:00
|
|
|
// Exactly one return value is non-nil. Server status return is returned in ResEndpointInfo.
|
2021-12-02 12:21:54 +00:00
|
|
|
// Reflects all internal errors in second return value (transport problems, response processing, etc.).
|
2022-01-24 10:33:43 +00:00
|
|
|
//
|
|
|
|
// Return statuses:
|
|
|
|
// - global (see Client docs).
|
2022-03-03 11:04:53 +00:00
|
|
|
func (c *Client) EndpointInfo(ctx context.Context, prm PrmEndpointInfo) (*ResEndpointInfo, error) {
|
2021-12-02 12:21:54 +00:00
|
|
|
// check context
|
|
|
|
if ctx == nil {
|
|
|
|
panic(panicMsgMissingContext)
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// form request
|
|
|
|
var req v2netmap.LocalNodeInfoRequest
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// init call context
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
var (
|
2021-12-02 12:21:54 +00:00
|
|
|
cc contextCall
|
2022-02-17 16:10:49 +00:00
|
|
|
res ResEndpointInfo
|
2021-11-16 18:17:25 +00:00
|
|
|
)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
c.initCallContext(&cc)
|
2022-03-03 11:04:53 +00:00
|
|
|
cc.meta = prm.prmCommonMeta
|
2021-12-02 12:21:54 +00:00
|
|
|
cc.req = &req
|
|
|
|
cc.statusRes = &res
|
|
|
|
cc.call = func() (responseV2, error) {
|
2022-03-07 11:39:49 +00:00
|
|
|
return rpcapi.LocalNodeInfo(&c.c, &req, client.WithContext(ctx))
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
cc.result = func(r responseV2) {
|
|
|
|
resp := r.(*v2netmap.LocalNodeInfoResponse)
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
body := resp.GetBody()
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2022-03-23 15:35:44 +00:00
|
|
|
var ver version.Version
|
|
|
|
if v2ver := body.GetVersion(); v2ver != nil {
|
|
|
|
ver.ReadFromV2(*v2ver)
|
|
|
|
}
|
|
|
|
res.setLatestVersion(&ver)
|
2022-06-07 08:25:34 +00:00
|
|
|
|
|
|
|
nodeV2 := body.GetNodeInfo()
|
|
|
|
if nodeV2 == nil {
|
|
|
|
cc.err = errors.New("missing node info in response body")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
|
|
|
|
cc.err = node.ReadFromV2(*nodeV2)
|
|
|
|
if cc.err != nil {
|
|
|
|
cc.err = fmt.Errorf("invalid node info: %w", cc.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setNodeInfo(&node)
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// process call
|
|
|
|
if !cc.processCall() {
|
|
|
|
return nil, cc.err
|
|
|
|
}
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
return &res, nil
|
2021-11-16 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// PrmNetworkInfo groups parameters of NetworkInfo operation.
|
2022-03-03 11:04:53 +00:00
|
|
|
type PrmNetworkInfo struct {
|
|
|
|
prmCommonMeta
|
|
|
|
}
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// ResNetworkInfo groups resulting values of NetworkInfo operation.
|
|
|
|
type ResNetworkInfo struct {
|
2021-11-16 18:17:25 +00:00
|
|
|
statusRes
|
|
|
|
|
|
|
|
info *netmap.NetworkInfo
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// Info returns structured information about the NeoFS network.
|
|
|
|
//
|
|
|
|
// Client doesn't retain value so modification is safe.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResNetworkInfo) Info() *netmap.NetworkInfo {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.info
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *ResNetworkInfo) setInfo(info *netmap.NetworkInfo) {
|
2021-11-16 18:17:25 +00:00
|
|
|
x.info = info
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// NetworkInfo requests information about the NeoFS network of which the remote server is a part.
|
2021-12-29 13:55:29 +00:00
|
|
|
//
|
2022-01-11 12:44:22 +00:00
|
|
|
// Any client's internal or transport errors are returned as `error`.
|
2022-07-04 15:33:33 +00:00
|
|
|
// If PrmInit.ResolveNeoFSFailures has been called, unsuccessful
|
2022-01-11 12:44:22 +00:00
|
|
|
// NeoFS status codes are returned as `error`, otherwise, are included
|
|
|
|
// in the returned result structure.
|
2021-12-02 12:21:54 +00:00
|
|
|
//
|
2022-02-17 16:10:49 +00:00
|
|
|
// Immediately panics if parameters are set incorrectly (see PrmNetworkInfo docs).
|
2021-12-02 12:21:54 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
2022-02-17 16:10:49 +00:00
|
|
|
// Exactly one return value is non-nil. Server status return is returned in ResNetworkInfo.
|
2021-12-02 12:21:54 +00:00
|
|
|
// Reflects all internal errors in second return value (transport problems, response processing, etc.).
|
2022-01-24 10:33:43 +00:00
|
|
|
//
|
|
|
|
// Return statuses:
|
|
|
|
// - global (see Client docs).
|
2022-03-03 11:04:53 +00:00
|
|
|
func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetworkInfo, error) {
|
2021-12-02 12:21:54 +00:00
|
|
|
// check context
|
|
|
|
if ctx == nil {
|
|
|
|
panic(panicMsgMissingContext)
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// form request
|
|
|
|
var req v2netmap.NetworkInfoRequest
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// init call context
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
var (
|
2021-12-02 12:21:54 +00:00
|
|
|
cc contextCall
|
2022-02-17 16:10:49 +00:00
|
|
|
res ResNetworkInfo
|
2021-11-16 18:17:25 +00:00
|
|
|
)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
c.initCallContext(&cc)
|
2022-03-03 11:04:53 +00:00
|
|
|
cc.meta = prm.prmCommonMeta
|
2021-12-02 12:21:54 +00:00
|
|
|
cc.req = &req
|
|
|
|
cc.statusRes = &res
|
|
|
|
cc.call = func() (responseV2, error) {
|
2022-03-07 11:39:49 +00:00
|
|
|
return rpcapi.NetworkInfo(&c.c, &req, client.WithContext(ctx))
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
cc.result = func(r responseV2) {
|
|
|
|
resp := r.(*v2netmap.NetworkInfoResponse)
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
netInfoV2 := resp.GetBody().GetNetworkInfo()
|
|
|
|
if netInfoV2 == nil {
|
|
|
|
cc.err = errors.New("missing network info in response body")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var netInfo netmap.NetworkInfo
|
|
|
|
|
|
|
|
cc.err = netInfo.ReadFromV2(*netInfoV2)
|
|
|
|
if cc.err != nil {
|
|
|
|
cc.err = fmt.Errorf("invalid network info: %w", cc.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setInfo(&netInfo)
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// process call
|
|
|
|
if !cc.processCall() {
|
|
|
|
return nil, cc.err
|
|
|
|
}
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
return &res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|