2022-02-17 18:58:51 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
2022-06-22 19:40:52 +00:00
|
|
|
"fmt"
|
2022-02-17 18:58:51 +00:00
|
|
|
|
2022-03-31 06:10:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2022-02-17 18:58:51 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
type PutBucketNotificationConfigurationParams struct {
|
|
|
|
RequestInfo *api.ReqInfo
|
|
|
|
BktInfo *data.BucketInfo
|
|
|
|
Configuration *data.NotificationConfiguration
|
2022-03-31 06:10:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 18:58:51 +00:00
|
|
|
func (n *layer) PutBucketNotificationConfiguration(ctx context.Context, p *PutBucketNotificationConfigurationParams) error {
|
2022-04-29 13:08:22 +00:00
|
|
|
confXML, err := xml.Marshal(p.Configuration)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("marshal notify configuration: %w", err)
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := &PutSystemObjectParams{
|
|
|
|
BktInfo: p.BktInfo,
|
|
|
|
ObjName: p.BktInfo.NotificationConfigurationObjectName(),
|
|
|
|
Metadata: map[string]string{},
|
2022-04-29 13:08:22 +00:00
|
|
|
Reader: bytes.NewReader(confXML),
|
2022-05-25 15:59:36 +00:00
|
|
|
Size: int64(len(confXML)),
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 15:59:36 +00:00
|
|
|
_, err = n.putSystemObjectIntoNeoFS(ctx, s)
|
2022-02-17 18:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
if err = n.systemCache.PutNotificationConfiguration(systemObjectKey(p.BktInfo, s.ObjName), p.Configuration); err != nil {
|
2022-02-17 18:58:51 +00:00
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) GetBucketNotificationConfiguration(ctx context.Context, bktInfo *data.BucketInfo) (*data.NotificationConfiguration, error) {
|
|
|
|
conf, err := n.getNotificationConf(ctx, bktInfo, bktInfo.NotificationConfigurationObjectName())
|
|
|
|
if err != nil {
|
|
|
|
if errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
return &data.NotificationConfiguration{}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) getNotificationConf(ctx context.Context, bkt *data.BucketInfo, sysName string) (*data.NotificationConfiguration, error) {
|
|
|
|
if conf := n.systemCache.GetNotificationConfiguration(systemObjectKey(bkt, sysName)); conf != nil {
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := n.getSystemObjectFromNeoFS(ctx, bkt, sysName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := &data.NotificationConfiguration{}
|
|
|
|
|
|
|
|
if err = xml.Unmarshal(obj.Payload(), &conf); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal notify configuration: %w", err)
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = n.systemCache.PutNotificationConfiguration(systemObjectKey(bkt, sysName), conf); err != nil {
|
2022-04-25 09:57:58 +00:00
|
|
|
objID, _ := obj.ID()
|
2022-02-17 18:58:51 +00:00
|
|
|
n.log.Warn("couldn't put system meta to objects cache",
|
2022-04-25 09:57:58 +00:00
|
|
|
zap.Stringer("object id", &objID),
|
2022-02-17 18:58:51 +00:00
|
|
|
zap.Stringer("bucket id", bkt.CID),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|