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

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

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.