feat(iaas): add iaas network v2 alpha (#899)
* add experimental network v2
This commit is contained in:
parent
a00b0466d5
commit
d9dc1d4495
28 changed files with 3777 additions and 923 deletions
53
stackit/internal/services/iaas/network/utils/model/model.go
Normal file
53
stackit/internal/services/iaas/network/utils/model/model.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package model
|
||||
|
||||
import "github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
type Model struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
NetworkId types.String `tfsdk:"network_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Nameservers types.List `tfsdk:"nameservers"`
|
||||
IPv4Gateway types.String `tfsdk:"ipv4_gateway"`
|
||||
IPv4Nameservers types.List `tfsdk:"ipv4_nameservers"`
|
||||
IPv4Prefix types.String `tfsdk:"ipv4_prefix"`
|
||||
IPv4PrefixLength types.Int64 `tfsdk:"ipv4_prefix_length"`
|
||||
Prefixes types.List `tfsdk:"prefixes"`
|
||||
IPv4Prefixes types.List `tfsdk:"ipv4_prefixes"`
|
||||
IPv6Gateway types.String `tfsdk:"ipv6_gateway"`
|
||||
IPv6Nameservers types.List `tfsdk:"ipv6_nameservers"`
|
||||
IPv6Prefix types.String `tfsdk:"ipv6_prefix"`
|
||||
IPv6PrefixLength types.Int64 `tfsdk:"ipv6_prefix_length"`
|
||||
IPv6Prefixes types.List `tfsdk:"ipv6_prefixes"`
|
||||
PublicIP types.String `tfsdk:"public_ip"`
|
||||
Labels types.Map `tfsdk:"labels"`
|
||||
Routed types.Bool `tfsdk:"routed"`
|
||||
NoIPv4Gateway types.Bool `tfsdk:"no_ipv4_gateway"`
|
||||
NoIPv6Gateway types.Bool `tfsdk:"no_ipv6_gateway"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
RoutingTableID types.String `tfsdk:"routing_table_id"`
|
||||
}
|
||||
|
||||
type DataSourceModel struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
NetworkId types.String `tfsdk:"network_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Nameservers types.List `tfsdk:"nameservers"`
|
||||
IPv4Gateway types.String `tfsdk:"ipv4_gateway"`
|
||||
IPv4Nameservers types.List `tfsdk:"ipv4_nameservers"`
|
||||
IPv4Prefix types.String `tfsdk:"ipv4_prefix"`
|
||||
IPv4PrefixLength types.Int64 `tfsdk:"ipv4_prefix_length"`
|
||||
Prefixes types.List `tfsdk:"prefixes"`
|
||||
IPv4Prefixes types.List `tfsdk:"ipv4_prefixes"`
|
||||
IPv6Gateway types.String `tfsdk:"ipv6_gateway"`
|
||||
IPv6Nameservers types.List `tfsdk:"ipv6_nameservers"`
|
||||
IPv6Prefix types.String `tfsdk:"ipv6_prefix"`
|
||||
IPv6PrefixLength types.Int64 `tfsdk:"ipv6_prefix_length"`
|
||||
IPv6Prefixes types.List `tfsdk:"ipv6_prefixes"`
|
||||
PublicIP types.String `tfsdk:"public_ip"`
|
||||
Labels types.Map `tfsdk:"labels"`
|
||||
Routed types.Bool `tfsdk:"routed"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
RoutingTableID types.String `tfsdk:"routing_table_id"`
|
||||
}
|
||||
|
|
@ -0,0 +1,203 @@
|
|||
package v1network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
networkModel "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
)
|
||||
|
||||
func DatasourceRead(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse, client *iaas.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
var model networkModel.DataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
networkResp, err := client.GetNetwork(ctx, projectId, networkId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading network",
|
||||
fmt.Sprintf("Network with ID %q does not exist in project %q.", networkId, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
err = mapDataSourceFields(ctx, networkResp, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network read")
|
||||
}
|
||||
|
||||
func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *networkModel.DataSourceModel) error {
|
||||
if networkResp == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
|
||||
var networkId string
|
||||
if model.NetworkId.ValueString() != "" {
|
||||
networkId = model.NetworkId.ValueString()
|
||||
} else if networkResp.NetworkId != nil {
|
||||
networkId = *networkResp.NetworkId
|
||||
} else {
|
||||
return fmt.Errorf("network id not present")
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), networkId)
|
||||
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// IPv4
|
||||
|
||||
if networkResp.Nameservers == nil {
|
||||
model.Nameservers = types.ListNull(types.StringType)
|
||||
model.IPv4Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respNameservers := *networkResp.Nameservers
|
||||
modelNameservers, err := utils.ListValuetoStringSlice(model.Nameservers)
|
||||
modelIPv4Nameservers, errIpv4 := utils.ListValuetoStringSlice(model.IPv4Nameservers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get current network nameservers from model: %w", err)
|
||||
}
|
||||
if errIpv4 != nil {
|
||||
return fmt.Errorf("get current IPv4 network nameservers from model: %w", errIpv4)
|
||||
}
|
||||
|
||||
reconciledNameservers := utils.ReconcileStringSlices(modelNameservers, respNameservers)
|
||||
reconciledIPv4Nameservers := utils.ReconcileStringSlices(modelIPv4Nameservers, respNameservers)
|
||||
|
||||
nameserversTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledNameservers)
|
||||
ipv4NameserversTF, ipv4Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv4Nameservers)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("map network nameservers: %w", core.DiagsToError(diags))
|
||||
}
|
||||
if ipv4Diags.HasError() {
|
||||
return fmt.Errorf("map IPv4 network nameservers: %w", core.DiagsToError(ipv4Diags))
|
||||
}
|
||||
|
||||
model.Nameservers = nameserversTF
|
||||
model.IPv4Nameservers = ipv4NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.Prefixes == nil {
|
||||
model.Prefixes = types.ListNull(types.StringType)
|
||||
model.IPv4Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixes := *networkResp.Prefixes
|
||||
prefixesTF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixes)
|
||||
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
|
||||
}
|
||||
|
||||
model.IPv4Gateway = types.StringNull()
|
||||
if networkResp.Gateway != nil {
|
||||
model.IPv4Gateway = types.StringPointerValue(networkResp.GetGateway())
|
||||
}
|
||||
|
||||
// IPv6
|
||||
|
||||
if networkResp.NameserversV6 == nil {
|
||||
model.IPv6Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respIPv6Nameservers := *networkResp.NameserversV6
|
||||
modelIPv6Nameservers, errIpv6 := utils.ListValuetoStringSlice(model.IPv6Nameservers)
|
||||
if errIpv6 != nil {
|
||||
return fmt.Errorf("get current IPv6 network nameservers from model: %w", errIpv6)
|
||||
}
|
||||
|
||||
reconciledIPv6Nameservers := utils.ReconcileStringSlices(modelIPv6Nameservers, respIPv6Nameservers)
|
||||
|
||||
ipv6NameserversTF, ipv6Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv6Nameservers)
|
||||
if ipv6Diags.HasError() {
|
||||
return fmt.Errorf("map IPv6 network nameservers: %w", core.DiagsToError(ipv6Diags))
|
||||
}
|
||||
|
||||
model.IPv6Nameservers = ipv6NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.PrefixesV6 == nil {
|
||||
model.IPv6Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixesV6 := *networkResp.PrefixesV6
|
||||
prefixesV6TF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixesV6)
|
||||
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
|
||||
}
|
||||
|
||||
model.IPv6Gateway = types.StringNull()
|
||||
if networkResp.Gatewayv6 != nil {
|
||||
model.IPv6Gateway = types.StringPointerValue(networkResp.GetGatewayv6())
|
||||
}
|
||||
|
||||
model.NetworkId = types.StringValue(networkId)
|
||||
model.Name = types.StringPointerValue(networkResp.Name)
|
||||
model.PublicIP = types.StringPointerValue(networkResp.PublicIp)
|
||||
model.Labels = labels
|
||||
model.Routed = types.BoolPointerValue(networkResp.Routed)
|
||||
model.RoutingTableID = types.StringNull()
|
||||
model.Region = types.StringNull()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,352 @@
|
|||
package v1network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
||||
networkModel "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
)
|
||||
|
||||
func TestMapDataSourceFields(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
state networkModel.DataSourceModel
|
||||
input *iaas.Network
|
||||
expected networkModel.DataSourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"id_ok",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Gateway: iaas.NewNullableString(nil),
|
||||
},
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
IPv4Prefix: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefix: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"values_ok",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
Prefixes: &[]string{
|
||||
"192.168.42.0/24",
|
||||
"10.100.10.0/16",
|
||||
},
|
||||
NameserversV6: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
PrefixesV6: &[]string{
|
||||
"fd12:3456:789a:1::/64",
|
||||
"fd12:3456:789a:2::/64",
|
||||
},
|
||||
PublicIp: utils.Ptr("publicIp"),
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(true),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Gatewayv6: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
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("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.Int64Value(64),
|
||||
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
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{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Nameservers: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
},
|
||||
},
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_nameservers_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
NameserversV6: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
},
|
||||
},
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_prefixes_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.42.0/24"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Prefixes: &[]string{
|
||||
"10.100.20.0/16",
|
||||
"10.100.10.0/16",
|
||||
},
|
||||
},
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Value(16),
|
||||
IPv4Prefix: types.StringValue("10.100.20.0/16"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("10.100.20.0/16"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("10.100.20.0/16"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_prefixes_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
PrefixesV6: &[]string{
|
||||
"fd12:3456:789a:3::/64",
|
||||
"fd12:3456:789a:4::/64",
|
||||
},
|
||||
},
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Value(64),
|
||||
IPv6Prefix: types.StringValue("fd12:3456:789a:3::/64"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:3::/64"),
|
||||
types.StringValue("fd12:3456:789a:4::/64"),
|
||||
}),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_ipv6_gateway_nil",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
},
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
networkModel.DataSourceModel{},
|
||||
nil,
|
||||
networkModel.DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&iaas.Network{},
|
||||
networkModel.DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapDataSourceFields(context.Background(), tt.input, &tt.state)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,512 @@
|
|||
package v1network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"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/conversion"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
networkModel "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
)
|
||||
|
||||
func Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse, client *iaas.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model networkModel.Model
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toCreatePayload(ctx, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Creating API payload: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Create new network
|
||||
|
||||
network, err := client.CreateNetwork(ctx, projectId).CreateNetworkPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
networkId := *network.NetworkId
|
||||
network, err = wait.CreateNetworkWaitHandler(ctx, client, projectId, networkId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Network creation waiting: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, network, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Set state to fully populated data
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network created")
|
||||
}
|
||||
|
||||
func Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse, client *iaas.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
var model networkModel.Model
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
networkResp, err := client.GetNetwork(ctx, projectId, networkId).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, networkResp, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network read")
|
||||
}
|
||||
|
||||
func Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse, client *iaas.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model networkModel.Model
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
// Retrieve values from state
|
||||
var stateModel networkModel.Model
|
||||
diags = req.State.Get(ctx, &stateModel)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toUpdatePayload(ctx, &model, &stateModel)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Creating API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Update existing network
|
||||
err = client.PartialUpdateNetwork(ctx, projectId, networkId).PartialUpdateNetworkPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
waitResp, err := wait.UpdateNetworkWaitHandler(ctx, client, projectId, networkId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Network update waiting: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
err = mapFields(ctx, waitResp, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network updated")
|
||||
}
|
||||
|
||||
func Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse, client *iaas.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from state
|
||||
var model networkModel.Model
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
// Delete existing network
|
||||
err := client.DeleteNetwork(ctx, projectId, networkId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
_, err = wait.DeleteNetworkWaitHandler(ctx, client, projectId, networkId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network", fmt.Sprintf("Network deletion waiting: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Network deleted")
|
||||
}
|
||||
|
||||
// ImportState imports a resource into the Terraform state on success.
|
||||
// The expected format of the resource import identifier is: project_id,network_id
|
||||
func ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics,
|
||||
"Error importing network",
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[network_id] Got: %q", req.ID),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
projectId := idParts[0]
|
||||
networkId := idParts[1]
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("network_id"), networkId)...)
|
||||
tflog.Info(ctx, "Network state imported")
|
||||
}
|
||||
|
||||
func mapFields(ctx context.Context, networkResp *iaas.Network, model *networkModel.Model) error {
|
||||
if networkResp == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
|
||||
var networkId string
|
||||
if model.NetworkId.ValueString() != "" {
|
||||
networkId = model.NetworkId.ValueString()
|
||||
} else if networkResp.NetworkId != nil {
|
||||
networkId = *networkResp.NetworkId
|
||||
} else {
|
||||
return fmt.Errorf("network id not present")
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), networkId)
|
||||
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// IPv4
|
||||
if networkResp.Nameservers == nil {
|
||||
model.Nameservers = types.ListNull(types.StringType)
|
||||
model.IPv4Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respNameservers := *networkResp.Nameservers
|
||||
modelNameservers, err := utils.ListValuetoStringSlice(model.Nameservers)
|
||||
modelIPv4Nameservers, errIpv4 := utils.ListValuetoStringSlice(model.IPv4Nameservers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get current network nameservers from model: %w", err)
|
||||
}
|
||||
if errIpv4 != nil {
|
||||
return fmt.Errorf("get current IPv4 network nameservers from model: %w", errIpv4)
|
||||
}
|
||||
|
||||
reconciledNameservers := utils.ReconcileStringSlices(modelNameservers, respNameservers)
|
||||
reconciledIPv4Nameservers := utils.ReconcileStringSlices(modelIPv4Nameservers, respNameservers)
|
||||
|
||||
nameserversTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledNameservers)
|
||||
ipv4NameserversTF, ipv4Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv4Nameservers)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("map network nameservers: %w", core.DiagsToError(diags))
|
||||
}
|
||||
if ipv4Diags.HasError() {
|
||||
return fmt.Errorf("map IPv4 network nameservers: %w", core.DiagsToError(ipv4Diags))
|
||||
}
|
||||
|
||||
model.Nameservers = nameserversTF
|
||||
model.IPv4Nameservers = ipv4NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.Prefixes == nil {
|
||||
model.Prefixes = types.ListNull(types.StringType)
|
||||
model.IPv4Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixes := *networkResp.Prefixes
|
||||
prefixesTF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixes)
|
||||
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
|
||||
}
|
||||
|
||||
if networkResp.Gateway != nil {
|
||||
model.IPv4Gateway = types.StringPointerValue(networkResp.GetGateway())
|
||||
} else {
|
||||
model.IPv4Gateway = types.StringNull()
|
||||
}
|
||||
|
||||
// IPv6
|
||||
|
||||
if networkResp.NameserversV6 == nil {
|
||||
model.IPv6Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respIPv6Nameservers := *networkResp.NameserversV6
|
||||
modelIPv6Nameservers, errIpv6 := utils.ListValuetoStringSlice(model.IPv6Nameservers)
|
||||
if errIpv6 != nil {
|
||||
return fmt.Errorf("get current IPv6 network nameservers from model: %w", errIpv6)
|
||||
}
|
||||
|
||||
reconciledIPv6Nameservers := utils.ReconcileStringSlices(modelIPv6Nameservers, respIPv6Nameservers)
|
||||
|
||||
ipv6NameserversTF, ipv6Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv6Nameservers)
|
||||
if ipv6Diags.HasError() {
|
||||
return fmt.Errorf("map IPv6 network nameservers: %w", core.DiagsToError(ipv6Diags))
|
||||
}
|
||||
|
||||
model.IPv6Nameservers = ipv6NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.PrefixesV6 == nil {
|
||||
model.IPv6Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixesV6 := *networkResp.PrefixesV6
|
||||
prefixesV6TF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixesV6)
|
||||
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
|
||||
}
|
||||
|
||||
if networkResp.Gatewayv6 != nil {
|
||||
model.IPv6Gateway = types.StringPointerValue(networkResp.GetGatewayv6())
|
||||
} else {
|
||||
model.IPv6Gateway = types.StringNull()
|
||||
}
|
||||
|
||||
model.NetworkId = types.StringValue(networkId)
|
||||
model.Name = types.StringPointerValue(networkResp.Name)
|
||||
model.PublicIP = types.StringPointerValue(networkResp.PublicIp)
|
||||
model.Labels = labels
|
||||
model.Routed = types.BoolPointerValue(networkResp.Routed)
|
||||
model.Region = types.StringNull()
|
||||
model.RoutingTableID = types.StringNull()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func toCreatePayload(ctx context.Context, model *networkModel.Model) (*iaas.CreateNetworkPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
addressFamily := &iaas.CreateNetworkAddressFamily{}
|
||||
|
||||
modelIPv6Nameservers := []string{}
|
||||
for _, ipv6ns := range model.IPv6Nameservers.Elements() {
|
||||
ipv6NameserverString, ok := ipv6ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv6Nameservers = append(modelIPv6Nameservers, ipv6NameserverString.ValueString())
|
||||
}
|
||||
|
||||
if !(model.IPv6Prefix.IsNull() || model.IPv6PrefixLength.IsNull() || model.IPv6Nameservers.IsNull()) {
|
||||
addressFamily.Ipv6 = &iaas.CreateNetworkIPv6Body{
|
||||
Nameservers: &modelIPv6Nameservers,
|
||||
Prefix: conversion.StringValueToPointer(model.IPv6Prefix),
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv6PrefixLength),
|
||||
}
|
||||
|
||||
if model.NoIPv6Gateway.ValueBool() {
|
||||
addressFamily.Ipv6.Gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv6Gateway.IsUnknown() || model.IPv6Gateway.IsNull()) {
|
||||
addressFamily.Ipv6.Gateway = iaas.NewNullableString(conversion.StringValueToPointer(model.IPv6Gateway))
|
||||
}
|
||||
}
|
||||
|
||||
modelIPv4Nameservers := []string{}
|
||||
var modelIPv4List []attr.Value
|
||||
|
||||
if !(model.IPv4Nameservers.IsNull() || model.IPv4Nameservers.IsUnknown()) {
|
||||
modelIPv4List = model.IPv4Nameservers.Elements()
|
||||
} else {
|
||||
modelIPv4List = model.Nameservers.Elements()
|
||||
}
|
||||
|
||||
for _, ipv4ns := range modelIPv4List {
|
||||
ipv4NameserverString, ok := ipv4ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv4Nameservers = append(modelIPv4Nameservers, ipv4NameserverString.ValueString())
|
||||
}
|
||||
|
||||
if !model.IPv4Prefix.IsNull() || !model.IPv4PrefixLength.IsNull() || !model.IPv4Nameservers.IsNull() || !model.Nameservers.IsNull() {
|
||||
addressFamily.Ipv4 = &iaas.CreateNetworkIPv4Body{
|
||||
Nameservers: &modelIPv4Nameservers,
|
||||
Prefix: conversion.StringValueToPointer(model.IPv4Prefix),
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv4PrefixLength),
|
||||
}
|
||||
|
||||
if model.NoIPv4Gateway.ValueBool() {
|
||||
addressFamily.Ipv4.Gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv4Gateway.IsUnknown() || model.IPv4Gateway.IsNull()) {
|
||||
addressFamily.Ipv4.Gateway = iaas.NewNullableString(conversion.StringValueToPointer(model.IPv4Gateway))
|
||||
}
|
||||
}
|
||||
|
||||
labels, err := conversion.ToStringInterfaceMap(ctx, model.Labels)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("converting to Go map: %w", err)
|
||||
}
|
||||
|
||||
payload := iaas.CreateNetworkPayload{
|
||||
Name: conversion.StringValueToPointer(model.Name),
|
||||
Labels: &labels,
|
||||
Routed: conversion.BoolValueToPointer(model.Routed),
|
||||
}
|
||||
|
||||
if addressFamily.Ipv6 != nil || addressFamily.Ipv4 != nil {
|
||||
payload.AddressFamily = addressFamily
|
||||
}
|
||||
|
||||
return &payload, nil
|
||||
}
|
||||
|
||||
func toUpdatePayload(ctx context.Context, model, stateModel *networkModel.Model) (*iaas.PartialUpdateNetworkPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
addressFamily := &iaas.UpdateNetworkAddressFamily{}
|
||||
|
||||
modelIPv6Nameservers := []string{}
|
||||
for _, ipv6ns := range model.IPv6Nameservers.Elements() {
|
||||
ipv6NameserverString, ok := ipv6ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv6Nameservers = append(modelIPv6Nameservers, ipv6NameserverString.ValueString())
|
||||
}
|
||||
|
||||
if !(model.IPv6Nameservers.IsNull() || model.IPv6Nameservers.IsUnknown()) {
|
||||
addressFamily.Ipv6 = &iaas.UpdateNetworkIPv6Body{
|
||||
Nameservers: &modelIPv6Nameservers,
|
||||
}
|
||||
|
||||
if model.NoIPv6Gateway.ValueBool() {
|
||||
addressFamily.Ipv6.Gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv6Gateway.IsUnknown() || model.IPv6Gateway.IsNull()) {
|
||||
addressFamily.Ipv6.Gateway = iaas.NewNullableString(conversion.StringValueToPointer(model.IPv6Gateway))
|
||||
}
|
||||
}
|
||||
|
||||
modelIPv4Nameservers := []string{}
|
||||
var modelIPv4List []attr.Value
|
||||
|
||||
if !(model.IPv4Nameservers.IsNull() || model.IPv4Nameservers.IsUnknown()) {
|
||||
modelIPv4List = model.IPv4Nameservers.Elements()
|
||||
} else {
|
||||
modelIPv4List = model.Nameservers.Elements()
|
||||
}
|
||||
for _, ipv4ns := range modelIPv4List {
|
||||
ipv4NameserverString, ok := ipv4ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv4Nameservers = append(modelIPv4Nameservers, ipv4NameserverString.ValueString())
|
||||
}
|
||||
|
||||
if !model.IPv4Nameservers.IsNull() || !model.Nameservers.IsNull() {
|
||||
addressFamily.Ipv4 = &iaas.UpdateNetworkIPv4Body{
|
||||
Nameservers: &modelIPv4Nameservers,
|
||||
}
|
||||
|
||||
if model.NoIPv4Gateway.ValueBool() {
|
||||
addressFamily.Ipv4.Gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv4Gateway.IsUnknown() || model.IPv4Gateway.IsNull()) {
|
||||
addressFamily.Ipv4.Gateway = iaas.NewNullableString(conversion.StringValueToPointer(model.IPv4Gateway))
|
||||
}
|
||||
}
|
||||
currentLabels := stateModel.Labels
|
||||
labels, err := conversion.ToJSONMapPartialUpdatePayload(ctx, currentLabels, model.Labels)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("converting to Go map: %w", err)
|
||||
}
|
||||
|
||||
payload := iaas.PartialUpdateNetworkPayload{
|
||||
Name: conversion.StringValueToPointer(model.Name),
|
||||
Labels: &labels,
|
||||
}
|
||||
|
||||
if addressFamily.Ipv6 != nil || addressFamily.Ipv4 != nil {
|
||||
payload.AddressFamily = addressFamily
|
||||
}
|
||||
|
||||
return &payload, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,691 @@
|
|||
package v1network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
)
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
state model.Model
|
||||
input *iaas.Network
|
||||
expected model.Model
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"id_ok",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Gateway: iaas.NewNullableString(nil),
|
||||
},
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
IPv4Prefix: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefix: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"values_ok",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
Prefixes: &[]string{
|
||||
"192.168.42.0/24",
|
||||
"10.100.10.0/16",
|
||||
},
|
||||
NameserversV6: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
PrefixesV6: &[]string{
|
||||
"fd12:3456:789a:1::/64",
|
||||
"fd12:3456:789b:1::/64",
|
||||
},
|
||||
PublicIp: utils.Ptr("publicIp"),
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(true),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Gatewayv6: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.42.0/24"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
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.Int64Value(64),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789b:1::/64"),
|
||||
}),
|
||||
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"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Nameservers: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
},
|
||||
},
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_nameservers_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
NameserversV6: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
},
|
||||
},
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_prefixes_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.42.0/24"),
|
||||
types.StringValue("10.100.10.0/24"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
Prefixes: &[]string{
|
||||
"192.168.54.0/24",
|
||||
"192.168.55.0/24",
|
||||
},
|
||||
},
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
IPv4Prefix: types.StringValue("192.168.54.0/24"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.54.0/24"),
|
||||
types.StringValue("192.168.55.0/24"),
|
||||
}),
|
||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.54.0/24"),
|
||||
types.StringValue("192.168.55.0/24"),
|
||||
}),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_prefixes_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
PrefixesV6: &[]string{
|
||||
"fd12:3456:789a:1::/64",
|
||||
"fd12:3456:789a:2::/64",
|
||||
},
|
||||
},
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Value(64),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_ipv6_gateway_nil",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaas.Network{
|
||||
NetworkId: utils.Ptr("nid"),
|
||||
},
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
model.Model{},
|
||||
nil,
|
||||
model.Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&iaas.Network{},
|
||||
model.Model{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapFields(context.Background(), tt.input, &tt.state)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *model.Model
|
||||
expected *iaas.CreateNetworkPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(false),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv4Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.CreateNetworkAddressFamily{
|
||||
Ipv4: &iaas.CreateNetworkIPv4Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
PrefixLength: utils.Ptr(int64(24)),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(false),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_okay",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(false),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv4Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.CreateNetworkAddressFamily{
|
||||
Ipv4: &iaas.CreateNetworkIPv4Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
PrefixLength: utils.Ptr(int64(24)),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(false),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv6PrefixLength: types.Int64Value(24),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(false),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
IPv6Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.CreateNetworkAddressFamily{
|
||||
Ipv6: &iaas.CreateNetworkIPv6Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
PrefixLength: utils.Ptr(int64(24)),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(false),
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toCreatePayload(context.Background(), tt.input)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaas.NullableString{}))
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToUpdatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *model.Model
|
||||
state model.Model
|
||||
expected *iaas.PartialUpdateNetworkPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.UpdateNetworkAddressFamily{
|
||||
Ipv4: &iaas.UpdateNetworkIPv4Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_okay",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.UpdateNetworkAddressFamily{
|
||||
Ipv4: &iaas.UpdateNetworkIPv4Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_gateway_nil",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.UpdateNetworkAddressFamily{
|
||||
Ipv4: &iaas.UpdateNetworkIPv4Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.UpdateNetworkAddressFamily{
|
||||
Ipv6: &iaas.UpdateNetworkIPv6Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_gateway_nil",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
AddressFamily: &iaas.UpdateNetworkAddressFamily{
|
||||
Ipv6: &iaas.UpdateNetworkIPv6Body{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toUpdatePayload(context.Background(), tt.input, &tt.state)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaas.NullableString{}))
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,215 @@
|
|||
package v2network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
networkModel "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
)
|
||||
|
||||
func DatasourceRead(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse, client *iaasalpha.APIClient, providerData core.ProviderData) { // nolint:gocritic // function signature required by Terraform
|
||||
var model networkModel.DataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
region := providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
networkResp, err := client.GetNetwork(ctx, projectId, region, networkId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading network",
|
||||
fmt.Sprintf("Network with ID %q does not exist in project %q.", networkId, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
err = mapDataSourceFields(ctx, networkResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network read")
|
||||
}
|
||||
|
||||
func mapDataSourceFields(ctx context.Context, networkResp *iaasalpha.Network, model *networkModel.DataSourceModel, region string) error {
|
||||
if networkResp == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
|
||||
var networkId string
|
||||
if model.NetworkId.ValueString() != "" {
|
||||
networkId = model.NetworkId.ValueString()
|
||||
} else if networkResp.Id != nil {
|
||||
networkId = *networkResp.Id
|
||||
} else {
|
||||
return fmt.Errorf("network id not present")
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, networkId)
|
||||
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// IPv4
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.Nameservers == nil {
|
||||
model.Nameservers = types.ListNull(types.StringType)
|
||||
model.IPv4Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respNameservers := *networkResp.Ipv4.Nameservers
|
||||
modelNameservers, err := utils.ListValuetoStringSlice(model.Nameservers)
|
||||
modelIPv4Nameservers, errIpv4 := utils.ListValuetoStringSlice(model.IPv4Nameservers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get current network nameservers from model: %w", err)
|
||||
}
|
||||
if errIpv4 != nil {
|
||||
return fmt.Errorf("get current IPv4 network nameservers from model: %w", errIpv4)
|
||||
}
|
||||
|
||||
reconciledNameservers := utils.ReconcileStringSlices(modelNameservers, respNameservers)
|
||||
reconciledIPv4Nameservers := utils.ReconcileStringSlices(modelIPv4Nameservers, respNameservers)
|
||||
|
||||
nameserversTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledNameservers)
|
||||
ipv4NameserversTF, ipv4Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv4Nameservers)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("map network nameservers: %w", core.DiagsToError(diags))
|
||||
}
|
||||
if ipv4Diags.HasError() {
|
||||
return fmt.Errorf("map IPv4 network nameservers: %w", core.DiagsToError(ipv4Diags))
|
||||
}
|
||||
|
||||
model.Nameservers = nameserversTF
|
||||
model.IPv4Nameservers = ipv4NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.Prefixes == nil {
|
||||
model.Prefixes = types.ListNull(types.StringType)
|
||||
model.IPv4Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixes := *networkResp.Ipv4.Prefixes
|
||||
prefixesTF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixes)
|
||||
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
|
||||
}
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.Gateway == nil {
|
||||
model.IPv4Gateway = types.StringNull()
|
||||
} else {
|
||||
model.IPv4Gateway = types.StringPointerValue(networkResp.Ipv4.GetGateway())
|
||||
}
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.PublicIp == nil {
|
||||
model.PublicIP = types.StringNull()
|
||||
} else {
|
||||
model.PublicIP = types.StringPointerValue(networkResp.Ipv4.PublicIp)
|
||||
}
|
||||
|
||||
// IPv6
|
||||
|
||||
if networkResp.Ipv6 == nil || networkResp.Ipv6.Nameservers == nil {
|
||||
model.IPv6Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respIPv6Nameservers := *networkResp.Ipv6.Nameservers
|
||||
modelIPv6Nameservers, errIpv6 := utils.ListValuetoStringSlice(model.IPv6Nameservers)
|
||||
if errIpv6 != nil {
|
||||
return fmt.Errorf("get current IPv6 network nameservers from model: %w", errIpv6)
|
||||
}
|
||||
|
||||
reconciledIPv6Nameservers := utils.ReconcileStringSlices(modelIPv6Nameservers, respIPv6Nameservers)
|
||||
|
||||
ipv6NameserversTF, ipv6Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv6Nameservers)
|
||||
if ipv6Diags.HasError() {
|
||||
return fmt.Errorf("map IPv6 network nameservers: %w", core.DiagsToError(ipv6Diags))
|
||||
}
|
||||
|
||||
model.IPv6Nameservers = ipv6NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.Ipv6 == nil || networkResp.Ipv6.Prefixes == nil {
|
||||
model.IPv6Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixesV6 := *networkResp.Ipv6.Prefixes
|
||||
prefixesV6TF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixesV6)
|
||||
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
|
||||
}
|
||||
|
||||
if networkResp.Ipv6 == nil || networkResp.Ipv6.Gateway == nil {
|
||||
model.IPv6Gateway = types.StringNull()
|
||||
} else {
|
||||
model.IPv6Gateway = types.StringPointerValue(networkResp.Ipv6.GetGateway())
|
||||
}
|
||||
|
||||
model.RoutingTableID = types.StringNull()
|
||||
if networkResp.RoutingTableId != nil {
|
||||
model.RoutingTableID = types.StringValue(*networkResp.RoutingTableId)
|
||||
}
|
||||
|
||||
model.NetworkId = types.StringValue(networkId)
|
||||
model.Name = types.StringPointerValue(networkResp.Name)
|
||||
model.Labels = labels
|
||||
model.Routed = types.BoolPointerValue(networkResp.Routed)
|
||||
model.Region = types.StringValue(region)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,387 @@
|
|||
package v2network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
|
||||
networkModel "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
)
|
||||
|
||||
const (
|
||||
testRegion = "region"
|
||||
)
|
||||
|
||||
func TestMapDataSourceFields(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
state networkModel.DataSourceModel
|
||||
input *iaasalpha.Network
|
||||
region string
|
||||
expected networkModel.DataSourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"id_ok",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Gateway: iaasalpha.NewNullableString(nil),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
IPv4Prefix: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefix: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"values_ok",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
Prefixes: &[]string{
|
||||
"192.168.42.0/24",
|
||||
"10.100.10.0/16",
|
||||
},
|
||||
PublicIp: utils.Ptr("publicIp"),
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
},
|
||||
Prefixes: &[]string{
|
||||
"fd12:3456:789a:1::/64",
|
||||
"fd12:3456:789a:2::/64",
|
||||
},
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(true),
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
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("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.Int64Value(64),
|
||||
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
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{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Nameservers: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
},
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_nameservers_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Nameservers: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
},
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_prefixes_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.42.0/24"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Prefixes: &[]string{
|
||||
"10.100.20.0/16",
|
||||
"10.100.10.0/16",
|
||||
},
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Value(16),
|
||||
IPv4Prefix: types.StringValue("10.100.20.0/16"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("10.100.20.0/16"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("10.100.20.0/16"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_prefixes_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Prefixes: &[]string{
|
||||
"fd12:3456:789a:3::/64",
|
||||
"fd12:3456:789a:4::/64",
|
||||
},
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Value(64),
|
||||
IPv6Prefix: types.StringValue("fd12:3456:789a:3::/64"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:3::/64"),
|
||||
types.StringValue("fd12:3456:789a:4::/64"),
|
||||
}),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_ipv6_gateway_nil",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
networkModel.DataSourceModel{},
|
||||
nil,
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
networkModel.DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&iaasalpha.Network{},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapDataSourceFields(context.Background(), tt.input, &tt.state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,555 @@
|
|||
package v2network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha/wait"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
networkModel "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
)
|
||||
|
||||
func Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse, client *iaasalpha.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model networkModel.Model
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toCreatePayload(ctx, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Creating API payload: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Create new network
|
||||
|
||||
network, err := client.CreateNetwork(ctx, projectId, region).CreateNetworkPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
networkId := *network.Id
|
||||
network, err = wait.CreateNetworkWaitHandler(ctx, client, projectId, region, networkId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Network creation waiting: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, network, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Set state to fully populated data
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network created")
|
||||
}
|
||||
|
||||
func Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse, client *iaasalpha.APIClient, providerData core.ProviderData) { // nolint:gocritic // function signature required by Terraform
|
||||
var model networkModel.Model
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
region := providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
networkResp, err := client.GetNetwork(ctx, projectId, region, networkId).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, networkResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network read")
|
||||
}
|
||||
|
||||
func Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse, client *iaasalpha.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model networkModel.Model
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
// Retrieve values from state
|
||||
var stateModel networkModel.Model
|
||||
diags = req.State.Get(ctx, &stateModel)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toUpdatePayload(ctx, &model, &stateModel)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Creating API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Update existing network
|
||||
err = client.PartialUpdateNetwork(ctx, projectId, region, networkId).PartialUpdateNetworkPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
waitResp, err := wait.UpdateNetworkWaitHandler(ctx, client, projectId, region, networkId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Network update waiting: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
err = mapFields(ctx, waitResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Network updated")
|
||||
}
|
||||
|
||||
func Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse, client *iaasalpha.APIClient) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from state
|
||||
var model networkModel.Model
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
networkId := model.NetworkId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
// Delete existing network
|
||||
err := client.DeleteNetwork(ctx, projectId, region, networkId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
_, err = wait.DeleteNetworkWaitHandler(ctx, client, projectId, region, networkId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network", fmt.Sprintf("Network deletion waiting: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Network deleted")
|
||||
}
|
||||
|
||||
// ImportState imports a resource into the Terraform state on success.
|
||||
// The expected format of the resource import identifier is: project_id,region,network_id
|
||||
func ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics,
|
||||
"Error importing network",
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[region],[network_id] Got: %q", req.ID),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
projectId := idParts[0]
|
||||
region := idParts[1]
|
||||
networkId := idParts[2]
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), region)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("network_id"), networkId)...)
|
||||
tflog.Info(ctx, "Network state imported")
|
||||
}
|
||||
|
||||
func mapFields(ctx context.Context, networkResp *iaasalpha.Network, model *networkModel.Model, region string) error {
|
||||
if networkResp == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
|
||||
var networkId string
|
||||
if model.NetworkId.ValueString() != "" {
|
||||
networkId = model.NetworkId.ValueString()
|
||||
} else if networkResp.Id != nil {
|
||||
networkId = *networkResp.Id
|
||||
} else {
|
||||
return fmt.Errorf("network id not present")
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, networkId)
|
||||
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// IPv4
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.Nameservers == nil {
|
||||
model.Nameservers = types.ListNull(types.StringType)
|
||||
model.IPv4Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respNameservers := *networkResp.Ipv4.Nameservers
|
||||
modelNameservers, err := utils.ListValuetoStringSlice(model.Nameservers)
|
||||
modelIPv4Nameservers, errIpv4 := utils.ListValuetoStringSlice(model.IPv4Nameservers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get current network nameservers from model: %w", err)
|
||||
}
|
||||
if errIpv4 != nil {
|
||||
return fmt.Errorf("get current IPv4 network nameservers from model: %w", errIpv4)
|
||||
}
|
||||
|
||||
reconciledNameservers := utils.ReconcileStringSlices(modelNameservers, respNameservers)
|
||||
reconciledIPv4Nameservers := utils.ReconcileStringSlices(modelIPv4Nameservers, respNameservers)
|
||||
|
||||
nameserversTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledNameservers)
|
||||
ipv4NameserversTF, ipv4Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv4Nameservers)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("map network nameservers: %w", core.DiagsToError(diags))
|
||||
}
|
||||
if ipv4Diags.HasError() {
|
||||
return fmt.Errorf("map IPv4 network nameservers: %w", core.DiagsToError(ipv4Diags))
|
||||
}
|
||||
|
||||
model.Nameservers = nameserversTF
|
||||
model.IPv4Nameservers = ipv4NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.Prefixes == nil {
|
||||
model.Prefixes = types.ListNull(types.StringType)
|
||||
model.IPv4Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixes := *networkResp.Ipv4.Prefixes
|
||||
prefixesTF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixes)
|
||||
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 {
|
||||
tflog.Error(ctx, fmt.Sprintf("ipv4_prefix_length: %+v", err))
|
||||
// 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
|
||||
}
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.Gateway == nil {
|
||||
model.IPv4Gateway = types.StringNull()
|
||||
} else {
|
||||
model.IPv4Gateway = types.StringPointerValue(networkResp.Ipv4.GetGateway())
|
||||
}
|
||||
|
||||
if networkResp.Ipv4 == nil || networkResp.Ipv4.PublicIp == nil {
|
||||
model.PublicIP = types.StringNull()
|
||||
} else {
|
||||
model.PublicIP = types.StringPointerValue(networkResp.Ipv4.PublicIp)
|
||||
}
|
||||
|
||||
// IPv6
|
||||
|
||||
if networkResp.Ipv6 == nil || networkResp.Ipv6.Nameservers == nil {
|
||||
model.IPv6Nameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respIPv6Nameservers := *networkResp.Ipv6.Nameservers
|
||||
modelIPv6Nameservers, errIpv6 := utils.ListValuetoStringSlice(model.IPv6Nameservers)
|
||||
if errIpv6 != nil {
|
||||
return fmt.Errorf("get current IPv6 network nameservers from model: %w", errIpv6)
|
||||
}
|
||||
|
||||
reconciledIPv6Nameservers := utils.ReconcileStringSlices(modelIPv6Nameservers, respIPv6Nameservers)
|
||||
|
||||
ipv6NameserversTF, ipv6Diags := types.ListValueFrom(ctx, types.StringType, reconciledIPv6Nameservers)
|
||||
if ipv6Diags.HasError() {
|
||||
return fmt.Errorf("map IPv6 network nameservers: %w", core.DiagsToError(ipv6Diags))
|
||||
}
|
||||
|
||||
model.IPv6Nameservers = ipv6NameserversTF
|
||||
}
|
||||
|
||||
if networkResp.Ipv6 == nil || networkResp.Ipv6.Prefixes == nil {
|
||||
model.IPv6Prefixes = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respPrefixesV6 := *networkResp.Ipv6.Prefixes
|
||||
prefixesV6TF, diags := types.ListValueFrom(ctx, types.StringType, respPrefixesV6)
|
||||
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
|
||||
}
|
||||
|
||||
if networkResp.Ipv6 == nil || networkResp.Ipv6.Gateway == nil {
|
||||
model.IPv6Gateway = types.StringNull()
|
||||
} else {
|
||||
model.IPv6Gateway = types.StringPointerValue(networkResp.Ipv6.GetGateway())
|
||||
}
|
||||
|
||||
if networkResp.RoutingTableId != nil {
|
||||
model.RoutingTableID = types.StringPointerValue(networkResp.RoutingTableId)
|
||||
} else {
|
||||
model.RoutingTableID = types.StringNull()
|
||||
}
|
||||
|
||||
model.NetworkId = types.StringValue(networkId)
|
||||
model.Name = types.StringPointerValue(networkResp.Name)
|
||||
model.Labels = labels
|
||||
model.Routed = types.BoolPointerValue(networkResp.Routed)
|
||||
model.Region = types.StringValue(region)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func toCreatePayload(ctx context.Context, model *networkModel.Model) (*iaasalpha.CreateNetworkPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
modelIPv6Nameservers := []string{}
|
||||
for _, ipv6ns := range model.IPv6Nameservers.Elements() {
|
||||
ipv6NameserverString, ok := ipv6ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv6Nameservers = append(modelIPv6Nameservers, ipv6NameserverString.ValueString())
|
||||
}
|
||||
|
||||
var ipv6Body *iaasalpha.CreateNetworkIPv6
|
||||
if !utils.IsUndefined(model.IPv6PrefixLength) {
|
||||
ipv6Body = &iaasalpha.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefixLength: &iaasalpha.CreateNetworkIPv6WithPrefixLength{
|
||||
Nameservers: &modelIPv6Nameservers,
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv6PrefixLength),
|
||||
},
|
||||
}
|
||||
} else if !utils.IsUndefined(model.IPv6Prefix) {
|
||||
var gateway *iaasalpha.NullableString
|
||||
if model.NoIPv6Gateway.ValueBool() {
|
||||
gateway = iaasalpha.NewNullableString(nil)
|
||||
} else if !(model.IPv6Gateway.IsUnknown() || model.IPv6Gateway.IsNull()) {
|
||||
gateway = iaasalpha.NewNullableString(conversion.StringValueToPointer(model.IPv6Gateway))
|
||||
}
|
||||
|
||||
ipv6Body = &iaasalpha.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaasalpha.CreateNetworkIPv6WithPrefix{
|
||||
Gateway: gateway,
|
||||
Nameservers: &modelIPv6Nameservers,
|
||||
Prefix: conversion.StringValueToPointer(model.IPv6Prefix),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
modelIPv4Nameservers := []string{}
|
||||
var modelIPv4List []attr.Value
|
||||
|
||||
if !(model.IPv4Nameservers.IsNull() || model.IPv4Nameservers.IsUnknown()) {
|
||||
modelIPv4List = model.IPv4Nameservers.Elements()
|
||||
} else {
|
||||
modelIPv4List = model.Nameservers.Elements()
|
||||
}
|
||||
|
||||
for _, ipv4ns := range modelIPv4List {
|
||||
ipv4NameserverString, ok := ipv4ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv4Nameservers = append(modelIPv4Nameservers, ipv4NameserverString.ValueString())
|
||||
}
|
||||
|
||||
var ipv4Body *iaasalpha.CreateNetworkIPv4
|
||||
if !utils.IsUndefined(model.IPv4PrefixLength) {
|
||||
ipv4Body = &iaasalpha.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefixLength: &iaasalpha.CreateNetworkIPv4WithPrefixLength{
|
||||
Nameservers: &modelIPv4Nameservers,
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv4PrefixLength),
|
||||
},
|
||||
}
|
||||
} else if !utils.IsUndefined(model.IPv4Prefix) {
|
||||
var gateway *iaasalpha.NullableString
|
||||
if model.NoIPv4Gateway.ValueBool() {
|
||||
gateway = iaasalpha.NewNullableString(nil)
|
||||
} else if !(model.IPv4Gateway.IsUnknown() || model.IPv4Gateway.IsNull()) {
|
||||
gateway = iaasalpha.NewNullableString(conversion.StringValueToPointer(model.IPv4Gateway))
|
||||
}
|
||||
|
||||
ipv4Body = &iaasalpha.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaasalpha.CreateNetworkIPv4WithPrefix{
|
||||
Nameservers: &modelIPv4Nameservers,
|
||||
Prefix: conversion.StringValueToPointer(model.IPv4Prefix),
|
||||
Gateway: gateway,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
labels, err := conversion.ToStringInterfaceMap(ctx, model.Labels)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("converting to Go map: %w", err)
|
||||
}
|
||||
|
||||
payload := iaasalpha.CreateNetworkPayload{
|
||||
Name: conversion.StringValueToPointer(model.Name),
|
||||
Labels: &labels,
|
||||
Routed: conversion.BoolValueToPointer(model.Routed),
|
||||
Ipv4: ipv4Body,
|
||||
Ipv6: ipv6Body,
|
||||
RoutingTableId: conversion.StringValueToPointer(model.RoutingTableID),
|
||||
}
|
||||
|
||||
return &payload, nil
|
||||
}
|
||||
|
||||
func toUpdatePayload(ctx context.Context, model, stateModel *networkModel.Model) (*iaasalpha.PartialUpdateNetworkPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
modelIPv6Nameservers := []string{}
|
||||
for _, ipv6ns := range model.IPv6Nameservers.Elements() {
|
||||
ipv6NameserverString, ok := ipv6ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv6Nameservers = append(modelIPv6Nameservers, ipv6NameserverString.ValueString())
|
||||
}
|
||||
|
||||
var ipv6Body *iaasalpha.UpdateNetworkIPv6Body
|
||||
if !(model.IPv6Nameservers.IsNull() || model.IPv6Nameservers.IsUnknown()) {
|
||||
ipv6Body = &iaasalpha.UpdateNetworkIPv6Body{
|
||||
Nameservers: &modelIPv6Nameservers,
|
||||
}
|
||||
|
||||
if model.NoIPv6Gateway.ValueBool() {
|
||||
ipv6Body.Gateway = iaasalpha.NewNullableString(nil)
|
||||
} else if !(model.IPv6Gateway.IsUnknown() || model.IPv6Gateway.IsNull()) {
|
||||
ipv6Body.Gateway = iaasalpha.NewNullableString(conversion.StringValueToPointer(model.IPv6Gateway))
|
||||
}
|
||||
}
|
||||
|
||||
modelIPv4Nameservers := []string{}
|
||||
var modelIPv4List []attr.Value
|
||||
|
||||
if !(model.IPv4Nameservers.IsNull() || model.IPv4Nameservers.IsUnknown()) {
|
||||
modelIPv4List = model.IPv4Nameservers.Elements()
|
||||
} else {
|
||||
modelIPv4List = model.Nameservers.Elements()
|
||||
}
|
||||
for _, ipv4ns := range modelIPv4List {
|
||||
ipv4NameserverString, ok := ipv4ns.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("type assertion failed")
|
||||
}
|
||||
modelIPv4Nameservers = append(modelIPv4Nameservers, ipv4NameserverString.ValueString())
|
||||
}
|
||||
|
||||
var ipv4Body *iaasalpha.UpdateNetworkIPv4Body
|
||||
if !model.IPv4Nameservers.IsNull() || !model.Nameservers.IsNull() {
|
||||
ipv4Body = &iaasalpha.UpdateNetworkIPv4Body{
|
||||
Nameservers: &modelIPv4Nameservers,
|
||||
}
|
||||
|
||||
if model.NoIPv4Gateway.ValueBool() {
|
||||
ipv4Body.Gateway = iaasalpha.NewNullableString(nil)
|
||||
} else if !(model.IPv4Gateway.IsUnknown() || model.IPv4Gateway.IsNull()) {
|
||||
ipv4Body.Gateway = iaasalpha.NewNullableString(conversion.StringValueToPointer(model.IPv4Gateway))
|
||||
}
|
||||
}
|
||||
currentLabels := stateModel.Labels
|
||||
labels, err := conversion.ToJSONMapPartialUpdatePayload(ctx, currentLabels, model.Labels)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("converting to Go map: %w", err)
|
||||
}
|
||||
|
||||
payload := iaasalpha.PartialUpdateNetworkPayload{
|
||||
Name: conversion.StringValueToPointer(model.Name),
|
||||
Labels: &labels,
|
||||
Ipv4: ipv4Body,
|
||||
Ipv6: ipv6Body,
|
||||
RoutingTableId: conversion.StringValueToPointer(model.RoutingTableID),
|
||||
}
|
||||
|
||||
return &payload, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,707 @@
|
|||
package v2network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
)
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
state model.Model
|
||||
input *iaasalpha.Network
|
||||
region string
|
||||
expected model.Model
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"id_ok",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Gateway: iaasalpha.NewNullableString(nil),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
IPv4Prefix: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefix: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"values_ok",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Nameservers: utils.Ptr([]string{"ns1", "ns2"}),
|
||||
Prefixes: utils.Ptr(
|
||||
[]string{
|
||||
"192.168.42.0/24",
|
||||
"10.100.10.0/16",
|
||||
},
|
||||
),
|
||||
PublicIp: utils.Ptr("publicIp"),
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Nameservers: utils.Ptr([]string{"ns1", "ns2"}),
|
||||
Prefixes: utils.Ptr([]string{
|
||||
"fd12:3456:789a:1::/64",
|
||||
"fd12:3456:789b:1::/64",
|
||||
}),
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(true),
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.42.0/24"),
|
||||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
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.Int64Value(64),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789b:1::/64"),
|
||||
}),
|
||||
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"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
}),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_nameservers_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
}),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns2"),
|
||||
types.StringValue("ns3"),
|
||||
}),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_prefixes_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.42.0/24"),
|
||||
types.StringValue("10.100.10.0/24"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Prefixes: utils.Ptr(
|
||||
[]string{
|
||||
"192.168.54.0/24",
|
||||
"192.168.55.0/24",
|
||||
},
|
||||
),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Value(24),
|
||||
IPv4Prefix: types.StringValue("192.168.54.0/24"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.54.0/24"),
|
||||
types.StringValue("192.168.55.0/24"),
|
||||
}),
|
||||
IPv4Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("192.168.54.0/24"),
|
||||
types.StringValue("192.168.55.0/24"),
|
||||
}),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_prefixes_changed_outside_tf",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Prefixes: utils.Ptr(
|
||||
[]string{
|
||||
"fd12:3456:789a:1::/64",
|
||||
"fd12:3456:789a:2::/64",
|
||||
},
|
||||
),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Value(64),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("fd12:3456:789a:1::/64"),
|
||||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
IPv6Prefix: types.StringValue("fd12:3456:789a:1::/64"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_ipv6_gateway_nil",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Name: types.StringNull(),
|
||||
Nameservers: types.ListNull(types.StringType),
|
||||
IPv4Nameservers: types.ListNull(types.StringType),
|
||||
IPv4PrefixLength: types.Int64Null(),
|
||||
IPv4Gateway: types.StringNull(),
|
||||
Prefixes: types.ListNull(types.StringType),
|
||||
IPv4Prefixes: types.ListNull(types.StringType),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
IPv6PrefixLength: types.Int64Null(),
|
||||
IPv6Gateway: types.StringNull(),
|
||||
IPv6Prefixes: types.ListNull(types.StringType),
|
||||
PublicIP: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Routed: types.BoolNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
model.Model{},
|
||||
nil,
|
||||
testRegion,
|
||||
model.Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&iaasalpha.Network{},
|
||||
testRegion,
|
||||
model.Model{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapFields(context.Background(), tt.input, &tt.state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *model.Model
|
||||
expected *iaasalpha.CreateNetworkPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(false),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv4Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaasalpha.CreateNetworkIPv4WithPrefix{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(false),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_okay",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(false),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv4Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaasalpha.CreateNetworkIPv4WithPrefix{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(false),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(false),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
IPv6Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaasalpha.CreateNetworkIPv6WithPrefix{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
Routed: utils.Ptr(false),
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toCreatePayload(context.Background(), tt.input)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaasalpha.NullableString{}))
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToUpdatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *model.Model
|
||||
state model.Model
|
||||
expected *iaasalpha.PartialUpdateNetworkPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.UpdateNetworkIPv4Body{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_nameservers_okay",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.UpdateNetworkIPv4Body{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv4_gateway_nil",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.UpdateNetworkIPv4Body{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_default_ok",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.UpdateNetworkIPv6Body{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_gateway_nil",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
types.StringValue("ns2"),
|
||||
}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
},
|
||||
model.Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.UpdateNetworkIPv6Body{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
}),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toUpdatePayload(context.Background(), tt.input, &tt.state)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaasalpha.NullableString{}))
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue