Skip to content

Commit 6278d71

Browse files
Laure-diremyleone
andauthored
feat(instance): add support filesystem (#3149)
* feat(instance): add support filesystem feat(file): add support v1beta1 Create, Read and Update function Delete Methode add tests fix date issue and register cassettes test: add validation for filesystem size granularity fix: convertion fix(instance): update schema filesystem test(instance): add test attach filesystem feat(instance): delete and update filesystem chore: master sdk-go go mod tidy update doc and tests fix fmt update go.mod fix(file): rename attribut size to size_in_gb fix: error return attachFS and typo doc fix: error return attachFS and typo doc update FileSystem Size to GB update FileSystem Size to GB refacto waiter tests: fix test and add waiter * update cassette * docs: fix size in file * docs: fix size in file --------- Co-authored-by: Rémy Léone <[email protected]>
1 parent 6fdd6f5 commit 6278d71

15 files changed

+7805
-160
lines changed

docs/resources/file_filesystem.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ This resource allows you to define and manage the size, tags, and region of a fi
1818
```terraform
1919
resource scaleway_file_filesystem file {
2020
name = "my-nfs-filesystem"
21-
size = 100000000000 # 100 GB
21+
size_in_gb = 100
2222
}
2323
```
2424

2525
## Argument Reference
2626

2727
- `name` - (Optional) The name of the filesystem. If not provided, a random name will be generated.
28-
- `size` - (Required) The size of the filesystem in bytes, with a granularity of 100 GB (10¹¹ bytes).
28+
- `size_in_gb` - (Required) The size of the filesystem in bytes, with a granularity of 100 GB (10¹¹ bytes).
2929
- Minimum: 100 GB (100000000000 bytes)
3030
- Maximum: 10 TB (10000000000000 bytes)
3131
- `tags` - (Optional) A list of tags associated with the filesystem.

docs/resources/instance_server.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@ resource "scaleway_instance_server" "web" {
4545
}
4646
```
4747

48+
### With filesystem
49+
50+
```terraform
51+
resource scaleway_block_volume volume {
52+
iops = 15000
53+
size_in_gb = 15
54+
}
55+
56+
resource scaleway_file_filesystem terraform_instance_filesystem {
57+
name = "filesystem-instance-terraform"
58+
size_in_gb = 100
59+
}
60+
61+
resource scaleway_instance_server base {
62+
type = "POP2-HM-2C-16G"
63+
state = "started"
64+
tags = ["terraform-test", "scaleway_instance_server", "state"]
65+
root_volume {
66+
volume_type = "sbs_volume"
67+
volume_id = scaleway_block_volume.volume.id
68+
}
69+
filesystems {
70+
filesystem_id = scaleway_file_filesystem.terraform_instance_filesystem.id
71+
}
72+
}
73+
```
74+
4875
### With a reserved IP
4976

5077
```terraform
@@ -227,6 +254,9 @@ attached to the server. Updates to this field will trigger a stop/start of the s
227254

228255
~> **Important:** If this field contains local volumes, you have to first detach them, in one apply, and then delete the volume in another apply.
229256

257+
- `filesystems` - (Optional) List of filesystems attached to the server.
258+
- `filesystem_id` - (Optional) The unique ID of the filesystem attached to the server.
259+
230260
- `ip_id` - (Optional) The ID of the reserved IP that is attached to the server.
231261

232262
- `ip_ids` - (Optional) List of ID of reserved IPs that are attached to the server. Cannot be used with `ip_id`.
@@ -289,6 +319,8 @@ In addition to all arguments above, the following attributes are exported:
289319
- `placement_group_policy_respected` - (Deprecated) Always false, use [instance_placement_group resource](instance_placement_group.md) to known when the placement group policy is respected.
290320
- `root_volume`
291321
- `volume_id` - The volume ID of the root volume of the server.
322+
- `filesystem`
323+
- `state` - The current status of the filesystem (e.g., attached, detached).
292324
- `public_ips` - The list of public IPs of the server.
293325
- `id` - The ID of the IP.
294326
- `address` - The address of the IP.

internal/services/file/filesystem.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
910
file "github.com/scaleway/scaleway-sdk-go/api/file/v1alpha1"
1011
"github.com/scaleway/scaleway-sdk-go/scw"
1112
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
@@ -37,10 +38,11 @@ func ResourceFileSystem() *schema.Resource {
3738
Optional: true,
3839
Description: "The name of the filesystem",
3940
},
40-
"size": {
41-
Type: schema.TypeInt,
42-
Required: true,
43-
Description: "The Filesystem size in bytes, with a granularity of 100 GB (10^11 bytes). Must be compliant with the minimum (100 GB) and maximum (10 TB) allowed size.",
41+
"size_in_gb": {
42+
Type: schema.TypeInt,
43+
Required: true,
44+
ValidateFunc: validation.IntBetween(1, 1000),
45+
Description: "The Filesystem size_in_gb in bytes, with a granularity of 100 GB (10^11 bytes). Must be compliant with the minimum (100 GB) and maximum (10 TB) allowed size_in_gb.",
4446
},
4547
"tags": {
4648
Type: schema.TypeList,
@@ -87,10 +89,16 @@ func ResourceFileSystemCreate(ctx context.Context, d *schema.ResourceData, m any
8789
Region: region,
8890
Name: types.ExpandOrGenerateString(d.Get("name").(string), "file"),
8991
ProjectID: d.Get("project_id").(string),
90-
Size: *types.ExpandUint64Ptr(d.Get("size")),
92+
Size: *types.ExpandUint64Ptr(d.Get("size_in_gb")),
9193
Tags: types.ExpandStrings(d.Get("tags")),
9294
}
9395

96+
if size, ok := d.GetOk("size_in_gb"); ok {
97+
sizeInGB := size.(int)
98+
sizeInBytes := uint64(sizeInGB) * uint64(scw.GB)
99+
req.Size = sizeInBytes
100+
}
101+
94102
file, err := api.CreateFileSystem(req, scw.WithContext(ctx))
95103
if err != nil {
96104
return diag.FromErr(err)
@@ -128,7 +136,7 @@ func ResourceFileSystemRead(ctx context.Context, d *schema.ResourceData, m any)
128136
_ = d.Set("region", fileSystem.Region)
129137
_ = d.Set("organization_id", fileSystem.OrganizationID)
130138
_ = d.Set("status", fileSystem.Status)
131-
_ = d.Set("size", int64(fileSystem.Size))
139+
_ = d.Set("size_in_gb", int(fileSystem.Size/scw.GB))
132140
_ = d.Set("tags", fileSystem.Tags)
133141
_ = d.Set("created_at", fileSystem.CreatedAt.Format(time.RFC3339))
134142
_ = d.Set("updated_at", fileSystem.UpdatedAt.Format(time.RFC3339))
@@ -163,8 +171,9 @@ func ResourceFileSystemUpdate(ctx context.Context, d *schema.ResourceData, m any
163171
req.Name = types.ExpandUpdatedStringPtr(d.Get("name"))
164172
}
165173

166-
if d.HasChange("size") {
167-
req.Size = types.ExpandUint64Ptr(d.Get("size"))
174+
if d.HasChange("size_in_gb") {
175+
sizeInGB := uint64(d.Get("size_in_gb").(int)) * uint64(scw.GB)
176+
req.Size = types.ExpandUint64Ptr(sizeInGB)
168177
}
169178

170179
if d.HasChange("tags") {

internal/services/file/filesystem_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestAccFileSystem_Basic(t *testing.T) {
2020

2121
fileSystemName := "TestAccFileSystem_Basic"
2222
fileSystemNameUpdated := "TestAccFileSystem_BasicUpdate"
23-
size := int64(100_000_000_000)
23+
sizeInGB := 100
2424

2525
resource.ParallelTest(t, resource.TestCase{
2626
PreCheck: func() { acctest.PreCheck(t) },
@@ -31,25 +31,25 @@ func TestAccFileSystem_Basic(t *testing.T) {
3131
Config: fmt.Sprintf(`
3232
resource "scaleway_file_filesystem" "fs" {
3333
name = "%s"
34-
size = %d
34+
size_in_gb = %d
3535
}
36-
`, fileSystemName, size),
36+
`, fileSystemName, sizeInGB),
3737
Check: resource.ComposeTestCheckFunc(
3838
testAccCheckFileSystemExists(tt, "scaleway_file_filesystem.fs"),
3939
resource.TestCheckResourceAttr("scaleway_file_filesystem.fs", "name", fileSystemName),
40-
resource.TestCheckResourceAttr("scaleway_file_filesystem.fs", "size", strconv.FormatInt(size, 10)),
40+
resource.TestCheckResourceAttr("scaleway_file_filesystem.fs", "size_in_gb", strconv.Itoa(sizeInGB)),
4141
),
4242
},
4343
{
4444
Config: fmt.Sprintf(`
4545
resource "scaleway_file_filesystem" "fs" {
4646
name = "%s"
47-
size = %d
47+
size_in_gb = %d
4848
}
49-
`, fileSystemNameUpdated, size),
49+
`, fileSystemNameUpdated, sizeInGB),
5050
Check: resource.ComposeTestCheckFunc(
5151
testAccCheckFileSystemExists(tt, "scaleway_file_filesystem.fs"),
52-
resource.TestCheckResourceAttr("scaleway_file_filesystem.fs", "size", strconv.FormatInt(size, 10)),
52+
resource.TestCheckResourceAttr("scaleway_file_filesystem.fs", "size_in_gb", strconv.Itoa(sizeInGB)),
5353
),
5454
},
5555
},
@@ -61,7 +61,7 @@ func TestAccFileSystem_SizeTooSmallFails(t *testing.T) {
6161
defer tt.Cleanup()
6262

6363
fileSystemName := "TestAccFileSystem_SizeTooSmallFails"
64-
size := int64(10_000_000_000)
64+
sizeInGB := 10
6565

6666
resource.ParallelTest(t, resource.TestCase{
6767
PreCheck: func() { acctest.PreCheck(t) },
@@ -72,9 +72,9 @@ func TestAccFileSystem_SizeTooSmallFails(t *testing.T) {
7272
Config: fmt.Sprintf(`
7373
resource "scaleway_file_filesystem" "fs" {
7474
name = "%s"
75-
size = %d
75+
size_in_gb = %d
7676
}
77-
`, fileSystemName, size),
77+
`, fileSystemName, sizeInGB),
7878
ExpectError: regexp.MustCompile("size must be greater or equal to 100000000000"),
7979
},
8080
},
@@ -86,7 +86,7 @@ func TestAccFileSystem_InvalidSizeGranularityFails(t *testing.T) {
8686
defer tt.Cleanup()
8787

8888
fileSystemName := "TestAccFileSystem_InvalidSizeGranularityFails"
89-
size := int64(25_000_000_000)
89+
sizeInGB := 250
9090

9191
resource.ParallelTest(t, resource.TestCase{
9292
PreCheck: func() { acctest.PreCheck(t) },
@@ -97,10 +97,10 @@ func TestAccFileSystem_InvalidSizeGranularityFails(t *testing.T) {
9797
Config: fmt.Sprintf(`
9898
resource "scaleway_file_filesystem" "fs" {
9999
name = "%s"
100-
size = %d
100+
size_in_gb = %d
101101
}
102-
`, fileSystemName, size),
103-
ExpectError: regexp.MustCompile("size must be greater or equal to 100000000000"),
102+
`, fileSystemName, sizeInGB),
103+
ExpectError: regexp.MustCompile("size does not respect constraint, size must be a multiple of 100000000000"),
104104
},
105105
},
106106
})

0 commit comments

Comments
 (0)