Skip to content

Commit f305d25

Browse files
authored
fix(LB): schema type and add migration type (#1428)
1 parent 5b2d4df commit f305d25

File tree

5 files changed

+2336
-59
lines changed

5 files changed

+2336
-59
lines changed

docs/data-sources/lb.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
page_title: "Scaleway: scaleway_lb"
33
description: |-
4-
Gets information about a Load Balancer.
4+
Gets information about a Load Balancer.
55
---
66

77
# scaleway_lb
@@ -25,21 +25,21 @@ data "scaleway_lb" "by_id" {
2525
## Argument Reference
2626

2727
- `name` - (Optional) The IP address.
28-
Only one of `name` and `lb_id` should be specified.
29-
30-
- `lb_id` - (Optional) The ID.
31-
Only one of `ip_address` and `lb_id` should be specified.
3228

33-
- `zone` - (Optional) (Defaults to [provider](../index.md#zone) `region`) The [region](../guides/regions_and_zones.md#zones) in which the LB exists.
34-
35-
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the LB is associated with.
29+
- `ip_id` - (Optional) The load balancer IP ID.
3630

3731
## Attributes Reference
3832

3933
In addition to all arguments above, the following attributes are exported:
4034

4135
- `id` - The ID of the load-balancer.
42-
- `ip_address` - The load-balancer public IP Address
43-
- `organization_id` - The organization ID the load-balancer is associated with.
44-
- `tags` - (Optional) The tags associated with the load-balancers.
45-
- `type` - (Required) The type of the load-balancer.
36+
37+
- `ip_address` - The load-balancer public IP Address.
38+
39+
- `type` - The type of the load-balancer.
40+
41+
- `tags` - The tags associated with the load-balancers.
42+
43+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the LB is associated with.
44+
45+
- `zone` - (Defaults to [provider](../index.md#zone) `region`) The [region](../guides/regions_and_zones.md#zones) in which the LB exists.

docs/resources/lb.md

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ For more information, see [the documentation](https://developers.scaleway.com/en
1414
### Basic
1515

1616
```hcl
17-
resource "scaleway_lb_ip" "ip" {
17+
resource "scaleway_lb_ip" "main" {
1818
zone = "fr-par-1"
1919
}
2020
2121
resource "scaleway_lb" "base" {
22-
ip_id = scaleway_lb_ip.ip.id
23-
zone = scaleway_lb_ip.ip.zone
22+
ip_id = scaleway_lb_ip.main.id
23+
zone = scaleway_lb_ip.main.zone
2424
type = "LB-S"
2525
}
2626
```
@@ -70,22 +70,22 @@ resource "scaleway_instance_server" "main" {
7070
}
7171
7272
### IP for LB IP
73-
resource scaleway_lb_ip ip01 {
73+
resource scaleway_lb_ip main {
7474
}
7575
7676
### Scaleway Private Network
77-
resource scaleway_vpc_private_network "static" {
77+
resource scaleway_vpc_private_network "main" {
7878
name = "private network with static config"
7979
}
8080
8181
### Scaleway Load Balancer
82-
resource scaleway_lb lb01 {
83-
ip_id = scaleway_lb_ip.ip01.id
84-
name = "test-lb-with-private-network-configs"
82+
resource scaleway_lb main {
83+
ip_id = scaleway_lb_ip.main.id
84+
name = "MyTest"
8585
type = "LB-S"
8686
8787
private_network {
88-
private_network_id = scaleway_vpc_private_network.static.id
88+
private_network_id = scaleway_vpc_private_network.main.id
8989
static_config = ["172.16.0.100", "172.16.0.101"]
9090
}
9191
@@ -102,13 +102,11 @@ resource scaleway_lb lb01 {
102102

103103
The following arguments are supported:
104104

105-
- `ip_id` - (Required) The ID of the associated IP. See below.
106-
107-
~> **Important:** Updates to `ip_id` will recreate the load-balancer.
105+
- `ip_id` - (Required) The ID of the associated LB IP. See below.
108106

109-
- `type` - (Required) The type of the load-balancer.
107+
~> **Important:** Updates to `ip_id` will not recreate the load-balancer.
110108

111-
~> **Important:** Updates to `type` will recreate the load-balancer.
109+
- `type` - (Required) The type of the load-balancer. Please check the [migration section](#migration) to upgrade the type
112110

113111
- `name` - (Optional) The name of the load-balancer.
114112

@@ -130,6 +128,14 @@ In addition to all arguments above, the following attributes are exported:
130128

131129
~> **Important:** `release_ip` will not be supported. This prevents the destruction of the IP from releasing a LBs.
132130
The `resource_lb_ip` will be the only resource that handles those IPs.
131+
132+
## Migration
133+
134+
In order to migrate to other types you can check the migration up or down via our CLI `scw lb lb-types list`.
135+
this change will not recreate your Load Balancer.
136+
137+
Please check our [documentation](https://developers.scaleway.com/en/products/lb/zoned_api/#post-355592) for further details
138+
133139
## IP ID
134140

135141
Since v1.15.0, `ip_id` is a required field. This means that now a separate `scaleway_lb_ip` is required.
@@ -139,7 +145,7 @@ When upgrading to v1.15.0, you will need to create a new `scaleway_lb_ip` resour
139145
For instance, if you had the following:
140146

141147
```hcl
142-
resource "scaleway_lb" "base" {
148+
resource "scaleway_lb" "main" {
143149
zone = "fr-par-1"
144150
type = "LB-S"
145151
}
@@ -148,11 +154,11 @@ resource "scaleway_lb" "base" {
148154
You will need to update it to:
149155

150156
```hcl
151-
resource "scaleway_lb_ip" "ip" {
157+
resource "scaleway_lb_ip" "main" {
152158
}
153159
154-
resource "scaleway_lb" "base" {
155-
ip_id = scaleway_lb_ip.ip.id
160+
resource "scaleway_lb" "main" {
161+
ip_id = scaleway_lb_ip.main.id
156162
zone = "fr-par-1"
157163
type = "LB-S"
158164
release_ip = false
@@ -162,29 +168,31 @@ resource "scaleway_lb" "base" {
162168
## Private Network with static config
163169

164170
```hcl
165-
resource scaleway_lb_ip ip01 {
171+
resource scaleway_lb_ip main {
166172
}
167173
168-
resource scaleway_vpc_private_network pnLB01 {
169-
name = "pn-with-lb-static"
174+
resource scaleway_vpc_private_network main {
175+
name = "MyTest"
170176
}
171177
172-
resource scaleway_lb lb01 {
173-
ip_id = scaleway_lb_ip.ip01.id
174-
name = "test-lb-with-pn-static-2"
178+
resource scaleway_lb main {
179+
ip_id = scaleway_lb_ip.main.id
180+
name = "MyTest"
175181
type = "LB-S"
176182
release_ip = false
177183
private_network {
178-
private_network_id = scaleway_vpc_private_network.pnLB01.id
184+
private_network_id = scaleway_vpc_private_network.main.id
179185
static_config = ["172.16.0.100", "172.16.0.101"]
180186
}
181187
}
182188
```
183189

184-
~> **Important:** Updates to `private_network` will recreate the attachment.
190+
## Attributes Reference
185191

186192
- `private_network_id` - (Required) The ID of the Private Network to associate.
187193

194+
- ~> **Important:** Updates to `private_network` will recreate the attachment.
195+
188196
- `static_config` - (Optional) Define two local ip address of your choice for each load balancer instance. See below.
189197

190198
- `dhcp_config` - (Optional) Set to true if you want to let DHCP assign IP addresses. See below.
@@ -193,32 +201,13 @@ resource scaleway_lb lb01 {
193201

194202
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the private network was created.
195203

196-
## Attributes Reference
197-
198-
In addition to all arguments above, the following attributes are exported:
199-
200-
- `status` - The Private Network attachment status
201-
202-
And before running `terraform apply` you will need to import the IP with:
203-
204-
```bash
205-
$ terraform import scaleway_lb_ip.ip fr-par/11111111-1111-1111-1111-111111111111
206-
```
207-
208-
The IP ID can either be found in the console, or you can run:
209-
210-
```bash
211-
$ terraform state show scaleway_lb.base
212-
```
213-
214-
and look for `ip_id`.
215204

216205
## Import
217206

218207
Load-Balancer can be imported using the `{zone}/{id}`, e.g.
219208

220209
```bash
221-
$ terraform import scaleway_lb.lb01 fr-par-1/11111111-1111-1111-1111-111111111111
210+
$ terraform import scaleway_lb.main fr-par-1/11111111-1111-1111-1111-111111111111
222211
```
223212

224213
Be aware that you will also need to import the `scaleway_lb_ip` resource.

scaleway/resource_lb.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func resourceScalewayLb() *schema.Resource {
4848
"type": {
4949
Type: schema.TypeString,
5050
Required: true,
51-
ForceNew: true,
5251
DiffSuppressFunc: diffSuppressFuncIgnoreCase,
5352
Description: "The type of load-balancer you want to create",
5453
},
@@ -66,6 +65,7 @@ func resourceScalewayLb() *schema.Resource {
6665
ForceNew: true,
6766
Description: "The load-balance public IP ID",
6867
DiffSuppressFunc: diffSuppressFuncLocality,
68+
ValidateFunc: validationUUIDorUUIDWithLocality(),
6969
},
7070
"ip_address": {
7171
Type: schema.TypeString,
@@ -229,6 +229,25 @@ func resourceScalewayLbUpdate(ctx context.Context, d *schema.ResourceData, meta
229229
return diag.FromErr(err)
230230
}
231231

232+
if d.HasChange("type") {
233+
lbType := d.Get("type").(string)
234+
migrateReq := &lbSDK.ZonedAPIMigrateLBRequest{
235+
Zone: zone,
236+
LBID: ID,
237+
Type: lbType,
238+
}
239+
240+
lb, err := lbAPI.MigrateLB(migrateReq, scw.WithContext(ctx))
241+
if err != nil {
242+
diag.FromErr(fmt.Errorf("couldn't migrate load balancer on type: %s. error: %w", lb.Type, err))
243+
}
244+
245+
_, err = waitForLB(ctx, lbAPI, zone, lb.ID, d.Timeout(schema.TimeoutUpdate))
246+
if err != nil {
247+
return diag.FromErr(err)
248+
}
249+
}
250+
232251
if d.HasChanges("name", "tags") {
233252
req := &lbSDK.ZonedAPIUpdateLBRequest{
234253
Zone: zone,

scaleway/resource_lb_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,117 @@ func testSweepLB(_ string) error {
6161
})
6262
}
6363

64+
func TestAccScalewayLbLb_Migrate(t *testing.T) {
65+
tt := NewTestTools(t)
66+
defer tt.Cleanup()
67+
68+
lbID := ""
69+
70+
resource.ParallelTest(t, resource.TestCase{
71+
PreCheck: func() { testAccPreCheck(t) },
72+
ProviderFactories: tt.ProviderFactories,
73+
CheckDestroy: testAccCheckScalewayLbDestroy(tt),
74+
Steps: []resource.TestStep{
75+
{
76+
Config: `
77+
### IP for LB IP
78+
resource scaleway_lb_ip main {
79+
}
80+
81+
resource scaleway_lb main {
82+
ip_id = scaleway_lb_ip.main.id
83+
name = "test-lb-migration"
84+
type = "LB-S"
85+
tags = ["basic", "tag2"]
86+
}
87+
`,
88+
Check: resource.ComposeTestCheckFunc(
89+
testAccCheckScalewayLbExists(tt, "scaleway_lb.main"),
90+
func(s *terraform.State) error {
91+
rs, ok := s.RootModule().Resources["scaleway_lb.main"]
92+
if !ok {
93+
return fmt.Errorf("resource not found: %s", "scaleway_lb.main")
94+
}
95+
lbID = rs.Primary.ID
96+
return nil
97+
},
98+
resource.TestCheckResourceAttr("scaleway_lb.main", "type", "LB-S"),
99+
resource.TestCheckResourceAttr("scaleway_lb.main", "name", "test-lb-migration"),
100+
resource.TestCheckResourceAttr("scaleway_lb.main", "tags.#", "2"),
101+
),
102+
},
103+
{
104+
Config: `
105+
### IP for LB IP
106+
resource scaleway_lb_ip main {
107+
}
108+
109+
resource scaleway_lb main {
110+
ip_id = scaleway_lb_ip.main.id
111+
name = "test-lb-migrate-lb-gp-m"
112+
type = "LB-GP-M"
113+
tags = ["migration"]
114+
}
115+
`,
116+
Check: resource.ComposeTestCheckFunc(
117+
testAccCheckScalewayLbExists(tt, "scaleway_lb.main"),
118+
func(s *terraform.State) error {
119+
rs, ok := s.RootModule().Resources["scaleway_lb.main"]
120+
if !ok {
121+
return fmt.Errorf("resource not found: %s", "scaleway_lb.main")
122+
}
123+
if rs.Primary.ID != lbID {
124+
return fmt.Errorf("LB id has changed")
125+
}
126+
return nil
127+
},
128+
resource.TestCheckResourceAttr("scaleway_lb.main", "type", "LB-GP-M"),
129+
resource.TestCheckResourceAttr("scaleway_lb.main", "name", "test-lb-migrate-lb-gp-m"),
130+
resource.TestCheckResourceAttr("scaleway_lb.main", "tags.#", "1"),
131+
),
132+
},
133+
{
134+
Config: `
135+
### IP for LB IP
136+
resource scaleway_lb_ip main {
137+
}
138+
139+
resource scaleway_lb main {
140+
ip_id = scaleway_lb_ip.main.id
141+
name = "test-lb-migrate-lb-gp-m"
142+
type = "LB-GP-M"
143+
}
144+
`,
145+
Check: resource.ComposeTestCheckFunc(
146+
testAccCheckScalewayLbExists(tt, "scaleway_lb.main"),
147+
resource.TestCheckResourceAttr("scaleway_lb.main", "type", "LB-GP-M"),
148+
resource.TestCheckResourceAttr("scaleway_lb.main", "name", "test-lb-migrate-lb-gp-m"),
149+
resource.TestCheckResourceAttr("scaleway_lb.main", "tags.#", "0"),
150+
),
151+
},
152+
{
153+
Config: `
154+
### IP for LB IP
155+
resource scaleway_lb_ip main {
156+
}
157+
158+
resource scaleway_lb main {
159+
ip_id = scaleway_lb_ip.main.id
160+
name = "test-lb-migrate-down"
161+
type = "LB-S"
162+
}
163+
`,
164+
Check: resource.ComposeTestCheckFunc(
165+
testAccCheckScalewayLbExists(tt, "scaleway_lb.main"),
166+
resource.TestCheckResourceAttr("scaleway_lb.main", "type", "LB-S"),
167+
resource.TestCheckResourceAttr("scaleway_lb.main", "name", "test-lb-migrate-down"),
168+
resource.TestCheckResourceAttr("scaleway_lb.main", "tags.#", "0"),
169+
),
170+
},
171+
},
172+
})
173+
}
174+
64175
func TestAccScalewayLbLb_WithIP(t *testing.T) {
65176
tt := NewTestTools(t)
66177
defer tt.Cleanup()

0 commit comments

Comments
 (0)