Skip to content

Commit 96ee89e

Browse files
authored
feat(object): add project_id resource attribute (#1703)
1 parent ed45b4b commit 96ee89e

File tree

39 files changed

+12076
-6209
lines changed

39 files changed

+12076
-6209
lines changed

.github/workflows/acceptance-tests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ jobs:
4747
SCW_DEBUG: 0
4848
SCW_ACCESS_KEY: "SCWXXXXXXXXXXXXXFAKE"
4949
SCW_SECRET_KEY: "11111111-1111-1111-1111-111111111111"
50-
SCW_DEFAULT_PROJECT_ID: "11111111-1111-1111-1111-111111111111"
5150
SCW_ENABLE_BETA: true

docs/data-sources/object_bucket.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ data "scaleway_object_bucket" "selected" {
2424
}
2525
```
2626

27+
28+
### Fetching the bucket from a specific project
29+
30+
```hcl
31+
data "scaleway_object_bucket" "selected" {
32+
name = "bucket.test.com"
33+
project_id = "11111111-1111-1111-1111-111111111111"
34+
}
35+
```
36+
2737
## Argument Reference
2838

2939
- `name` - (Required) The bucket name.
3040
- `object_lock_enabled` - (Optional) Enable object lock on the bucket. Defaults to `false`. Updating this field will force creating a new bucket.
3141
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#zones) in which the Object Storage exists.
42+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
3243

3344

3445
## Attributes Reference

docs/resources/object.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The following arguments are supported:
3838
* `visibility` - (Optional) Visibility of the object, `public-read` or `private`
3939
* `metadata` - (Optional) Map of metadata used for the object, keys must be lowercase
4040
* `tags` - (Optional) Map of tags
41+
* `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
4142

4243
## Attributes Reference
4344

docs/resources/object_bucket.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ resource "scaleway_object_bucket" "some_bucket" {
2020
}
2121
```
2222

23+
### Creating the bucket in a specific project
24+
25+
```hcl
26+
resource "scaleway_object_bucket" "some_bucket" {
27+
name = "some-unique-name"
28+
project_id = "11111111-1111-1111-1111-111111111111"
29+
}
30+
```
31+
2332
### Using object lifecycle
2433

2534
```hcl
@@ -115,6 +124,7 @@ The following arguments are supported:
115124
* `versioning` - (Optional) A state of [versioning](https://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html) (documented below)
116125
* `cors_rule` - (Optional) A rule of [Cross-Origin Resource Sharing](https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html) (documented below).
117126
* `force_destroy` - (Optional) Enable deletion of objects in bucket before destroying, locked objects or under legal hold are also deleted and **not** recoverable
127+
* `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
118128

119129
The `acl` attribute is deprecated. See [scaleway_object_bucket_acl](object_bucket_acl.md) resource documentation.
120130
Please check the [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl_overview.html#canned-acl) documentation for supported values.

docs/resources/object_bucket_acl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The following arguments are supported:
6868
* `access_control_policy` - (Optional, Conflicts with acl) A configuration block that sets the ACL permissions for an object per grantee documented below.
6969
* `expected_bucket_owner` - (Optional, Forces new resource) The project ID of the expected bucket owner.
7070
* `region` - (Optional) The [region](https://developers.scaleway.com/en/quickstart/#region-definition) in which the bucket should be created.
71+
* `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
7172

7273

7374
## The ACL

docs/resources/object_bucket_object_lock_configuration.md

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

5656
- `years` - (Optional) The number of years that you want to specify for the default retention period.
5757

58+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
59+
5860
## Import
5961

6062
Lock configuration Bucket can be imported using the `{region}/{bucketName}` identifier, e.g.

docs/resources/object_bucket_policy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The following arguments are supported:
8484

8585
* `bucket` - (Required) The name of the bucket.
8686
* `policy` - (Required) The policy document. This is a JSON formatted string. For more information about building AWS IAM policy documents with Terraform, see the [AWS IAM Policy Document Guide](https://learn.hashicorp.com/tutorials/terraform/aws-iam-policy?_ga=2.164714495.1557487853.1659960650-563504983.1635944492).
87+
* `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
8788

8889
~> **Important:** The [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) data source may be used, so long as it specifies a principal.
8990

docs/resources/object_bucket_website_configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ The following arguments are supported:
7070
* `bucket` - (Required, Forces new resource) The name of the bucket.
7171
* `index_document` - (Required) The name of the index document for the website [detailed below](#index_document).
7272
* `error_document` - (Optional) The name of the error document for the website [detailed below](#error_document).
73+
* `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
7374

7475
## index_document
7576

scaleway/data_source_object_bucket.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func dataSourceScalewayObjectBucket() *schema.Resource {
1616
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayObjectBucket().Schema)
1717

1818
// Set 'Optional' schema elements
19-
addOptionalFieldsToSchema(dsSchema, "name", "region")
19+
addOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")
2020

2121
return &schema.Resource{
2222
ReadContext: dataSourceScalewayObjectStorageRead,
@@ -43,6 +43,14 @@ func dataSourceScalewayObjectStorageRead(ctx context.Context, d *schema.Resource
4343
return diag.FromErr(fmt.Errorf("failed getting Object Storage bucket (%s): %w", bucket, err))
4444
}
4545

46+
acl, err := s3Client.GetBucketAclWithContext(ctx, &s3.GetBucketAclInput{
47+
Bucket: aws.String(bucket),
48+
})
49+
if err != nil {
50+
return diag.FromErr(fmt.Errorf("couldn't read bucket acl: %s", err))
51+
}
52+
_ = d.Set("project_id", *normalizeOwnerID(acl.Owner.ID))
53+
4654
bucketRegionalID := newRegionalIDString(region, bucket)
4755
d.SetId(bucketRegionalID)
4856
return resourceScalewayObjectBucketRead(ctx, d, meta)

scaleway/data_source_object_bucket_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package scaleway
22

33
import (
4+
"context"
45
"fmt"
6+
"regexp"
57
"testing"
68

79
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12+
"github.com/stretchr/testify/require"
913
)
1014

1115
func TestAccScalewayDataSourceObjectStorage_Basic(t *testing.T) {
@@ -38,3 +42,90 @@ func TestAccScalewayDataSourceObjectStorage_Basic(t *testing.T) {
3842
},
3943
})
4044
}
45+
46+
func TestAccScalewayDataSourceObjectStorage_ProjectIDAllowed(t *testing.T) {
47+
tt := NewTestTools(t)
48+
defer tt.Cleanup()
49+
bucketName := sdkacctest.RandomWithPrefix("test-acc-scaleway-object-bucket")
50+
51+
project, iamAPIKey, terminateFakeSideProject, err := createFakeSideProject(tt)
52+
require.NoError(t, err)
53+
54+
ctx := context.Background()
55+
56+
resource.ParallelTest(t, resource.TestCase{
57+
PreCheck: func() { testAccPreCheck(t) },
58+
ProviderFactories: fakeSideProjectProviders(ctx, tt, project, iamAPIKey),
59+
CheckDestroy: resource.ComposeAggregateTestCheckFunc(
60+
func(s *terraform.State) error {
61+
return terminateFakeSideProject()
62+
},
63+
testAccCheckScalewayObjectDestroy(tt),
64+
),
65+
Steps: []resource.TestStep{
66+
// Create a bucket from the main provider into the side project and read it from the side provider
67+
// The side provider should only be able to read the bucket from the side project
68+
{
69+
Config: fmt.Sprintf(`
70+
resource "scaleway_object_bucket" "base" {
71+
name = "%[1]s"
72+
project_id = "%[2]s"
73+
}
74+
75+
data "scaleway_object_bucket" "selected" {
76+
name = scaleway_object_bucket.base.name
77+
provider = side
78+
}
79+
`,
80+
bucketName,
81+
project.ID,
82+
),
83+
Check: resource.ComposeTestCheckFunc(
84+
resource.TestCheckResourceAttr("data.scaleway_object_bucket.selected", "name", bucketName),
85+
resource.TestCheckResourceAttr("data.scaleway_object_bucket.selected", "project_id", project.ID),
86+
),
87+
},
88+
},
89+
})
90+
}
91+
92+
func TestAccScalewayDataSourceObjectStorage_ProjectIDForbidden(t *testing.T) {
93+
tt := NewTestTools(t)
94+
defer tt.Cleanup()
95+
bucketName := sdkacctest.RandomWithPrefix("test-acc-scaleway-object-bucket")
96+
97+
project, iamAPIKey, terminateFakeSideProject, err := createFakeSideProject(tt)
98+
require.NoError(t, err)
99+
100+
ctx := context.Background()
101+
102+
resource.ParallelTest(t, resource.TestCase{
103+
PreCheck: func() { testAccPreCheck(t) },
104+
ProviderFactories: fakeSideProjectProviders(ctx, tt, project, iamAPIKey),
105+
CheckDestroy: resource.ComposeAggregateTestCheckFunc(
106+
func(s *terraform.State) error {
107+
return terminateFakeSideProject()
108+
},
109+
testAccCheckScalewayObjectDestroy(tt),
110+
),
111+
Steps: []resource.TestStep{
112+
// The side provider should not be able to read the bucket from the main project
113+
{
114+
Config: fmt.Sprintf(`
115+
resource "scaleway_object_bucket" "base" {
116+
name = "%[1]s"
117+
}
118+
119+
data "scaleway_object_bucket" "selected" {
120+
name = scaleway_object_bucket.base.name
121+
provider = side
122+
}
123+
`,
124+
bucketName,
125+
project.ID,
126+
),
127+
ExpectError: regexp.MustCompile("failed getting Object Storage bucket"),
128+
},
129+
},
130+
})
131+
}

0 commit comments

Comments
 (0)