parent
460c18c202
commit
53a3697850
124 changed files with 8342 additions and 6042 deletions
|
|
@ -2,14 +2,11 @@ package network
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/v1network"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/v2network"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
iaasAlphaUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaasalpha/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
|
|
@ -18,7 +15,9 @@ import (
|
|||
"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/conversion"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
||||
|
|
@ -27,6 +26,30 @@ var (
|
|||
_ datasource.DataSource = &networkDataSource{}
|
||||
)
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
// NewNetworkDataSource is a helper function to simplify the provider implementation.
|
||||
func NewNetworkDataSource() datasource.DataSource {
|
||||
return &networkDataSource{}
|
||||
|
|
@ -34,11 +57,8 @@ func NewNetworkDataSource() datasource.DataSource {
|
|||
|
||||
// networkDataSource is the data source implementation.
|
||||
type networkDataSource struct {
|
||||
client *iaas.APIClient
|
||||
// alphaClient will be used in case the experimental flag "network" is set
|
||||
alphaClient *iaasalpha.APIClient
|
||||
isExperimental bool
|
||||
providerData core.ProviderData
|
||||
client *iaas.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
|
|
@ -53,24 +73,11 @@ func (d *networkDataSource) Configure(ctx context.Context, req datasource.Config
|
|||
return
|
||||
}
|
||||
|
||||
d.isExperimental = features.CheckExperimentEnabledWithoutError(ctx, &d.providerData, features.NetworkExperiment, "stackit_network", core.Datasource, &resp.Diagnostics)
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
if d.isExperimental {
|
||||
alphaApiClient := iaasAlphaUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
d.alphaClient = alphaApiClient
|
||||
} else {
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
d.client = apiClient
|
||||
}
|
||||
d.client = apiClient
|
||||
tflog.Info(ctx, "IaaS client configured")
|
||||
}
|
||||
|
||||
|
|
@ -197,9 +204,199 @@ func (d *networkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
|
|||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (d *networkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
if !d.isExperimental {
|
||||
v1network.DatasourceRead(ctx, req, resp, d.client)
|
||||
} else {
|
||||
v2network.DatasourceRead(ctx, req, resp, d.alphaClient, d.providerData)
|
||||
var model 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 := d.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
networkResp, err := d.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 *iaas.Network, model *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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
package v2network
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
||||
|
||||
"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 (
|
||||
|
|
@ -19,26 +19,26 @@ const (
|
|||
func TestMapDataSourceFields(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
state networkModel.DataSourceModel
|
||||
input *iaasalpha.Network
|
||||
state DataSourceModel
|
||||
input *iaas.Network
|
||||
region string
|
||||
expected networkModel.DataSourceModel
|
||||
expected DataSourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"id_ok",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Gateway: iaasalpha.NewNullableString(nil),
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Gateway: iaas.NewNullableString(nil),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -64,14 +64,14 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"values_ok",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -81,9 +81,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
"10.100.10.0/16",
|
||||
},
|
||||
PublicIp: utils.Ptr("publicIp"),
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Ipv6: &iaas.NetworkIPv6{
|
||||
Nameservers: &[]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -92,7 +92,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
"fd12:3456:789a:1::/64",
|
||||
"fd12:3456:789a:2::/64",
|
||||
},
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
|
|
@ -100,7 +100,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
Routed: utils.Ptr(true),
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -146,7 +146,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_nameservers_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -158,9 +158,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Nameservers: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
|
|
@ -168,7 +168,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -192,7 +192,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_nameservers_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -200,9 +200,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Ipv6: &iaas.NetworkIPv6{
|
||||
Nameservers: &[]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
|
|
@ -210,7 +210,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -231,7 +231,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_prefixes_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -239,9 +239,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
types.StringValue("10.100.10.0/16"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Prefixes: &[]string{
|
||||
"10.100.20.0/16",
|
||||
"10.100.10.0/16",
|
||||
|
|
@ -249,7 +249,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -276,7 +276,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_prefixes_changed_outside_tf",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -284,9 +284,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Ipv6: &iaas.NetworkIPv6{
|
||||
Prefixes: &[]string{
|
||||
"fd12:3456:789a:3::/64",
|
||||
"fd12:3456:789a:4::/64",
|
||||
|
|
@ -294,7 +294,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -318,15 +318,15 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_ipv6_gateway_nil",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -350,20 +350,20 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
networkModel.DataSourceModel{},
|
||||
DataSourceModel{},
|
||||
nil,
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{},
|
||||
DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
networkModel.DataSourceModel{
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&iaasalpha.Network{},
|
||||
&iaas.Network{},
|
||||
testRegion,
|
||||
networkModel.DataSourceModel{},
|
||||
DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
|
@ -3,9 +3,13 @@ package network
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
|
|
@ -18,16 +22,12 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"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/iaasalpha"
|
||||
"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"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/model"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/v1network"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/network/utils/v2network"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
iaasAlphaUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaasalpha/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
|
@ -37,6 +37,7 @@ var (
|
|||
_ resource.Resource = &networkResource{}
|
||||
_ resource.ResourceWithConfigure = &networkResource{}
|
||||
_ resource.ResourceWithImportState = &networkResource{}
|
||||
_ resource.ResourceWithModifyPlan = &networkResource{}
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -46,6 +47,32 @@ const (
|
|||
"In cases where `ipv4_nameservers` are defined within the resource, the existing behavior will remain unchanged."
|
||||
)
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
// NewNetworkResource is a helper function to simplify the provider implementation.
|
||||
func NewNetworkResource() resource.Resource {
|
||||
return &networkResource{}
|
||||
|
|
@ -53,11 +80,8 @@ func NewNetworkResource() resource.Resource {
|
|||
|
||||
// networkResource is the resource implementation.
|
||||
type networkResource struct {
|
||||
client *iaas.APIClient
|
||||
// alphaClient will be used in case the experimental flag "network" is set
|
||||
alphaClient *iaasalpha.APIClient
|
||||
isExperimental bool
|
||||
providerData core.ProviderData
|
||||
client *iaas.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the resource type name.
|
||||
|
|
@ -73,31 +97,18 @@ func (r *networkResource) Configure(ctx context.Context, req resource.ConfigureR
|
|||
return
|
||||
}
|
||||
|
||||
r.isExperimental = features.CheckExperimentEnabledWithoutError(ctx, &r.providerData, features.NetworkExperiment, "stackit_network", core.Resource, &resp.Diagnostics)
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
if r.isExperimental {
|
||||
alphaApiClient := iaasAlphaUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
r.alphaClient = alphaApiClient
|
||||
} else {
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
r.client = apiClient
|
||||
}
|
||||
r.client = apiClient
|
||||
tflog.Info(ctx, "IaaS client configured")
|
||||
}
|
||||
|
||||
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
||||
// Use the modifier to set the effective region in the current plan.
|
||||
func (r *networkResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel model.Model
|
||||
var configModel Model
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
|
|
@ -107,7 +118,7 @@ func (r *networkResource) ModifyPlan(ctx context.Context, req resource.ModifyPla
|
|||
return
|
||||
}
|
||||
|
||||
var planModel model.Model
|
||||
var planModel Model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -118,10 +129,6 @@ func (r *networkResource) ModifyPlan(ctx context.Context, req resource.ModifyPla
|
|||
addIPv4Warning(&resp.Diagnostics)
|
||||
}
|
||||
|
||||
// If the v1 api is used, it's not required to get the fallback region because it isn't used
|
||||
if !r.isExperimental {
|
||||
return
|
||||
}
|
||||
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -134,7 +141,7 @@ func (r *networkResource) ModifyPlan(ctx context.Context, req resource.ModifyPla
|
|||
}
|
||||
|
||||
func (r *networkResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
|
||||
var resourceModel model.Model
|
||||
var resourceModel Model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &resourceModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -143,14 +150,6 @@ func (r *networkResource) ValidateConfig(ctx context.Context, req resource.Valid
|
|||
if !resourceModel.Nameservers.IsUnknown() && !resourceModel.IPv4Nameservers.IsUnknown() && !resourceModel.Nameservers.IsNull() && !resourceModel.IPv4Nameservers.IsNull() {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring network", "You cannot provide both the `nameservers` and `ipv4_nameservers` fields simultaneously. Please remove the deprecated `nameservers` field, and use `ipv4_nameservers` to configure nameservers for IPv4.")
|
||||
}
|
||||
if !r.isExperimental {
|
||||
if !utils.IsUndefined(resourceModel.Region) {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring network", "Setting the `region` is not supported yet. This can only be configured when the experiments `network` is set.")
|
||||
}
|
||||
if !utils.IsUndefined(resourceModel.RoutingTableID) {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring network", "Setting the field `routing_table_id` is not supported yet. This can only be configured when the experiments `network` is set.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ConfigValidators validates the resource configuration
|
||||
|
|
@ -192,7 +191,7 @@ func (r *networkResource) Schema(_ context.Context, _ resource.SchemaRequest, re
|
|||
Description: description,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`network_id`\".",
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`network_id`\".",
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.UseStateForUnknown(),
|
||||
|
|
@ -359,7 +358,7 @@ func (r *networkResource) Schema(_ context.Context, _ resource.SchemaRequest, re
|
|||
},
|
||||
},
|
||||
"routing_table_id": schema.StringAttribute{
|
||||
Description: "Can only be used when experimental \"network\" is set.\nThe ID of the routing table associated with the network.",
|
||||
Description: "The ID of the routing table associated with the network.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
|
|
@ -374,7 +373,7 @@ func (r *networkResource) Schema(_ context.Context, _ resource.SchemaRequest, re
|
|||
Optional: true,
|
||||
// must be computed to allow for storing the override value from the provider
|
||||
Computed: true,
|
||||
Description: "Can only be used when experimental \"network\" is set.\nThe resource region. If not defined, the provider region is used.",
|
||||
Description: "The resource region. If not defined, the provider region is used.",
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplaceIfConfigured(),
|
||||
},
|
||||
|
|
@ -386,59 +385,568 @@ func (r *networkResource) Schema(_ context.Context, _ resource.SchemaRequest, re
|
|||
// Create creates the resource and sets the initial Terraform state.
|
||||
func (r *networkResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var planModel model.Model
|
||||
diags := req.Plan.Get(ctx, &planModel)
|
||||
var model Model
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// When IPv4Nameserver is not set, print warning that the behavior of ipv4_nameservers will change
|
||||
if utils.IsUndefined(planModel.IPv4Nameservers) {
|
||||
if utils.IsUndefined(model.IPv4Nameservers) {
|
||||
addIPv4Warning(&resp.Diagnostics)
|
||||
}
|
||||
|
||||
if !r.isExperimental {
|
||||
v1network.Create(ctx, req, resp, r.client)
|
||||
} else {
|
||||
v2network.Create(ctx, req, resp, r.alphaClient)
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
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 := r.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
|
||||
ctx = tflog.SetField(ctx, "network_id", networkId)
|
||||
|
||||
network, err = wait.CreateNetworkWaitHandler(ctx, r.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
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (r *networkResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
if !r.isExperimental {
|
||||
v1network.Read(ctx, req, resp, r.client)
|
||||
} else {
|
||||
v2network.Read(ctx, req, resp, r.alphaClient, r.providerData)
|
||||
var model 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 := r.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 := r.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")
|
||||
}
|
||||
|
||||
// Update updates the resource and sets the updated Terraform state on success.
|
||||
func (r *networkResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
if !r.isExperimental {
|
||||
v1network.Update(ctx, req, resp, r.client)
|
||||
} else {
|
||||
v2network.Update(ctx, req, resp, r.alphaClient)
|
||||
// Retrieve values from plan
|
||||
var model 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 := r.providerData.GetRegionWithOverride(model.Region)
|
||||
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 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 = r.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, r.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")
|
||||
}
|
||||
|
||||
// Delete deletes the resource and removes the Terraform state on success.
|
||||
func (r *networkResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
if !r.isExperimental {
|
||||
v1network.Delete(ctx, req, resp, r.client)
|
||||
} else {
|
||||
v2network.Delete(ctx, req, resp, r.alphaClient)
|
||||
// Retrieve values from state
|
||||
var model 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 := r.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, r.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,network_id
|
||||
// The expected format of the resource import identifier is: project_id,region,network_id
|
||||
func (r *networkResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
if !r.isExperimental {
|
||||
v1network.ImportState(ctx, req, resp)
|
||||
} else {
|
||||
v2network.ImportState(ctx, req, resp)
|
||||
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 *iaas.Network, model *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
|
||||
}
|
||||
|
||||
model.IPv4PrefixLength = types.Int64Null()
|
||||
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
|
||||
}
|
||||
|
||||
model.IPv6PrefixLength = types.Int64Null()
|
||||
model.IPv6Prefix = types.StringNull()
|
||||
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.StringPointerValue(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
|
||||
}
|
||||
|
||||
func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNetworkPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
var modelIPv6Nameservers []string
|
||||
// Is true when IPv6Nameservers is not null or unset
|
||||
if !utils.IsUndefined(model.IPv6Nameservers) {
|
||||
// If ipv6Nameservers is empty, modelIPv6Nameservers will be set to an empty slice.
|
||||
// empty slice != nil slice. Empty slice will result in an empty list in the payload []. Nil slice will result in a payload without the property set
|
||||
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 *iaas.CreateNetworkIPv6
|
||||
if !utils.IsUndefined(model.IPv6PrefixLength) {
|
||||
ipv6Body = &iaas.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefixLength: &iaas.CreateNetworkIPv6WithPrefixLength{
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv6PrefixLength),
|
||||
},
|
||||
}
|
||||
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
ipv6Body.CreateNetworkIPv6WithPrefixLength.Nameservers = &modelIPv6Nameservers
|
||||
}
|
||||
} else if !utils.IsUndefined(model.IPv6Prefix) {
|
||||
var gateway *iaas.NullableString
|
||||
if model.NoIPv6Gateway.ValueBool() {
|
||||
gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv6Gateway.IsUnknown() || model.IPv6Gateway.IsNull()) {
|
||||
gateway = iaas.NewNullableString(conversion.StringValueToPointer(model.IPv6Gateway))
|
||||
}
|
||||
|
||||
ipv6Body = &iaas.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaas.CreateNetworkIPv6WithPrefix{
|
||||
Gateway: gateway,
|
||||
Prefix: conversion.StringValueToPointer(model.IPv6Prefix),
|
||||
},
|
||||
}
|
||||
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
ipv6Body.CreateNetworkIPv6WithPrefix.Nameservers = &modelIPv6Nameservers
|
||||
}
|
||||
}
|
||||
|
||||
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 *iaas.CreateNetworkIPv4
|
||||
if !utils.IsUndefined(model.IPv4PrefixLength) {
|
||||
ipv4Body = &iaas.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefixLength: &iaas.CreateNetworkIPv4WithPrefixLength{
|
||||
Nameservers: &modelIPv4Nameservers,
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv4PrefixLength),
|
||||
},
|
||||
}
|
||||
} else if !utils.IsUndefined(model.IPv4Prefix) {
|
||||
var gateway *iaas.NullableString
|
||||
if model.NoIPv4Gateway.ValueBool() {
|
||||
gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv4Gateway.IsUnknown() || model.IPv4Gateway.IsNull()) {
|
||||
gateway = iaas.NewNullableString(conversion.StringValueToPointer(model.IPv4Gateway))
|
||||
}
|
||||
|
||||
ipv4Body = &iaas.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaas.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 := iaas.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 *Model) (*iaas.PartialUpdateNetworkPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
var modelIPv6Nameservers []string
|
||||
// Is true when IPv6Nameservers is not null or unset
|
||||
if !utils.IsUndefined(model.IPv6Nameservers) {
|
||||
// If ipv6Nameservers is empty, modelIPv6Nameservers will be set to an empty slice.
|
||||
// empty slice != nil slice. Empty slice will result in an empty list in the payload []. Nil slice will result in a payload without the property set
|
||||
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 *iaas.UpdateNetworkIPv6Body
|
||||
if modelIPv6Nameservers != nil || !utils.IsUndefined(model.NoIPv6Gateway) || !utils.IsUndefined(model.IPv6Gateway) {
|
||||
ipv6Body = &iaas.UpdateNetworkIPv6Body{}
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
ipv6Body.Nameservers = &modelIPv6Nameservers
|
||||
}
|
||||
|
||||
if model.NoIPv6Gateway.ValueBool() {
|
||||
ipv6Body.Gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv6Gateway.IsUnknown() || model.IPv6Gateway.IsNull()) {
|
||||
ipv6Body.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())
|
||||
}
|
||||
|
||||
var ipv4Body *iaas.UpdateNetworkIPv4Body
|
||||
if !model.IPv4Nameservers.IsNull() || !model.Nameservers.IsNull() {
|
||||
ipv4Body = &iaas.UpdateNetworkIPv4Body{
|
||||
Nameservers: &modelIPv4Nameservers,
|
||||
}
|
||||
|
||||
if model.NoIPv4Gateway.ValueBool() {
|
||||
ipv4Body.Gateway = iaas.NewNullableString(nil)
|
||||
} else if !(model.IPv4Gateway.IsUnknown() || model.IPv4Gateway.IsNull()) {
|
||||
ipv4Body.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,
|
||||
Ipv4: ipv4Body,
|
||||
Ipv6: ipv6Body,
|
||||
RoutingTableId: conversion.StringValueToPointer(model.RoutingTableID),
|
||||
}
|
||||
|
||||
return &payload, nil
|
||||
}
|
||||
|
||||
func addIPv4Warning(diags *diag.Diagnostics) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package v2network
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
@ -8,34 +8,33 @@ import (
|
|||
"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"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
||||
)
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
state model.Model
|
||||
input *iaasalpha.Network
|
||||
state Model
|
||||
input *iaas.Network
|
||||
region string
|
||||
expected model.Model
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"id_ok",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Gateway: iaasalpha.NewNullableString(nil),
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Gateway: iaas.NewNullableString(nil),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -61,14 +60,14 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"values_ok",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Nameservers: utils.Ptr([]string{"ns1", "ns2"}),
|
||||
Prefixes: utils.Ptr(
|
||||
[]string{
|
||||
|
|
@ -77,15 +76,15 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
),
|
||||
PublicIp: utils.Ptr("publicIp"),
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Ipv6: &iaas.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")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
|
|
@ -93,7 +92,7 @@ func TestMapFields(t *testing.T) {
|
|||
Routed: utils.Ptr(true),
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -139,7 +138,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_nameservers_changed_outside_tf",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -151,9 +150,9 @@ func TestMapFields(t *testing.T) {
|
|||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
|
|
@ -161,7 +160,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -185,7 +184,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_nameservers_changed_outside_tf",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -193,9 +192,9 @@ func TestMapFields(t *testing.T) {
|
|||
types.StringValue("ns2"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Ipv6: &iaas.NetworkIPv6{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns2",
|
||||
"ns3",
|
||||
|
|
@ -203,7 +202,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -224,7 +223,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_prefixes_changed_outside_tf",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -232,9 +231,9 @@ func TestMapFields(t *testing.T) {
|
|||
types.StringValue("10.100.10.0/24"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv4: &iaasalpha.NetworkIPv4{
|
||||
Ipv4: &iaas.NetworkIPv4{
|
||||
Prefixes: utils.Ptr(
|
||||
[]string{
|
||||
"192.168.54.0/24",
|
||||
|
|
@ -244,7 +243,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -271,7 +270,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_prefixes_changed_outside_tf",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
IPv6Prefixes: types.ListValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -279,9 +278,9 @@ func TestMapFields(t *testing.T) {
|
|||
types.StringValue("fd12:3456:789a:2::/64"),
|
||||
}),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
Ipv6: &iaasalpha.NetworkIPv6{
|
||||
Ipv6: &iaas.NetworkIPv6{
|
||||
Prefixes: utils.Ptr(
|
||||
[]string{
|
||||
"fd12:3456:789a:1::/64",
|
||||
|
|
@ -291,7 +290,7 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -315,15 +314,15 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_ipv6_gateway_nil",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
},
|
||||
&iaasalpha.Network{
|
||||
&iaas.Network{
|
||||
Id: utils.Ptr("nid"),
|
||||
},
|
||||
testRegion,
|
||||
model.Model{
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,nid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
|
|
@ -347,20 +346,20 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
model.Model{},
|
||||
Model{},
|
||||
nil,
|
||||
testRegion,
|
||||
model.Model{},
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&iaasalpha.Network{},
|
||||
&iaas.Network{},
|
||||
testRegion,
|
||||
model.Model{},
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
|
@ -386,13 +385,13 @@ func TestMapFields(t *testing.T) {
|
|||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *model.Model
|
||||
expected *iaasalpha.CreateNetworkPayload
|
||||
input *Model
|
||||
expected *iaas.CreateNetworkPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_ok",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -405,11 +404,11 @@ func TestToCreatePayload(t *testing.T) {
|
|||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv4Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaasalpha.CreateNetworkIPv4WithPrefix{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Ipv4: &iaas.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaas.CreateNetworkIPv4WithPrefix{
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -426,7 +425,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_nameservers_okay",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -439,11 +438,11 @@ func TestToCreatePayload(t *testing.T) {
|
|||
IPv4Gateway: types.StringValue("gateway"),
|
||||
IPv4Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaasalpha.CreateNetworkIPv4WithPrefix{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Ipv4: &iaas.CreateNetworkIPv4{
|
||||
CreateNetworkIPv4WithPrefix: &iaas.CreateNetworkIPv4WithPrefix{
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -460,7 +459,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_default_ok",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -473,11 +472,11 @@ func TestToCreatePayload(t *testing.T) {
|
|||
IPv6Gateway: types.StringValue("gateway"),
|
||||
IPv6Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaasalpha.CreateNetworkIPv6WithPrefix{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Ipv6: &iaas.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaas.CreateNetworkIPv6WithPrefix{
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -494,7 +493,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_nameserver_null",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
|
|
@ -504,12 +503,12 @@ func TestToCreatePayload(t *testing.T) {
|
|||
IPv6Gateway: types.StringValue("gateway"),
|
||||
IPv6Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaasalpha.CreateNetworkIPv6WithPrefix{
|
||||
Ipv6: &iaas.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaas.CreateNetworkIPv6WithPrefix{
|
||||
Nameservers: nil,
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
|
|
@ -522,7 +521,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_nameserver_empty_list",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
|
|
@ -532,12 +531,12 @@ func TestToCreatePayload(t *testing.T) {
|
|||
IPv6Gateway: types.StringValue("gateway"),
|
||||
IPv6Prefix: types.StringValue("prefix"),
|
||||
},
|
||||
&iaasalpha.CreateNetworkPayload{
|
||||
&iaas.CreateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaasalpha.CreateNetworkIPv6WithPrefix{
|
||||
Ipv6: &iaas.CreateNetworkIPv6{
|
||||
CreateNetworkIPv6WithPrefix: &iaas.CreateNetworkIPv6WithPrefix{
|
||||
Nameservers: utils.Ptr([]string{}),
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
},
|
||||
},
|
||||
|
|
@ -559,7 +558,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaasalpha.NullableString{}))
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaas.NullableString{}))
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
|
@ -571,14 +570,14 @@ func TestToCreatePayload(t *testing.T) {
|
|||
func TestToUpdatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *model.Model
|
||||
state model.Model
|
||||
expected *iaasalpha.PartialUpdateNetworkPayload
|
||||
input *Model
|
||||
state Model
|
||||
expected *iaas.PartialUpdateNetworkPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_ok",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -590,15 +589,15 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.UpdateNetworkIPv4Body{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Ipv4: &iaas.UpdateNetworkIPv4Body{
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -612,7 +611,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_nameservers_okay",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -624,15 +623,15 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
Routed: types.BoolValue(true),
|
||||
IPv4Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.UpdateNetworkIPv4Body{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Ipv4: &iaas.UpdateNetworkIPv4Body{
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -646,7 +645,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv4_gateway_nil",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -657,14 +656,14 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
},
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv4: &iaasalpha.UpdateNetworkIPv4Body{
|
||||
Ipv4: &iaas.UpdateNetworkIPv4Body{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -678,7 +677,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_default_ok",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -690,15 +689,15 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
Routed: types.BoolValue(true),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.UpdateNetworkIPv6Body{
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Ipv6: &iaas.UpdateNetworkIPv6Body{
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -712,7 +711,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_gateway_nil",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ns1"),
|
||||
|
|
@ -723,14 +722,14 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
}),
|
||||
Routed: types.BoolValue(true),
|
||||
},
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.UpdateNetworkIPv6Body{
|
||||
Ipv6: &iaas.UpdateNetworkIPv6Body{
|
||||
Nameservers: utils.Ptr([]string{
|
||||
"ns1",
|
||||
"ns2",
|
||||
|
|
@ -744,7 +743,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_nameserver_null",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
|
|
@ -753,16 +752,16 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
Routed: types.BoolValue(true),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.UpdateNetworkIPv6Body{
|
||||
Ipv6: &iaas.UpdateNetworkIPv6Body{
|
||||
Nameservers: nil,
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
|
|
@ -772,7 +771,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"ipv6_nameserver_empty_list",
|
||||
&model.Model{
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{}),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
|
|
@ -781,16 +780,16 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
Routed: types.BoolValue(true),
|
||||
IPv6Gateway: types.StringValue("gateway"),
|
||||
},
|
||||
model.Model{
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
NetworkId: types.StringValue("nid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
&iaasalpha.PartialUpdateNetworkPayload{
|
||||
&iaas.PartialUpdateNetworkPayload{
|
||||
Name: utils.Ptr("name"),
|
||||
Ipv6: &iaasalpha.UpdateNetworkIPv6Body{
|
||||
Ipv6: &iaas.UpdateNetworkIPv6Body{
|
||||
Nameservers: utils.Ptr([]string{}),
|
||||
Gateway: iaasalpha.NewNullableString(utils.Ptr("gateway")),
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
|
|
@ -809,7 +808,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaasalpha.NullableString{}))
|
||||
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaas.NullableString{}))
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
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"`
|
||||
}
|
||||
|
|
@ -1,208 +0,0 @@
|
|||
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
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -1,352 +0,0 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,558 +0,0 @@
|
|||
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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// 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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
_, 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 || len(*networkResp.PrefixesV6) == 0 {
|
||||
model.IPv6Prefixes = types.ListNull(types.StringType)
|
||||
model.IPv6Prefix = types.StringNull()
|
||||
model.IPv6PrefixLength = types.Int64Null()
|
||||
} 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{}
|
||||
|
||||
var modelIPv6Nameservers []string
|
||||
// Is true when IPv6Nameservers is not null or unset
|
||||
if !utils.IsUndefined(model.IPv6Nameservers) {
|
||||
// If ipv6Nameservers is empty, modelIPv6Nameservers will be set to an empty slice.
|
||||
// empty slice != nil slice. Empty slice will result in an empty list in the payload []. Nil slice will result in a payload without the property set
|
||||
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 !utils.IsUndefined(model.IPv6Prefix) || !utils.IsUndefined(model.IPv6PrefixLength) || (modelIPv6Nameservers != nil) {
|
||||
addressFamily.Ipv6 = &iaas.CreateNetworkIPv6Body{
|
||||
Prefix: conversion.StringValueToPointer(model.IPv6Prefix),
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv6PrefixLength),
|
||||
}
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
addressFamily.Ipv6.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.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{}
|
||||
|
||||
var modelIPv6Nameservers []string
|
||||
// Is true when IPv6Nameservers is not null or unset
|
||||
if !utils.IsUndefined(model.IPv6Nameservers) {
|
||||
// If ipv6Nameservers is empty, modelIPv6Nameservers will be set to an empty slice.
|
||||
// empty slice != nil slice. Empty slice will result in an empty list in the payload []. Nil slice will result in a payload without the property set
|
||||
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 !utils.IsUndefined(model.NoIPv6Gateway) || !utils.IsUndefined(model.IPv6Gateway) || modelIPv6Nameservers != nil {
|
||||
addressFamily.Ipv6 = &iaas.UpdateNetworkIPv6Body{}
|
||||
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
addressFamily.Ipv6.Nameservers = &modelIPv6Nameservers
|
||||
}
|
||||
|
||||
if model.NoIPv6Gateway.ValueBool() {
|
||||
addressFamily.Ipv6.Gateway = iaas.NewNullableString(nil)
|
||||
} else if !utils.IsUndefined(model.IPv6Gateway) {
|
||||
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
|
||||
}
|
||||
|
|
@ -1,811 +0,0 @@
|
|||
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,
|
||||
},
|
||||
{
|
||||
"ipv6_nameserver_null",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
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: nil,
|
||||
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_nameserver_empty_list",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{}),
|
||||
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: utils.Ptr([]string{}),
|
||||
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,
|
||||
},
|
||||
{
|
||||
"ipv6_nameserver_null",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListNull(types.StringType),
|
||||
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: nil,
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ipv6_nameserver_empty_list",
|
||||
&model.Model{
|
||||
Name: types.StringValue("name"),
|
||||
IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{}),
|
||||
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{},
|
||||
Gateway: iaas.NewNullableString(utils.Ptr("gateway")),
|
||||
},
|
||||
},
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,220 +0,0 @@
|
|||
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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -1,603 +0,0 @@
|
|||
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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// 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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
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 = core.InitProviderContext(ctx)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
_, 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")
|
||||
}
|
||||
|
||||
var modelIPv6Nameservers []string
|
||||
// Is true when IPv6Nameservers is not null or unset
|
||||
if !utils.IsUndefined(model.IPv6Nameservers) {
|
||||
// If ipv6Nameservers is empty, modelIPv6Nameservers will be set to an empty slice.
|
||||
// empty slice != nil slice. Empty slice will result in an empty list in the payload []. Nil slice will result in a payload without the property set
|
||||
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{
|
||||
PrefixLength: conversion.Int64ValueToPointer(model.IPv6PrefixLength),
|
||||
},
|
||||
}
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
ipv6Body.CreateNetworkIPv6WithPrefixLength.Nameservers = &modelIPv6Nameservers
|
||||
}
|
||||
} 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,
|
||||
Prefix: conversion.StringValueToPointer(model.IPv6Prefix),
|
||||
},
|
||||
}
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
ipv6Body.CreateNetworkIPv6WithPrefix.Nameservers = &modelIPv6Nameservers
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
var modelIPv6Nameservers []string
|
||||
// Is true when IPv6Nameservers is not null or unset
|
||||
if !utils.IsUndefined(model.IPv6Nameservers) {
|
||||
// If ipv6Nameservers is empty, modelIPv6Nameservers will be set to an empty slice.
|
||||
// empty slice != nil slice. Empty slice will result in an empty list in the payload []. Nil slice will result in a payload without the property set
|
||||
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 modelIPv6Nameservers != nil || !utils.IsUndefined(model.NoIPv6Gateway) || !utils.IsUndefined(model.IPv6Gateway) {
|
||||
ipv6Body = &iaasalpha.UpdateNetworkIPv6Body{}
|
||||
// IPv6 nameservers should only be set, if it contains any value. If the slice is nil, it should NOT be set.
|
||||
// Setting it to a nil slice would result in a payload, where nameservers is set to null in the json payload,
|
||||
// but it should actually be unset. Setting it to "null" will result in an error, because it's NOT nullable.
|
||||
if modelIPv6Nameservers != nil {
|
||||
ipv6Body.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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue