@@ -28,6 +28,12 @@ type userpassword struct {
2828 Tokens []string `json:"tokens,omitempty"`
2929}
3030
31+ type Server struct {
32+ Name string
33+ ProviderID string
34+ DatacenterID string
35+ }
36+
3137func New (datacenterId string , secret []byte ) (IONOSClient , error ) {
3238 var cfg * ionoscloud.Configuration
3339 if secret [0 ] == '{' {
@@ -65,6 +71,119 @@ func (a *IONOSClient) GetServer(ctx context.Context, providerID string) (*cloudp
6571 return a .convertServerToInstanceMetadata (ctx , & server )
6672}
6773
74+ func (a * IONOSClient ) RemoveIPFromNode (ctx context.Context , loadBalancerIP , providerID string ) error {
75+ if a .client == nil {
76+ return errors .New ("client isn't initialized" )
77+ }
78+
79+ serverReq := a .client .NetworkInterfacesApi .DatacentersServersNicsGet (ctx , a .DatacenterId , providerID )
80+ nics , req , err := serverReq .Depth (3 ).Execute ()
81+ if err != nil {
82+ if req != nil && req .StatusCode == 404 {
83+ return nil
84+ }
85+ return err
86+ }
87+
88+ if ! nics .HasItems () {
89+ return errors .New ("node has no nics" )
90+ }
91+
92+ primaryNic := getPrimaryNic (* nics .Items )
93+ ips := * primaryNic .Properties .Ips
94+
95+ for idx , v := range ips {
96+ if v == loadBalancerIP {
97+ ips = append (ips [:idx ], ips [idx + 1 :]... )
98+ }
99+ }
100+
101+ _ , _ , err = a .client .NetworkInterfacesApi .DatacentersServersNicsPatch (ctx , a .DatacenterId , providerID , * primaryNic .Id ).Nic (ionoscloud.NicProperties {
102+ Ips : & ips ,
103+ }).Execute ()
104+
105+ return err
106+ }
107+
108+ func getPrimaryNic (nics []ionoscloud.Nic ) * ionoscloud.Nic {
109+ for _ , nic := range nics {
110+ if * nic .Properties .PciSlot == 6 {
111+ return & nic
112+ }
113+ }
114+ return nil
115+ }
116+
117+ func (a * IONOSClient ) AttachIPToNode (ctx context.Context , loadBalancerIP , providerID string ) (bool , error ) {
118+ if a .client == nil {
119+ return false , errors .New ("client isn't initialized" )
120+ }
121+
122+ serverReq := a .client .NetworkInterfacesApi .DatacentersServersNicsGet (ctx , a .DatacenterId , providerID )
123+ nics , req , err := serverReq .Depth (3 ).Execute ()
124+ if err != nil {
125+ if req != nil && req .StatusCode == 404 {
126+ return false , nil
127+ }
128+ return false , err
129+ }
130+
131+ if ! nics .HasItems () {
132+ return false , errors .New ("node has no nics" )
133+ }
134+
135+ primaryNic := getPrimaryNic (* nics .Items )
136+ ips := * primaryNic .Properties .Ips
137+ ips = append (ips , loadBalancerIP )
138+
139+ _ , _ , err = a .client .NetworkInterfacesApi .DatacentersServersNicsPatch (ctx , a .DatacenterId , providerID , * primaryNic .Id ).Nic (ionoscloud.NicProperties {
140+ Ips : & ips ,
141+ }).Execute ()
142+
143+ return true , err
144+ }
145+
146+ func (a * IONOSClient ) GetServerByIP (ctx context.Context , loadBalancerIP string ) (* Server , error ) {
147+ if a .client == nil {
148+ return nil , errors .New ("client isn't initialized" )
149+ }
150+
151+ serverReq := a .client .ServersApi .DatacentersServersGet (ctx , a .DatacenterId )
152+ servers , _ , err := serverReq .Depth (3 ).Execute ()
153+ if err != nil {
154+ return nil , err
155+ }
156+
157+ if ! servers .HasItems () {
158+ return nil , nil
159+ }
160+
161+ for _ , server := range * servers .Items {
162+ klog .Infof ("Checking server %s" , server .Properties .Name )
163+ if ! server .Entities .HasNics () {
164+ continue
165+ }
166+ for _ , nic := range * server .Entities .Nics .Items {
167+ if nic .Properties .HasIps () {
168+ for _ , ip := range * nic .Properties .Ips {
169+ klog .Infof ("Found ip %s" , ip )
170+ if loadBalancerIP == ip {
171+ klog .Info ("Its a match!" )
172+ return & Server {
173+ Name : * server .Properties .Name ,
174+ ProviderID : * server .Id ,
175+ DatacenterID : a .DatacenterId ,
176+ }, nil
177+ }
178+ }
179+ }
180+ }
181+ }
182+ klog .Infof ("IP %s not found on any node in datacenter %s" , loadBalancerIP , a .DatacenterId )
183+
184+ return nil , nil
185+ }
186+
68187func (a * IONOSClient ) datacenterLocation (ctx context.Context ) (string , error ) {
69188 if a .client == nil {
70189 return "" , errors .New ("client isn't initialized" )
0 commit comments