Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat [cli] - object storage locking #1313

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mgc/sdk/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module magalu.cloud/sdk
go 1.23.0

require (
github.com/PaesslerAG/gval v1.2.2
github.com/geffersonFerraz/brazilian-words-sorter v1.1.0
github.com/getkin/kin-openapi v0.118.0
github.com/go-openapi/jsonpointer v0.20.0
Expand All @@ -21,6 +20,7 @@ require (
)

require (
github.com/PaesslerAG/gval v1.2.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
Expand Down Expand Up @@ -65,7 +65,7 @@ require (
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1
)

replace magalu.cloud/core => ../core
107 changes: 53 additions & 54 deletions mgc/sdk/static/object_storage/common/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,46 @@ type deleteBatchRequestBody struct {
Objects []objectIdentifier `xml:"Object"`
}


func DeleteSingle(ctx context.Context, params DeleteObjectParams, cfg Config) error {
objectKey := params.Destination.AsFilePath().String()
versionID := params.Version
objectKey := params.Destination.AsFilePath().String()
versionID := params.Version

req, err := newDeleteSingleRequest(ctx, cfg, NewBucketNameFromURI(params.Destination), objectKey, versionID)
if err != nil {
return err
}
req, err := newDeleteSingleRequest(ctx, cfg, NewBucketNameFromURI(params.Destination), objectKey, versionID)
if err != nil {
return err
}

resp, err := SendRequest(ctx, req)
if err != nil {
return err
}
resp, err := SendRequest(ctx, req)
if err != nil {
return err
}

err = ExtractErr(resp, req)
if err != nil {
return err
}
err = ExtractErr(resp, req)
if err != nil {
return err
}

return nil
return nil
}

func newDeleteSingleRequest(ctx context.Context, cfg Config, bucketName BucketName, objectKey string, versionID string) (*http.Request, error) {
host, err := BuildBucketHost(cfg, bucketName)
if err != nil {
return nil, core.UsageError{Err: err}
}
host, err := BuildBucketHost(cfg, bucketName)
if err != nil {
return nil, core.UsageError{Err: err}
}

url := fmt.Sprintf("%s/%s", host, objectKey)
url := fmt.Sprintf("%s/%s", host, objectKey)

if versionID != "" {
url = fmt.Sprintf("%s?versionId=%s", url, versionID)
}
if versionID != "" {
url = fmt.Sprintf("%s?versionId=%s", url, versionID)
}

req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return nil, err
}

return req, nil
return req, nil
}

func newDeleteBatchRequest(ctx context.Context, cfg Config, bucketName BucketName, objKeys []objectIdentifier) (*http.Request, error) {
Expand Down Expand Up @@ -243,31 +242,31 @@ func DeleteBucket(ctx context.Context, params DeleteBucketParams, cfg Config) er
}

func Delete(ctx context.Context, params DeleteObjectParams, cfg Config) error {
objKeys := []objectIdentifier{{Key: params.Destination.AsFilePath().String(), VersionId: params.Version}}

if len(objKeys) > 1 {
req, err := newDeleteBatchRequest(ctx, cfg, NewBucketNameFromURI(params.Destination), objKeys)
if err != nil {
return err
}

resp, err := SendRequest(ctx, req)
if err != nil {
return err
}

err = ExtractErr(resp, req)
if err != nil {
return err
}
} else {
err := DeleteSingle(ctx, params, cfg)
if err != nil {
return err
}
}

return nil
objKeys := []objectIdentifier{{Key: params.Destination.AsFilePath().String(), VersionId: params.Version}}

if len(objKeys) > 1 {
req, err := newDeleteBatchRequest(ctx, cfg, NewBucketNameFromURI(params.Destination), objKeys)
if err != nil {
return err
}

resp, err := SendRequest(ctx, req)
if err != nil {
return err
}

err = ExtractErr(resp, req)
if err != nil {
return err
}
} else {
err := DeleteSingle(ctx, params, cfg)
if err != nil {
return err
}
}

return nil
}

func DeleteObjects(ctx context.Context, params DeleteObjectsParams, cfg Config) error {
Expand Down
2 changes: 1 addition & 1 deletion mgc/sdk/static/object_storage/common/object-lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func DefaultObjectRetentionBody(retainUntilDate time.Time) ObjectRetention {
return ObjectRetention{
Namespace: namespace,
Mode: ObjectLockModeCompliance,
RetainUntilDate: retainUntilDate.UTC().Format("2006-01-02T15:04:05"),
RetainUntilDate: retainUntilDate.UTC().Format(time.RFC3339),
}

}
Expand Down
33 changes: 29 additions & 4 deletions mgc/sdk/static/object_storage/objects/object-lock/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ func setObjectLocking(ctx context.Context, params setObjectLockParams, cfg commo
return
}

func parseISODate(dateStr string) (string, error) {
formats := []string{
time.RFC3339, // "2006-01-02T15:04:05Z07:00"
"2006-01-02T15:04:05", // "2006-01-02T15:04:05"
"2006-01-02", // "2006-01-02"
"2006-01-02 15:04:05", // "2006-01-02 15:04:05"
"2006-01-02T15:04:05.000000Z07:00", // "2006-01-02T15:04:05.000000Z07:00"
"2006-01-02T15:04:05Z", // "2006-01-02T15:04:05Z"
}

for _, format := range formats {
date, err := time.Parse(format, dateStr)
if err == nil {
return date.UTC().Format(time.RFC3339), nil
}
}

return "", fmt.Errorf("invalid date format: %s", dateStr)
}

func newSetObjectLockingRequest(ctx context.Context, p setObjectLockParams, cfg common.Config) (*http.Request, error) {
url, err := common.BuildBucketHostWithPath(cfg, common.NewBucketNameFromURI(p.Object), p.Object.Path())
if err != nil {
Expand All @@ -72,20 +92,25 @@ func newSetObjectLockingRequest(ctx context.Context, p setObjectLockParams, cfg
req.URL.RawQuery = query.Encode()

getBody := func() (io.ReadCloser, error) {
var parsedTime time.Time

parsedTime, err = time.Parse("2006-01-02T15:04:05", p.RetainUntilDate)
parsedTimeStr, err := parseISODate(p.RetainUntilDate)
if err != nil {
return nil, core.UsageError{Err: err}
}
parsedTime, err := time.Parse(time.RFC3339, parsedTimeStr)
if err != nil {
return nil, core.UsageError{Err: err}
}
bodyObj := common.DefaultObjectRetentionBody(parsedTime.In(time.Now().Location()))

bodyObj := common.DefaultObjectRetentionBody(parsedTime)
if p.Mode == string(common.ObjectLockModeGovernance) {
bodyObj.Mode = common.ObjectLockModeGovernance
}

body, err := xml.MarshalIndent(bodyObj, "", " ")
if err != nil {
return nil, err
}

reader := bytes.NewReader(body)
return io.NopCloser(reader), nil
}
Expand Down
Loading