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
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2022-02-17 18:58:51 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-04-29 13:08:22 +00:00
|
|
|
type PutBucketNotificationConfigurationParams struct {
|
2023-07-05 14:05:45 +00:00
|
|
|
RequestInfo *middleware.ReqInfo
|
2022-04-29 13:08:22 +00:00
|
|
|
BktInfo *data.BucketInfo
|
|
|
|
Configuration *data.NotificationConfiguration
|
2023-04-24 23:49:12 +00:00
|
|
|
CopiesNumbers []uint32
|
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-26 13:11:14 +00:00
|
|
|
prm := PrmObjectCreate{
|
2022-08-11 22:48:56 +00:00
|
|
|
Container: p.BktInfo.CID,
|
|
|
|
Payload: bytes.NewReader(confXML),
|
2022-10-03 14:33:49 +00:00
|
|
|
Filepath: p.BktInfo.NotificationConfigurationObjectName(),
|
2022-11-08 09:12:55 +00:00
|
|
|
CreationTime: TimeNow(ctx),
|
2023-04-24 23:49:12 +00:00
|
|
|
CopiesNumber: p.CopiesNumbers,
|
2022-02-17 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:45:28 +00:00
|
|
|
_, objID, _, err := n.objectPutAndHash(ctx, prm, p.BktInfo)
|
2022-02-17 18:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
objIDToDelete, err := n.treeService.PutNotificationConfigurationNode(ctx, p.BktInfo, objID)
|
2022-06-27 09:33:36 +00:00
|
|
|
objIDToDeleteNotFound := errorsStd.Is(err, ErrNoNodeToRemove)
|
|
|
|
if err != nil && !objIDToDeleteNotFound {
|
2022-05-12 01:50:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if !objIDToDeleteNotFound {
|
|
|
|
if err = n.objectDelete(ctx, p.BktInfo, objIDToDelete); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
n.reqLogger(ctx).Error(logs.CouldntDeleteNotificationConfigurationObject, zap.Error(err),
|
2023-06-09 13:19:23 +00:00
|
|
|
zap.String("cid", p.BktInfo.CID.EncodeToString()),
|
|
|
|
zap.String("oid", objIDToDelete.EncodeToString()))
|
2022-05-12 01:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 12:08:22 +00:00
|
|
|
n.cache.PutNotificationConfiguration(n.BearerOwner(ctx), p.BktInfo, p.Configuration)
|
2022-02-17 18:58:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) GetBucketNotificationConfiguration(ctx context.Context, bktInfo *data.BucketInfo) (*data.NotificationConfiguration, error) {
|
2023-08-03 12:08:22 +00:00
|
|
|
owner := n.BearerOwner(ctx)
|
2022-10-06 08:46:53 +00:00
|
|
|
if conf := n.cache.GetNotificationConfiguration(owner, bktInfo); conf != nil {
|
2022-02-17 18:58:51 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
objID, err := n.treeService.GetNotificationConfigurationNode(ctx, bktInfo)
|
2022-06-27 09:33:36 +00:00
|
|
|
objIDNotFound := errorsStd.Is(err, ErrNodeNotFound)
|
|
|
|
if err != nil && !objIDNotFound {
|
2022-02-17 18:58:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := &data.NotificationConfiguration{}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if !objIDNotFound {
|
|
|
|
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-10-06 08:46:53 +00:00
|
|
|
n.cache.PutNotificationConfiguration(owner, bktInfo, conf)
|
2022-02-17 18:58:51 +00:00
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|