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 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/internal/svc"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/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. existAdapter := types.AdapterResp{}
  27. resp = &types.AdapterResp{}
  28. utils.Convert(req, &adapter)
  29. //check name
  30. exist := l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
  31. if !errors.Is(exist, gorm.ErrRecordNotFound) {
  32. resp = &existAdapter
  33. return resp, errors.New("name already exists")
  34. }
  35. //check type
  36. var arr = [...]string{"0", "1", "2"}
  37. found := false
  38. for _, str := range arr {
  39. if str == req.Type {
  40. found = true
  41. break
  42. }
  43. }
  44. if found == false {
  45. return nil, errors.New("type not found")
  46. }
  47. //check resourceTypeDict
  48. sql := `select t_dict_item.item_value
  49. from t_dict
  50. join t_dict_item on t_dict.id = t_dict_item.dict_id
  51. where dict_code = 'adapter_type' and item_value = ?
  52. and t_dict_item.parent_id != 0`
  53. err = l.svcCtx.DbEngin.Raw(sql, req.ResourceType).First(&types.DictItemInfo{}).Error
  54. if errors.Is(err, gorm.ErrRecordNotFound) {
  55. return nil, errors.New("resourceType error, please check!")
  56. }
  57. adapter.Id = utils.GenSnowflakeIDStr()
  58. adapter.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  59. result := l.svcCtx.DbEngin.Table("t_adapter").Create(&adapter)
  60. if result.Error != nil {
  61. return resp, result.Error
  62. }
  63. _ = l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
  64. resp = &existAdapter
  65. return resp, nil
  66. }

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.