Handle network prefixes correctly (#753)

* wip

* fix: corrected testcase

* fix: change prefix to workaround bug in current environment

* fix: made acceptance test more robust for randomized nameserver order

* fix: updated documentation

* fix: linter issue

* fix: acceptance test still relied on a fixed order of nameservers

* fix: fixed import acceptance testcase
This commit is contained in:
Rüdiger Schmitz 2025-04-04 08:26:03 +02:00 committed by GitHub
parent 6d49b2ff81
commit c7ed274647
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 280 additions and 68 deletions

View file

@ -2,7 +2,9 @@ package iaas_test
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
@ -12,8 +14,10 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
)
@ -31,9 +35,10 @@ var networkResource = map[string]string{
"ipv4_prefix_length": "24",
"nameserver0": "1.2.3.4",
"nameserver1": "5.6.7.8",
"ipv4_gateway": "10.1.2.1",
"ipv4_prefix": "10.1.2.1/24",
"ipv4_gateway": "10.2.2.1",
"ipv4_prefix": "10.2.2.0/24",
"routed": "false",
"name_updated": fmt.Sprintf("acc-test-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)),
}
var networkAreaResource = map[string]string{
@ -444,6 +449,117 @@ func testAccImageConfig(name string) string {
)
}
func TestAccNetwork(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckNetworkDestroy,
Steps: []resource.TestStep{
// Creation
{
Config: networkResourceConfig(
networkResource["name"],
fmt.Sprintf("[%q, %q]",
networkResource["nameserver0"],
networkResource["nameserver1"]),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("stackit_network.network", "network_id"),
resource.TestCheckResourceAttr("stackit_network.network", "name", networkResource["name"]),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_nameservers.#", "2"),
// nameservers may be returned in a randomized order, so we have to check them with a helper function
resource.TestCheckTypeSetElemAttr("stackit_network.network", "nameservers.*", networkResource["nameserver0"]),
resource.TestCheckTypeSetElemAttr("stackit_network.network", "nameservers.*", networkResource["nameserver1"]),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_gateway", networkResource["ipv4_gateway"]),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_prefix", networkResource["ipv4_prefix"]),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_prefix_length", networkResource["ipv4_prefix_length"]),
),
},
// Data source
{
Config: fmt.Sprintf(`
%s
data "stackit_network" "network" {
project_id = "%s"
network_id = stackit_network.network.network_id
}
`, networkResourceConfig(
networkResource["name"],
fmt.Sprintf("[%q, %q]",
networkResource["nameserver0"],
networkResource["nameserver1"]),
),
testutil.ProjectId,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.stackit_network.network", "network_id"),
resource.TestCheckResourceAttr("data.stackit_network.network", "name", networkResource["name"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_gateway", networkResource["ipv4_gateway"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_nameservers.#", "2"),
// nameservers may be returned in a randomized order, so we have to check them with a helper function
resource.TestCheckTypeSetElemAttr("stackit_network.network", "nameservers.*", networkResource["nameserver0"]),
resource.TestCheckTypeSetElemAttr("stackit_network.network", "nameservers.*", networkResource["nameserver1"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefix", networkResource["ipv4_prefix"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefix_length", networkResource["ipv4_prefix_length"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefixes.#", "1"),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefixes.0", networkResource["ipv4_prefix"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "routed", networkResource["routed"]),
),
},
// Import
{
ResourceName: "stackit_network.network",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_network.network"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_network.network")
}
networkId, ok := r.Primary.Attributes["network_id"]
if !ok {
return "", fmt.Errorf("couldn't find attribute network_id")
}
return fmt.Sprintf("%s,%s", testutil.ProjectId, networkId), nil
},
ImportState: true,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.stackit_network.network", "network_id"),
resource.TestCheckResourceAttr("data.stackit_network.network", "name", networkResource["name"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_gateway", networkResource["ipv4_gateway"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_nameservers.#", "2"),
// nameservers may be returned in a randomized order, so we have to check them with a helper function
resource.TestCheckTypeSetElemAttr("stackit_network.network", "nameservers.*", networkResource["nameserver0"]),
resource.TestCheckTypeSetElemAttr("stackit_network.network", "nameservers.*", networkResource["nameserver1"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefix", networkResource["ipv4_prefix"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefix_length", networkResource["ipv4_prefix_length"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefixes.#", "1"),
resource.TestCheckResourceAttr("data.stackit_network.network", "ipv4_prefixes.0", networkResource["ipv4_prefix"]),
resource.TestCheckResourceAttr("data.stackit_network.network", "routed", networkResource["routed"]),
),
},
// Update
{
Config: networkResourceConfig(
networkResource["name_updated"],
fmt.Sprintf("[%q, %q]",
networkResource["nameserver0"],
networkResource["nameserver1"]),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("stackit_network.network", "network_id"),
resource.TestCheckResourceAttr("stackit_network.network", "name", networkResource["name_updated"]),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_nameservers.#", "2"),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_gateway", networkResource["ipv4_gateway"]),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_prefix", networkResource["ipv4_prefix"]),
resource.TestCheckResourceAttr("stackit_network.network", "ipv4_prefix_length", networkResource["ipv4_prefix_length"])),
},
// Deletion is done by the framework implicitly
},
})
}
func TestAccNetworkArea(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
@ -1557,6 +1673,49 @@ func TestAccImage(t *testing.T) {
})
}
func testAccCheckNetworkDestroy(s *terraform.State) error {
ctx := context.Background()
var client *iaas.APIClient
var err error
if testutil.IaaSCustomEndpoint == "" {
client, err = iaas.NewAPIClient(
config.WithRegion("eu01"),
)
} else {
client, err = iaas.NewAPIClient(
config.WithEndpoint(testutil.IaaSCustomEndpoint),
)
}
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
var errs []error
// networks
for _, rs := range s.RootModule().Resources {
if rs.Type != "stackit_network" {
continue
}
networkId := strings.Split(rs.Primary.ID, core.Separator)[1]
err := client.DeleteNetworkExecute(ctx, testutil.ProjectId, networkId)
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) {
if oapiErr.StatusCode == http.StatusNotFound {
continue
}
}
errs = append(errs, fmt.Errorf("cannot trigger network deletion %q: %w", networkId, err))
}
_, err = wait.DeleteNetworkWaitHandler(ctx, client, testutil.ProjectId, networkId).WaitWithContext(ctx)
if err != nil {
errs = append(errs, fmt.Errorf("cannot delete network %q: %w", networkId, err))
}
}
return errors.Join(errs...)
}
func testAccCheckNetworkAreaDestroy(s *terraform.State) error {
ctx := context.Background()
var client *iaas.APIClient

View file

@ -3,6 +3,7 @@ package network
import (
"context"
"fmt"
"net"
"net/http"
"strings"
@ -147,8 +148,9 @@ func (d *networkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
ElementType: types.StringType,
},
"ipv4_prefix": schema.StringAttribute{
Description: "The IPv4 prefix of the network (CIDR).",
Computed: true,
Description: "The IPv4 prefix of the network (CIDR).",
DeprecationMessage: "The API supports reading multiple prefixes. So using the attribute 'ipv4_prefixes` should be preferred. This attribute will be populated with the first element from the list",
Computed: true,
},
"ipv4_prefix_length": schema.Int64Attribute{
Description: "The IPv4 prefix length of the network.",
@ -175,8 +177,9 @@ func (d *networkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
ElementType: types.StringType,
},
"ipv6_prefix": schema.StringAttribute{
Description: "The IPv6 prefix of the network (CIDR).",
Computed: true,
Description: "The IPv6 prefix of the network (CIDR).",
DeprecationMessage: "The API supports reading multiple prefixes. So using the attribute 'ipv6_prefixes` should be preferred. This attribute will be populated with the first element from the list",
Computed: true,
},
"ipv6_prefix_length": schema.Int64Attribute{
Description: "The IPv6 prefix length of the network.",
@ -321,6 +324,17 @@ func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *
if diags.HasError() {
return fmt.Errorf("map network prefixes: %w", core.DiagsToError(diags))
}
if len(respPrefixes) > 0 {
model.IPv4Prefix = types.StringValue(respPrefixes[0])
_, netmask, err := net.ParseCIDR(respPrefixes[0])
if err != nil {
// silently ignore parsing error for the netmask
model.IPv4PrefixLength = types.Int64Null()
} else {
ones, _ := netmask.Mask.Size()
model.IPv4PrefixLength = types.Int64Value(int64(ones))
}
}
model.Prefixes = prefixesTF
model.IPv4Prefixes = prefixesTF
@ -361,7 +375,17 @@ func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *
if diags.HasError() {
return fmt.Errorf("map network IPv6 prefixes: %w", core.DiagsToError(diags))
}
if len(respPrefixesV6) > 0 {
model.IPv6Prefix = types.StringValue(respPrefixesV6[0])
_, netmask, err := net.ParseCIDR(respPrefixesV6[0])
if err != nil {
// silently ignore parsing error for the netmask
model.IPv6PrefixLength = types.Int64Null()
} else {
ones, _ := netmask.Mask.Size()
model.IPv6PrefixLength = types.Int64Value(int64(ones))
}
}
model.IPv6Prefixes = prefixesV6TF
}

View file

@ -66,16 +66,16 @@ func TestMapDataSourceFields(t *testing.T) {
"ns2",
},
Prefixes: &[]string{
"prefix1",
"prefix2",
"192.168.42.0/24",
"10.100.10.0/16",
},
NameserversV6: &[]string{
"ns1",
"ns2",
},
PrefixesV6: &[]string{
"prefix1",
"prefix2",
"fd12:3456:789a:1::/64",
"fd12:3456:789a:2::/64",
},
PublicIp: utils.Ptr("publicIp"),
Labels: &map[string]interface{}{
@ -98,23 +98,25 @@ func TestMapDataSourceFields(t *testing.T) {
types.StringValue("ns1"),
types.StringValue("ns2"),
}),
IPv4PrefixLength: types.Int64Null(),
IPv4PrefixLength: types.Int64Value(24),
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("192.168.42.0/24"),
types.StringValue("10.100.10.0/16"),
}),
IPv4Prefix: types.StringValue("192.168.42.0/24"),
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("192.168.42.0/24"),
types.StringValue("10.100.10.0/16"),
}),
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("ns1"),
types.StringValue("ns2"),
}),
IPv6PrefixLength: types.Int64Null(),
IPv6PrefixLength: types.Int64Value(64),
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("fd12:3456:789a:1::/64"),
types.StringValue("fd12:3456:789a:2::/64"),
}),
PublicIP: types.StringValue("publicIp"),
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
@ -209,15 +211,15 @@ func TestMapDataSourceFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
NetworkId: types.StringValue("nid"),
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("192.168.42.0/24"),
types.StringValue("10.100.10.0/16"),
}),
},
&iaas.Network{
NetworkId: utils.Ptr("nid"),
Prefixes: &[]string{
"prefix2",
"prefix3",
"10.100.20.0/16",
"10.100.10.0/16",
},
},
DataSourceModel{
@ -231,14 +233,15 @@ func TestMapDataSourceFields(t *testing.T) {
Labels: types.MapNull(types.StringType),
Nameservers: types.ListNull(types.StringType),
IPv4Nameservers: types.ListNull(types.StringType),
IPv4PrefixLength: types.Int64Null(),
IPv4PrefixLength: types.Int64Value(16),
IPv4Prefix: types.StringValue("10.100.20.0/16"),
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix2"),
types.StringValue("prefix3"),
types.StringValue("10.100.20.0/16"),
types.StringValue("10.100.10.0/16"),
}),
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix2"),
types.StringValue("prefix3"),
types.StringValue("10.100.20.0/16"),
types.StringValue("10.100.10.0/16"),
}),
},
true,
@ -249,15 +252,15 @@ func TestMapDataSourceFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
NetworkId: types.StringValue("nid"),
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("fd12:3456:789a:1::/64"),
types.StringValue("fd12:3456:789a:2::/64"),
}),
},
&iaas.Network{
NetworkId: utils.Ptr("nid"),
PrefixesV6: &[]string{
"prefix2",
"prefix3",
"fd12:3456:789a:3::/64",
"fd12:3456:789a:4::/64",
},
},
DataSourceModel{
@ -272,10 +275,11 @@ func TestMapDataSourceFields(t *testing.T) {
Labels: types.MapNull(types.StringType),
Nameservers: types.ListNull(types.StringType),
IPv6Nameservers: types.ListNull(types.StringType),
IPv6PrefixLength: types.Int64Null(),
IPv6PrefixLength: types.Int64Value(64),
IPv6Prefix: types.StringValue("fd12:3456:789a:3::/64"),
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix2"),
types.StringValue("prefix3"),
types.StringValue("fd12:3456:789a:3::/64"),
types.StringValue("fd12:3456:789a:4::/64"),
}),
},
true,

