2022-02-17 18:58:51 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
2022-05-12 01:50:52 +00:00
|
|
|
errorsStd "errors"
|
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"
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
2022-05-12 01:50:52 +00:00
|
|
|
sysName := p.BktInfo.NotificationConfigurationObjectName()
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
prm := PrmObjectCreate{
|
|
|
|
Container: p.BktInfo.CID,
|
|
|
|
Creator: p.BktInfo.Owner,
|
|
|
|
Payload: bytes.NewReader(confXML),
|
|
|
|
Filename: sysName,
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
objID, _, err := n.objectPutAndHash(ctx, prm, p.BktInfo)
|
2022-02-17 18:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
objIDToDelete, err := n.treeService.PutNotificationConfigurationNode(ctx, &p.BktInfo.CID, objID)
|
2022-05-17 13:52:51 +00:00
|
|
|
if err != nil {
|
2022-05-12 01:50:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:52:51 +00:00
|
|
|
if objIDToDelete != nil {
|
|
|
|
if err = n.objectDelete(ctx, p.BktInfo, *objIDToDelete); err != nil {
|
2022-05-12 01:50:52 +00:00
|
|
|
n.log.Error("couldn't delete notification configuration object", zap.Error(err),
|
|
|
|
zap.String("cnrID", p.BktInfo.CID.EncodeToString()),
|
|
|
|
zap.String("bucket name", p.BktInfo.Name),
|
2022-05-17 13:52:51 +00:00
|
|
|
zap.String("objID", objIDToDelete.EncodeToString()))
|
2022-05-12 01:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = n.systemCache.PutNotificationConfiguration(systemObjectKey(p.BktInfo, sysName), 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) {
|
2022-05-12 01:50:52 +00:00
|
|
|
systemCacheKey := systemObjectKey(bktInfo, bktInfo.NotificationConfigurationObjectName())
|
2022-02-17 18:58:51 +00:00
|
|
|
|
2022-05-12 01:50:52 +00:00
|
|
|
if conf := n.systemCache.GetNotificationConfiguration(systemCacheKey); conf != nil {
|
2022-02-17 18:58:51 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:52:51 +00:00
|
|
|
objId, err := n.treeService.GetNotificationConfigurationNode(ctx, &bktInfo.CID)
|
2022-05-12 01:50:52 +00:00
|
|
|
if err != nil && !errorsStd.Is(err, ErrNodeNotFound) {
|
2022-02-17 18:58:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := &data.NotificationConfiguration{}
|
|
|
|
|
2022-05-17 13:52:51 +00:00
|
|
|
if objId != nil {
|
|
|
|
obj, err := n.objectGet(ctx, bktInfo, *objId)
|
2022-05-12 01:50:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = xml.Unmarshal(obj.Payload(), &conf); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal notify configuration: %w", err)
|
|
|
|
}
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 01:50:52 +00:00
|
|
|
if err = n.systemCache.PutNotificationConfiguration(systemCacheKey, conf); err != nil {
|
2022-02-17 18:58:51 +00:00
|
|
|
n.log.Warn("couldn't put system meta to objects cache",
|
2022-05-12 01:50:52 +00:00
|
|
|
zap.Stringer("bucket id", bktInfo.CID),
|
2022-02-17 18:58:51 +00:00
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|