Skip to content

Commit

Permalink
chore(instance): purge nat information (#2887)
Browse files Browse the repository at this point in the history
* chore: purge nat information

* Fix

* Fix
  • Loading branch information
remyleone authored Jan 31, 2025
1 parent 75cc56b commit 9ca8ced
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 101 deletions.
5 changes: 1 addition & 4 deletions docs/resources/instance_ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ resource "scaleway_instance_ip" "server_ip" {}

The following arguments are supported:

- `type` - (Defaults to `nat`) The type of the IP (`nat`, `routed_ipv4`, `routed_ipv6`), more information in [the documentation](https://www.scaleway.com/en/docs/compute/instances/api-cli/using-routed-ips/)

~> **Important:** An IP can migrate from `nat` to `routed_ipv4` but cannot be converted back

- `type` - The type of the IP (`routed_ipv4`, `routed_ipv6`), more information in [the documentation](https://www.scaleway.com/en/docs/compute/instances/api-cli/using-routed-ips/)
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the IP should be reserved.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the IP is associated with.

Expand Down
45 changes: 7 additions & 38 deletions internal/services/instance/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package instance
import (
"context"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func ResourceIP() *schema.Resource {
Expand Down Expand Up @@ -39,22 +39,12 @@ func ResourceIP() *schema.Resource {
Description: "The IP prefix",
},
"type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The type of instance IP",
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
if i.(string) == "nat" {
return diag.Diagnostics{{
Severity: diag.Error,
Summary: "NAT IPs are not supported anymore",
Detail: "Remove explicit nat configuration, migrate to routed ips or downgrade terraform.\nLearn more about migration: https://www.scaleway.com/en/docs/compute/instances/how-to/migrate-routed-ips/",
AttributePath: path,
}}
}

return nil
},
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: "The type of instance IP",
ValidateDiagFunc: verify.ValidateEnum[instanceSDK.IPType](),
},
"reverse": {
Type: schema.TypeString,
Expand All @@ -78,23 +68,6 @@ func ResourceIP() *schema.Resource {
"organization_id": account.OrganizationIDSchema(),
"project_id": account.ProjectIDSchema(),
},
CustomizeDiff: func(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
// The only allowed change is
// nat -> routed_ipv4
if diff.HasChange("type") {
before, after := diff.GetChange("type")
oldType := instanceSDK.IPType(before.(string))
newType := instanceSDK.IPType(after.(string))

if oldType == "nat" && newType == "routed_ipv4" {
return nil
}

return diff.ForceNew("type")
}

return nil
},
}
}

Expand Down Expand Up @@ -149,10 +122,6 @@ func ResourceInstanceIPUpdate(ctx context.Context, d *schema.ResourceData, m int
req.Tags = types.ExpandUpdatedStringsPtr(d.Get("tags"))
}

if d.HasChange("type") {
req.Type = instanceSDK.IPType(d.Get("type").(string))
}

_, err = instanceAPI.UpdateIP(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down
54 changes: 0 additions & 54 deletions internal/services/instance/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,28 +340,6 @@ func ResourceServer() *schema.Resource {
},
},
},
"routed_ip_enabled": {
Type: schema.TypeBool,
Description: "If server supports routed IPs, default to true",
Optional: true,
Computed: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
if i == nil {
return nil
}
if !i.(bool) {
return diag.Diagnostics{{
Severity: diag.Error,
Summary: "NAT IPs are not supported anymore",
Detail: "Remove explicit disabling, enable it or downgrade terraform.\nLearn more about migration: https://www.scaleway.com/en/docs/compute/instances/how-to/migrate-routed-ips/",
AttributePath: path,
}}
}

return nil
},
Deprecated: "Routed IP is the default configuration, it should always be true",
},
"zone": zonal.Schema(),
"organization_id": account.OrganizationIDSchema(),
"project_id": account.ProjectIDSchema(),
Expand Down Expand Up @@ -402,7 +380,6 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
SecurityGroup: types.ExpandStringPtr(zonal.ExpandID(d.Get("security_group_id")).ID),
DynamicIPRequired: scw.BoolPtr(d.Get("enable_dynamic_ip").(bool)),
Tags: types.ExpandStrings(d.Get("tags")),
RoutedIPEnabled: types.ExpandBoolPtr(types.GetBool(d, "routed_ip_enabled")),
}

enableIPv6, ok := d.GetOk("enable_ipv6")
Expand Down Expand Up @@ -627,7 +604,6 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m i
_ = d.Set("enable_dynamic_ip", server.DynamicIPRequired)
_ = d.Set("organization_id", server.Organization)
_ = d.Set("project_id", server.Project)
_ = d.Set("routed_ip_enabled", server.RoutedIPEnabled) //nolint:staticcheck

// Image could be empty in an import context.
image := regional.ExpandID(d.Get("image").(string))
Expand Down Expand Up @@ -1037,13 +1013,6 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m
}
}

if d.HasChanges("routed_ip_enabled") {
err := ResourceInstanceServerEnableRoutedIP(ctx, d, api.API, zone, id)
if err != nil {
return diag.FromErr(err)
}
}

if d.HasChanges("root_volume.0.sbs_iops") {
warnings = append(warnings, ResourceInstanceServerUpdateRootVolumeIOPS(ctx, api, zone, id, types.ExpandUint32Ptr(d.Get("root_volume.0.sbs_iops")))...)
}
Expand Down Expand Up @@ -1312,29 +1281,6 @@ func ResourceInstanceServerMigrate(ctx context.Context, d *schema.ResourceData,
return nil
}

func ResourceInstanceServerEnableRoutedIP(ctx context.Context, d *schema.ResourceData, instanceAPI *instanceSDK.API, zone scw.Zone, id string) error {
server, err := waitForServer(ctx, instanceAPI, zone, id, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}

_, err = instanceAPI.ServerAction(&instanceSDK.ServerActionRequest{
Zone: server.Zone,
ServerID: server.ID,
Action: "enable_routed_ip",
})
if err != nil {
return fmt.Errorf("failed to enable routed ip: %w", err)
}

_, err = waitForServer(ctx, instanceAPI, zone, id, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}

return nil
}

func ResourceInstanceServerUpdateIPs(ctx context.Context, d *schema.ResourceData, instanceAPI *instanceSDK.API, zone scw.Zone, id string) error {
server, err := waitForServer(ctx, instanceAPI, zone, id, d.Timeout(schema.TimeoutUpdate))
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions internal/services/instance/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,6 @@ func TestAccServer_IPs(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "true"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "public_ips.#", "1"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "public_ips.0.id", "scaleway_instance_ip.ip1", "id"),
),
Expand All @@ -1485,7 +1484,6 @@ func TestAccServer_IPs(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "true"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "public_ips.#", "2"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "public_ips.0.id", "scaleway_instance_ip.ip1", "id"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "public_ips.1.id", "scaleway_instance_ip.ip2", "id"),
Expand All @@ -1510,7 +1508,6 @@ func TestAccServer_IPs(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "true"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "public_ips.#", "1"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "public_ips.0.id", "scaleway_instance_ip.ip2", "id"),
),
Expand Down Expand Up @@ -1588,7 +1585,6 @@ func TestAccServer_IPsRemoved(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "true"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "public_ips.#", "1"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "public_ips.0.id", "scaleway_instance_ip.main", "id"),
),
Expand All @@ -1608,7 +1604,6 @@ func TestAccServer_IPsRemoved(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
serverHasNoIPAssigned(tt, "scaleway_instance_server.main"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "true"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "public_ips.#", "0"),
),
},
Expand Down

0 comments on commit 9ca8ced

Please sign in to comment.