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.

createclusterlogic.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package adapters
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  8. tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  9. "gorm.io/gorm"
  10. "io/ioutil"
  11. "k8s.io/apimachinery/pkg/util/json"
  12. "net/http"
  13. "net/url"
  14. "time"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type CreateClusterLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewCreateClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateClusterLogic {
  23. return &CreateClusterLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. }
  28. }
  29. func (l *CreateClusterLogic) CreateCluster(req *types.ClusterCreateReq) (resp *types.ClusterResp, err error) {
  30. // 校验集群名称是否唯一
  31. var count int64
  32. l.svcCtx.DbEngin.Table("t_cluster").Where("name = ?", req.Name).Count(&count)
  33. if count > 0 {
  34. return nil, errors.New("the cluster name is already in use")
  35. }
  36. // 校验驱动器是否存在
  37. adapter := &types.AdapterInfo{}
  38. result := l.svcCtx.DbEngin.Table("t_adapter").First(&adapter, req.AdapterId)
  39. if errors.Is(result.Error, gorm.ErrRecordNotFound) {
  40. return nil, errors.New("adapter does not exist")
  41. }
  42. cluster := types.ClusterInfo{}
  43. tool.Convert(req, &cluster)
  44. cluster.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  45. cluster.OwnerId = "0"
  46. // 获取集群经纬度
  47. //location, err := GeoMap(req.RegionName)
  48. //if err != nil {
  49. // return nil, err
  50. //}
  51. //cluster.Location = location
  52. cluster.Id = tool.GenSnowflakeIDStr()
  53. tx := l.svcCtx.DbEngin.Table("t_cluster").Create(&cluster)
  54. if tx.Error != nil {
  55. logx.Errorf(tx.Error.Error())
  56. return nil, errors.New("cluster create failed")
  57. }
  58. // push cluster info to adapter
  59. go func() {
  60. var adapterServer string
  61. l.svcCtx.DbEngin.Raw("select server from t_adapter where id = ?", req.AdapterId).Scan(&adapterServer)
  62. l.svcCtx.HttpClient.R().
  63. SetBody(&types.ClusterInfo{
  64. Name: req.Name,
  65. Server: req.Server,
  66. Token: req.Token,
  67. MonitorServer: req.MonitorServer,
  68. }).
  69. ForceContentType("application/json").
  70. Post(adapterServer + "/api/v1/cluster/info")
  71. }()
  72. return
  73. }
  74. func GeoMap(address string) (string, error) {
  75. // 此处填写您在控制台-应用管理-创建应用后获取的AK
  76. ak := "d3cc9eee0266d39a52498726d1b82f87"
  77. // 接口地址
  78. uri := "https://restapi.amap.com/v3/geocode/geo"
  79. // 设置请求参数
  80. params := url.Values{
  81. "address": []string{address},
  82. "output": []string{"json"},
  83. "key": []string{ak},
  84. }
  85. // 发起请求
  86. request, err := url.Parse(uri + "?" + params.Encode())
  87. if nil != err {
  88. fmt.Printf("host error: %v", err)
  89. return "", err
  90. }
  91. resp, err1 := http.Get(request.String())
  92. fmt.Printf("url: %s\n", request.String())
  93. defer resp.Body.Close()
  94. if err1 != nil {
  95. fmt.Printf("request error: %v", err1)
  96. return "", err
  97. }
  98. body, err2 := ioutil.ReadAll(resp.Body)
  99. if err2 != nil {
  100. fmt.Printf("response error: %v", err2)
  101. }
  102. fmt.Println(string(body))
  103. geoResponse := GeoResponse{}
  104. json.Unmarshal(body, &geoResponse)
  105. return geoResponse.Geocodes[0].Location, err
  106. }
  107. type GeoResponse struct {
  108. Status string `json:"status"`
  109. Info string `json:"info"`
  110. InfoCode string `json:"infocode"`
  111. Count string `json:"count"`
  112. Geocodes []GeoCode `json:"geocodes"`
  113. }
  114. type GeoCode struct {
  115. FormattedAddress string `json:"formatted_address"`
  116. Country string `json:"country"`
  117. Province string `json:"province"`
  118. CityCode string `json:"citycode"`
  119. City string `json:"city"`
  120. District string `json:"district"`
  121. Adcode string `json:"adcode"`
  122. Number string `json:"number"`
  123. Location string `json:"location"`
  124. Level string `json:"level"`
  125. }

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.