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:
parent
6d49b2ff81
commit
c7ed274647
6 changed files with 280 additions and 68 deletions
|
|
@ -32,12 +32,12 @@ data "stackit_network" "example" {
|
||||||
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`network_id`".
|
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`network_id`".
|
||||||
- `ipv4_gateway` (String) The IPv4 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway.
|
- `ipv4_gateway` (String) The IPv4 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway.
|
||||||
- `ipv4_nameservers` (List of String) The IPv4 nameservers of the network.
|
- `ipv4_nameservers` (List of String) The IPv4 nameservers of the network.
|
||||||
- `ipv4_prefix` (String) The IPv4 prefix of the network (CIDR).
|
- `ipv4_prefix` (String, Deprecated) The IPv4 prefix of the network (CIDR).
|
||||||
- `ipv4_prefix_length` (Number) The IPv4 prefix length of the network.
|
- `ipv4_prefix_length` (Number) The IPv4 prefix length of the network.
|
||||||
- `ipv4_prefixes` (List of String) The IPv4 prefixes of the network.
|
- `ipv4_prefixes` (List of String) The IPv4 prefixes of the network.
|
||||||
- `ipv6_gateway` (String) The IPv6 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway.
|
- `ipv6_gateway` (String) The IPv6 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway.
|
||||||
- `ipv6_nameservers` (List of String) The IPv6 nameservers of the network.
|
- `ipv6_nameservers` (List of String) The IPv6 nameservers of the network.
|
||||||
- `ipv6_prefix` (String) The IPv6 prefix of the network (CIDR).
|
- `ipv6_prefix` (String, Deprecated) The IPv6 prefix of the network (CIDR).
|
||||||
- `ipv6_prefix_length` (Number) The IPv6 prefix length of the network.
|
- `ipv6_prefix_length` (Number) The IPv6 prefix length of the network.
|
||||||
- `ipv6_prefixes` (List of String) The IPv6 prefixes of the network.
|
- `ipv6_prefixes` (List of String) The IPv6 prefixes of the network.
|
||||||
- `labels` (Map of String) Labels are key-value string pairs which can be attached to a resource container
|
- `labels` (Map of String) Labels are key-value string pairs which can be attached to a resource container
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,9 @@ package iaas_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -12,8 +14,10 @@ import (
|
||||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||||
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
"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/core/utils"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
"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/core"
|
||||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
|
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
|
||||||
)
|
)
|
||||||
|
|
@ -31,9 +35,10 @@ var networkResource = map[string]string{
|
||||||
"ipv4_prefix_length": "24",
|
"ipv4_prefix_length": "24",
|
||||||
"nameserver0": "1.2.3.4",
|
"nameserver0": "1.2.3.4",
|
||||||
"nameserver1": "5.6.7.8",
|
"nameserver1": "5.6.7.8",
|
||||||
"ipv4_gateway": "10.1.2.1",
|
"ipv4_gateway": "10.2.2.1",
|
||||||
"ipv4_prefix": "10.1.2.1/24",
|
"ipv4_prefix": "10.2.2.0/24",
|
||||||
"routed": "false",
|
"routed": "false",
|
||||||
|
"name_updated": fmt.Sprintf("acc-test-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)),
|
||||||
}
|
}
|
||||||
|
|
||||||
var networkAreaResource = map[string]string{
|
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) {
|
func TestAccNetworkArea(t *testing.T) {
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
|
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 {
|
func testAccCheckNetworkAreaDestroy(s *terraform.State) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
var client *iaas.APIClient
|
var client *iaas.APIClient
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package network
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -147,8 +148,9 @@ func (d *networkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
|
||||||
ElementType: types.StringType,
|
ElementType: types.StringType,
|
||||||
},
|
},
|
||||||
"ipv4_prefix": schema.StringAttribute{
|
"ipv4_prefix": schema.StringAttribute{
|
||||||
Description: "The IPv4 prefix of the network (CIDR).",
|
Description: "The IPv4 prefix of the network (CIDR).",
|
||||||
Computed: true,
|
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{
|
"ipv4_prefix_length": schema.Int64Attribute{
|
||||||
Description: "The IPv4 prefix length of the network.",
|
Description: "The IPv4 prefix length of the network.",
|
||||||
|
|
@ -175,8 +177,9 @@ func (d *networkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
|
||||||
ElementType: types.StringType,
|
ElementType: types.StringType,
|
||||||
},
|
},
|
||||||
"ipv6_prefix": schema.StringAttribute{
|
"ipv6_prefix": schema.StringAttribute{
|
||||||
Description: "The IPv6 prefix of the network (CIDR).",
|
Description: "The IPv6 prefix of the network (CIDR).",
|
||||||
Computed: true,
|
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{
|
"ipv6_prefix_length": schema.Int64Attribute{
|
||||||
Description: "The IPv6 prefix length of the network.",
|
Description: "The IPv6 prefix length of the network.",
|
||||||
|
|
@ -321,6 +324,17 @@ func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *
|
||||||
if diags.HasError() {
|
if diags.HasError() {
|
||||||
return fmt.Errorf("map network prefixes: %w", core.DiagsToError(diags))
|
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.Prefixes = prefixesTF
|
||||||
model.IPv4Prefixes = prefixesTF
|
model.IPv4Prefixes = prefixesTF
|
||||||
|
|
@ -361,7 +375,17 @@ func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *
|
||||||
if diags.HasError() {
|
if diags.HasError() {
|
||||||
return fmt.Errorf("map network IPv6 prefixes: %w", core.DiagsToError(diags))
|
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
|
model.IPv6Prefixes = prefixesV6TF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,16 +66,16 @@ func TestMapDataSourceFields(t *testing.T) {
|
||||||
"ns2",
|
"ns2",
|
||||||
},
|
},
|
||||||
Prefixes: &[]string{
|
Prefixes: &[]string{
|
||||||
"prefix1",
|
"192.168.42.0/24",
|
||||||
"prefix2",
|
"10.100.10.0/16",
|
||||||
},
|
},
|
||||||
NameserversV6: &[]string{
|
NameserversV6: &[]string{
|
||||||
"ns1",
|
"ns1",
|
||||||
"ns2",
|
"ns2",
|
||||||
},
|
},
|
||||||
PrefixesV6: &[]string{
|
PrefixesV6: &[]string{
|
||||||
"prefix1",
|
"fd12:3456:789a:1::/64",
|
||||||
"prefix2",
|
"fd12:3456:789a:2::/64",
|
||||||
},
|
},
|
||||||
PublicIp: utils.Ptr("publicIp"),
|
PublicIp: utils.Ptr("publicIp"),
|
||||||
Labels: &map[string]interface{}{
|
Labels: &map[string]interface{}{
|
||||||
|
|
@ -98,23 +98,25 @@ func TestMapDataSourceFields(t *testing.T) {
|
||||||
types.StringValue("ns1"),
|
types.StringValue("ns1"),
|
||||||
types.StringValue("ns2"),
|
types.StringValue("ns2"),
|
||||||
}),
|
}),
|
||||||
IPv4PrefixLength: types.Int64Null(),
|
IPv4PrefixLength: types.Int64Value(24),
|
||||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("192.168.42.0/24"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.10.0/16"),
|
||||||
}),
|
}),
|
||||||
|
IPv4Prefix: types.StringValue("192.168.42.0/24"),
|
||||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("192.168.42.0/24"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.10.0/16"),
|
||||||
}),
|
}),
|
||||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("ns1"),
|
types.StringValue("ns1"),
|
||||||
types.StringValue("ns2"),
|
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{
|
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("fd12:3456:789a:1::/64"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("fd12:3456:789a:2::/64"),
|
||||||
}),
|
}),
|
||||||
PublicIP: types.StringValue("publicIp"),
|
PublicIP: types.StringValue("publicIp"),
|
||||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||||
|
|
@ -209,15 +211,15 @@ func TestMapDataSourceFields(t *testing.T) {
|
||||||
ProjectId: types.StringValue("pid"),
|
ProjectId: types.StringValue("pid"),
|
||||||
NetworkId: types.StringValue("nid"),
|
NetworkId: types.StringValue("nid"),
|
||||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("192.168.42.0/24"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.10.0/16"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
&iaas.Network{
|
&iaas.Network{
|
||||||
NetworkId: utils.Ptr("nid"),
|
NetworkId: utils.Ptr("nid"),
|
||||||
Prefixes: &[]string{
|
Prefixes: &[]string{
|
||||||
"prefix2",
|
"10.100.20.0/16",
|
||||||
"prefix3",
|
"10.100.10.0/16",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
DataSourceModel{
|
DataSourceModel{
|
||||||
|
|
@ -231,14 +233,15 @@ func TestMapDataSourceFields(t *testing.T) {
|
||||||
Labels: types.MapNull(types.StringType),
|
Labels: types.MapNull(types.StringType),
|
||||||
Nameservers: types.ListNull(types.StringType),
|
Nameservers: types.ListNull(types.StringType),
|
||||||
IPv4Nameservers: 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{
|
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.20.0/16"),
|
||||||
types.StringValue("prefix3"),
|
types.StringValue("10.100.10.0/16"),
|
||||||
}),
|
}),
|
||||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.20.0/16"),
|
||||||
types.StringValue("prefix3"),
|
types.StringValue("10.100.10.0/16"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
|
|
@ -249,15 +252,15 @@ func TestMapDataSourceFields(t *testing.T) {
|
||||||
ProjectId: types.StringValue("pid"),
|
ProjectId: types.StringValue("pid"),
|
||||||
NetworkId: types.StringValue("nid"),
|
NetworkId: types.StringValue("nid"),
|
||||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("fd12:3456:789a:1::/64"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("fd12:3456:789a:2::/64"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
&iaas.Network{
|
&iaas.Network{
|
||||||
NetworkId: utils.Ptr("nid"),
|
NetworkId: utils.Ptr("nid"),
|
||||||
PrefixesV6: &[]string{
|
PrefixesV6: &[]string{
|
||||||
"prefix2",
|
"fd12:3456:789a:3::/64",
|
||||||
"prefix3",
|
"fd12:3456:789a:4::/64",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
DataSourceModel{
|
DataSourceModel{
|
||||||
|
|
@ -272,10 +275,11 @@ func TestMapDataSourceFields(t *testing.T) {
|
||||||
Labels: types.MapNull(types.StringType),
|
Labels: types.MapNull(types.StringType),
|
||||||
Nameservers: types.ListNull(types.StringType),
|
Nameservers: types.ListNull(types.StringType),
|
||||||
IPv6Nameservers: 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{
|
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("fd12:3456:789a:3::/64"),
|
||||||
types.StringValue("prefix3"),
|
types.StringValue("fd12:3456:789a:4::/64"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package network
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -545,7 +546,6 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model) err
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPv4
|
// IPv4
|
||||||
|
|
||||||
if networkResp.Nameservers == nil {
|
if networkResp.Nameservers == nil {
|
||||||
model.Nameservers = types.ListNull(types.StringType)
|
model.Nameservers = types.ListNull(types.StringType)
|
||||||
model.IPv4Nameservers = 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() {
|
if diags.HasError() {
|
||||||
return fmt.Errorf("map network prefixes: %w", core.DiagsToError(diags))
|
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.Prefixes = prefixesTF
|
||||||
model.IPv4Prefixes = prefixesTF
|
model.IPv4Prefixes = prefixesTF
|
||||||
|
|
@ -625,7 +636,17 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model) err
|
||||||
if diags.HasError() {
|
if diags.HasError() {
|
||||||
return fmt.Errorf("map network IPv6 prefixes: %w", core.DiagsToError(diags))
|
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
|
model.IPv6Prefixes = prefixesV6TF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,16 +66,16 @@ func TestMapFields(t *testing.T) {
|
||||||
"ns2",
|
"ns2",
|
||||||
},
|
},
|
||||||
Prefixes: &[]string{
|
Prefixes: &[]string{
|
||||||
"prefix1",
|
"192.168.42.0/24",
|
||||||
"prefix2",
|
"10.100.10.0/16",
|
||||||
},
|
},
|
||||||
NameserversV6: &[]string{
|
NameserversV6: &[]string{
|
||||||
"ns1",
|
"ns1",
|
||||||
"ns2",
|
"ns2",
|
||||||
},
|
},
|
||||||
PrefixesV6: &[]string{
|
PrefixesV6: &[]string{
|
||||||
"prefix1",
|
"fd12:3456:789a:1::/64",
|
||||||
"prefix2",
|
"fd12:3456:789b:1::/64",
|
||||||
},
|
},
|
||||||
PublicIp: utils.Ptr("publicIp"),
|
PublicIp: utils.Ptr("publicIp"),
|
||||||
Labels: &map[string]interface{}{
|
Labels: &map[string]interface{}{
|
||||||
|
|
@ -98,25 +98,27 @@ func TestMapFields(t *testing.T) {
|
||||||
types.StringValue("ns1"),
|
types.StringValue("ns1"),
|
||||||
types.StringValue("ns2"),
|
types.StringValue("ns2"),
|
||||||
}),
|
}),
|
||||||
IPv4PrefixLength: types.Int64Null(),
|
IPv4PrefixLength: types.Int64Value(24),
|
||||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("192.168.42.0/24"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.10.0/16"),
|
||||||
}),
|
}),
|
||||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("192.168.42.0/24"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.10.0/16"),
|
||||||
}),
|
}),
|
||||||
|
IPv4Prefix: types.StringValue("192.168.42.0/24"),
|
||||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("ns1"),
|
types.StringValue("ns1"),
|
||||||
types.StringValue("ns2"),
|
types.StringValue("ns2"),
|
||||||
}),
|
}),
|
||||||
IPv6PrefixLength: types.Int64Null(),
|
IPv6PrefixLength: types.Int64Value(64),
|
||||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("fd12:3456:789a:1::/64"),
|
||||||
types.StringValue("prefix2"),
|
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{
|
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||||
"key": types.StringValue("value"),
|
"key": types.StringValue("value"),
|
||||||
}),
|
}),
|
||||||
|
|
@ -209,15 +211,15 @@ func TestMapFields(t *testing.T) {
|
||||||
ProjectId: types.StringValue("pid"),
|
ProjectId: types.StringValue("pid"),
|
||||||
NetworkId: types.StringValue("nid"),
|
NetworkId: types.StringValue("nid"),
|
||||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("192.168.42.0/24"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("10.100.10.0/24"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
&iaas.Network{
|
&iaas.Network{
|
||||||
NetworkId: utils.Ptr("nid"),
|
NetworkId: utils.Ptr("nid"),
|
||||||
Prefixes: &[]string{
|
Prefixes: &[]string{
|
||||||
"prefix2",
|
"192.168.54.0/24",
|
||||||
"prefix3",
|
"192.168.55.0/24",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Model{
|
Model{
|
||||||
|
|
@ -231,14 +233,15 @@ func TestMapFields(t *testing.T) {
|
||||||
Labels: types.MapNull(types.StringType),
|
Labels: types.MapNull(types.StringType),
|
||||||
Nameservers: types.ListNull(types.StringType),
|
Nameservers: types.ListNull(types.StringType),
|
||||||
IPv4Nameservers: 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{
|
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("192.168.54.0/24"),
|
||||||
types.StringValue("prefix3"),
|
types.StringValue("192.168.55.0/24"),
|
||||||
}),
|
}),
|
||||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("192.168.54.0/24"),
|
||||||
types.StringValue("prefix3"),
|
types.StringValue("192.168.55.0/24"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
|
|
@ -249,15 +252,15 @@ func TestMapFields(t *testing.T) {
|
||||||
ProjectId: types.StringValue("pid"),
|
ProjectId: types.StringValue("pid"),
|
||||||
NetworkId: types.StringValue("nid"),
|
NetworkId: types.StringValue("nid"),
|
||||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix1"),
|
types.StringValue("fd12:3456:789a:1::/64"),
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("fd12:3456:789a:2::/64"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
&iaas.Network{
|
&iaas.Network{
|
||||||
NetworkId: utils.Ptr("nid"),
|
NetworkId: utils.Ptr("nid"),
|
||||||
PrefixesV6: &[]string{
|
PrefixesV6: &[]string{
|
||||||
"prefix2",
|
"fd12:3456:789a:1::/64",
|
||||||
"prefix3",
|
"fd12:3456:789a:2::/64",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Model{
|
Model{
|
||||||
|
|
@ -272,11 +275,12 @@ func TestMapFields(t *testing.T) {
|
||||||
Labels: types.MapNull(types.StringType),
|
Labels: types.MapNull(types.StringType),
|
||||||
Nameservers: types.ListNull(types.StringType),
|
Nameservers: types.ListNull(types.StringType),
|
||||||
IPv6Nameservers: types.ListNull(types.StringType),
|
IPv6Nameservers: types.ListNull(types.StringType),
|
||||||
IPv6PrefixLength: types.Int64Null(),
|
IPv6PrefixLength: types.Int64Value(64),
|
||||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||||
types.StringValue("prefix2"),
|
types.StringValue("fd12:3456:789a:1::/64"),
|
||||||
types.StringValue("prefix3"),
|
types.StringValue("fd12:3456:789a:2::/64"),
|
||||||
}),
|
}),
|
||||||
|
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue