...
1 package services
2
3 import (
4 "net/http"
5
6 "github.com/gin-gonic/gin"
7 "k8s.io/apimachinery/pkg/api/errors"
8 "sigs.k8s.io/controller-runtime/pkg/client"
9
10 api "edge-infra.dev/pkg/edge/device-registrar/api/v1alpha1"
11 "edge-infra.dev/pkg/edge/device-registrar/config"
12 )
13
14
15
16
17
18
19
20
21
22
23 func DeleteDeviceService(c *gin.Context) {
24 k8sClient, ctx, cancel := config.GetClientandContext(c)
25 defer cancel()
26
27 name := c.Param("name")
28 if name == "" {
29 c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"})
30 return
31 }
32
33
34 device := &api.ExternalDevice{}
35 err := k8sClient.Get(ctx, client.ObjectKey{
36 Name: name,
37 Namespace: config.Namespace,
38 }, device)
39 if err != nil {
40 if errors.IsNotFound(err) {
41 c.JSON(http.StatusNotFound, gin.H{"error": "device not found"})
42 return
43 }
44 c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve device:" + err.Error()})
45 return
46 }
47
48
49 if err := deleteDevice(ctx, k8sClient, device); err != nil {
50 c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete device:" + err.Error()})
51 return
52 }
53
54 c.JSON(http.StatusNoContent, nil)
55 }
56
View as plain text