2020-08-25 11:52:18 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-09-17 10:08:04 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
|
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"
|
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
|
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()
|
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&cnrOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
cnr := new(Container)
|
|
|
|
cnr.SetNonce(cnrOptions.nonce[:])
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Nonce() []byte {
|
|
|
|
return c.v2.GetNonce() // return uuid?
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) SetNonce(v []byte) {
|
|
|
|
c.v2.SetNonce(v) // set uuid?
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|