Skip to content

Commit bf944eb

Browse files
committed
feat(instance_server): prevent usage of routed_ip_enabled as false
1 parent 4863479 commit bf944eb

File tree

6 files changed

+625
-9540
lines changed

6 files changed

+625
-9540
lines changed

internal/services/instance/server.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func ResourceServer() *schema.Resource {
229229
"ipv6_prefix_length": {
230230
Type: schema.TypeInt,
231231
Computed: true,
232+
Deprecated: "Please use a scaleway_instance_ip with a `routed_ipv6` type",
232233
Description: "The IPv6 prefix length routed to the server.",
233234
},
234235
"enable_dynamic_ip": {
@@ -338,9 +339,25 @@ func ResourceServer() *schema.Resource {
338339
},
339340
"routed_ip_enabled": {
340341
Type: schema.TypeBool,
341-
Description: "If server supports routed IPs, default to true if public_ips is used",
342+
Description: "If server supports routed IPs, default to true",
342343
Optional: true,
343344
Computed: true,
345+
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
346+
if i == nil {
347+
return nil
348+
}
349+
if i.(bool) == false {
350+
return diag.Diagnostics{{
351+
Severity: diag.Error,
352+
Summary: "NAT IPs are not supported anymore",
353+
Detail: "Remove explicit disabling, enable it or downgrade terraform.",
354+
AttributePath: cty.GetAttrPath("routed_ip_enabled"),
355+
}}
356+
}
357+
358+
return nil
359+
},
360+
Deprecated: "Routed IP is the default configuration, it should always be true",
344361
},
345362
"zone": zonal.Schema(),
346363
"organization_id": account.OrganizationIDSchema(),

internal/services/instance/server_test.go

Lines changed: 9 additions & 276 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package instance_test
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
76
"regexp"
87
"strings"
98
"testing"
109

1110
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1311
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1412
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1513
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
1614
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1715
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
18-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
19-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/provider"
2016
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance"
2117
instancechecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance/testfuncs"
22-
"github.com/stretchr/testify/require"
2318
)
2419

2520
func TestAccServer_Minimal1(t *testing.T) {
@@ -731,36 +726,34 @@ func TestAccServer_Ipv6(t *testing.T) {
731726
Steps: []resource.TestStep{
732727
{
733728
Config: `
729+
resource "scaleway_instance_ip" "ip" {
730+
type = "routed_ipv6"
731+
}
732+
734733
resource "scaleway_instance_server" "server01" {
735734
image = "ubuntu_focal"
736-
type = "DEV1-S"
737-
enable_ipv6 = true
738-
routed_ip_enabled = false
735+
type = "PLAY2-PICO"
736+
ip_ids = [scaleway_instance_ip.ip.id]
739737
}
740738
`,
741739
Check: resource.ComposeTestCheckFunc(
742740
isServerPresent(tt, "scaleway_instance_server.server01"),
743-
// enable_ipv6, ipv6_address and ipv6_gateway are marked as deprecated
744-
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "enable_ipv6", "true"),
745-
acctest.CheckResourceAttrIPv6("scaleway_instance_server.server01", "ipv6_address"),
746-
acctest.CheckResourceAttrIPv6("scaleway_instance_server.server01", "ipv6_gateway"),
747-
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_prefix_length", "64"),
741+
acctest.CheckResourceAttrIPv6("scaleway_instance_server.server01", "public_ips.0.address"),
748742
),
749743
},
750744
{
751745
Config: `
752746
resource "scaleway_instance_server" "server01" {
753747
image = "ubuntu_focal"
754-
type = "DEV1-S"
755-
enable_ipv6 = false
756-
routed_ip_enabled = false
748+
type = "PLAY2-PICO"
757749
}
758750
`,
759751
Check: resource.ComposeTestCheckFunc(
760752
isServerPresent(tt, "scaleway_instance_server.server01"),
761753
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_address", ""),
762754
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_gateway", ""),
763755
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_prefix_length", "0"),
756+
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "public_ips.#", "0"),
764757
),
765758
},
766759
},
@@ -1496,94 +1489,6 @@ func serverIDsAreDifferent(nameFirst, nameSecond string) resource.TestCheckFunc
14961489
}
14971490
}
14981491

