From 3c7c3630024803b8fe34b53697c82aa1f8536afc Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 15 Sep 2020 11:04:36 +0300 Subject: [PATCH] [#142] sdk/container: Add converters to and from v2 Signed-off-by: Alex Vanin --- pkg/client/container.go | 6 ++---- pkg/container/container.go | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pkg/client/container.go b/pkg/client/container.go index 2888ebc9..98a6f9c2 100644 --- a/pkg/client/container.go +++ b/pkg/client/container.go @@ -100,7 +100,7 @@ func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, op } reqBody := new(v2container.PutRequestBody) - reqBody.SetContainer(&cnr.Container) + reqBody.SetContainer(cnr.ToV2()) // sign container signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetContainer()} @@ -182,9 +182,7 @@ func (c Client) getContainerV2(ctx context.Context, id *container.ID, opts ...Ca return nil, errors.Wrap(err, "can't verify response message") } - return &container.Container{ - Container: *resp.GetBody().GetContainer(), - }, nil + return container.NewContainerFromV2(resp.GetBody().GetContainer()), nil default: return nil, unsupportedProtocolErr } diff --git a/pkg/container/container.go b/pkg/container/container.go index 5626af64..07765ca8 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -8,7 +8,7 @@ type Container struct { container.Container } -func New(opts ...NewOption) (*Container, error) { +func New(opts ...NewOption) *Container { cnrOptions := defaultContainerOptions() for i := range opts { opts[i].apply(&cnrOptions) @@ -18,14 +18,14 @@ func New(opts ...NewOption) (*Container, error) { cnr.SetNonce(cnrOptions.nonce[:]) cnr.SetBasicACL(cnrOptions.acl) - if cnrOptions.policy != "" { - // todo: set placement policy - } - if cnrOptions.owner != nil { cnr.SetOwnerID(cnrOptions.owner.ToV2()) } + if cnrOptions.policy != nil { + cnr.SetPlacementPolicy(cnrOptions.policy) + } + attributes := make([]*container.Attribute, len(cnrOptions.attributes)) for i := range cnrOptions.attributes { attribute := new(container.Attribute) @@ -37,5 +37,19 @@ func New(opts ...NewOption) (*Container, error) { cnr.SetAttributes(attributes) } - return cnr, nil + return cnr +} + +func (c Container) ToV2() *container.Container { + return &c.Container +} + +func NewContainerFromV2(c *container.Container) *Container { + cnr := new(Container) + + if c != nil { + cnr.Container = *c + } + + return cnr }