Skip to content

Commit

Permalink
Add resource_minio_s3_object 'source' support (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhemberger committed Mar 11, 2024
1 parent c9154f4 commit 67ee876
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dz
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
Expand Down
31 changes: 27 additions & 4 deletions minio/resource_minio_s3_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/minio/minio-go/v7"
"io"
"github.com/mitchellh/go-homedir"
)

func resourceMinioObject() *schema.Resource {
Expand Down Expand Up @@ -78,7 +84,26 @@ func minioPutObject(ctx context.Context, d *schema.ResourceData, meta interface{

var body io.ReadSeeker

if v, ok := d.GetOk("content"); ok {
if v, ok := d.GetOk("source"); ok {
source := v.(string)
path, err := homedir.Expand(source)
if err != nil {
return NewResourceError(fmt.Sprintf("expanding homedir in source (%s)", source), d.Id(), err)
}
path = filepath.Clean(path)
file, err := os.Open(path)
if err != nil {
return NewResourceError(fmt.Sprintf("opening S3 object source (%s)", path), d.Id(), err)
}

body = file
defer func() {
err := file.Close()
if err != nil {
log.Printf("[WARN] Error closing S3 object source (%s): %s", path, err)
}
}()
} else if v, ok := d.GetOk("content"); ok {
content := v.(string)
body = bytes.NewReader([]byte(content))
} else if v, ok := d.GetOk("content_base64"); ok {
Expand All @@ -88,8 +113,6 @@ func minioPutObject(ctx context.Context, d *schema.ResourceData, meta interface{
return NewResourceError("error decoding content_base64", d.Id(), err)
}
body = bytes.NewReader(contentRaw)
} else if _, ok := d.GetOk("content_base64"); ok {
return NewResourceError("putting object failed", d.Id(), errors.New("sorry, unsupported yet"))
} else {
return NewResourceError("putting object failed", d.Id(), errors.New("one of source / content / content_base64 is not set"))
}
Expand Down

0 comments on commit 67ee876

Please sign in to comment.