View file

@ -3,6 +3,7 @@ package network
import (
"context"
"fmt"
"net"
"net/http"
"strings"
@ -545,7 +546,6 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model) err
}
// IPv4
if networkResp.Nameservers == nil {
model.Nameservers = types.ListNull(types.StringType)
model.IPv4Nameservers = types.ListNull(types.StringType)
@ -585,6 +585,17 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model) err
if diags.HasError() {
return fmt.Errorf("map network prefixes: %w", core.DiagsToError(diags))
}
if len(respPrefixes) > 0 {
model.IPv4Prefix = types.StringValue(respPrefixes[0])
_, netmask, err := net.ParseCIDR(respPrefixes[0])
if err != nil {
// silently ignore parsing error for the netmask
model.IPv4PrefixLength = types.Int64Null()
} else {
ones, _ := netmask.Mask.Size()
model.IPv4PrefixLength = types.Int64Value(int64(ones))
}
}
model.Prefixes = prefixesTF
model.IPv4Prefixes = prefixesTF
@ -625,7 +636,17 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model) err
if diags.HasError() {
return fmt.Errorf("map network IPv6 prefixes: %w", core.DiagsToError(diags))
}
if len(respPrefixesV6) > 0 {
model.IPv6Prefix = types.StringValue(respPrefixesV6[0])
_, netmask, err := net.ParseCIDR(respPrefixesV6[0])
if err != nil {
// silently ignore parsing error for the netmask
model.IPv6PrefixLength = types.Int64Null()
} else {
ones, _ := netmask.Mask.Size()
model.IPv6PrefixLength = types.Int64Value(int64(ones))
}
}
model.IPv6Prefixes = prefixesV6TF
}

