configitem.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package controller
  2. import (
  3. "fmt"
  4. "git.cestong.com.cn/cecf/cecf-golib/pkg/apollo"
  5. "git.cestong.com.cn/cecf/cecf-golib/pkg/errors"
  6. gin2 "git.cestong.com.cn/cecf/cecf-golib/pkg/gin"
  7. "github.com/gin-gonic/gin"
  8. "github.com/go-playground/validator/v10"
  9. "gorm.io/gorm"
  10. )
  11. type ConfigItemController struct {
  12. db *gorm.DB
  13. val *validator.Validate
  14. apolloCli *apollo.HTTPClient
  15. }
  16. func NewConfigItemController(db *gorm.DB, val *validator.Validate, apolloCli *apollo.HTTPClient) *ConfigItemController {
  17. return &ConfigItemController{db: db, val: val, apolloCli: apolloCli}
  18. }
  19. type CreateConfigItemReqVO struct {
  20. Block string `json:"block"`
  21. Key string `json:"key" validate:"min=1"`
  22. Value string `json:"value"`
  23. Comment string `json:"comment"`
  24. ConstraintIDList []uint `json:"constraintIDList"`
  25. }
  26. func (cic *ConfigItemController) Create(c *gin.Context) (interface{}, error) {
  27. appID, namespace := c.Param("appID"), c.Param("namespaceID")
  28. var req CreateConfigItemReqVO
  29. if errParse := c.ShouldBindJSON(&req); errParse != nil {
  30. return nil, errors.ErrBadRequest
  31. }
  32. if errVal := cic.val.Struct(&req); errVal != nil {
  33. return nil, errors.ErrBadRequest
  34. }
  35. // call apollo create item
  36. apolloKey := fmt.Sprintf("%s.%s", req.Block, req.Key)
  37. if errCreateApollo := cic.apolloCli.CreateConfigItem(appID, namespace, apolloKey,
  38. req.Value); errCreateApollo != nil {
  39. return nil, errCreateApollo
  40. }
  41. // save constraint list to db
  42. return gin2.SuccessGeneralVO(), nil
  43. }