Skip to content

Commit c5bda54

Browse files
authored
feat(instance): allow block volume resize (#779)
1 parent a9b68b4 commit c5bda54

4 files changed

+782
-2
lines changed

scaleway/resource_instance_volume.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ func resourceScalewayInstanceVolume() *schema.Resource {
4343
},
4444
"size_in_gb": {
4545
Type: schema.TypeInt,
46-
Computed: true,
4746
Optional: true,
48-
ForceNew: true,
4947
Description: "The size of the volume in gigabyte",
5048
ConflictsWith: []string{"from_snapshot_id", "from_volume_id"},
5149
},
@@ -166,6 +164,25 @@ func resourceScalewayInstanceVolumeUpdate(ctx context.Context, d *schema.Resourc
166164
}
167165
}
168166

167+
if d.HasChange("size_in_gb") {
168+
if d.Get("type") != instance.VolumeVolumeTypeBSSD.String() {
169+
return diag.FromErr(fmt.Errorf("only block volume can be resized"))
170+
}
171+
if oldSize, newSize := d.GetChange("size_in_gb"); oldSize.(int) > newSize.(int) {
172+
return diag.FromErr(fmt.Errorf("block volumes cannot be resized down"))
173+
}
174+
175+
volumeSizeInBytes := scw.Size(uint64(d.Get("size_in_gb").(int)) * gb)
176+
_, err := instanceAPI.UpdateVolume(&instance.UpdateVolumeRequest{
177+
VolumeID: id,
178+
Zone: zone,
179+
Size: &volumeSizeInBytes,
180+
}, scw.WithContext(ctx))
181+
if err != nil {
182+
return diag.FromErr(fmt.Errorf("couldn't resize volume: %s", err))
183+
}
184+
}
185+
169186
return resourceScalewayInstanceVolumeRead(ctx, d, m)
170187
}
171188

scaleway/resource_instance_volume_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scaleway
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -138,6 +139,67 @@ func TestAccScalewayInstanceVolume_DifferentNameGenerated(t *testing.T) {
138139
})
139140
}
140141

