You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

createadapterlogic.go 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package adapters
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type CreateAdapterLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewCreateAdapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAdapterLogic {
  18. return &CreateAdapterLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. func (l *CreateAdapterLogic) CreateAdapter(req *types.AdapterCreateReq) (resp *types.AdapterResp, err error) {
  25. adapter := types.AdapterInfo{}
  26. utils.Convert(req, &adapter)
  27. //check name
  28. exist := l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&types.AdapterInfo{}).Error
  29. if !errors.Is(exist, gorm.ErrRecordNotFound) {
  30. return nil, errors.New("name already exists")
  31. }
  32. //check type
  33. var arr = [...]string{"0", "1", "2"}
  34. found := false
  35. for _, str := range arr {
  36. if str == req.Type {
  37. found = true
  38. break
  39. }
  40. }
  41. if found == false {
  42. return nil, errors.New("type not found")
  43. }
  44. //check resourceTypeDict
  45. sql := `select t_dict_item.item_value
  46. from t_dict
  47. join t_dict_item on t_dict.id = t_dict_item.dict_id
  48. where dict_code = 'adapter_type' and item_value = ?
  49. and t_dict_item.parent_id != 0`
  50. err = l.svcCtx.DbEngin.Raw(sql, req.ResourceType).First(&types.DictItemInfo{}).Error
  51. if errors.Is(err, gorm.ErrRecordNotFound) {
  52. return nil, errors.New("resourceType error, please check!")
  53. }
  54. adapter.Id = utils.GenSnowflakeIDStr()
  55. adapter.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  56. result := l.svcCtx.DbEngin.Table("t_adapter").Create(&adapter)
  57. if result.Error != nil {
  58. return nil, result.Error
  59. }
  60. return
  61. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.