2022-02-15 05:36:47 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-25 10:51:46 +00:00
|
|
|
"fmt"
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2022-03-24 08:05:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/acl"
|
2022-02-15 05:36:47 +00:00
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
|
|
|
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-03-24 08:05:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2022-02-15 05:36:47 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2023-04-25 08:31:27 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
2022-02-15 05:36:47 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PrmObjectDelete groups parameters of ObjectDelete operation.
|
|
|
|
type PrmObjectDelete struct {
|
|
|
|
meta v2session.RequestMetaHeader
|
|
|
|
|
|
|
|
body v2object.DeleteRequestBody
|
|
|
|
|
|
|
|
addr v2refs.Address
|
2022-02-21 12:02:23 +00:00
|
|
|
|
|
|
|
keySet bool
|
2023-04-25 08:31:27 +00:00
|
|
|
signer neofscrypto.Signer
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithinSession specifies session within which object should be read.
|
|
|
|
//
|
|
|
|
// 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.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (x *PrmObjectDelete) WithinSession(t session.Object) {
|
|
|
|
var tv2 v2session.Token
|
|
|
|
t.WriteToV2(&tv2)
|
|
|
|
|
|
|
|
x.meta.SetSessionToken(&tv2)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithBearerToken attaches bearer token to be used for the operation.
|
|
|
|
//
|
|
|
|
// If set, underlying eACL rules will be used in access control.
|
|
|
|
//
|
|
|
|
// Must be signed.
|
2022-03-24 08:05:41 +00:00
|
|
|
func (x *PrmObjectDelete) WithBearerToken(t bearer.Token) {
|
|
|
|
var v2token acl.BearerToken
|
|
|
|
t.WriteToV2(&v2token)
|
|
|
|
x.meta.SetBearerToken(&v2token)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromContainer specifies NeoFS container of the object.
|
2023-05-04 07:09:43 +00:00
|
|
|
// Required parameter. It is an alternative to [PrmObjectDelete.ByAddress].
|
2022-02-15 05:36:47 +00:00
|
|
|
func (x *PrmObjectDelete) FromContainer(id cid.ID) {
|
2022-04-11 16:25:14 +00:00
|
|
|
var cidV2 v2refs.ContainerID
|
|
|
|
id.WriteToV2(&cidV2)
|
|
|
|
|
|
|
|
x.addr.SetContainerID(&cidV2)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ByID specifies identifier of the requested object.
|
2023-05-04 07:09:43 +00:00
|
|
|
// Required parameter. It is an alternative to [PrmObjectDelete.ByAddress].
|
2022-02-15 05:36:47 +00:00
|
|
|
func (x *PrmObjectDelete) ByID(id oid.ID) {
|
2022-04-11 16:25:14 +00:00
|
|
|
var idV2 v2refs.ObjectID
|
|
|
|
id.WriteToV2(&idV2)
|
|
|
|
|
|
|
|
x.addr.SetObjectID(&idV2)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 07:09:43 +00:00
|
|
|
// ByAddress specifies address of the requested object.
|
|
|
|
// Required parameter. It is an alternative to [PrmObjectDelete.ByID], [PrmObjectDelete.FromContainer].
|
|
|
|
func (x *PrmObjectDelete) ByAddress(addr oid.Address) {
|
|
|
|
addr.WriteToV2(&x.addr)
|
|
|
|
}
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
// UseSigner specifies private signer to sign the requests.
|
|
|
|
// If signer is not provided, then Client default signer is used.
|
|
|
|
func (x *PrmObjectDelete) UseSigner(signer neofscrypto.Signer) {
|
2022-02-21 12:02:23 +00:00
|
|
|
x.keySet = true
|
2023-04-25 08:31:27 +00:00
|
|
|
x.signer = signer
|
2022-02-21 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:04:53 +00:00
|
|
|
// 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.
|
|
|
|
func (x *PrmObjectDelete) WithXHeaders(hs ...string) {
|
2022-08-23 17:55:00 +00:00
|
|
|
writeXHeadersToMeta(hs, &x.meta)
|
2022-03-03 11:04:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
// ResObjectDelete groups resulting values of ObjectDelete operation.
|
|
|
|
type ResObjectDelete struct {
|
2022-08-05 07:56:49 +00:00
|
|
|
tomb oid.ID
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 07:56:49 +00:00
|
|
|
// Tombstone returns identifier of the created tombstone object.
|
|
|
|
func (x ResObjectDelete) Tombstone() oid.ID {
|
|
|
|
return x.tomb
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectDelete marks an object for deletion from the container using NeoFS API protocol.
|
|
|
|
// 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.
|
|
|
|
// Explicit deletion is done asynchronously, and is generally not guaranteed.
|
|
|
|
//
|
|
|
|
// Returns a list of checksums in raw form: the format of hashes and their number
|
|
|
|
// is left for the caller to check. Client preserves the order of the server's response.
|
|
|
|
//
|
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
|
|
|
// Any client's internal or transport errors are returned as `error`,
|
2023-05-16 08:36:28 +00:00
|
|
|
// see [apistatus] package for NeoFS-specific error types.
|
2022-02-15 05:36:47 +00:00
|
|
|
//
|
2022-02-17 15:32:53 +00:00
|
|
|
// Immediately panics if parameters are set incorrectly (see PrmObjectDelete docs).
|
2022-02-15 05:36:47 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
2023-05-16 08:36:28 +00:00
|
|
|
// Return errors:
|
2022-02-28 10:56:46 +00:00
|
|
|
// - global (see Client docs)
|
2023-05-16 08:36:28 +00:00
|
|
|
// - [apistatus.ErrContainerNotFound];
|
|
|
|
// - [apistatus.ErrObjectAccessDenied];
|
|
|
|
// - [apistatus.ErrObjectLocked];
|
|
|
|
// - [apistatus.ErrSessionTokenExpired].
|
2022-02-15 05:36:47 +00:00
|
|
|
func (c *Client) ObjectDelete(ctx context.Context, prm PrmObjectDelete) (*ResObjectDelete, error) {
|
|
|
|
switch {
|
|
|
|
case prm.addr.GetContainerID() == nil:
|
|
|
|
panic(panicMsgMissingContainer)
|
|
|
|
case prm.addr.GetObjectID() == nil:
|
2022-08-23 17:15:55 +00:00
|
|
|
panic(panicMsgMissingObject)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// form request body
|
|
|
|
prm.body.SetAddress(&prm.addr)
|
|
|
|
|
|
|
|
// form request
|
|
|
|
var req v2object.DeleteRequest
|
|
|
|
req.SetBody(&prm.body)
|
2022-08-25 10:51:46 +00:00
|
|
|
c.prepareRequest(&req, &prm.meta)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
signer := prm.signer
|
|
|
|
if signer == nil {
|
|
|
|
signer = c.prm.signer
|
2022-08-25 10:51:46 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
err := signServiceMessage(signer, &req)
|
2022-08-25 10:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := rpcapi.DeleteObject(&c.c, &req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-21 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 10:51:46 +00:00
|
|
|
var res ResObjectDelete
|
2023-05-17 12:32:42 +00:00
|
|
|
_, err = c.processResponse(resp)
|
2022-08-25 10:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
2022-08-25 10:51:46 +00:00
|
|
|
|
|
|
|
const fieldTombstone = "tombstone"
|
|
|
|
|
|
|
|
idTombV2 := resp.GetBody().GetTombstone().GetObjectID()
|
|
|
|
if idTombV2 == nil {
|
|
|
|
return nil, newErrMissingResponseField(fieldTombstone)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 10:51:46 +00:00
|
|
|
err = res.tomb.ReadFromV2(*idTombV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newErrInvalidResponseField(fieldTombstone, err)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|