2021-08-09 14:29:44 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"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 {
|
|
|
|
h.logAndSendError(w, "couldn't decode versioning configuration", reqInfo, api.GetAPIError(api.ErrIllegalVersioningConfigurationException))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.PutVersioningParams{
|
2021-08-10 08:58:40 +00:00
|
|
|
Bucket: reqInfo.BucketName,
|
|
|
|
Settings: &layer.BucketSettings{VersioningEnabled: configuration.Status == "Enabled"},
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := h.obj.PutBucketVersioning(r.Context(), p); err != nil {
|
|
|
|
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())
|
|
|
|
|
2021-08-10 08:58:40 +00:00
|
|
|
settings, err := h.obj.GetBucketVersioning(r.Context(), reqInfo.BucketName)
|
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),
|
|
|
|
zap.String("object_name", reqInfo.ObjectName),
|
|
|
|
zap.Error(err))
|
2021-08-10 10:03:09 +00:00
|
|
|
return
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 08:58:40 +00:00
|
|
|
func formVersioningConfiguration(settings *layer.BucketSettings) *VersioningConfiguration {
|
2021-08-09 14:29:44 +00:00
|
|
|
res := &VersioningConfiguration{Status: "Suspended"}
|
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-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|