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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -19,6 +19,16 @@ type PrmEndpointInfo struct {
prmCommonMeta 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. // ResEndpointInfo group resulting values of EndpointInfo operation.
type ResEndpointInfo struct { type ResEndpointInfo struct {
statusRes statusRes
@ -56,25 +66,25 @@ func (x ResEndpointInfo) NodeInfo() netmap.NodeInfo {
// Return statuses: // Return statuses:
// - global (see Client docs). // - global (see Client docs).
func (c *Client) EndpointInfo(ctx context.Context, prm PrmEndpointInfo) (*ResEndpointInfo, error) { func (c *Client) EndpointInfo(ctx context.Context, prm PrmEndpointInfo) (*ResEndpointInfo, error) {
// form request req, err := prm.buildRequest(c)
var req v2netmap.LocalNodeInfoRequest if err != nil {
return nil, err
// init call context }
var ( if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
cc contextCall return nil, fmt.Errorf("sign request: %w", err)
res ResEndpointInfo }
)
resp, err := rpcapi.LocalNodeInfo(&c.c, req, client.WithContext(ctx))
c.initCallContext(&cc) if err != nil {
cc.meta = prm.prmCommonMeta return nil, err
cc.req = &req }
cc.statusRes = &res
cc.call = func() (responseV2, error) { var res ResEndpointInfo
return rpcapi.LocalNodeInfo(&c.c, &req, client.WithContext(ctx)) res.st, err = c.processResponse(resp)
if err != nil || !apistatus.IsSuccessful(res.st) {
return &res, err
} }
cc.result = func(r responseV2) {
resp := r.(*v2netmap.LocalNodeInfoResponse)
body := resp.GetBody() body := resp.GetBody()
@ -82,36 +92,21 @@ func (c *Client) EndpointInfo(ctx context.Context, prm PrmEndpointInfo) (*ResEnd
verV2 := body.GetVersion() verV2 := body.GetVersion()
if verV2 == nil { if verV2 == nil {
cc.err = newErrMissingResponseField(fieldVersion) 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.
return
} }
if err := res.version.ReadFromV2(*verV2); err != nil {
cc.err = res.version.ReadFromV2(*verV2) return nil, newErrInvalidResponseField(fieldVersion, err)
if cc.err != nil {
cc.err = newErrInvalidResponseField(fieldVersion, cc.err)
return
} }
const fieldNodeInfo = "node info" const fieldNodeInfo = "node info"
nodeInfoV2 := body.GetNodeInfo() nodeInfoV2 := body.GetNodeInfo()
if nodeInfoV2 == nil { if nodeInfoV2 == nil {
cc.err = newErrMissingResponseField(fieldNodeInfo) return nil, newErrMissingResponseField(fieldNodeInfo)
return
} }
if err := res.ni.ReadFromV2(*nodeInfoV2); err != nil {
cc.err = res.ni.ReadFromV2(*nodeInfoV2) return nil, newErrInvalidResponseField(fieldNodeInfo, err)
if cc.err != nil {
cc.err = newErrInvalidResponseField(fieldNodeInfo, cc.err)
return
} }
}
// process call
if !cc.processCall() {
return nil, cc.err
}
return &res, nil return &res, nil
} }
@ -120,6 +115,16 @@ type PrmNetworkInfo struct {
prmCommonMeta 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. // ResNetworkInfo groups resulting values of NetworkInfo operation.
type ResNetworkInfo struct { type ResNetworkInfo struct {
statusRes statusRes
@ -148,46 +153,35 @@ func (x ResNetworkInfo) Info() netmap.NetworkInfo {
// Return statuses: // Return statuses:
// - global (see Client docs). // - global (see Client docs).
func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetworkInfo, error) { func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetworkInfo, error) {
// form request req, err := prm.buildRequest(c)
var req v2netmap.NetworkInfoRequest if err != nil {
return nil, err
// init call context }
var ( if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
cc contextCall return nil, fmt.Errorf("sign request: %w", err)
res ResNetworkInfo }
)
resp, err := rpcapi.NetworkInfo(&c.c, req, client.WithContext(ctx))
c.initCallContext(&cc) if err != nil {
cc.meta = prm.prmCommonMeta return nil, err
cc.req = &req }
cc.statusRes = &res
cc.call = func() (responseV2, error) { var res ResNetworkInfo
return rpcapi.NetworkInfo(&c.c, &req, client.WithContext(ctx)) res.st, err = c.processResponse(resp)
if err != nil || !apistatus.IsSuccessful(res.st) {
return &res, err
} }
cc.result = func(r responseV2) {
resp := r.(*v2netmap.NetworkInfoResponse)
const fieldNetInfo = "network info" const fieldNetInfo = "network info"
netInfoV2 := resp.GetBody().GetNetworkInfo() netInfoV2 := resp.GetBody().GetNetworkInfo()
if netInfoV2 == nil { if netInfoV2 == nil {
cc.err = newErrMissingResponseField(fieldNetInfo) return nil, newErrMissingResponseField(fieldNetInfo)
return
} }
if err := res.info.ReadFromV2(*netInfoV2); err != nil {
cc.err = res.info.ReadFromV2(*netInfoV2) return nil, newErrInvalidResponseField(fieldNetInfo, err)
if cc.err != nil {
cc.err = newErrInvalidResponseField(fieldNetInfo, cc.err)
return
} }
}
// process call
if !cc.processCall() {
return nil, cc.err
}
return &res, nil return &res, nil
} }

View file

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