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

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

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.