2020-07-24 13:54:03 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
containercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// Put marshals container, and passes it to Wrapper's Put method
|
|
|
|
// along with sig.Key() and sig.Sign().
|
|
|
|
//
|
|
|
|
// Returns error if container is nil.
|
2022-06-22 10:55:31 +00:00
|
|
|
func Put(c *Client, cnr containercore.Container) (*cid.ID, error) {
|
2022-06-28 07:01:05 +00:00
|
|
|
data := cnr.Value.Marshal()
|
2022-01-31 13:34:01 +00:00
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
d := container.ReadDomain(cnr.Value)
|
2021-05-25 09:25:55 +00:00
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
var prm PutPrm
|
|
|
|
prm.SetContainer(data)
|
2022-06-28 07:01:05 +00:00
|
|
|
prm.SetName(d.Name())
|
|
|
|
prm.SetZone(d.Zone())
|
2022-05-16 13:15:31 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
if cnr.Session != nil {
|
|
|
|
prm.SetToken(cnr.Session.Marshal())
|
2022-05-18 15:20:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
// TODO(@cthulhu-rider): #1387 implement and use another approach to avoid conversion
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
cnr.Signature.WriteToV2(&sigV2)
|
2022-05-16 13:15:31 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
prm.SetKey(sigV2.GetKey())
|
|
|
|
prm.SetSignature(sigV2.GetSign())
|
2022-05-16 13:15:31 +00:00
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
err := c.Put(prm)
|
2022-01-31 13:34:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-12 14:46:45 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var id cid.ID
|
2022-06-28 07:01:05 +00:00
|
|
|
container.CalculateIDFromBinary(&id, data)
|
2022-01-31 13:34:01 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
return &id, nil
|
2022-01-31 13:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutPrm groups parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
|
|
|
cnr []byte
|
|
|
|
key []byte
|
|
|
|
sig []byte
|
|
|
|
token []byte
|
|
|
|
name string
|
|
|
|
zone string
|
2021-11-12 15:14:55 +00:00
|
|
|
|
|
|
|
client.InvokePrmOptional
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetContainer sets container data.
|
|
|
|
func (p *PutPrm) SetContainer(cnr []byte) {
|
|
|
|
p.cnr = cnr
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetKey sets public key.
|
|
|
|
func (p *PutPrm) SetKey(key []byte) {
|
|
|
|
p.key = key
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetSignature sets signature.
|
|
|
|
func (p *PutPrm) SetSignature(sig []byte) {
|
|
|
|
p.sig = sig
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetToken sets session token.
|
|
|
|
func (p *PutPrm) SetToken(token []byte) {
|
|
|
|
p.token = token
|
2021-05-25 15:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetName sets native name.
|
|
|
|
func (p *PutPrm) SetName(name string) {
|
|
|
|
p.name = name
|
2021-10-12 14:46:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetZone sets zone.
|
|
|
|
func (p *PutPrm) SetZone(zone string) {
|
|
|
|
p.zone = zone
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves binary container with its session token, key and signature
|
2023-02-05 15:59:38 +00:00
|
|
|
// in FrostFS system through Container contract call.
|
2022-01-31 13:34:01 +00:00
|
|
|
//
|
|
|
|
// Returns calculated container identifier and any error
|
|
|
|
// encountered that caused the saving to interrupt.
|
|
|
|
//
|
|
|
|
// If TryNotary is provided, calls notary contract.
|
|
|
|
func (c *Client) Put(p PutPrm) error {
|
|
|
|
if len(p.sig) == 0 || len(p.key) == 0 {
|
|
|
|
return errNilArgument
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:46:45 +00:00
|
|
|
var (
|
|
|
|
method string
|
2021-11-09 20:52:29 +00:00
|
|
|
prm client.InvokePrm
|
2021-05-18 08:12:51 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
if p.name != "" {
|
|
|
|
method = putNamedMethod
|
|
|
|
prm.SetArgs(p.cnr, p.sig, p.key, p.token, p.name, p.zone)
|
2021-11-09 20:52:29 +00:00
|
|
|
} else {
|
2022-01-29 13:06:36 +00:00
|
|
|
method = putMethod
|
2022-01-31 13:34:01 +00:00
|
|
|
prm.SetArgs(p.cnr, p.sig, p.key, p.token)
|
2021-10-12 14:46:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 20:52:29 +00:00
|
|
|
prm.SetMethod(method)
|
2022-01-31 13:34:01 +00:00
|
|
|
prm.InvokePrmOptional = p.InvokePrmOptional
|
2021-11-09 20:52:29 +00:00
|
|
|
|
|
|
|
err := c.client.Invoke(prm)
|
2021-05-18 08:12:51 +00:00
|
|
|
if err != nil {
|
2021-10-12 14:46:45 +00:00
|
|
|
return fmt.Errorf("could not invoke method (%s): %w", method, err)
|
2021-05-18 08:12:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|