2021-08-09 14:29:44 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2021-08-09 14:29:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *handler) PutBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2021-08-09 14:29:44 +00:00
|
|
|
|
|
|
|
configuration := new(VersioningConfiguration)
|
2023-10-09 12:34:51 +00:00
|
|
|
if err := h.cfg.NewXMLDecoder(r.Body).Decode(configuration); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, 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 {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2021-08-19 06:55:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(ctx, bktInfo)
|
2022-02-28 08:02:05 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't get bucket settings", reqInfo, err)
|
2022-02-28 08:02:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:30:19 +00:00
|
|
|
if configuration.Status != data.VersioningEnabled && configuration.Status != data.VersioningSuspended {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "invalid versioning configuration", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
|
2022-07-19 14:58:18 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-22 13:32:09 +00:00
|
|
|
|
|
|
|
// settings pointer is stored in the cache, so modify a copy of the settings
|
|
|
|
newSettings := *settings
|
|
|
|
newSettings.Versioning = configuration.Status
|
2022-02-28 08:02:05 +00:00
|
|
|
|
|
|
|
p := &layer.PutSettingsParams{
|
|
|
|
BktInfo: bktInfo,
|
2022-08-22 13:32:09 +00:00
|
|
|
Settings: &newSettings,
|
2022-02-28 08:02:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:58:18 +00:00
|
|
|
if p.Settings.VersioningSuspended() && bktInfo.ObjectLockEnabled {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't suspend bucket versioning", reqInfo, errors.GetAPIError(errors.ErrObjectLockConfigurationVersioningCannotBeChanged))
|
2022-02-25 09:22:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
if err = h.obj.PutBucketSettings(ctx, p); err != nil {
|
|
|
|
h.logAndSendError(ctx, w, "couldn't put update versioning settings", reqInfo, err)
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketVersioningHandler implements bucket versioning getter handler.
|
|
|
|
func (h *handler) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2021-08-09 14:29:44 +00:00
|
|
|
|
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 {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2021-08-19 06:55:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(ctx, bktInfo)
|
2021-08-09 14:29:44 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't get version settings", reqInfo, err)
|
2022-10-06 08:46:53 +00:00
|
|
|
return
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, formVersioningConfiguration(settings)); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "something went wrong", reqInfo, err)
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:02:05 +00:00
|
|
|
func formVersioningConfiguration(settings *data.BucketSettings) *VersioningConfiguration {
|
2021-08-19 06:55:22 +00:00
|
|
|
res := &VersioningConfiguration{}
|
2022-07-19 14:58:18 +00:00
|
|
|
if !settings.Unversioned() {
|
|
|
|
res.Status = settings.Versioning
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
2022-07-19 14:58:18 +00:00
|
|
|
|
2021-08-09 14:29:44 +00:00
|
|
|
return res
|
|
|
|
}
|