forked from TrueCloudLab/frostfs-api-go
[#126] sdk: implement container rpc for in client
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
25e6b45b57
commit
aa539cd0c4
4 changed files with 470 additions and 0 deletions
45
pkg/container/container.go
Normal file
45
pkg/container/container.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/container"
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
container.Container
|
||||
}
|
||||
|
||||
func New(opts ...NewOption) (*Container, error) {
|
||||
cnrOptions := defaultContainerOptions()
|
||||
for i := range opts {
|
||||
opts[i].apply(&cnrOptions)
|
||||
}
|
||||
|
||||
cnr := new(Container)
|
||||
cnr.SetNonce(cnrOptions.nonce[:])
|
||||
cnr.SetBasicACL(cnrOptions.acl)
|
||||
|
||||
if cnrOptions.policy != "" {
|
||||
// todo: set placement policy
|
||||
}
|
||||
|
||||
if cnrOptions.owner != nil {
|
||||
owner := new(refs.OwnerID)
|
||||
owner.SetValue(cnrOptions.owner[:])
|
||||
|
||||
cnr.SetOwnerID(owner)
|
||||
}
|
||||
|
||||
attributes := make([]*container.Attribute, len(cnrOptions.attributes))
|
||||
for i := range cnrOptions.attributes {
|
||||
attribute := new(container.Attribute)
|
||||
attribute.SetKey(cnrOptions.attributes[i].key)
|
||||
attribute.SetValue(cnrOptions.attributes[i].value)
|
||||
attributes[i] = attribute
|
||||
}
|
||||
if len(attributes) > 0 {
|
||||
cnr.SetAttributes(attributes)
|
||||
}
|
||||
|
||||
return cnr, nil
|
||||
}
|
99
pkg/container/opts.go
Normal file
99
pkg/container/opts.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/acl"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/refs"
|
||||
)
|
||||
|
||||
type (
|
||||
NewOption interface {
|
||||
apply(*containerOptions)
|
||||
}
|
||||
|
||||
attribute struct {
|
||||
key string
|
||||
value string
|
||||
}
|
||||
|
||||
containerOptions struct {
|
||||
acl uint32
|
||||
policy string
|
||||
attributes []attribute
|
||||
owner *refs.NEO3Wallet
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
func WithOwner(owner refs.NEO3Wallet) NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
option.owner = &owner
|
||||
})
|
||||
}
|
||||
|
||||
func WithPolicy(policy string) NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
// todo: make sanity check and store binary structure
|
||||
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)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue