2021-11-08 12:25:13 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2022-01-13 13:16:35 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
|
2021-11-08 12:25:13 +00:00
|
|
|
"github.com/google/uuid"
|
2022-06-20 15:28:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container/acl"
|
2021-11-08 12:25:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-11-08 12:25:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-11-08 12:25:13 +00:00
|
|
|
Option func(*containerOptions)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
|
|
|
containerOptions struct {
|
2022-06-20 15:28:27 +00:00
|
|
|
acl acl.Basic
|
2021-11-08 12:25:13 +00:00
|
|
|
policy *netmap.PlacementPolicy
|
|
|
|
attributes Attributes
|
2022-04-11 06:30:22 +00:00
|
|
|
owner *user.ID
|
2021-11-08 12:25:13 +00:00
|
|
|
nonce uuid.UUID
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func defaultContainerOptions() containerOptions {
|
|
|
|
rand, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
panic("can't create new random " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return containerOptions{
|
2022-06-20 15:28:27 +00:00
|
|
|
acl: acl.Private,
|
2021-11-08 12:25:13 +00:00
|
|
|
nonce: rand,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:25:13 +00:00
|
|
|
func WithPublicBasicACL() Option {
|
|
|
|
return func(option *containerOptions) {
|
2022-06-20 15:28:27 +00:00
|
|
|
option.acl = acl.PublicRW
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:25:13 +00:00
|
|
|
func WithReadOnlyBasicACL() Option {
|
|
|
|
return func(option *containerOptions) {
|
2022-06-20 15:28:27 +00:00
|
|
|
option.acl = acl.PublicRO
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 15:28:27 +00:00
|
|
|
func WithCustomBasicACL(acl acl.Basic) Option {
|
2021-11-08 12:25:13 +00:00
|
|
|
return func(option *containerOptions) {
|
2021-11-08 12:25:13 +00:00
|
|
|
option.acl = acl
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:25:13 +00:00
|
|
|
func WithNonce(nonce uuid.UUID) Option {
|
|
|
|
return func(option *containerOptions) {
|
2021-11-08 12:25:13 +00:00
|
|
|
option.nonce = nonce
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
func WithOwnerID(id *user.ID) Option {
|
2021-11-08 12:25:13 +00:00
|
|
|
return func(option *containerOptions) {
|
2021-11-08 12:25:13 +00:00
|
|
|
option.owner = id
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 13:16:35 +00:00
|
|
|
func WithOwnerPublicKey(pub *ecdsa.PublicKey) Option {
|
2021-11-08 12:25:13 +00:00
|
|
|
return func(option *containerOptions) {
|
2021-11-08 12:25:13 +00:00
|
|
|
if option.owner == nil {
|
2022-04-11 06:30:22 +00:00
|
|
|
option.owner = new(user.ID)
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
user.IDFromKey(option.owner, *pub)
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:25:13 +00:00
|
|
|
func WithPolicy(policy *netmap.PlacementPolicy) Option {
|
|
|
|
return func(option *containerOptions) {
|
2021-11-08 12:25:13 +00:00
|
|
|
option.policy = policy
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:25:13 +00:00
|
|
|
func WithAttribute(key, value string) Option {
|
|
|
|
return func(option *containerOptions) {
|
2022-03-11 09:13:56 +00:00
|
|
|
index := len(option.attributes)
|
|
|
|
option.attributes = append(option.attributes, Attribute{})
|
|
|
|
option.attributes[index].SetKey(key)
|
|
|
|
option.attributes[index].SetValue(value)
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|