From 8529aa2a931e005f980f0d2632f8255213f5b4f8 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 17 Feb 2021 21:03:48 +0300 Subject: [PATCH] [#259] pkg/client: Add NetworkInfo method Implement NetworkInfo method that performs NeoFS API NetmapService NetworkInfo RPC. Signed-off-by: Leonard Lyubich --- pkg/client/netmap.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/pkg/client/netmap.go b/pkg/client/netmap.go index 749b640b..f45eb786 100644 --- a/pkg/client/netmap.go +++ b/pkg/client/netmap.go @@ -114,3 +114,60 @@ func v2NetmapClientFromOptions(opts *clientOptions) (cli *v2netmap.Client, err e return cli, nil } + +// NetworkInfo returns information about the NeoFS network of which the remote server is a part. +func (c Client) NetworkInfo(ctx context.Context, opts ...CallOption) (*netmap.NetworkInfo, error) { + switch c.remoteNode.Version.Major() { + case 2: + resp, err := c.networkInfoV2(ctx, opts...) + if err != nil { + return nil, err + } + + return netmap.NewNetworkInfoFromV2(resp.GetBody().GetNetworkInfo()), nil + default: + return nil, errUnsupportedProtocol + } +} + +func (c Client) networkInfoV2(ctx context.Context, opts ...CallOption) (*v2netmap.NetworkInfoResponse, error) { + // apply all available options + callOptions := c.defaultCallOptions() + + for i := range opts { + opts[i].apply(&callOptions) + } + + reqBody := new(v2netmap.NetworkInfoRequestBody) + + req := new(v2netmap.NetworkInfoRequest) + req.SetBody(reqBody) + req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions)) + + err := v2signature.SignServiceMessage(c.key, req) + if err != nil { + return nil, err + } + + switch c.remoteNode.Protocol { + case GRPC: + cli, err := v2NetmapClientFromOptions(c.opts) + if err != nil { + return nil, errors.Wrap(err, "could not create grpc client") + } + + resp, err := cli.NetworkInfo(ctx, req) + if err != nil { + return nil, errors.Wrap(err, "v2 NetworkInfo RPC failure") + } + + err = v2signature.VerifyServiceMessage(resp) + if err != nil { + return nil, errors.Wrap(err, "response message verification failed") + } + + return resp, nil + default: + return nil, errUnsupportedProtocol + } +}