/
machine_networkinterface_detach.go
51 lines (40 loc) · 1.27 KB
/
machine_networkinterface_detach.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package server
import (
"context"
"fmt"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"github.com/ironcore-dev/libvirt-provider/api"
)
func (s *Server) DetachNetworkInterface(
ctx context.Context,
req *iri.DetachNetworkInterfaceRequest,
) (*iri.DetachNetworkInterfaceResponse, error) {
log := s.loggerFrom(ctx)
log.V(1).Info("Detaching nic from machine")
if req == nil {
return nil, fmt.Errorf("DetachNetworkInterface is nil")
}
apiMachine, err := s.machineStore.Get(ctx, req.MachineId)
if err != nil {
return nil, fmt.Errorf("failed to get machine: %w", err)
}
var updatedNICS []*api.NetworkInterfaceSpec
found := false
for _, nic := range apiMachine.Spec.NetworkInterfaces {
if nic.Name != req.Name {
updatedNICS = append(updatedNICS, nic)
} else {
found = true
}
}
if !found {
return nil, fmt.Errorf("nic '%s' not found in machine '%s'", req.Name, req.MachineId)
}
apiMachine.Spec.NetworkInterfaces = updatedNICS
if _, err := s.machineStore.Update(ctx, apiMachine); err != nil {
return nil, fmt.Errorf("failed to update machine: %w", err)
}
return &iri.DetachNetworkInterfaceResponse{}, nil
}