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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. adapter := &types.AdapterInfo{}
  31. result := l.svcCtx.DbEngin.Table("t_adapter").First(&adapter, req.AdapterId)
  32. if errors.Is(result.Error, gorm.ErrRecordNotFound) {
  33. return nil, errors.New("adapter does not exist")
  34. }
  35. cluster := types.ClusterInfo{}
  36. tool.Convert(req, &cluster)
  37. cluster.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  38. cluster.OwnerId = "0"
  39. // 获取集群经纬度
  40. location, err := GeoMap(req.RegionName)
  41. cluster.Location = location
  42. cluster.Id = tool.GenSnowflakeIDStr()
  43. tx := l.svcCtx.DbEngin.Table("t_cluster").Create(&cluster)
  44. if tx.Error != nil {
  45. logx.Errorf(tx.Error.Error())
  46. return nil, errors.New("cluster create failed")
  47. }
  48. if err != nil {
  49. return nil, err
  50. }
  51. // push cluster info to adapter
  52. var adapterServer string
  53. l.svcCtx.DbEngin.Raw("select server from t_adapter where id = ?", req.AdapterId).Scan(&adapterServer)
  54. response, err := l.svcCtx.HttpClient.R().
  55. SetBody(&types.ClusterInfo{
  56. Name: req.Name,
  57. Server: req.Server,
  58. Token: req.Token,
  59. MonitorServer: req.MonitorServer,
  60. }).
  61. ForceContentType("application/json").
  62. Post(adapterServer + "/api/v1/cluster/info")
  63. if err != nil {
  64. return nil, err
  65. }
  66. if response.IsError() {
  67. return nil, errors.New(string(response.Body()))
  68. }
  69. return
  70. }
  71. func GeoMap(address string) (string, error) {
  72. // 此处填写您在控制台-应用管理-创建应用后获取的AK
  73. ak := "d3cc9eee0266d39a52498726d1b82f87"
  74. // 接口地址
  75. uri := "https://restapi.amap.com/v3/geocode/geo"
  76. // 设置请求参数
  77. params := url.Values{
  78. "address": []string{address},
  79. "output": []string{"json"},
  80. "key": []string{ak},
  81. }
  82. // 发起请求
  83. request, err := url.Parse(uri + "?" + params.Encode())
  84. if nil != err {
  85. fmt.Printf("host error: %v", err)
  86. return "", err
  87. }
  88. resp, err1 := http.Get(request.String())
  89. fmt.Printf("url: %s\n", request.String())
  90. defer resp.Body.Close()
  91. if err1 != nil {
  92. fmt.Printf("request error: %v", err1)
  93. return "", err
  94. }
  95. body, err2 := ioutil.ReadAll(resp.Body)
  96. if err2 != nil {
  97. fmt.Printf("response error: %v", err2)
  98. }
  99. fmt.Println(string(body))
  100. geoResponse := GeoResponse{}
  101. json.Unmarshal(body, &geoResponse)
  102. return geoResponse.Geocodes[0].Location, err
  103. }
  104. type GeoResponse struct {
  105. Status string `json:"status"`
  106. Info string `json:"info"`
  107. InfoCode string `json:"infocode"`
  108. Count string `json:"count"`
  109. Geocodes []GeoCode `json:"geocodes"`
  110. }
  111. type GeoCode struct {
  112. FormattedAddress string `json:"formatted_address"`
  113. Country string `json:"country"`
  114. Province string `json:"province"`
  115. CityCode string `json:"citycode"`
  116. City string `json:"city"`
  117. District string `json:"district"`
  118. Adcode string `json:"adcode"`
  119. Number string `json:"number"`
  120. Location string `json:"location"`
  121. Level string `json:"level"`
  122. }

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.