[#564] Upgrade NeoFS SDK Go with changed container API

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-06-29 17:43:52 +03:00 committed by Kira
parent d2a3ba0c06
commit 4a8a248f34
6 changed files with 57 additions and 67 deletions

View file

@ -20,6 +20,7 @@ import (
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
"github.com/nspcc-dev/neofs-sdk-go/user" "github.com/nspcc-dev/neofs-sdk-go/user"
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -73,13 +74,15 @@ func prepareHandlerContext(t *testing.T) *handlerContext {
func createTestBucket(ctx context.Context, t *testing.T, h *handlerContext, bktName string) { func createTestBucket(ctx context.Context, t *testing.T, h *handlerContext, bktName string) {
_, err := h.MockedPool().CreateContainer(ctx, layer.PrmContainerCreate{ _, err := h.MockedPool().CreateContainer(ctx, layer.PrmContainerCreate{
Name: bktName, Creator: *usertest.ID(),
Name: bktName,
}) })
require.NoError(t, err) require.NoError(t, err)
} }
func createTestBucketWithLock(ctx context.Context, t *testing.T, h *handlerContext, bktName string, conf *data.ObjectLockConfiguration) *data.BucketInfo { func createTestBucketWithLock(ctx context.Context, t *testing.T, h *handlerContext, bktName string, conf *data.ObjectLockConfiguration) *data.BucketInfo {
cnrID, err := h.MockedPool().CreateContainer(ctx, layer.PrmContainerCreate{ cnrID, err := h.MockedPool().CreateContainer(ctx, layer.PrmContainerCreate{
Creator: *usertest.ID(),
Name: bktName, Name: bktName,
AdditionalAttributes: [][2]string{{layer.AttributeLockEnabled, "true"}}, AdditionalAttributes: [][2]string{{layer.AttributeLockEnabled, "true"}},
}) })

View file

@ -52,31 +52,21 @@ func (n *layer) containerInfo(ctx context.Context, idCnr cid.ID) (*data.BucketIn
return nil, fmt.Errorf("get neofs container: %w", err) return nil, fmt.Errorf("get neofs container: %w", err)
} }
info.Owner = *res.OwnerID() cnr := *res
for _, attr := range res.Attributes() { info.Owner = cnr.Owner()
switch key, val := attr.Key(), attr.Value(); key { info.Name = container.Name(cnr)
case container.AttributeName: info.Created = container.CreatedAt(cnr)
info.Name = val info.LocationConstraint = cnr.Attribute(attributeLocationConstraint)
case container.AttributeTimestamp:
unix, err := strconv.ParseInt(attr.Value(), 10, 64)
if err != nil {
log.Error("could not parse container creation time",
zap.String("created_at", val), zap.Error(err))
continue attrLockEnabled := cnr.Attribute(AttributeLockEnabled)
}
info.Created = time.Unix(unix, 0) info.ObjectLockEnabled, err = strconv.ParseBool(attrLockEnabled)
case attributeLocationConstraint: if err != nil {
info.LocationConstraint = val log.Error("could not parse container object lock enabled attribute",
case AttributeLockEnabled: zap.String("lock_enabled", attrLockEnabled),
info.ObjectLockEnabled, err = strconv.ParseBool(val) zap.Error(err),
if err != nil { )
log.Error("could not parse container object lock enabled attribute",
zap.String("lock_enabled", val), zap.Error(err))
}
}
} }
if err = n.bucketCache.Put(info); err != nil { if err = n.bucketCache.Put(info); err != nil {

View file

@ -7,7 +7,6 @@ import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"io" "io"
"strconv"
"strings" "strings"
"time" "time"
@ -55,36 +54,35 @@ func (t *TestNeoFS) AddObject(key string, obj *object.Object) {
func (t *TestNeoFS) ContainerID(name string) (*cid.ID, error) { func (t *TestNeoFS) ContainerID(name string) (*cid.ID, error) {
for id, cnr := range t.containers { for id, cnr := range t.containers {
for _, attr := range cnr.Attributes() { if container.Name(*cnr) == name {
if attr.Key() == container.AttributeName && attr.Value() == name { var cnrID cid.ID
var cnrID cid.ID return &cnrID, cnrID.DecodeString(id)
return &cnrID, cnrID.DecodeString(id)
}
} }
} }
return nil, fmt.Errorf("not found") return nil, fmt.Errorf("not found")
} }
func (t *TestNeoFS) CreateContainer(_ context.Context, prm PrmContainerCreate) (*cid.ID, error) { func (t *TestNeoFS) CreateContainer(_ context.Context, prm PrmContainerCreate) (*cid.ID, error) {
opts := []container.Option{ var cnr container.Container
container.WithOwnerID(&prm.Creator), cnr.Init()
container.WithPolicy(&prm.Policy), cnr.SetOwner(prm.Creator)
container.WithCustomBasicACL(prm.BasicACL), cnr.SetPlacementPolicy(prm.Policy)
container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(time.Now().Unix(), 10)), cnr.SetBasicACL(prm.BasicACL)
container.SetCreationTime(&cnr, time.Now())
if prm.Name != "" {
container.SetName(&cnr, prm.Name)
}
for i := range prm.AdditionalAttributes {
cnr.SetAttribute(prm.AdditionalAttributes[i][0], prm.AdditionalAttributes[i][1])
} }
if prm.Name != "" { if prm.Name != "" {
opts = append(opts, container.WithAttribute(container.AttributeName, prm.Name)) var d container.Domain
} d.SetName(prm.Name)
for _, attr := range prm.AdditionalAttributes { container.WriteDomain(&cnr, d)
opts = append(opts, container.WithAttribute(attr[0], attr[1]))
}
cnr := container.New(opts...)
if prm.Name != "" {
container.SetNativeName(cnr, prm.Name)
} }
b := make([]byte, 32) b := make([]byte, 32)
@ -94,7 +92,7 @@ func (t *TestNeoFS) CreateContainer(_ context.Context, prm PrmContainerCreate) (
var id cid.ID var id cid.ID
id.SetSHA256(sha256.Sum256(b)) id.SetSHA256(sha256.Sum256(b))
t.containers[id.EncodeToString()] = cnr t.containers[id.EncodeToString()] = &cnr
return &id, nil return &id, nil
} }

4
go.mod
View file

@ -9,8 +9,8 @@ require (
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/nats-io/nats.go v1.13.1-0.20220121202836-972a071d373d github.com/nats-io/nats.go v1.13.1-0.20220121202836-972a071d373d
github.com/nspcc-dev/neo-go v0.98.2 github.com/nspcc-dev/neo-go v0.98.2
github.com/nspcc-dev/neofs-api-go/v2 v2.12.3-0.20220621170933-dd233c3fbc84 github.com/nspcc-dev/neofs-api-go/v2 v2.12.3-0.20220630100506-c6f7ab3ef1bf
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220628114722-ab4d1e34a8ac github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220704082116-2ad89085a341
github.com/prometheus/client_golang v1.11.0 github.com/prometheus/client_golang v1.11.0
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1

8
go.sum
View file

@ -296,8 +296,8 @@ github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220321113211-526c423a6152 h1:JK
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220321113211-526c423a6152/go.mod h1:QBE0I30F2kOAISNpT5oks82yF4wkkUq3SCfI3Hqgx/Y= github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220321113211-526c423a6152/go.mod h1:QBE0I30F2kOAISNpT5oks82yF4wkkUq3SCfI3Hqgx/Y=
github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211201134523-3604d96f3fe1/go.mod h1:oS8dycEh8PPf2Jjp6+8dlwWyEv2Dy77h/XhhcdxYEFs= github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211201134523-3604d96f3fe1/go.mod h1:oS8dycEh8PPf2Jjp6+8dlwWyEv2Dy77h/XhhcdxYEFs=
github.com/nspcc-dev/neofs-api-go/v2 v2.11.1/go.mod h1:oS8dycEh8PPf2Jjp6+8dlwWyEv2Dy77h/XhhcdxYEFs= github.com/nspcc-dev/neofs-api-go/v2 v2.11.1/go.mod h1:oS8dycEh8PPf2Jjp6+8dlwWyEv2Dy77h/XhhcdxYEFs=
github.com/nspcc-dev/neofs-api-go/v2 v2.12.3-0.20220621170933-dd233c3fbc84 h1:4tZSQ2DL/oatbte35+vDSt2nYpQ0G2et1DrpxodGwRM= github.com/nspcc-dev/neofs-api-go/v2 v2.12.3-0.20220630100506-c6f7ab3ef1bf h1:QRPx+DdyN2KmJ5/oDYH4c86Bl81d1ZacQL5Q9IC9wZA=
github.com/nspcc-dev/neofs-api-go/v2 v2.12.3-0.20220621170933-dd233c3fbc84/go.mod h1:73j09Xa7I2zQbM3HCvAHnDHPYiiWnEHa1d6Z6RDMBLU= github.com/nspcc-dev/neofs-api-go/v2 v2.12.3-0.20220630100506-c6f7ab3ef1bf/go.mod h1:73j09Xa7I2zQbM3HCvAHnDHPYiiWnEHa1d6Z6RDMBLU=
github.com/nspcc-dev/neofs-contract v0.15.1 h1:1r27t4SGKF7W1PRPOIfircEXHvALThNYNagT+SIabcA= github.com/nspcc-dev/neofs-contract v0.15.1 h1:1r27t4SGKF7W1PRPOIfircEXHvALThNYNagT+SIabcA=
github.com/nspcc-dev/neofs-contract v0.15.1/go.mod h1:kxO5ZTqdzFnRM5RMvM+Fhd+3GGrJo6AmG2ZyA9OCqqQ= github.com/nspcc-dev/neofs-contract v0.15.1/go.mod h1:kxO5ZTqdzFnRM5RMvM+Fhd+3GGrJo6AmG2ZyA9OCqqQ=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
@ -306,8 +306,8 @@ github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnB
github.com/nspcc-dev/neofs-crypto v0.3.0/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.3.0/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211201182451-a5b61c4f6477/go.mod h1:dfMtQWmBHYpl9Dez23TGtIUKiFvCIxUZq/CkSIhEpz4= github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211201182451-a5b61c4f6477/go.mod h1:dfMtQWmBHYpl9Dez23TGtIUKiFvCIxUZq/CkSIhEpz4=
github.com/nspcc-dev/neofs-sdk-go v0.0.0-20220113123743-7f3162110659/go.mod h1:/jay1lr3w7NQd/VDBkEhkJmDmyPNsu4W+QV2obsUV40= github.com/nspcc-dev/neofs-sdk-go v0.0.0-20220113123743-7f3162110659/go.mod h1:/jay1lr3w7NQd/VDBkEhkJmDmyPNsu4W+QV2obsUV40=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220628114722-ab4d1e34a8ac h1:FRxNic8MeVF7bNWE31H5MLh7EoD/6uNpskwJxfEuiM4= github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220704082116-2ad89085a341 h1:poXrrTjCClTMUpEZ7xlQ3Pk4+vtLH+EqaTduBmn+Z9Y=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220628114722-ab4d1e34a8ac/go.mod h1:EpzpilARa1/7Pgtn8qB/iXXyvC1AIzhlm8mbU+S52MU= github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220704082116-2ad89085a341/go.mod h1:ck/wjPGFZ7mqcz6vZLMuQFEfL1Qu5zVrBrBJrH0VjOo=
github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso= github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=
github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE= github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE=
github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso= github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=

View file

@ -112,20 +112,19 @@ func (x *NeoFS) CreateContainer(ctx context.Context, prm layer.PrmContainerCreat
prm.BasicACL = acl.PublicRWExtended prm.BasicACL = acl.PublicRWExtended
} }
// fill container structure var cnr container.Container
cnrOptions := []container.Option{ cnr.Init()
container.WithPolicy(&prm.Policy), cnr.SetPlacementPolicy(prm.Policy)
container.WithOwnerID(&prm.Creator), cnr.SetOwner(prm.Creator)
container.WithCustomBasicACL(prm.BasicACL), cnr.SetBasicACL(prm.BasicACL)
container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(time.Now().Unix(), 10)), container.SetCreationTime(&cnr, time.Now())
}
if prm.Name != "" { if prm.Name != "" {
cnrOptions = append(cnrOptions, container.WithAttribute(container.AttributeName, prm.Name)) container.SetName(&cnr, prm.Name)
} }
for _, attr := range prm.AdditionalAttributes { for i := range prm.AdditionalAttributes {
cnrOptions = append(cnrOptions, container.WithAttribute(attr[0], attr[1])) cnr.SetAttribute(prm.AdditionalAttributes[i][0], prm.AdditionalAttributes[i][1])
} }
// https://github.com/nspcc-dev/neofs-s3-gw/issues/435 // https://github.com/nspcc-dev/neofs-s3-gw/issues/435
@ -134,18 +133,18 @@ func (x *NeoFS) CreateContainer(ctx context.Context, prm layer.PrmContainerCreat
if hhDisabled, err := isHomomorphicHashDisabled(ctx, x.pool); err != nil { if hhDisabled, err := isHomomorphicHashDisabled(ctx, x.pool); err != nil {
return nil, fmt.Errorf("check homomorphic hash enabled: %w", err) return nil, fmt.Errorf("check homomorphic hash enabled: %w", err)
} else if hhDisabled { } else if hhDisabled {
cnrOptions = append(cnrOptions, container.WithAttribute( container.DisableHomomorphicHashing(&cnr)
"__NEOFS__DISABLE_HOMOMORPHIC_HASHING", "true"))
} }
cnr := container.New(cnrOptions...)
if prm.Name != "" { if prm.Name != "" {
container.SetNativeName(cnr, prm.Name) var d container.Domain
d.SetName(prm.Name)
container.WriteDomain(&cnr, d)
} }
var prmPut pool.PrmContainerPut var prmPut pool.PrmContainerPut
prmPut.SetContainer(*cnr) prmPut.SetContainer(cnr)
prmPut.SetWaitParams(x.await) prmPut.SetWaitParams(x.await)
if prm.SessionToken != nil { if prm.SessionToken != nil {