...

Source file src/edge-infra.dev/pkg/edge/device-registrar/services/device_delete_service.go

Documentation: edge-infra.dev/pkg/edge/device-registrar/services

     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  // swagger:route DELETE /devices/{name} additional DeleteDevice
    15  //
    16  // Deletes a device with the given device name.
    17  //
    18  // responses:
    19  //
    20  //		204: description:Success
    21  //	    400: description:Bad Request
    22  //		500: description:Internal Server Error
    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  	// Find the device CR by name
    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  	// Delete the device
    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