feat(business-line): auto bind Wayne namespace on create
This commit is contained in:
@@ -144,9 +144,53 @@ func (h *BusinessLineHandler) Create(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
if req.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "business line name is required"})
|
||||
return
|
||||
}
|
||||
|
||||
var existing model.BusinessLine
|
||||
if err := h.db.Where("name = ?", req.Name).First(&existing).Error; err == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "business line already exists"})
|
||||
return
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if h.wayne == nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "wayne native api is not configured"})
|
||||
return
|
||||
}
|
||||
namespace, err := h.wayne.EnsureNamespace(c.Request.Context(), req.Name)
|
||||
if err != nil {
|
||||
writeWayneRoleBindingError(c, nil, err)
|
||||
return
|
||||
}
|
||||
if namespace == nil || namespace.ID == 0 {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "wayne namespace response is invalid"})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(namespace.Name) == "" {
|
||||
namespace.Name = req.Name
|
||||
}
|
||||
if strings.TrimSpace(namespace.KubeNamespace) == "" {
|
||||
namespace.KubeNamespace = req.Name
|
||||
}
|
||||
|
||||
item := model.BusinessLine{Name: req.Name}
|
||||
if err := h.db.Create(&item).Error; err != nil {
|
||||
if err := h.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(&item).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Create(&model.BusinessLineWayneNamespace{
|
||||
BusinessLineID: item.ID,
|
||||
WayneNamespaceID: namespace.ID,
|
||||
WayneNamespaceName: namespace.Name,
|
||||
KubeNamespace: namespace.KubeNamespace,
|
||||
}).Error
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user