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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/models"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  10. "gorm.io/gorm"
  11. "strconv"
  12. "time"
  13. )
  14. type CreateAdapterLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewCreateAdapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAdapterLogic {
  20. return &CreateAdapterLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. func (l *CreateAdapterLogic) CreateAdapter(req *types.AdapterCreateReq) (resp *types.AdapterResp, err error) {
  27. adapter := types.AdapterInfo{}
  28. existAdapter := types.AdapterResp{}
  29. resp = &types.AdapterResp{}
  30. utils.Convert(req, &adapter)
  31. //check name
  32. exist := l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
  33. if !errors.Is(exist, gorm.ErrRecordNotFound) {
  34. resp = &existAdapter
  35. return resp, errors.New("name already exists")
  36. }
  37. //check type
  38. var arr = [...]string{"0", "1", "2"}
  39. found := false
  40. for _, str := range arr {
  41. if str == req.Type {
  42. found = true
  43. break
  44. }
  45. }
  46. if found == false {
  47. return nil, errors.New("type not found")
  48. }
  49. //check resourceTypeDict
  50. sql := `select t_dict_item.item_value
  51. from t_dict
  52. join t_dict_item on t_dict.id = t_dict_item.dict_id
  53. where dict_code = 'adapter_type' and item_value = ?
  54. and t_dict_item.parent_id != 0`
  55. err = l.svcCtx.DbEngin.Raw(sql, req.ResourceType).First(&types.DictItemInfo{}).Error
  56. if errors.Is(err, gorm.ErrRecordNotFound) {
  57. return nil, errors.New("resourceType error, please check!")
  58. }
  59. adapter.Id = utils.GenSnowflakeIDStr()
  60. adapter.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  61. result := l.svcCtx.DbEngin.Table("t_adapter").Create(&adapter)
  62. if result.Error != nil {
  63. return resp, result.Error
  64. }
  65. _ = l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
  66. resp = &existAdapter
  67. Tadapter := models.TAdapterInfo{}
  68. if req.Type == "0" {
  69. if req.ResourceType == "01" {
  70. Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
  71. Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
  72. Tadapter.InfoName = "CloudInfoList"
  73. l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
  74. resp.InfoName = "CloudInfoList"
  75. } else if req.ResourceType == "02" {
  76. Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
  77. Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
  78. Tadapter.InfoName = "VmInfoList"
  79. l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
  80. resp.InfoName = "VmInfoList"
  81. }
  82. } else if req.Type == "1" {
  83. Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
  84. Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
  85. Tadapter.InfoName = "AiInfoList"
  86. l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
  87. resp.InfoName = "AiInfoList"
  88. } else if req.Type == "2" {
  89. Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
  90. Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
  91. Tadapter.InfoName = "HpcInfoList"
  92. l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
  93. resp.InfoName = "HpcInfoList"
  94. }
  95. return resp, nil
  96. }

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.