Skip to content

Commit 5e7adae

Browse files
committed
add flag to manage PN attachments externally
1 parent e039b5c commit 5e7adae

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

docs/resources/lb.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ The following arguments are supported:
109109
- `private_network_id` - (Required) The ID of the Private Network to attach to.
110110
- ~> **Important:** Updates to `private_network` will recreate the attachment.
111111
- `ipam_ids` - (Optional) IPAM ID of a pre-reserved IP address to assign to the Load Balancer on this Private Network.
112+
- `external_private_networks` - (Defaults to `false`) A boolean to specify whether to use [lb_private_network](../resources/lb_private_network.md).
113+
If `external_private_networks` is set to `true`, `private_network` can not be set directly in the Load Balancer.
112114
- `ssl_compatibility_level` - (Optional) Enforces minimal SSL version (in SSL/TLS offloading context). Please check [possible values](https://www.scaleway.com/en/developers/api/load-balancer/zoned-api/#path-load-balancer-create-a-load-balancer).
113115
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) of the Load Balancer.
114116
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the Project the Load Balancer is associated with.

internal/services/lb/lb.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,13 @@ func ResourceLb() *schema.Resource {
107107
Deprecated: "The resource ip will be destroyed by it's own resource. Please set this to `false`",
108108
},
109109
"private_network": {
110-
Type: schema.TypeSet,
111-
Optional: true,
112-
Computed: true,
113-
MaxItems: 8,
114-
Set: lbPrivateNetworkSetHash,
115-
Description: "List of private network to connect with your load balancer",
110+
Type: schema.TypeSet,
111+
Optional: true,
112+
Computed: true,
113+
MaxItems: 8,
114+
Set: lbPrivateNetworkSetHash,
115+
ConflictsWith: []string{"external_private_networks"},
116+
Description: "List of private network to connect with your load balancer",
116117
DiffSuppressFunc: func(k, oldValue, newValue string, _ *schema.ResourceData) bool {
117118
// Check if the key is for the 'private_network_id' attribute
118119
if strings.HasSuffix(k, "private_network_id") {
@@ -169,6 +170,13 @@ func ResourceLb() *schema.Resource {
169170
},
170171
},
171172
},
173+
"external_private_networks": {
174+
Type: schema.TypeBool,
175+
Optional: true,
176+
Default: false,
177+
Description: "This boolean determines if private network attachments should be managed externally through the `scaleway_lb_private_network` resource. When set, `private_network` must not be configured in this resource",
178+
ConflictsWith: []string{"private_network"},
179+
},
172180
"ssl_compatibility_level": {
173181
Type: schema.TypeString,
174182
Optional: true,
@@ -266,22 +274,24 @@ func resourceLbCreate(ctx context.Context, d *schema.ResourceData, m any) diag.D
266274
return diag.FromErr(err)
267275
}
268276

269-
// attach private network
270-
pnConfigs, pnExist := d.GetOk("private_network")
271-
if pnExist {
272-
pnConfigs, err := expandPrivateNetworks(pnConfigs)
273-
if err != nil {
274-
return diag.FromErr(err)
275-
}
277+
if !d.Get("external_private_networks").(bool) {
278+
// attach private network
279+
pnConfigs, pnExist := d.GetOk("private_network")
280+
if pnExist {
281+
pnConfigs, err := expandPrivateNetworks(pnConfigs)
282+
if err != nil {
283+
return diag.FromErr(err)
284+
}
276285

277-
_, err = attachLBPrivateNetworks(ctx, lbAPI, zone, pnConfigs, lb.ID, d.Timeout(schema.TimeoutCreate))
278-
if err != nil {
279-
return diag.FromErr(err)
280-
}
286+
_, err = attachLBPrivateNetworks(ctx, lbAPI, zone, pnConfigs, lb.ID, d.Timeout(schema.TimeoutCreate))
287+
if err != nil {
288+
return diag.FromErr(err)
289+
}
281290

282-
_, err = waitForLB(ctx, lbAPI, zone, lb.ID, d.Timeout(schema.TimeoutCreate))
283-
if err != nil {
284-
return diag.FromErr(err)
291+
_, err = waitForLB(ctx, lbAPI, zone, lb.ID, d.Timeout(schema.TimeoutCreate))
292+
if err != nil {
293+
return diag.FromErr(err)
294+
}
285295
}
286296
}
287297

@@ -353,7 +363,12 @@ func resourceLbRead(ctx context.Context, d *schema.ResourceData, m any) diag.Dia
353363
return diag.FromErr(err)
354364
}
355365

356-
_ = d.Set("private_network", flattenPrivateNetworkConfigs(privateNetworks))
366+
external := d.Get("external_private_networks").(bool)
367+
if !external {
368+
_ = d.Set("private_network", flattenPrivateNetworkConfigs(privateNetworks))
369+
} else {
370+
_ = d.Set("private_network", schema.NewSet(lbPrivateNetworkSetHash, []any{}))
371+
}
357372

358373
privateNetworkIDs := make([]string, 0, len(privateNetworks))
359374
for _, pn := range privateNetworks {
@@ -521,7 +536,8 @@ func resourceLbUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.D
521536
////
522537
// Attach / Detach Private Networks
523538
////
524-
if d.HasChange("private_network") {
539+
external := d.Get("external_private_networks").(bool)
540+
if !external && d.HasChange("private_network") {
525541
// check current lb stability state
526542
_, err = waitForLB(ctx, lbAPI, zone, ID, d.Timeout(schema.TimeoutUpdate))
527543
if err != nil {

internal/services/lb/private_network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func resourceLbPrivateNetworkRead(ctx context.Context, d *schema.ResourceData, m
167167

168168
_ = d.Set("private_network_id", regional.NewIDString(region, foundPN.PrivateNetworkID))
169169
_ = d.Set("lb_id", zonal.NewIDString(zone, foundPN.LB.ID))
170-
_ = d.Set("ipam_ip_ids", regional.NewRegionalIDs(region, foundPN.IpamIDs))
170+
_ = d.Set("ipam_ip_ids", regional.NewIDStrings(region, foundPN.IpamIDs))
171171
_ = d.Set("status", foundPN.Status.String())
172172
_ = d.Set("created_at", types.FlattenTime(foundPN.CreatedAt))
173173
_ = d.Set("updated_at", types.FlattenTime(foundPN.UpdatedAt))

0 commit comments

Comments
 (0)