client: Simplify netmap/session methods #55

Merged
fyrchik merged 4 commits from fyrchik/frostfs-sdk-go:netmap-refactor into master 2023-04-18 09:58:35 +00:00
9 changed files with 140 additions and 156 deletions

View file

@ -34,7 +34,7 @@ func (x *PrmContainerDelete) SetContainer(id cid.ID) {
x.idSet = true
}
func (x *PrmContainerDelete) formRequest(c *Client) (*v2container.DeleteRequest, error) {
func (x *PrmContainerDelete) buildRequest(c *Client) (*v2container.DeleteRequest, error) {
if !x.idSet {
return nil, errorMissingContainer
}
@ -114,7 +114,7 @@ type ResContainerDelete struct {
// Return statuses:
// - global (see Client docs).
func (c *Client) ContainerDelete(ctx context.Context, prm PrmContainerDelete) (*ResContainerDelete, error) {
req, err := prm.formRequest(c)
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}

View file

@ -30,7 +30,7 @@ func (x *PrmContainerEACL) SetContainer(id cid.ID) {
x.idSet = true
}
func (x *PrmContainerEACL) formRequest(c *Client) (*v2container.GetExtendedACLRequest, error) {
func (x *PrmContainerEACL) buildRequest(c *Client) (*v2container.GetExtendedACLRequest, error) {
if !x.idSet {
return nil, errorMissingContainer
}
@ -75,7 +75,7 @@ func (x ResContainerEACL) Table() eacl.Table {
// - *apistatus.ContainerNotFound;
// - *apistatus.EACLNotFound.
func (c *Client) ContainerEACL(ctx context.Context, prm PrmContainerEACL) (*ResContainerEACL, error) {
req, err := prm.formRequest(c)
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}

View file

@ -31,7 +31,7 @@ func (x *PrmContainerGet) SetContainer(id cid.ID) {
x.idSet = true
}
func (x *PrmContainerGet) formRequest(c *Client) (*v2container.GetRequest, error) {
func (x *PrmContainerGet) buildRequest(c *Client) (*v2container.GetRequest, error) {
if !x.idSet {
return nil, errorMissingContainer
}
@ -77,7 +77,7 @@ func (x ResContainerGet) Container() container.Container {
// - global (see Client docs);
// - *apistatus.ContainerNotFound.
func (c *Client) ContainerGet(ctx context.Context, prm PrmContainerGet) (*ResContainerGet, error) {
req, err := prm.formRequest(c)
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}

View file

@ -30,7 +30,7 @@ func (x *PrmContainerList) SetAccount(id user.ID) {
x.ownerSet = true
}
func (x *PrmContainerList) formRequest(c *Client) (*v2container.ListRequest, error) {
func (x *PrmContainerList) buildRequest(c *Client) (*v2container.ListRequest, error) {
if !x.ownerSet {
return nil, errorAccountNotSet
}
@ -75,7 +75,7 @@ func (x ResContainerList) Containers() []cid.ID {
// Return statuses:
// - global (see Client docs).
func (c *Client) ContainerList(ctx context.Context, prm PrmContainerList) (*ResContainerList, error) {
req, err := prm.formRequest(c)
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}

View file

@ -48,7 +48,7 @@ func (x *PrmContainerPut) WithinSession(s session.Container) {
x.sessionSet = true
}
func (x *PrmContainerPut) formRequest(c *Client) (*v2container.PutRequest, error) {
func (x *PrmContainerPut) buildRequest(c *Client) (*v2container.PutRequest, error) {
if !x.cnrSet {
return nil, errorMissingContainer
}
@ -122,7 +122,7 @@ func (x ResContainerPut) ID() cid.ID {
//
// nolint: funlen
func (c *Client) ContainerPut(ctx context.Context, prm PrmContainerPut) (*ResContainerPut, error) {
req, err := prm.formRequest(c)
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}

View file

@ -50,7 +50,7 @@ func (x *PrmContainerSetEACL) WithinSession(s session.Container) {
x.sessionSet = true
}
func (x *PrmContainerSetEACL) formRequest(c *Client) (*v2container.SetExtendedACLRequest, error) {
func (x *PrmContainerSetEACL) buildRequest(c *Client) (*v2container.SetExtendedACLRequest, error) {
if !x.tableSet {
return nil, errorEACLTableNotSet
}
@ -111,7 +111,7 @@ type ResContainerSetEACL struct {
// Return statuses:
// - global (see Client docs).
func (c *Client) ContainerSetEACL(ctx context.Context, prm PrmContainerSetEACL) (*ResContainerSetEACL, error) {
req, err := prm.formRequest(c)
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}

View file

@ -27,7 +27,7 @@ func (x *PrmAnnounceSpace) SetValues(vs []container.SizeEstimation) {
x.announcements = vs
}
func (x *PrmAnnounceSpace) formRequest(c *Client) (*v2container.AnnounceUsedSpaceRequest, error) {
func (x *PrmAnnounceSpace) buildRequest(c *Client) (*v2container.AnnounceUsedSpaceRequest, error) {
if len(x.announcements) == 0 {
return nil, errorMissingAnnouncements
}
@ -70,7 +70,7 @@ type ResAnnounceSpace struct {
// Return statuses:
// - global (see Client docs).
func (c *Client) ContainerAnnounceUsedSpace(ctx context.Context, prm PrmAnnounceSpace) (*ResAnnounceSpace, error) {
req, err := prm.formRequest(c)
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}

View file

@ -19,6 +19,16 @@ type PrmEndpointInfo struct {
prmCommonMeta
}
func (x *PrmEndpointInfo) buildRequest(c *Client) (*v2netmap.LocalNodeInfoRequest, error) {

can we call it buildRequest, asRequest or simply request instead? Maybe I'm biased, but this sounds more like "requesting a form" than "forming a request" :)

can we call it `buildRequest`, `asRequest` or simply `request` instead? Maybe I'm biased, but this sounds more like "requesting a form" than "forming a request" :)

or toRequest

or `toRequest`

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed
meta := new(v2session.RequestMetaHeader)
writeXHeadersToMeta(x.xHeaders, meta)
req := new(v2netmap.LocalNodeInfoRequest)
req.SetBody(new(v2netmap.LocalNodeInfoRequestBody))
c.prepareRequest(req, meta)
return req, nil
}
// ResEndpointInfo group resulting values of EndpointInfo operation.
type ResEndpointInfo struct {
statusRes
@ -56,62 +66,47 @@ func (x ResEndpointInfo) NodeInfo() netmap.NodeInfo {
// Return statuses:
// - global (see Client docs).
func (c *Client) EndpointInfo(ctx context.Context, prm PrmEndpointInfo) (*ResEndpointInfo, error) {
// form request
var req v2netmap.LocalNodeInfoRequest
// init call context
var (
cc contextCall
res ResEndpointInfo
)
c.initCallContext(&cc)
cc.meta = prm.prmCommonMeta
cc.req = &req
cc.statusRes = &res
cc.call = func() (responseV2, error) {
return rpcapi.LocalNodeInfo(&c.c, &req, client.WithContext(ctx))
}
cc.result = func(r responseV2) {
resp := r.(*v2netmap.LocalNodeInfoResponse)
body := resp.GetBody()
const fieldVersion = "version"
verV2 := body.GetVersion()
if verV2 == nil {
cc.err = newErrMissingResponseField(fieldVersion)
return
}
cc.err = res.version.ReadFromV2(*verV2)
if cc.err != nil {
cc.err = newErrInvalidResponseField(fieldVersion, cc.err)
return
}
const fieldNodeInfo = "node info"
nodeInfoV2 := body.GetNodeInfo()
if nodeInfoV2 == nil {
cc.err = newErrMissingResponseField(fieldNodeInfo)
return
}
cc.err = res.ni.ReadFromV2(*nodeInfoV2)
if cc.err != nil {
cc.err = newErrInvalidResponseField(fieldNodeInfo, cc.err)
return
}
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}
// process call
if !cc.processCall() {
return nil, cc.err
if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
return nil, fmt.Errorf("sign request: %w", err)
}
resp, err := rpcapi.LocalNodeInfo(&c.c, req, client.WithContext(ctx))
if err != nil {
return nil, err
}
var res ResEndpointInfo
res.st, err = c.processResponse(resp)
if err != nil || !apistatus.IsSuccessful(res.st) {
return &res, err
}
body := resp.GetBody()
const fieldVersion = "version"
verV2 := body.GetVersion()
if verV2 == nil {
return nil, newErrMissingResponseField(fieldVersion)

I think it's more natural to return nil instead &res in the error case

I think it's more natural to return `nil` instead `&res` in the error case

Fixed. I don't really have arguments, besides returning status if we already have it. You suggestion is backwards compatible, however.

Fixed. I don't really have arguments, besides returning status if we already have it. You suggestion is backwards compatible, however.
}
if err := res.version.ReadFromV2(*verV2); err != nil {
return nil, newErrInvalidResponseField(fieldVersion, err)
}
const fieldNodeInfo = "node info"
nodeInfoV2 := body.GetNodeInfo()
if nodeInfoV2 == nil {
return nil, newErrMissingResponseField(fieldNodeInfo)
}
if err := res.ni.ReadFromV2(*nodeInfoV2); err != nil {
return nil, newErrInvalidResponseField(fieldNodeInfo, err)
}
return &res, nil
}
@ -120,6 +115,16 @@ type PrmNetworkInfo struct {
prmCommonMeta
}
func (x PrmNetworkInfo) buildRequest(c *Client) (*v2netmap.NetworkInfoRequest, error) {
meta := new(v2session.RequestMetaHeader)
writeXHeadersToMeta(x.xHeaders, meta)
var req v2netmap.NetworkInfoRequest
req.SetBody(new(v2netmap.NetworkInfoRequestBody))
c.prepareRequest(&req, meta)
return &req, nil
}
// ResNetworkInfo groups resulting values of NetworkInfo operation.
type ResNetworkInfo struct {
statusRes
@ -148,46 +153,35 @@ func (x ResNetworkInfo) Info() netmap.NetworkInfo {
// Return statuses:
// - global (see Client docs).
func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetworkInfo, error) {
// form request
var req v2netmap.NetworkInfoRequest
// init call context
var (
cc contextCall
res ResNetworkInfo
)
c.initCallContext(&cc)
cc.meta = prm.prmCommonMeta
cc.req = &req
cc.statusRes = &res
cc.call = func() (responseV2, error) {
return rpcapi.NetworkInfo(&c.c, &req, client.WithContext(ctx))
}
cc.result = func(r responseV2) {
resp := r.(*v2netmap.NetworkInfoResponse)
const fieldNetInfo = "network info"
netInfoV2 := resp.GetBody().GetNetworkInfo()
if netInfoV2 == nil {
cc.err = newErrMissingResponseField(fieldNetInfo)
return
}
cc.err = res.info.ReadFromV2(*netInfoV2)
if cc.err != nil {
cc.err = newErrInvalidResponseField(fieldNetInfo, cc.err)
return
}
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}
// process call
if !cc.processCall() {
return nil, cc.err
if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
return nil, fmt.Errorf("sign request: %w", err)
}
resp, err := rpcapi.NetworkInfo(&c.c, req, client.WithContext(ctx))
if err != nil {
return nil, err
}
var res ResNetworkInfo
res.st, err = c.processResponse(resp)
if err != nil || !apistatus.IsSuccessful(res.st) {
return &res, err
}
const fieldNetInfo = "network info"
netInfoV2 := resp.GetBody().GetNetworkInfo()
if netInfoV2 == nil {
return nil, newErrMissingResponseField(fieldNetInfo)
}
if err := res.info.ReadFromV2(*netInfoV2); err != nil {
return nil, newErrInvalidResponseField(fieldNetInfo, err)
}
return &res, nil
}

View file

@ -3,11 +3,14 @@ package client
import (
"context"
"crypto/ecdsa"
"fmt"
"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"
v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
)
@ -33,6 +36,30 @@ func (x *PrmSessionCreate) UseKey(key ecdsa.PrivateKey) {
x.key = key
}
func (x *PrmSessionCreate) buildRequest(c *Client) (*v2session.CreateRequest, error) {
ownerKey := c.prm.key.PublicKey
if x.keySet {
ownerKey = x.key.PublicKey
}
var ownerID user.ID
user.IDFromKey(&ownerID, ownerKey)
var ownerIDV2 refs.OwnerID
ownerID.WriteToV2(&ownerIDV2)
reqBody := new(v2session.CreateRequestBody)
reqBody.SetOwnerID(&ownerIDV2)
reqBody.SetExpiration(x.exp)
var meta v2session.RequestMetaHeader
writeXHeadersToMeta(x.xHeaders, &meta)
var req v2session.CreateRequest
req.SetBody(reqBody)
c.prepareRequest(&req, &meta)
return &req, nil
}
// ResSessionCreate groups resulting values of SessionCreate operation.
type ResSessionCreate struct {
statusRes
@ -42,10 +69,6 @@ type ResSessionCreate struct {
sessionKey []byte
}
func (x *ResSessionCreate) setID(id []byte) {
x.id = id
}
// ID returns identifier of the opened session in a binary FrostFS API protocol format.
//
// Client doesn't retain value so modification is safe.
@ -53,10 +76,6 @@ func (x ResSessionCreate) ID() []byte {
return x.id
}
func (x *ResSessionCreate) setSessionKey(key []byte) {
x.sessionKey = key
}
// PublicKey returns public key of the opened session in a binary FrostFS API protocol format.
func (x ResSessionCreate) PublicKey() []byte {
return x.sessionKey
@ -78,57 +97,28 @@ func (x ResSessionCreate) PublicKey() []byte {
// Return statuses:
// - global (see Client docs).
func (c *Client) SessionCreate(ctx context.Context, prm PrmSessionCreate) (*ResSessionCreate, error) {
ownerKey := c.prm.key.PublicKey
if prm.keySet {
ownerKey = prm.key.PublicKey
}
var ownerID user.ID
user.IDFromKey(&ownerID, ownerKey)
var ownerIDV2 refs.OwnerID
ownerID.WriteToV2(&ownerIDV2)
// form request body
reqBody := new(v2session.CreateRequestBody)
reqBody.SetOwnerID(&ownerIDV2)
reqBody.SetExpiration(prm.exp)
// for request
var req v2session.CreateRequest
req.SetBody(reqBody)
// init call context
var (
cc contextCall
res ResSessionCreate
)
c.initCallContext(&cc)
if prm.keySet {
cc.key = prm.key
req, err := prm.buildRequest(c)
if err != nil {
return nil, err
}
cc.meta = prm.prmCommonMeta
cc.req = &req
cc.statusRes = &res
cc.call = func() (responseV2, error) {
return rpcapi.CreateSession(&c.c, &req, client.WithContext(ctx))
}
cc.result = func(r responseV2) {
resp := r.(*v2session.CreateResponse)
body := resp.GetBody()
res.setID(body.GetID())
res.setSessionKey(body.GetSessionKey())
if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
return nil, fmt.Errorf("sign request: %w", err)
}
// process call
if !cc.processCall() {
return nil, cc.err
resp, err := rpcapi.CreateSession(&c.c, req, client.WithContext(ctx))
if err != nil {
return nil, err
}
var res ResSessionCreate
res.st, err = c.processResponse(resp)
if err != nil || !apistatus.IsSuccessful(res.st) {
return &res, err
}
body := resp.GetBody()
res.id = body.GetID()
res.sessionKey = body.GetSessionKey()
return &res, nil
}