1499-
func TestAccServer_RoutedIPEnable(t *testing.T) {
1500-
tt := acctest.NewTestTools(t)
1501-
defer tt.Cleanup()
1502-
resource.ParallelTest(t, resource.TestCase{
1503-
PreCheck: func() { acctest.PreCheck(t) },
1504-
ProviderFactories: tt.ProviderFactories,
1505-
CheckDestroy: instancechecks.IsServerDestroyed(tt),
1506-
Steps: []resource.TestStep{
1507-
{
1508-
Config: `
1509-
resource "scaleway_instance_server" "main" {
1510-
name = "tf-tests-instance-server-routedip"
1511-
image = "ubuntu_jammy"
1512-
type = "PRO2-XXS"
1513-
state = "stopped"
1514-
routed_ip_enabled = false
1515-
}`,
1516-
Check: resource.ComposeTestCheckFunc(
1517-
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
1518-
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "false"),
1519-
),
1520-
},
1521-
{
1522-
Config: `
1523-
resource "scaleway_instance_server" "main" {
1524-
name = "tf-tests-instance-server-routedip"
1525-
image = "ubuntu_jammy"
1526-
type = "PRO2-XXS"
1527-
routed_ip_enabled = true
1528-
state = "stopped"
1529-
}`,
1530-
Check: resource.ComposeTestCheckFunc(
1531-
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
1532-
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "true"),
1533-
),
1534-
},
1535-
},
1536-
})
1537-
}
1538-
1539-
func TestAccServer_RoutedIPEnableWithIP(t *testing.T) {
1540-
tt := acctest.NewTestTools(t)
1541-
defer tt.Cleanup()
1542-
resource.ParallelTest(t, resource.TestCase{
1543-
PreCheck: func() { acctest.PreCheck(t) },
1544-
ProviderFactories: tt.ProviderFactories,
1545-
CheckDestroy: instancechecks.IsServerDestroyed(tt),
1546-
Steps: []resource.TestStep{
1547-
{
1548-
Config: `
1549-
resource "scaleway_instance_ip" "main" {
1550-
type = "nat"
1551-
}
1552-
1553-
resource "scaleway_instance_server" "main" {
1554-
name = "tf-tests-instance-server-routedip-enable-with-ip"
1555-
ip_id = scaleway_instance_ip.main.id
1556-
image = "ubuntu_jammy"
1557-
type = "PRO2-XXS"
1558-
state = "stopped"
1559-
routed_ip_enabled = false
1560-
}`,
1561-
Check: resource.ComposeTestCheckFunc(
1562-
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
1563-
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "false"),
1564-
),
1565-
},
1566-
{
1567-
Config: `
1568-
resource "scaleway_instance_ip" "main" {}
1569-
1570-
resource "scaleway_instance_server" "main" {
1571-
name = "tf-tests-instance-server-routedip-enable-with-ip"
1572-
ip_id = scaleway_instance_ip.main.id
1573-
image = "ubuntu_jammy"
1574-
type = "PRO2-XXS"
1575-
state = "stopped"
1576-
routed_ip_enabled = true
1577-
}`,
1578-
Check: resource.ComposeTestCheckFunc(
1579-
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
1580-
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "true"),
1581-
),
1582-
},
1583-
},
1584-
})
1585-
}
1586-
15871492
func TestAccServer_IPs(t *testing.T) {
15881493
tt := acctest.NewTestTools(t)
15891494
defer tt.Cleanup()
@@ -1762,178 +1667,6 @@ func TestAccServer_IPsRemoved(t *testing.T) {
17621667
})
17631668
}
17641669

