Skip to content

Commit a71bb37

Browse files
Laure-diremyleone
andauthored
feat(object): add content_type attribute (#3138)
* fix(object): add content_type attribut * add and correct documentation * add computed to content_type * undo removed line --------- Co-authored-by: Rémy Léone <[email protected]>
1 parent e454232 commit a71bb37

File tree

7 files changed

+1941
-350
lines changed

7 files changed

+1941
-350
lines changed

docs/resources/object.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ The following arguments are supported:
4949

5050
* `metadata` - (Optional) Map of metadata used for the object (keys must be lowercase).
5151

52+
* `content_type` - (Optional) The standard MIME type of the object's content (e.g., 'application/json', 'text/plain'). This specifies how the object should be interpreted by clients. See RFC 9110: https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type
53+
5254
* `tags` - (Optional) Map of tags.
5355

5456
* `sse_customer_key` - (Optional) Customer's encryption keys to encrypt data (SSE-C)

docs/resources/object_bucket_website_configuration.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,27 @@ Refer to the [dedicated documentation](https://www.scaleway.com/en/docs/object-s
1212
## Example Usage
1313

1414
```terraform
15-
resource "scaleway_object_bucket" "main" {
16-
name = "MyBucket"
15+
resource "scaleway_object_bucket" "test" {
16+
name = "my-bucket"
1717
acl = "public-read"
1818
}
1919
20-
resource "scaleway_object_bucket_website_configuration" "main" {
21-
bucket = scaleway_object_bucket.main.id
20+
resource scaleway_object "some_file" {
21+
bucket = scaleway_object_bucket.test.name
22+
key = "index.html"
23+
file = "index.html"
24+
visibility = "public-read"
25+
content_type = "text/html"
26+
}
27+
28+
resource "scaleway_object_bucket_website_configuration" "test" {
29+
bucket = scaleway_object_bucket.test.name
2230
index_document {
2331
suffix = "index.html"
2432
}
33+
error_document {
34+
key = "error.html"
35+
}
2536
}
2637
```
2738

internal/services/block/testdata/snapshot-from-s3.cassette.yaml

Lines changed: 348 additions & 346 deletions
Large diffs are not rendered by default.

internal/services/object/object.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ func ResourceObject() *schema.Resource {
8989
},
9090
ValidateDiagFunc: validateMapKeyLowerCase(),
9191
},
92+
"content_type": {
93+
Type: schema.TypeString,
94+
Optional: true,
95+
Computed: true,
96+
Description: "The standard MIME type of the object's content (e.g., 'application/json', 'text/plain'). This specifies how the object should be interpreted by clients. See RFC 9110: https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type",
97+
},
9298
"tags": {
9399
Optional: true,
94100
Type: schema.TypeMap,
@@ -153,6 +159,10 @@ func resourceObjectCreate(ctx context.Context, d *schema.ResourceData, m interfa
153159
Metadata: types.ExpandMapStringString(d.Get("metadata")),
154160
}
155161

162+
if contentType, ok := d.GetOk("content_type"); ok {
163+
req.ContentType = types.ExpandStringPtr(contentType)
164+
}
165+
156166
visibilityStr := types.ExpandStringPtr(d.Get("visibility").(string))
157167
if visibilityStr != nil {
158168
req.ACL = s3Types.ObjectCannedACL(*visibilityStr)
@@ -253,6 +263,10 @@ func resourceObjectUpdate(ctx context.Context, d *schema.ResourceData, m interfa
253263
ACL: s3Types.ObjectCannedACL(d.Get("visibility").(string)),
254264
}
255265

266+
if contentType, ok := d.GetOk("content_type"); ok {
267+
req.ContentType = types.ExpandStringPtr(contentType)
268+
}
269+
256270
if encryptionKey, ok := d.GetOk("sse_customer_key"); ok {
257271
digestMD5, encryption, err := EncryptCustomerKey(encryptionKey.(string))
258272
if err != nil {
@@ -286,6 +300,10 @@ func resourceObjectUpdate(ctx context.Context, d *schema.ResourceData, m interfa
286300
ACL: s3Types.ObjectCannedACL(d.Get("visibility").(string)),
287301
}
288302

303+
if contentType, ok := d.GetOk("content_type"); ok {
304+
req.ContentType = types.ExpandStringPtr(contentType)
305+
}
306+
289307
if encryptionKey, ok := d.GetOk("sse_customer_key"); ok {
290308
digestMD5, encryption, err := EncryptCustomerKey(encryptionKey.(string))
291309
if err != nil {
@@ -368,6 +386,7 @@ func resourceObjectRead(ctx context.Context, d *schema.ResourceData, m interface
368386
}
369387

370388
_ = d.Set("metadata", types.FlattenMap(obj.Metadata))
389+
_ = d.Set("content_type", &obj.ContentType)
371390

372391
tags, err := s3Client.GetObjectTagging(ctx, &s3.GetObjectTaggingInput{
373392
Bucket: types.ExpandStringPtr(bucket),

internal/services/object/object_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,45 @@ func TestAccObject_Basic(t *testing.T) {
107107
})
108108
}
109109

110+
func TestAccObject_ContentType(t *testing.T) {
111+
tt := acctest.NewTestTools(t)
112+
defer tt.Cleanup()
113+
114+
bucketName := sdkacctest.RandomWithPrefix("test-acc-scaleway-object-content-type")
115+
resource.ParallelTest(t, resource.TestCase{
116+
PreCheck: func() { acctest.PreCheck(t) },
117+
ProviderFactories: tt.ProviderFactories,
118+
CheckDestroy: resource.ComposeTestCheckFunc(
119+
objectchecks.IsObjectDestroyed(tt),
120+
objectchecks.IsBucketDestroyed(tt),
121+
),
122+
Steps: []resource.TestStep{
123+
{
124+
Config: fmt.Sprintf(`
125+
resource "scaleway_object_bucket" "main" {
126+
name = "%s"
127+
region = "%s"
128+
}
129+
130+
resource "scaleway_object" "file" {
131+
bucket = scaleway_object_bucket.main.id
132+
key = "index.html"
133+
file = "testfixture/index.html"
134+
visibility = "public-read"
135+
content_type = "text/html"
136+
}
137+
138+
`, bucketName, objectTestsMainRegion),
139+
Check: resource.ComposeTestCheckFunc(
140+
objectchecks.CheckBucketExists(tt, "scaleway_object_bucket.main", true),
141+
testAccCheckObjectExists(tt, "scaleway_object.file"),
142+
resource.TestCheckResourceAttr("scaleway_object.file", "content_type", "text/html"),
143+
),
144+
},
145+
},
146+
})
147+
}
148+
110149
func TestAccObject_Hash(t *testing.T) {
111150
tt := acctest.NewTestTools(t)
112151
defer tt.Cleanup()

0 commit comments

Comments
 (0)