package services import ( "net/http" "github.com/gin-gonic/gin" "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" api "edge-infra.dev/pkg/edge/device-registrar/api/v1alpha1" "edge-infra.dev/pkg/edge/device-registrar/config" ) // swagger:route DELETE /devices/{name} additional DeleteDevice // // Deletes a device with the given device name. // // responses: // // 204: description:Success // 400: description:Bad Request // 500: description:Internal Server Error func DeleteDeviceService(c *gin.Context) { k8sClient, ctx, cancel := config.GetClientandContext(c) defer cancel() name := c.Param("name") if name == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"}) return } // Find the device CR by name device := &api.ExternalDevice{} err := k8sClient.Get(ctx, client.ObjectKey{ Name: name, Namespace: config.Namespace, }, device) if err != nil { if errors.IsNotFound(err) { c.JSON(http.StatusNotFound, gin.H{"error": "device not found"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve device:" + err.Error()}) return } // Delete the device if err := deleteDevice(ctx, k8sClient, device); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete device:" + err.Error()}) return } c.JSON(http.StatusNoContent, nil) }