View file

@ -66,16 +66,16 @@ func TestMapFields(t *testing.T) {
"ns2",
},
Prefixes: &[]string{
"prefix1",
"prefix2",
"192.168.42.0/24",
"10.100.10.0/16",
},
NameserversV6: &[]string{
"ns1",
"ns2",
},
PrefixesV6: &[]string{
"prefix1",
"prefix2",
"fd12:3456:789a:1::/64",
"fd12:3456:789b:1::/64",
},
PublicIp: utils.Ptr("publicIp"),
Labels: &map[string]interface{}{
@ -98,25 +98,27 @@ func TestMapFields(t *testing.T) {
types.StringValue("ns1"),
types.StringValue("ns2"),
}),
IPv4PrefixLength: types.Int64Null(),
IPv4PrefixLength: types.Int64Value(24),
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("192.168.42.0/24"),
types.StringValue("10.100.10.0/16"),
}),
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("192.168.42.0/24"),
types.StringValue("10.100.10.0/16"),
}),
IPv4Prefix: types.StringValue("192.168.42.0/24"),
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("ns1"),
types.StringValue("ns2"),
}),
IPv6PrefixLength: types.Int64Null(),
IPv6PrefixLength: types.Int64Value(64),
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("fd12:3456:789a:1::/64"),
types.StringValue("fd12:3456:789b:1::/64"),
}),
PublicIP: types.StringValue("publicIp"),
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
PublicIP: types.StringValue("publicIp"),
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
"key": types.StringValue("value"),
}),
@ -209,15 +211,15 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
NetworkId: types.StringValue("nid"),
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("192.168.42.0/24"),
types.StringValue("10.100.10.0/24"),
}),
},
&iaas.Network{
NetworkId: utils.Ptr("nid"),
Prefixes: &[]string{
"prefix2",
"prefix3",
"192.168.54.0/24",
"192.168.55.0/24",
},
},
Model{
@ -231,14 +233,15 @@ func TestMapFields(t *testing.T) {
Labels: types.MapNull(types.StringType),
Nameservers: types.ListNull(types.StringType),
IPv4Nameservers: types.ListNull(types.StringType),
IPv4PrefixLength: types.Int64Null(),
IPv4PrefixLength: types.Int64Value(24),
IPv4Prefix: types.StringValue("192.168.54.0/24"),
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix2"),
types.StringValue("prefix3"),
types.StringValue("192.168.54.0/24"),
types.StringValue("192.168.55.0/24"),
}),
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix2"),
types.StringValue("prefix3"),
types.StringValue("192.168.54.0/24"),
types.StringValue("192.168.55.0/24"),
}),
},
true,
@ -249,15 +252,15 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
NetworkId: types.StringValue("nid"),
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix1"),
types.StringValue("prefix2"),
types.StringValue("fd12:3456:789a:1::/64"),
types.StringValue("fd12:3456:789a:2::/64"),
}),
},
&iaas.Network{
NetworkId: utils.Ptr("nid"),
PrefixesV6: &[]string{
"prefix2",
"prefix3",
"fd12:3456:789a:1::/64",
"fd12:3456:789a:2::/64",
},
},
Model{
@ -272,11 +275,12 @@ func TestMapFields(t *testing.T) {
Labels: types.MapNull(types.StringType),
Nameservers: types.ListNull(types.StringType),
IPv6Nameservers: types.ListNull(types.StringType),
IPv6PrefixLength: types.Int64Null(),
IPv6PrefixLength: types.Int64Value(64),
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("prefix2"),
types.StringValue("prefix3"),
types.StringValue("fd12:3456:789a:1::/64"),
types.StringValue("fd12:3456:789a:2::/64"),
}),
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
},
true,
},