Skip to content

Commit

Permalink
Merge pull request #258 from ionos-cloud/fix/ipv6_missing_nic_property
Browse files Browse the repository at this point in the history
Fix: add missing ipv6 nic property
  • Loading branch information
ncldmg committed Jul 22, 2024
2 parents a28a965 + e4b7404 commit 24eeea6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
41 changes: 37 additions & 4 deletions internal/clients/compute/nic/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package nic
import (
"context"
"fmt"
"net"
"reflect"

"github.com/rung/go-safecast"

sdkgo "github.com/ionos-cloud/sdk-go/v6"
"github.com/rung/go-safecast"

"github.com/ionos-cloud/crossplane-provider-ionoscloud/apis/compute/v1alpha1"
"github.com/ionos-cloud/crossplane-provider-ionoscloud/internal/clients"
Expand Down Expand Up @@ -164,7 +164,13 @@ func GenerateUpdateNicInput(cr *v1alpha1.Nic, ips []string) (*sdkgo.NicPropertie
instanceUpdateInput.SetName(cr.Spec.ForProvider.Name)
}
if len(ips) > 0 {
instanceUpdateInput.SetIps(ips)
ipv4s, ipv6s := GetIPvSlices(ips)
if len(ipv4s) > 0 {
instanceUpdateInput.SetIps(ips)
}
if len(ipv6s) > 0 {
instanceUpdateInput.SetIpv6Ips(ips)
}
}
if cr.Spec.ForProvider.FirewallType != "" {
instanceUpdateInput.SetFirewallType(cr.Spec.ForProvider.FirewallType)
Expand All @@ -178,6 +184,13 @@ func GenerateUpdateNicInput(cr *v1alpha1.Nic, ips []string) (*sdkgo.NicPropertie

// IsNicUpToDate returns true if the Nic is up-to-date or false if it does not
func IsNicUpToDate(cr *v1alpha1.Nic, nic sdkgo.Nic, ips []string) bool { // nolint:gocyclo
var ipv4s []string
var ipv6s []string

if len(ips) > 0 {
ipv4s, ipv6s = GetIPvSlices(ips)
}

switch {
case cr == nil && nic.Properties == nil:
return true
Expand All @@ -201,9 +214,29 @@ func IsNicUpToDate(cr *v1alpha1.Nic, nic sdkgo.Nic, ips []string) bool { // noli
return false
case nic.Properties.Vnet != nil && *nic.Properties.Vnet != cr.Spec.ForProvider.Vnet:
return false
case len(ips) != 0 && nic.Properties.HasIps() && !utils.ContainsStringSlices(ips, *nic.Properties.Ips):
case len(ipv4s) != 0 && nic.Properties.HasIps() && !utils.ContainsStringSlices(ipv4s, *nic.Properties.Ips):
return false
case len(ipv6s) != 0 && nic.Properties.HasIpv6Ips() && !utils.ContainsStringSlices(ipv6s, *nic.Properties.Ipv6Ips):
return false
default:
return true
}
}

// GetIPvSlices splits ips into ipv4 / ipv6 slices
func GetIPvSlices(ips []string) (ipv4s []string, ipv6s []string) {
for _, ip := range ips {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
continue
}

if parsedIP.To4() != nil {
ipv4s = append(ipv4s, ip)
} else {
ipv6s = append(ipv6s, ip)
}
}

return ipv4s, ipv6s
}
40 changes: 29 additions & 11 deletions internal/clients/compute/nic/nic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"testing"

"github.com/ionos-cloud/sdk-go-bundle/shared"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"

"github.com/ionos-cloud/crossplane-provider-ionoscloud/apis/compute/v1alpha1"

ionoscloud "github.com/ionos-cloud/sdk-go/v6"
)

func TestIsNicUpToDate(t *testing.T) {
Expand Down Expand Up @@ -73,7 +72,8 @@ func TestIsNicUpToDate(t *testing.T) {
FirewallActive: shared.ToPtr(false),
FirewallType: shared.ToPtr("INGRESS"),
Vnet: shared.ToPtr("1"),
}},
},
},
},
want: true,
},
Expand Down Expand Up @@ -140,7 +140,8 @@ func TestIsNicUpToDate(t *testing.T) {
},
Properties: &ionoscloud.NicProperties{
Name: shared.ToPtr("empty"),
}},
},
},
},
want: true,
},
Expand All @@ -155,6 +156,7 @@ func TestIsNicUpToDate(t *testing.T) {
IPs: []string{
"10.10.10.10",
"10.10.10.11",
"2001:0db8:85a3::8a2e:0370:7335",
},
},
},
Expand All @@ -164,6 +166,7 @@ func TestIsNicUpToDate(t *testing.T) {
ips: []string{
"10.10.10.10",
"10.10.10.11",
"2001:0db8:85a3::8a2e:0370:7335",
},
Nic: ionoscloud.Nic{
Properties: &ionoscloud.NicProperties{
Expand All @@ -172,7 +175,11 @@ func TestIsNicUpToDate(t *testing.T) {
"10.11.12.13",
"192.168.8.14",
},
}},
Ipv6Ips: &[]string{
"2001:0db8:85a3::8a2e:0370:7334",
},
},
},
},
want: false,
},
Expand All @@ -187,6 +194,7 @@ func TestIsNicUpToDate(t *testing.T) {
IPs: []string{
"10.10.10.10",
"10.10.10.11",
"2001:0db8:85a3::8a2e:0370:7335",
},
},
},
Expand All @@ -196,6 +204,7 @@ func TestIsNicUpToDate(t *testing.T) {
ips: []string{
"10.10.10.10",
"10.10.10.11",
"2001:0db8:85a3::8a2e:0370:7335",
},
Nic: ionoscloud.Nic{
Properties: &ionoscloud.NicProperties{
Expand All @@ -204,7 +213,11 @@ func TestIsNicUpToDate(t *testing.T) {
"10.10.10.10",
"10.10.10.11",
},
}},
Ipv6Ips: &[]string{
"2001:0db8:85a3::8a2e:0370:7335",
},
},
},
},
want: true,
},
Expand All @@ -221,7 +234,8 @@ func TestIsNicUpToDate(t *testing.T) {
Nic: ionoscloud.Nic{
Properties: &ionoscloud.NicProperties{
Dhcpv6: nil,
}},
},
},
},
want: true,
},
Expand All @@ -238,7 +252,8 @@ func TestIsNicUpToDate(t *testing.T) {
Nic: ionoscloud.Nic{
Properties: &ionoscloud.NicProperties{
Dhcpv6: ionoscloud.PtrBool(true),
}},
},
},
},
want: true,
},
Expand All @@ -255,7 +270,8 @@ func TestIsNicUpToDate(t *testing.T) {
Nic: ionoscloud.Nic{
Properties: &ionoscloud.NicProperties{
Dhcpv6: nil,
}},
},
},
},
want: true,
},
Expand All @@ -272,7 +288,8 @@ func TestIsNicUpToDate(t *testing.T) {
Nic: ionoscloud.Nic{
Properties: &ionoscloud.NicProperties{
Dhcpv6: ionoscloud.PtrBool(true),
}},
},
},
},
want: true,
},
Expand All @@ -289,7 +306,8 @@ func TestIsNicUpToDate(t *testing.T) {
Nic: ionoscloud.Nic{
Properties: &ionoscloud.NicProperties{
Dhcpv6: ionoscloud.PtrBool(true),
}},
},
},
},
want: false,
},
Expand Down

0 comments on commit 24eeea6

Please sign in to comment.