[#121] client: Make PrmObjectDelete fields public

* Introduce buildRequest for PrmObjectDelete
* Refactor the usage of these params in pool

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2023-09-04 13:23:10 +03:00 committed by Evgenii Stratonikov
parent b5fe52d6bd
commit 5a471e5002
2 changed files with 72 additions and 85 deletions

View file

@ -21,71 +21,25 @@ import (
// PrmObjectDelete groups parameters of ObjectDelete operation. // PrmObjectDelete groups parameters of ObjectDelete operation.
type PrmObjectDelete struct { type PrmObjectDelete struct {
meta v2session.RequestMetaHeader XHeaders []string
body v2object.DeleteRequestBody BearerToken *bearer.Token
addr v2refs.Address Session *session.Object
keySet bool ContainerID *cid.ID
key ecdsa.PrivateKey
}
// WithinSession specifies session within which object should be read. ObjectID *oid.ID
//
// Creator of the session acquires the authorship of the request.
// This may affect the execution of an operation (e.g. access control).
//
// Must be signed.
func (x *PrmObjectDelete) WithinSession(t session.Object) {
var tv2 v2session.Token
t.WriteToV2(&tv2)
x.meta.SetSessionToken(&tv2) Key *ecdsa.PrivateKey
}
// WithBearerToken attaches bearer token to be used for the operation.
//
// If set, underlying eACL rules will be used in access control.
//
// Must be signed.
func (x *PrmObjectDelete) WithBearerToken(t bearer.Token) {
var v2token acl.BearerToken
t.WriteToV2(&v2token)
x.meta.SetBearerToken(&v2token)
}
// FromContainer specifies FrostFS container of the object.
// Required parameter.
func (x *PrmObjectDelete) FromContainer(id cid.ID) {
var cidV2 v2refs.ContainerID
id.WriteToV2(&cidV2)
x.addr.SetContainerID(&cidV2)
}
// ByID specifies identifier of the requested object.
// Required parameter.
func (x *PrmObjectDelete) ByID(id oid.ID) {
var idV2 v2refs.ObjectID
id.WriteToV2(&idV2)
x.addr.SetObjectID(&idV2)
} }
// UseKey specifies private key to sign the requests. // UseKey specifies private key to sign the requests.
// If key is not provided, then Client default key is used. // If key is not provided, then Client default key is used.
func (x *PrmObjectDelete) UseKey(key ecdsa.PrivateKey) {
x.keySet = true
x.key = key
}
// WithXHeaders specifies list of extended headers (string key-value pairs)
// to be attached to the request. Must have an even length.
// //
// Slice must not be mutated until the operation completes. // Deprecated: Use PrmObjectDelete.Key instead.
func (x *PrmObjectDelete) WithXHeaders(hs ...string) { func (prm *PrmObjectDelete) UseKey(key ecdsa.PrivateKey) {
writeXHeadersToMeta(hs, &x.meta) prm.Key = &key
} }
// ResObjectDelete groups resulting values of ObjectDelete operation. // ResObjectDelete groups resulting values of ObjectDelete operation.
@ -100,6 +54,54 @@ func (x ResObjectDelete) Tombstone() oid.ID {
return x.tomb return x.tomb
} }
func (prm *PrmObjectDelete) buildRequest(c *Client) (*v2object.DeleteRequest, error) {
if prm.ContainerID == nil {
return nil, errorMissingContainer
}
if prm.ObjectID == nil {
return nil, errorMissingObject
}
if len(prm.XHeaders)%2 != 0 {
return nil, errorInvalidXHeaders
}
meta := new(v2session.RequestMetaHeader)
writeXHeadersToMeta(prm.XHeaders, meta)
if prm.BearerToken != nil {
v2BearerToken := new(acl.BearerToken)
prm.BearerToken.WriteToV2(v2BearerToken)
meta.SetBearerToken(v2BearerToken)
}
if prm.Session != nil {
v2SessionToken := new(v2session.Token)
prm.Session.WriteToV2(v2SessionToken)
meta.SetSessionToken(v2SessionToken)
}
addr := new(v2refs.Address)
cnrV2 := new(v2refs.ContainerID)
prm.ContainerID.WriteToV2(cnrV2)
addr.SetContainerID(cnrV2)
objV2 := new(v2refs.ObjectID)
prm.ObjectID.WriteToV2(objV2)
addr.SetObjectID(objV2)
body := new(v2object.DeleteRequestBody)
body.SetAddress(addr)
req := new(v2object.DeleteRequest)
req.SetBody(body)
c.prepareRequest(req, meta)
return req, nil
}
// ObjectDelete marks an object for deletion from the container using FrostFS API protocol. // ObjectDelete marks an object for deletion from the container using FrostFS API protocol.
// As a marker, a special unit called a tombstone is placed in the container. // As a marker, a special unit called a tombstone is placed in the container.
// It confirms the user's intent to delete the object, and is itself a container object. // It confirms the user's intent to delete the object, and is itself a container object.
@ -124,32 +126,22 @@ func (x ResObjectDelete) Tombstone() oid.ID {
// - *apistatus.ObjectLocked; // - *apistatus.ObjectLocked;
// - *apistatus.SessionTokenExpired. // - *apistatus.SessionTokenExpired.
func (c *Client) ObjectDelete(ctx context.Context, prm PrmObjectDelete) (*ResObjectDelete, error) { func (c *Client) ObjectDelete(ctx context.Context, prm PrmObjectDelete) (*ResObjectDelete, error) {
switch { req, err := prm.buildRequest(c)
case prm.addr.GetContainerID() == nil: if err != nil {
return nil, errorMissingContainer return nil, err
case prm.addr.GetObjectID() == nil:
return nil, errorMissingObject
} }
// form request body
prm.body.SetAddress(&prm.addr)
// form request
var req v2object.DeleteRequest
req.SetBody(&prm.body)
c.prepareRequest(&req, &prm.meta)
key := c.prm.key key := c.prm.key
if prm.keySet { if prm.Key != nil {
key = prm.key key = *prm.Key
} }
err := signature.SignServiceMessage(&key, &req) err = signature.SignServiceMessage(&key, req)
if err != nil { if err != nil {
return nil, fmt.Errorf("sign request: %w", err) return nil, fmt.Errorf("sign request: %w", err)
} }
resp, err := rpcapi.DeleteObject(&c.c, &req, client.WithContext(ctx)) resp, err := rpcapi.DeleteObject(&c.c, req, client.WithContext(ctx))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -799,20 +799,15 @@ func (c *clientWrapper) objectDelete(ctx context.Context, prm PrmObjectDelete) e
return err return err
} }
var cliPrm sdkClient.PrmObjectDelete cnr := prm.addr.Container()
cliPrm.FromContainer(prm.addr.Container()) obj := prm.addr.Object()
cliPrm.ByID(prm.addr.Object())
if prm.stoken != nil { cliPrm := sdkClient.PrmObjectDelete{
cliPrm.WithinSession(*prm.stoken) BearerToken: prm.btoken,
} Session: prm.stoken,
ContainerID: &cnr,
if prm.btoken != nil { ObjectID: &obj,
cliPrm.WithBearerToken(*prm.btoken) Key: prm.key,
}
if prm.key != nil {
cliPrm.UseKey(*prm.key)
} }
start := time.Now() start := time.Now()