2020-08-25 11:52:18 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-09-17 10:08:04 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
|
2020-12-24 09:26:41 +00:00
|
|
|
"github.com/google/uuid"
|
2020-11-11 13:49:54 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-05-24 17:34:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2020-08-25 11:52:18 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Container struct {
|
2020-11-11 13:49:54 +00:00
|
|
|
v2 container.Container
|
2021-05-24 16:48:41 +00:00
|
|
|
|
2021-05-24 17:34:56 +00:00
|
|
|
token *session.Token
|
2021-05-24 16:48:41 +00:00
|
|
|
|
|
|
|
sig *pkg.Signature
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 08:04:36 +00:00
|
|
|
func New(opts ...NewOption) *Container {
|
2020-08-25 11:52:18 +00:00
|
|
|
cnrOptions := defaultContainerOptions()
|
2020-11-16 14:39:06 +00:00
|
|
|
|
2020-08-25 11:52:18 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&cnrOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
cnr := new(Container)
|
2020-12-24 09:26:41 +00:00
|
|
|
cnr.SetNonceUUID(cnrOptions.nonce)
|
2020-08-25 11:52:18 +00:00
|
|
|
cnr.SetBasicACL(cnrOptions.acl)
|
|
|
|
|
|
|
|
if cnrOptions.owner != nil {
|
2020-11-11 13:49:54 +00:00
|
|
|
cnr.SetOwnerID(cnrOptions.owner)
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 08:04:36 +00:00
|
|
|
if cnrOptions.policy != nil {
|
2020-11-11 13:49:54 +00:00
|
|
|
cnr.SetPlacementPolicy(cnrOptions.policy)
|
2020-09-15 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 13:49:54 +00:00
|
|
|
cnr.SetAttributes(cnrOptions.attributes)
|
2020-08-25 11:52:18 +00:00
|
|
|
|
2020-09-15 08:04:36 +00:00
|
|
|
return cnr
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:39:06 +00:00
|
|
|
func (c *Container) ToV2() *container.Container {
|
2020-11-11 13:49:54 +00:00
|
|
|
return &c.v2
|
2020-09-15 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 09:28:33 +00:00
|
|
|
// NewVerifiedFromV2 constructs Container from NeoFS API V2 Container message.
|
|
|
|
//
|
|
|
|
// Does not perform if message meets NeoFS API V2 specification. To do this
|
|
|
|
// use NewVerifiedFromV2 constructor.
|
2020-09-15 08:04:36 +00:00
|
|
|
func NewContainerFromV2(c *container.Container) *Container {
|
|
|
|
cnr := new(Container)
|
|
|
|
|
|
|
|
if c != nil {
|
2020-11-11 13:49:54 +00:00
|
|
|
cnr.v2 = *c
|
2020-09-15 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cnr
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
2020-09-17 10:08:04 +00:00
|
|
|
|
|
|
|
// CalculateID calculates container identifier
|
|
|
|
// based on its structure.
|
|
|
|
func CalculateID(c *Container) *ID {
|
|
|
|
data, err := c.ToV2().StableMarshal(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id := NewID()
|
|
|
|
id.SetSHA256(sha256.Sum256(data))
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
2020-11-11 13:49:54 +00:00
|
|
|
|
|
|
|
func (c *Container) Version() *pkg.Version {
|
|
|
|
return pkg.NewVersionFromV2(c.v2.GetVersion())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) SetVersion(v *pkg.Version) {
|
|
|
|
c.v2.SetVersion(v.ToV2())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) OwnerID() *owner.ID {
|
|
|
|
return owner.NewIDFromV2(c.v2.GetOwnerID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) SetOwnerID(v *owner.ID) {
|
|
|
|
c.v2.SetOwnerID(v.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-12-24 09:26:41 +00:00
|
|
|
// Nonce returns container nonce in a binary format.
|
|
|
|
//
|
|
|
|
// Returns nil if container nonce is not a valid UUID.
|
|
|
|
//
|
|
|
|
// Deprecated: use NonceUUID instead.
|
2020-11-11 13:49:54 +00:00
|
|
|
func (c *Container) Nonce() []byte {
|
2020-12-24 09:26:41 +00:00
|
|
|
uid, err := c.NonceUUID()
|
|
|
|
if err == nil {
|
|
|
|
data, _ := uid.MarshalBinary()
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-11-11 13:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 09:26:41 +00:00
|
|
|
// SetNonce sets container nonce in a binary format.
|
|
|
|
//
|
|
|
|
// If slice length is less than UUID size, than
|
|
|
|
// value is padded with a sequence of zeros.
|
|
|
|
// If slice length is more than UUID size, than
|
|
|
|
// value is cut.
|
|
|
|
//
|
|
|
|
// Deprecated: use SetNonceUUID instead.
|
2020-11-11 13:49:54 +00:00
|
|
|
func (c *Container) SetNonce(v []byte) {
|
2020-12-24 09:26:41 +00:00
|
|
|
u := uuid.UUID{}
|
|
|
|
copy(u[:], v)
|
|
|
|
c.v2.SetNonce(u[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns container nonce in UUID format.
|
|
|
|
//
|
|
|
|
// Returns error if container nonce is not a valid UUID.
|
|
|
|
func (c *Container) NonceUUID() (uuid.UUID, error) {
|
|
|
|
return uuid.FromBytes(c.v2.GetNonce())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNonceUUID sets container nonce as UUID.
|
|
|
|
func (c *Container) SetNonceUUID(v uuid.UUID) {
|
|
|
|
data, _ := v.MarshalBinary()
|
|
|
|
c.v2.SetNonce(data)
|
2020-11-11 13:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) BasicACL() uint32 {
|
|
|
|
return c.v2.GetBasicACL()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) SetBasicACL(v uint32) {
|
|
|
|
c.v2.SetBasicACL(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Attributes() Attributes {
|
|
|
|
return NewAttributesFromV2(c.v2.GetAttributes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) SetAttributes(v Attributes) {
|
|
|
|
c.v2.SetAttributes(v.ToV2())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) PlacementPolicy() *netmap.PlacementPolicy {
|
|
|
|
return netmap.NewPlacementPolicyFromV2(c.v2.GetPlacementPolicy())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) SetPlacementPolicy(v *netmap.PlacementPolicy) {
|
|
|
|
c.v2.SetPlacementPolicy(v.ToV2())
|
|
|
|
}
|
2020-11-13 12:21:34 +00:00
|
|
|
|
2021-05-24 16:48:41 +00:00
|
|
|
// SessionToken returns token of the session within
|
|
|
|
// which container was created.
|
2021-05-24 17:34:56 +00:00
|
|
|
func (c Container) SessionToken() *session.Token {
|
2021-05-24 16:48:41 +00:00
|
|
|
return c.token
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionToken sets token of the session within
|
|
|
|
// which container was created.
|
2021-05-24 17:34:56 +00:00
|
|
|
func (c *Container) SetSessionToken(t *session.Token) {
|
2021-05-24 16:48:41 +00:00
|
|
|
c.token = t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signature returns signature of the marshaled container.
|
|
|
|
func (c Container) Signature() *pkg.Signature {
|
|
|
|
return c.sig
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSignature sets signature of the marshaled container.
|
|
|
|
func (c *Container) SetSignature(sig *pkg.Signature) {
|
|
|
|
c.sig = sig
|
|
|
|
}
|
|
|
|
|
2020-11-13 12:21:34 +00:00
|
|
|
// Marshal marshals Container into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (c *Container) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.v2.
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Container.
|
|
|
|
func (c *Container) Unmarshal(data []byte) error {
|
|
|
|
return c.v2.
|
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Container to protobuf JSON format.
|
|
|
|
func (c *Container) MarshalJSON() ([]byte, error) {
|
|
|
|
return c.v2.
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Container from protobuf JSON format.
|
|
|
|
func (c *Container) UnmarshalJSON(data []byte) error {
|
|
|
|
return c.v2.
|
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|