2020-08-25 11:52:18 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl"
|
2020-09-15 08:03:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-08-31 08:15:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-08-25 11:52:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
NewOption interface {
|
|
|
|
apply(*containerOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
attribute struct {
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
containerOptions struct {
|
|
|
|
acl uint32
|
2020-09-15 08:03:15 +00:00
|
|
|
policy *netmap.PlacementPolicy
|
2020-08-25 11:52:18 +00:00
|
|
|
attributes []attribute
|
2020-08-31 08:15:46 +00:00
|
|
|
owner *owner.ID
|
2020-08-25 11:52:18 +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{
|
|
|
|
acl: acl.PrivateBasicRule,
|
|
|
|
nonce: rand,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type funcContainerOption struct {
|
|
|
|
f func(*containerOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fco *funcContainerOption) apply(co *containerOptions) {
|
|
|
|
fco.f(co)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFuncContainerOption(f func(option *containerOptions)) *funcContainerOption {
|
|
|
|
return &funcContainerOption{
|
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithPublicBasicACL() NewOption {
|
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
|
|
|
option.acl = acl.PublicBasicRule
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithReadOnlyBasicACL() NewOption {
|
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
|
|
|
option.acl = acl.ReadOnlyBasicRule
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCustomBasicACL(acl uint32) NewOption {
|
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
|
|
|
option.acl = acl
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithNonce(nonce uuid.UUID) NewOption {
|
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
|
|
|
option.nonce = nonce
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
func WithOwnerID(id *owner.ID) NewOption {
|
2020-08-25 11:52:18 +00:00
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
2020-08-31 08:15:46 +00:00
|
|
|
option.owner = id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithNEO3Wallet(w *owner.NEO3Wallet) NewOption {
|
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
|
|
|
if option.owner == nil {
|
|
|
|
option.owner = new(owner.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
option.owner.SetNeo3Wallet(w)
|
2020-08-25 11:52:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-15 08:03:15 +00:00
|
|
|
func WithPolicy(policy *netmap.PlacementPolicy) NewOption {
|
2020-08-25 11:52:18 +00:00
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
|
|
|
option.policy = policy
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAttribute(key, value string) NewOption {
|
|
|
|
return newFuncContainerOption(func(option *containerOptions) {
|
|
|
|
attr := attribute{
|
|
|
|
key: key,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
option.attributes = append(option.attributes, attr)
|
|
|
|
})
|
|
|
|
}
|