2020-10-08 12:06:36 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-17 12:56:17 +00:00
|
|
|
"fmt"
|
2020-10-08 12:06:36 +00:00
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-11-05 12:49:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2021-03-12 13:07:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/rpc/client"
|
2020-11-05 12:49:31 +00:00
|
|
|
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2021-03-12 13:07:52 +00:00
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2020-10-08 12:06:36 +00:00
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
)
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// Netmap contains methods related to netmap.
|
|
|
|
type Netmap interface {
|
|
|
|
// EndpointInfo returns attributes, address and public key of the node, specified
|
|
|
|
// in client constructor via address or open connection. This can be used as a
|
|
|
|
// health check to see if node is alive and responses to requests.
|
2021-03-12 13:07:52 +00:00
|
|
|
EndpointInfo(context.Context, ...CallOption) (*EndpointInfo, error)
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// NetworkInfo returns information about the NeoFS network of which the remote server is a part.
|
|
|
|
NetworkInfo(context.Context, ...CallOption) (*netmap.NetworkInfo, error)
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
// EACLWithSignature represents eACL table/signature pair.
|
|
|
|
type EndpointInfo struct {
|
|
|
|
version *pkg.Version
|
|
|
|
|
|
|
|
ni *netmap.NodeInfo
|
2020-10-08 12:06:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
// LatestVersion returns latest NeoFS API version in use.
|
|
|
|
func (e *EndpointInfo) LatestVersion() *pkg.Version {
|
|
|
|
return e.version
|
2020-11-03 15:59:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
// NodeInfo returns returns information about the NeoFS node.
|
|
|
|
func (e *EndpointInfo) NodeInfo() *netmap.NodeInfo {
|
|
|
|
return e.ni
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointInfo returns attributes, address and public key of the node, specified
|
|
|
|
// in client constructor via address or open connection. This can be used as a
|
|
|
|
// health check to see if node is alive and responses to requests.
|
|
|
|
func (c *clientImpl) EndpointInfo(ctx context.Context, opts ...CallOption) (*EndpointInfo, error) {
|
2020-10-08 12:06:36 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-10-08 12:06:36 +00:00
|
|
|
for i := range opts {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOptions)
|
2020-10-08 12:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
reqBody := new(v2netmap.LocalNodeInfoRequestBody)
|
2020-10-08 12:06:36 +00:00
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
req := new(v2netmap.LocalNodeInfoRequest)
|
2020-10-08 12:06:36 +00:00
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
2021-03-10 12:15:54 +00:00
|
|
|
err := v2signature.SignServiceMessage(callOptions.key, req)
|
2020-10-08 12:06:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
resp, err := rpcapi.LocalNodeInfo(c.Raw(), req)
|
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("transport error: %w", err)
|
2020-10-08 12:06:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
2020-10-08 12:06:36 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("can't verify response message: %w", err)
|
2020-10-08 12:06:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
body := resp.GetBody()
|
2020-10-08 12:06:36 +00:00
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
return &EndpointInfo{
|
|
|
|
version: pkg.NewVersionFromV2(body.GetVersion()),
|
|
|
|
ni: netmap.NewNodeInfoFromV2(body.GetNodeInfo()),
|
|
|
|
}, nil
|
2020-10-08 12:06:36 +00:00
|
|
|
}
|
2021-02-17 18:03:48 +00:00
|
|
|
|
|
|
|
// NetworkInfo returns information about the NeoFS network of which the remote server is a part.
|
2021-03-12 13:07:52 +00:00
|
|
|
func (c *clientImpl) NetworkInfo(ctx context.Context, opts ...CallOption) (*netmap.NetworkInfo, error) {
|
2021-02-17 18:03:48 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := c.defaultCallOptions()
|
|
|
|
|
|
|
|
for i := range opts {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOptions)
|
2021-02-17 18:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2netmap.NetworkInfoRequestBody)
|
|
|
|
|
|
|
|
req := new(v2netmap.NetworkInfoRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
2021-03-10 12:15:54 +00:00
|
|
|
err := v2signature.SignServiceMessage(callOptions.key, req)
|
2021-02-17 18:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
resp, err := rpcapi.NetworkInfo(c.Raw(), req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("v2 NetworkInfo RPC failure: %w", err)
|
2021-03-12 13:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("response message verification failed: %w", err)
|
2021-02-17 18:03:48 +00:00
|
|
|
}
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
return netmap.NewNetworkInfoFromV2(resp.GetBody().GetNetworkInfo()), nil
|
2021-02-17 18:03:48 +00:00
|
|
|
}
|