1765-
func TestAccServer_IPMigrate(t *testing.T) {
1766-
tt := acctest.NewTestTools(t)
1767-
defer tt.Cleanup()
1768-
1769-
ctx := context.Background()
1770-
// This come from iam_policy tests to use policies in tests
1771-
project, iamAPIKey, terminateFakeSideProject, err := acctest.CreateFakeIAMManager(tt)
1772-
require.NoError(t, err)
1773-
1774-
// This is the provider factory that will use the temporary project
1775-
providerFactories := acctest.FakeSideProjectProviders(ctx, tt, project, iamAPIKey)
1776-
1777-
// Goal of this test is to check that an IP will not get detached if moved from ip_id to ip_ids
1778-
// Between the two steps we will create an API key that cannot update the IP,
1779-
// it should fail if the provider tries to detach
1780-
temporaryAccessKey := ""
1781-
temporarySecretKey := ""
1782-
customProviderFactory := map[string]func() (*schema.Provider, error){
1783-
"scaleway": func() (*schema.Provider, error) {
1784-
m, err := meta.NewMeta(context.Background(), &meta.Config{
1785-
ProviderSchema: nil,
1786-
TerraformVersion: "terraform-tests",
1787-
HTTPClient: tt.Meta.HTTPClient(),
1788-
ForceAccessKey: temporaryAccessKey,
1789-
ForceSecretKey: temporarySecretKey,
1790-
})
1791-
if err != nil {
1792-
return nil, err
1793-
}
1794-
return provider.Provider(&provider.Config{Meta: m})(), nil
1795-
},
1796-
}
1797-
1798-
resource.ParallelTest(t, resource.TestCase{
1799-
PreCheck: func() { acctest.PreCheck(t) },
1800-
CheckDestroy: resource.ComposeAggregateTestCheckFunc(
1801-
func(_ *terraform.State) error {
1802-
return terminateFakeSideProject()
1803-
},
1804-
instancechecks.IsServerDestroyed(tt),
1805-
),
1806-
Steps: []resource.TestStep{
1807-
{
1808-
ProviderFactories: providerFactories,
1809-
Config: fmt.Sprintf(`
1810-
resource "scaleway_instance_ip" "ip" {
1811-
type = "nat"
1812-
}
1813-
1814-
resource "scaleway_instance_server" "main" {
1815-
ip_id = scaleway_instance_ip.ip.id
1816-
image = "ubuntu_jammy"
1817-
type = "PRO2-XXS"
1818-
state = "stopped"
1819-
routed_ip_enabled = false
1820-
}
1821-
1822-
resource "scaleway_iam_application" "app" {
1823-
name = "tf_tests_instance_server_ipmigrate"
1824-
}
1825-
1826-
resource "scaleway_iam_policy" "policy" {
1827-
application_id = scaleway_iam_application.app.id
1828-
rule {
1829-
permission_set_names = ["InstancesReadOnly"]
1830-
organization_id = %[1]q
1831-
}
1832-
rule {
1833-
permission_set_names = ["ProjectReadOnly", "IAMReadOnly"]
1834-
organization_id = %[1]q
1835-
}
1836-
}
1837-
1838-
resource "scaleway_iam_api_key" "key" {
1839-
application_id = scaleway_iam_application.app.id
1840-
}`, project.OrganizationID),
1841-
Check: resource.ComposeTestCheckFunc(
1842-
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
1843-
resource.TestCheckResourceAttr("scaleway_instance_server.main", "routed_ip_enabled", "false"),
1844-
resource.TestCheckResourceAttr("scaleway_instance_server.main", "public_ips.#", "1"),
1845-
func(s *terraform.State) error {
1846-
rs, ok := s.RootModule().Resources["scaleway_iam_api_key.key"]
1847-
if !ok {
1848-
return fmt.Errorf("resource was not found: %s", "scaleway_iam_api_key.key")
1849-
}
1850-
temporaryAccessKey = rs.Primary.Attributes["access_key"]
1851-
temporarySecretKey = rs.Primary.Attributes["secret_key"]
1852-
1853-
return nil
1854-
},
1855-
),
1856-
},
1857-
{
1858-
ProviderFactories: customProviderFactory,
1859-
// With migration supported, this should make no changes
1860-
// This is validated because we cannot add a nat IP to ip_ids
1861-
// This would fail if not moved from ip_id to ip_ids
1862-
Config: fmt.Sprintf(`
1863-
resource "scaleway_instance_ip" "ip" {
1864-
type = "nat"
1865-
}
1866-
1867-
resource "scaleway_instance_server" "main" {
1868-
ip_ids = [scaleway_instance_ip.ip.id]
1869-
image = "ubuntu_jammy"
1870-
type = "PRO2-XXS"
1871-
state = "stopped"
1872-
}
1873-
1874-
resource "scaleway_iam_application" "app" {
1875-
name = "tf_tests_instance_server_ipmigrate"
1876-
}
1877-
1878-
resource "scaleway_iam_policy" "policy" {
1879-
application_id = scaleway_iam_application.app.id
1880-
rule {
1881-
permission_set_names = ["InstancesReadOnly"]
1882-
organization_id = %[1]q
1883-
}
1884-
rule {
1885-
permission_set_names = ["ProjectReadOnly", "IAMReadOnly"]
1886-
organization_id = %[1]q
1887-
}
1888-
}
1889-
1890-
resource "scaleway_iam_api_key" "key" {
1891-
application_id = scaleway_iam_application.app.id
1892-
}`, project.OrganizationID),
1893-
Check: resource.ComposeTestCheckFunc(
1894-
arePrivateNICsPresent(tt, "scaleway_instance_server.main"),
1895-
resource.TestCheckResourceAttr("scaleway_instance_server.main", "public_ips.#", "1"),
1896-
),
1897-
},
1898-
{
1899-
ProviderFactories: tt.ProviderFactories,
1900-
// Last step with default api key to remove resources
1901-
Config: fmt.Sprintf(`
1902-
resource "scaleway_instance_ip" "ip" {
1903-
type = "nat"
1904-
}
1905-
1906-
resource "scaleway_instance_server" "main" {
1907-
ip_ids = [scaleway_instance_ip.ip.id]
1908-
image = "ubuntu_jammy"
1909-
type = "PRO2-XXS"
1910-
state = "stopped"
1911-
}
1912-
1913-
resource "scaleway_iam_application" "app" {
1914-
name = "tf_tests_instance_server_ipmigrate"
1915-
}
1916-
1917-
resource "scaleway_iam_policy" "policy" {
1918-
application_id = scaleway_iam_application.app.id
1919-
rule {
1920-
permission_set_names = ["InstancesReadOnly"]
1921-
organization_id = %[1]q
1922-
}
1923-
rule {
1924-
permission_set_names = ["ProjectReadOnly", "IAMReadOnly"]
1925-
organization_id = %[1]q
1926-
}
1927-
}
1928-
1929-
resource "scaleway_iam_api_key" "key" {
1930-
application_id = scaleway_iam_application.app.id
1931-
}`, project.OrganizationID),
1932-
},
1933-
},
1934-
})
1935-
}
1936-
19371670
func TestAccServer_BlockExternal(t *testing.T) {
19381671
tt := acctest.NewTestTools(t)
19391672
defer tt.Cleanup()

0 commit comments

Comments
 (0)