2021-08-09 14:29:44 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2022-02-25 09:22:22 +00:00
|
|
|
"fmt"
|
2021-08-09 14:29:44 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2022-02-28 08:02:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2021-08-11 10:02:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-08-09 14:29:44 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *handler) PutBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
configuration := new(VersioningConfiguration)
|
|
|
|
if err := xml.NewDecoder(r.Body).Decode(configuration); err != nil {
|
2021-08-11 10:02:13 +00:00
|
|
|
h.logAndSendError(w, "couldn't decode versioning configuration", reqInfo, errors.GetAPIError(errors.ErrIllegalVersioningConfigurationException))
|
2021-08-09 14:29:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-08-19 06:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:02:05 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.VersioningEnabled = configuration.Status == "Enabled"
|
|
|
|
|
|
|
|
p := &layer.PutSettingsParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
Settings: settings,
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:22:22 +00:00
|
|
|
if !p.Settings.VersioningEnabled && bktInfo.ObjectLockEnabled {
|
|
|
|
h.logAndSendError(w, "couldn't suspend bucket versioning", reqInfo, fmt.Errorf("object lock is enabled"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:02:05 +00:00
|
|
|
if err = h.obj.PutBucketSettings(r.Context(), p); err != nil {
|
2021-08-09 14:29:44 +00:00
|
|
|
h.logAndSendError(w, "couldn't put update versioning settings", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketVersioningHandler implements bucket versioning getter handler.
|
|
|
|
func (h *handler) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-08-19 06:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:02:05 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
2021-08-09 14:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
h.log.Warn("couldn't get version settings object: default version settings will be used",
|
|
|
|
zap.String("request_id", reqInfo.RequestID),
|
|
|
|
zap.String("method", reqInfo.API),
|
2021-08-19 06:55:22 +00:00
|
|
|
zap.String("bucket_name", reqInfo.BucketName),
|
2021-08-09 14:29:44 +00:00
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-08-10 08:58:40 +00:00
|
|
|
if err = api.EncodeToResponse(w, formVersioningConfiguration(settings)); err != nil {
|
2021-08-09 14:29:44 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:02:05 +00:00
|
|
|
func formVersioningConfiguration(settings *data.BucketSettings) *VersioningConfiguration {
|
2021-08-19 06:55:22 +00:00
|
|
|
res := &VersioningConfiguration{}
|
2021-08-10 08:58:40 +00:00
|
|
|
if settings == nil {
|
2021-08-09 14:29:44 +00:00
|
|
|
return res
|
|
|
|
}
|
2021-08-10 08:58:40 +00:00
|
|
|
if settings.VersioningEnabled {
|
|
|
|
res.Status = "Enabled"
|
2021-08-19 06:55:22 +00:00
|
|
|
} else {
|
|
|
|
res.Status = "Suspended"
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|