Skip to content

Commit 438a6ff

Browse files
authored
feat(object): create object by string content (#2011)
1 parent c847f46 commit 438a6ff

5 files changed

+2335
-659
lines changed

docs/resources/object.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ The following arguments are supported:
3131

3232
* `bucket` - (Required) The name of the bucket.
3333
* `key` - (Required) The path of the object.
34-
* `file` - (Optional) The name of the file to upload, defaults to an empty file. Only one of `file` or `content` can be defined.
35-
* `content` - (Optional) The content of the file to upload. Only one of `file` or `content` can be defined.
34+
* `file` - (Optional) The name of the file to upload, defaults to an empty file. Only one of `file`, `content` or `content_base64` can be defined.
35+
* `content` - (Optional) The content of the file to upload. Only one of `file`, `content` or `content_base64` can be defined.
36+
* `content_base64` - (Optional) The base64-encoded content of the file to upload. Only one of `file`, `content` or `content_base64` can be defined.
3637
* `hash` - (Optional) Hash of the file, used to trigger upload on file change
3738
* `storage_class` - (Optional) Specifies the Scaleway [storage class](https://www.scaleway.com/en/docs/storage/object/concepts/#storage-class) `STANDARD`, `GLACIER`, `ONEZONE_IA` used to store the object.
3839
* `visibility` - (Optional) Visibility of the object, `public-read` or `private`

scaleway/resource_object.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ func resourceScalewayObject() *schema.Resource {
4646
Type: schema.TypeString,
4747
Optional: true,
4848
Description: "Path of the file to upload, defaults to an empty file",
49-
ConflictsWith: []string{"content"},
49+
ConflictsWith: []string{"content", "content_base64"},
5050
},
5151
"content": {
52+
Type: schema.TypeString,
53+
Optional: true,
54+
Description: "Content of the file to upload",
55+
ConflictsWith: []string{"file", "content_base64"},
56+
},
57+
"content_base64": {
5258
Type: schema.TypeString,
5359
Optional: true,
5460
Description: "Content of the file to upload, should be base64 encoded",
55-
ConflictsWith: []string{"file"},
61+
ConflictsWith: []string{"file", "content"},
5662
},
5763
"hash": {
5864
Type: schema.TypeString,
@@ -125,6 +131,9 @@ func resourceScalewayObjectCreate(ctx context.Context, d *schema.ResourceData, m
125131
}
126132
req.Body = file
127133
} else if content, hasContent := d.GetOk("content"); hasContent {
134+
contentString := []byte(content.(string))
135+
req.Body = bytes.NewReader(contentString)
136+
} else if content, hasContent := d.GetOk("content_base64"); hasContent {
128137
contentString := []byte(content.(string))
129138
decoded := make([]byte, base64.StdEncoding.DecodedLen(len(contentString)))
130139
_, err = base64.StdEncoding.Decode(decoded, contentString)

scaleway/resource_object_test.go

+60-9
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,6 @@ func TestAccScalewayObject_ByContent(t *testing.T) {
490490

491491
fileContentStep1 := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
492492
fileContentStep2 := "This is a different content"
493-
fileEncodedStep1 := base64.StdEncoding.EncodeToString([]byte(fileContentStep1))
494-
fileEncodedStep2 := base64.StdEncoding.EncodeToString([]byte(fileContentStep2))
495493

496494
resource.ParallelTest(t, resource.TestCase{
497495
PreCheck: func() { testAccPreCheck(t) },
@@ -507,12 +505,12 @@ func TestAccScalewayObject_ByContent(t *testing.T) {
507505
resource scaleway_object "by-content" {
508506
bucket = scaleway_object_bucket.base-01.name
509507
key = "test-by-content"
510-
content = base64encode("%s")
508+
content = "%s"
511509
}
512510
`, bucketName, fileContentStep1),
513511
Check: resource.ComposeTestCheckFunc(
514512
testAccCheckScalewayObjectExists(tt, "scaleway_object.by-content"),
515-
resource.TestCheckResourceAttr("scaleway_object.by-content", "content", fileEncodedStep1),
513+
resource.TestCheckResourceAttr("scaleway_object.by-content", "content", fileContentStep1),
516514
),
517515
},
518516
{
@@ -524,24 +522,77 @@ func TestAccScalewayObject_ByContent(t *testing.T) {
524522
resource scaleway_object "by-content" {
525523
bucket = scaleway_object_bucket.base-01.name
526524
key = "test-by-content"
527-
content = base64encode("%s")
525+
content = "%s"
528526
}
529527
`, bucketName, fileContentStep2),
530528
Check: resource.ComposeTestCheckFunc(
531529
testAccCheckScalewayObjectExists(tt, "scaleway_object.by-content"),
532-
resource.TestCheckResourceAttr("scaleway_object.by-content", "content", fileEncodedStep2),
530+
resource.TestCheckResourceAttr("scaleway_object.by-content", "content", fileContentStep2),
533531
),
534532
},
533+
},
534+
})
535+
}
536+
537+
func TestAccScalewayObject_ByContentBase64(t *testing.T) {
538+
tt := NewTestTools(t)
539+
defer tt.Cleanup()
540+
bucketName := sdkacctest.RandomWithPrefix("test-acc-scaleway-object-by-content-base64")
541+
542+
fileContentStep1 := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
543+
fileContentStep2 := "This is a different content"
544+
fileEncodedStep1 := base64.StdEncoding.EncodeToString([]byte(fileContentStep1))
545+
fileEncodedStep2 := base64.StdEncoding.EncodeToString([]byte(fileContentStep2))
546+
547+
resource.ParallelTest(t, resource.TestCase{
548+
PreCheck: func() { testAccPreCheck(t) },
549+
ProviderFactories: tt.ProviderFactories,
550+
CheckDestroy: testAccCheckScalewayObjectDestroy(tt),
551+
Steps: []resource.TestStep{
535552
{
536553
Config: fmt.Sprintf(`
537554
resource "scaleway_object_bucket" "base-01" {
538555
name = "%s"
539556
}
540557
541-
resource scaleway_object "by-content" {
558+
resource scaleway_object "by-content-base64" {
542559
bucket = scaleway_object_bucket.base-01.name
543-
key = "test-by-content"
544-
content = "%s"
560+
key = "test-by-content-base64"
561+
content_base64 = base64encode("%s")
562+
}
563+
`, bucketName, fileContentStep1),
564+
Check: resource.ComposeTestCheckFunc(
565+
testAccCheckScalewayObjectExists(tt, "scaleway_object.by-content-base64"),
566+
resource.TestCheckResourceAttr("scaleway_object.by-content-base64", "content_base64", fileEncodedStep1),
567+
),
568+
},
569+
{
570+
Config: fmt.Sprintf(`
571+
resource "scaleway_object_bucket" "base-01" {
572+
name = "%s"
573+
}
574+
575+
resource scaleway_object "by-content-base64" {
576+
bucket = scaleway_object_bucket.base-01.name
577+
key = "test-by-content-base64"
578+
content_base64 = base64encode("%s")
579+
}
580+
`, bucketName, fileContentStep2),
581+
Check: resource.ComposeTestCheckFunc(
582+
testAccCheckScalewayObjectExists(tt, "scaleway_object.by-content-base64"),
583+
resource.TestCheckResourceAttr("scaleway_object.by-content-base64", "content_base64", fileEncodedStep2),
584+
),
585+
},
586+
{
587+
Config: fmt.Sprintf(`
588+
resource "scaleway_object_bucket" "base-01" {
589+
name = "%s"
590+
}
591+
592+
resource scaleway_object "by-content-base64" {
593+
bucket = scaleway_object_bucket.base-01.name
594+
key = "test-by-content-base64"
595+
content_base64 = "%s"
545596
}
546597
`, bucketName, fileContentStep2),
547598
ExpectError: regexp.MustCompile("illegal base64 data at input byte 4"),

0 commit comments

Comments
 (0)