@@ -3,10 +3,11 @@ package ionos
33import (
44 "context"
55 "fmt"
6- "github.com/GDATASoftwareAG/cluster-api-provider-ionoscloud/api/v1alpha1"
7- ionoscloud "github.com/ionos-cloud/sdk-go/v6"
86 "strings"
97 "sync"
8+
9+ "github.com/GDATASoftwareAG/cluster-api-provider-ionoscloud/api/v1alpha1"
10+ ionoscloud "github.com/ionos-cloud/sdk-go/v6"
1011)
1112
1213var _ Client = (* APIClient )(nil )
@@ -29,7 +30,7 @@ type IPBlockAPI interface {
2930type LanAPI interface {
3031 CreateLan (ctx context.Context , datacenterId string , public bool ) (ionoscloud.LanPost , * ionoscloud.APIResponse , error )
3132 GetLan (ctx context.Context , datacenterId , lanId string ) (ionoscloud.Lan , * ionoscloud.APIResponse , error )
32- EnsureFailoverIPOnLan (ctx context.Context , datacenterId , lanId , ip , nicUuid string ) error
33+ EnsureFailoverIPsOnLan (ctx context.Context , datacenterId , lanId , nicUuid string , ips [] string ) error
3334}
3435
3536type DefaultAPI interface {
@@ -51,7 +52,7 @@ type ServerAPI interface {
5152 CreateServer (ctx context.Context , datacenterId string , server ionoscloud.Server ) (ionoscloud.Server , * ionoscloud.APIResponse , error )
5253 GetServer (ctx context.Context , datacenterId , serverId string ) (ionoscloud.Server , * ionoscloud.APIResponse , error )
5354 DeleteServer (ctx context.Context , datacenterId , serverId string ) (* ionoscloud.APIResponse , error )
54- EnsureAdditionalIPOnNic (ctx context.Context , datacenterId , serverId , nic , ip string ) error
55+ EnsureAdditionalIPsOnNic (ctx context.Context , datacenterId , serverId , nicUuid string , ips [] string ) error
5556}
5657
5758type Client interface {
@@ -85,53 +86,66 @@ type APIClient struct {
8586 client * ionoscloud.APIClient
8687}
8788
88- func (c * APIClient ) EnsureAdditionalIPOnNic (ctx context.Context , datacenterId , serverId , nicUuid , ip string ) error {
89+ func (c * APIClient ) EnsureAdditionalIPsOnNic (ctx context.Context , datacenterId , serverId , nicUuid string , toEnsureIPs [] string ) error {
8990 serverId = strings .TrimPrefix (serverId , "ionos://" )
9091 nic , _ , err := c .client .NetworkInterfacesApi .DatacentersServersNicsFindById (ctx , datacenterId , serverId , nicUuid ).Execute ()
9192 if err != nil {
9293 return err
9394 }
94- ips := []string {}
95+ ips := make ( []string , 0 )
9596 if nic .Properties .Ips != nil {
96- for _ , current := range * nic .Properties .Ips {
97+ ips = * nic .Properties .Ips
98+ }
99+ for _ , ip := range toEnsureIPs {
100+ toAdd := true
101+ for _ , current := range ips {
97102 if current == ip {
98- return nil
103+ toAdd = false
104+ break
99105 }
100106 }
101- ips = * nic .Properties .Ips
107+ if toAdd {
108+ ips = append (ips , ip )
109+ }
102110 }
103- ips = append ( ips , ip )
111+
104112 _ , _ , err = c .client .NetworkInterfacesApi .DatacentersServersNicsPatch (ctx , datacenterId , serverId , nicUuid ).Nic (ionoscloud.NicProperties {
105113 Ips : & ips ,
106114 }).Execute ()
107115 return err
108116}
109117
110- func (c * APIClient ) EnsureFailoverIPOnLan (ctx context.Context , datacenterId , lanId , ip , nicUuid string ) error {
118+ func (c * APIClient ) EnsureFailoverIPsOnLan (ctx context.Context , datacenterId , lanId , nicUuid string , toEnsureIPs [] string ) error {
111119 lan , _ , err := c .client .LANsApi .DatacentersLansFindById (ctx , datacenterId , lanId ).Execute ()
112120 if err != nil {
113121 return err
114122 }
115- failovers := []ionoscloud.IPFailover {}
116- ignore := false
123+
124+ requireRegister := false
125+ failovers := make ([]ionoscloud.IPFailover , 0 )
117126 if lan .Properties .IpFailover != nil {
118- for _ , failover := range * lan .Properties .IpFailover {
119- if * failover .Ip == ip {
120- if * failover .NicUuid == nicUuid {
121- return nil
122- }
123- failover .NicUuid = & nicUuid
124- ignore = true
127+ failovers = * lan .Properties .IpFailover
128+ }
129+ for _ , ip := range toEnsureIPs {
130+ toAdd := true
131+ for _ , current := range failovers {
132+ if * current .Ip == ip {
133+ toAdd = false
134+ break
125135 }
126- failovers = append (failovers , failover )
136+ }
137+ if toAdd {
138+ requireRegister = true
139+ failovers = append (failovers , ionoscloud.IPFailover {
140+ Ip : & ip ,
141+ NicUuid : & nicUuid ,
142+ })
127143 }
128144 }
129- if ! ignore {
130- failovers = append (failovers , ionoscloud.IPFailover {
131- Ip : & ip ,
132- NicUuid : & nicUuid ,
133- })
145+ if ! requireRegister {
146+ return nil
134147 }
148+
135149 _ , _ , err = c .client .LANsApi .DatacentersLansPatch (ctx , datacenterId , lanId ).Lan (ionoscloud.LanProperties {
136150 IpFailover : & failovers ,
137151 }).Execute ()
0 commit comments