142+
func TestAccScalewayInstanceVolume_ResizeBlock(t *testing.T) {
143+
tt := NewTestTools(t)
144+
defer tt.Cleanup()
145+
resource.Test(t, resource.TestCase{
146+
PreCheck: func() { testAccPreCheck(t) },
147+
ProviderFactories: tt.ProviderFactories,
148+
CheckDestroy: testAccCheckScalewayInstanceVolumeDestroy(tt),
149+
Steps: []resource.TestStep{
150+
{
151+
Config: `
152+
resource "scaleway_instance_volume" "main" {
153+
type = "b_ssd"
154+
size_in_gb = 20
155+
}`,
156+
Check: resource.ComposeTestCheckFunc(
157+
testAccCheckScalewayInstanceVolumeExists(tt, "scaleway_instance_volume.main"),
158+
resource.TestCheckResourceAttr("scaleway_instance_volume.main", "size_in_gb", "20"),
159+
),
160+
},
161+
{
162+
Config: `
163+
resource "scaleway_instance_volume" "main" {
164+
type = "b_ssd"
165+
size_in_gb = 30
166+
}`,
167+
Check: resource.ComposeTestCheckFunc(
168+
testAccCheckScalewayInstanceVolumeExists(tt, "scaleway_instance_volume.main"),
169+
resource.TestCheckResourceAttr("scaleway_instance_volume.main", "size_in_gb", "30"),
170+
),
171+
},
172+
},
173+
})
174+
}
175+
176+
func TestAccScalewayInstanceVolume_CannotResizeBlockDown(t *testing.T) {
177+
tt := NewTestTools(t)
178+
defer tt.Cleanup()
179+
resource.Test(t, resource.TestCase{
180+
PreCheck: func() { testAccPreCheck(t) },
181+
ProviderFactories: tt.ProviderFactories,
182+
CheckDestroy: testAccCheckScalewayInstanceVolumeDestroy(tt),
183+
Steps: []resource.TestStep{
184+
{
185+
Config: `
186+
resource "scaleway_instance_volume" "main" {
187+
type = "b_ssd"
188+
size_in_gb = 20
189+
}`,
190+
},
191+
{
192+
Config: `
193+
resource "scaleway_instance_volume" "main" {
194+
type = "b_ssd"
195+
size_in_gb = 10
196+
}`,
197+
ExpectError: regexp.MustCompile("block volumes cannot be resized down"),
198+
},
199+
},
200+
})
201+
}
202+
141203
func testAccCheckScalewayInstanceVolumeExists(tt *TestTools, n string) resource.TestCheckFunc {
142204
return func(s *terraform.State) error {
143205
rs, ok := s.RootModule().Resources[n]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"name":"tf-vol-elegant-dewdney","project":"951df375-e094-4d26-97c1-ba548eeb9c42","volume_type":"b_ssd","size":20000000000}'
6+
form: {}
7+
headers:
8+
Content-Type:
9+
- application/json
10+
User-Agent:
11+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) terraform-provider/develop
12+
terraform/terraform-tests
13+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes
14+
method: POST
15+
response:
16+
body: '{"volume": {"id": "6286e8b4-6641-446a-8ea5-6ade7664b863", "name": "tf-vol-elegant-dewdney",
17+
"volume_type": "b_ssd", "export_uri": null, "organization": "951df375-e094-4d26-97c1-ba548eeb9c42",
18+
"project": "951df375-e094-4d26-97c1-ba548eeb9c42", "server": null, "size": 20000000000,
19+
"state": "available", "creation_date": "2021-02-15T11:33:33.486335+00:00", "modification_date":
20+
"2021-02-15T11:33:33.486335+00:00", "zone": "fr-par-1"}}'
21+
headers:
22+
Content-Length:
23+
- "432"
24+
Content-Security-Policy:
25+
- default-src 'none'; frame-ancestors 'none'
26+
Content-Type:
27+
- application/json
28+
Date:
29+
- Mon, 15 Feb 2021 11:33:33 GMT
30+
Location:
31+
- https://par1-cmp-prd-api01.internal.scaleway.com/volumes/6286e8b4-6641-446a-8ea5-6ade7664b863
32+
Server:
33+
- Scaleway API-Gateway
34+
Strict-Transport-Security:
35+
- max-age=63072000
36+
X-Content-Type-Options:
37+
- nosniff
38+
X-Frame-Options:
39+
- DENY
40+
X-Request-Id:
41+
- 2d78ca56-b228-4dad-9d2a-6034fbd8ce39
42+
status: 201 Created
43+
code: 201
44+
duration: ""
45+
- request:
46+
body: ""
47+
form: {}
48+
headers:
49+
User-Agent:
50+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) terraform-provider/develop
51+
terraform/terraform-tests
52+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes/6286e8b4-6641-446a-8ea5-6ade7664b863
53+
method: GET
54+
response:
55+
body: '{"volume": {"id": "6286e8b4-6641-446a-8ea5-6ade7664b863", "name": "tf-vol-elegant-dewdney",
56+
"volume_type": "b_ssd", "export_uri": null, "organization": "951df375-e094-4d26-97c1-ba548eeb9c42",
57+
"project": "951df375-e094-4d26-97c1-ba548eeb9c42", "server": null, "size": 20000000000,
58+
"state": "available", "creation_date": "2021-02-15T11:33:33.486335+00:00", "modification_date":
59+
"2021-02-15T11:33:33.486335+00:00", "zone": "fr-par-1"}}'
60+
headers:
61+
Content-Length:
62+
- "432"
63+
Content-Security-Policy:
64+
- default-src 'none'; frame-ancestors 'none'
65+
Content-Type:
66+
- application/json
67+
Date:
68+
- Mon, 15 Feb 2021 11:33:33 GMT
69+
Server:
70+
- Scaleway API-Gateway
71+
Strict-Transport-Security:
72+
- max-age=63072000
73+
X-Content-Type-Options:
74+
- nosniff
75+
X-Frame-Options:
76+
- DENY
77+
X-Request-Id:
78+
- b2c4c8fc-c15e-4f6a-861e-fba0a87571d3
79+
status: 200 OK
80+
code: 200
81+
duration: ""
82+
- request:
83+
body: ""
84+
form: {}
85+
headers:
86+
User-Agent:
87+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) terraform-provider/develop
88+
terraform/terraform-tests
89+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes/6286e8b4-6641-446a-8ea5-6ade7664b863
90+
method: GET
91+
response:
92+
body: '{"volume": {"id": "6286e8b4-6641-446a-8ea5-6ade7664b863", "name": "tf-vol-elegant-dewdney",
93+
"volume_type": "b_ssd", "export_uri": null, "organization": "951df375-e094-4d26-97c1-ba548eeb9c42",
94+
"project": "951df375-e094-4d26-97c1-ba548eeb9c42", "server": null, "size": 20000000000,
95+
"state": "available", "creation_date": "2021-02-15T11:33:33.486335+00:00", "modification_date":
96+
"2021-02-15T11:33:33.486335+00:00", "zone": "fr-par-1"}}'
97+
headers:
98+
Content-Length:
99+
- "432"
100+
Content-Security-Policy:
101+
- default-src 'none'; frame-ancestors 'none'
102+
Content-Type:
103+
- application/json
104+
Date:
105+
- Mon, 15 Feb 2021 11:33:34 GMT
106+
Server:
107+
- Scaleway API-Gateway
108+
Strict-Transport-Security:
109+
- max-age=63072000
110+
X-Content-Type-Options:
111+
- nosniff
112+
X-Frame-Options:
113+
- DENY
114+
X-Request-Id:
115+
- 564bc69c-a5d2-4f58-b646-601aeae334a0
116+
status: 200 OK
117+
code: 200
118+
duration: ""
119+
- request:
120+
body: ""
121+
form: {}
122+
headers:
123+
User-Agent:
124+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) terraform-provider/develop
125+
terraform/terraform-tests
126+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes/6286e8b4-6641-446a-8ea5-6ade7664b863
127+
method: GET
128+
response:
129+
body: '{"volume": {"id": "6286e8b4-6641-446a-8ea5-6ade7664b863", "name": "tf-vol-elegant-dewdney",
130+
"volume_type": "b_ssd", "export_uri": null, "organization": "951df375-e094-4d26-97c1-ba548eeb9c42",
131+
"project": "951df375-e094-4d26-97c1-ba548eeb9c42", "server": null, "size": 20000000000,
132+
"state": "available", "creation_date": "2021-02-15T11:33:33.486335+00:00", "modification_date":
133+
"2021-02-15T11:33:33.486335+00:00", "zone": "fr-par-1"}}'
134+
headers:
135+
Content-Length:
136+
- "432"
137+
Content-Security-Policy:
138+
- default-src 'none'; frame-ancestors 'none'
139+
Content-Type:
140+
- application/json
141+
Date:
142+
- Mon, 15 Feb 2021 11:33:35 GMT
143+
Server:
144+
- Scaleway API-Gateway
145+
Strict-Transport-Security:
146+
- max-age=63072000
147+
X-Content-Type-Options:
148+
- nosniff
149+
X-Frame-Options:
150+
- DENY
151+
X-Request-Id:
152+
- ca0768d8-1295-4615-b385-1d420877f588
153+
status: 200 OK
154+
code: 200
155+
duration: ""
156+
- request:
157+
body: ""
158+
form: {}
159+
headers:
160+
User-Agent:
161+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) terraform-provider/develop
162+
terraform/terraform-tests
163+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes/6286e8b4-6641-446a-8ea5-6ade7664b863
164+
method: GET
165+
response:
166+
body: '{"volume": {"id": "6286e8b4-6641-446a-8ea5-6ade7664b863", "name": "tf-vol-elegant-dewdney",
167+
"volume_type": "b_ssd", "export_uri": null, "organization": "951df375-e094-4d26-97c1-ba548eeb9c42",
168+
"project": "951df375-e094-4d26-97c1-ba548eeb9c42", "server": null, "size": 20000000000,
169+
"state": "available", "creation_date": "2021-02-15T11:33:33.486335+00:00", "modification_date":
170+
"2021-02-15T11:33:33.486335+00:00", "zone": "fr-par-1"}}'
171+
headers:
172+
Content-Length:
173+
- "432"
174+
Content-Security-Policy:
175+
- default-src 'none'; frame-ancestors 'none'
176+
Content-Type:
177+
- application/json
178+
Date:
179+
- Mon, 15 Feb 2021 11:33:35 GMT
180+
Server:
181+
- Scaleway API-Gateway
182+
Strict-Transport-Security:
183+
- max-age=63072000
184+
X-Content-Type-Options:
185+
- nosniff
186+
X-Frame-Options:
187+
- DENY
188+
X-Request-Id:
189+
- 44a41308-8b04-4256-b7d0-b3918f134276
190+
status: 200 OK
191+
code: 200
192+
duration: ""
193+
- request:
194+
body: ""
195+
form: {}
196+
headers:
197+
User-Agent:
198+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) terraform-provider/develop
199+
terraform/terraform-tests
200+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes/6286e8b4-6641-446a-8ea5-6ade7664b863
201+
method: DELETE
202+
response:
203+
body: ""
204+
headers:
205+
Content-Security-Policy:
206+
- default-src 'none'; frame-ancestors 'none'
207+
Content-Type:
208+
- application/json
209+
Date:
210+
- Mon, 15 Feb 2021 11:33:35 GMT
211+
Server:
212+
- Scaleway API-Gateway
213+
Strict-Transport-Security:
214+
- max-age=63072000
215+
X-Content-Type-Options:
216+
- nosniff
217+
X-Frame-Options:
218+
- DENY
219+
X-Request-Id:
220+
- 85c85e3b-d552-4b43-b9a5-aad1bbd0b698
221+
status: 204 No Content
222+
code: 204
223+
duration: ""
224+
- request:
225+
body: ""
226+
form: {}
227+
headers:
228+
User-Agent:
229+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) terraform-provider/develop
230+
terraform/terraform-tests
231+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/volumes/6286e8b4-6641-446a-8ea5-6ade7664b863
232+
method: GET
233+
response:
234+
body: '{"type": "unknown_resource", "message": "Volume ''6286e8b4-6641-446a-8ea5-6ade7664b863''
235+
not found."}'
236+
headers:
237+
Content-Length:
238+
- "99"
239+
Content-Security-Policy:
240+
- default-src 'none'; frame-ancestors 'none'
241+
Content-Type:
242+
- application/json
243+
Date:
244+
- Mon, 15 Feb 2021 11:33:36 GMT
245+
Server:
246+
- Scaleway API-Gateway
247+
Strict-Transport-Security:
248+
- max-age=63072000
249+
X-Content-Type-Options:
250+
- nosniff
251+
X-Frame-Options:
252+
- DENY
253+
X-Request-Id:
254+
- e8501d5b-74b4-4782-ab29-6219b991c6cf
255+
status: 404 Not Found
256+
code: 404
257+
duration: ""

0 commit comments

Comments
 (0)