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.

cloud.go 2.6 kB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package cloud
  2. import (
  3. "fmt"
  4. "github.com/go-resty/resty/v2"
  5. "github.com/pkg/errors"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database"
  7. "net/http"
  8. "sync"
  9. )
  10. const CreateContainer = "/cloud/container/create"
  11. const DeleteContainer = "/cloud/container/delete"
  12. const GetContainer = "/cloud/container/get"
  13. type Cloud struct {
  14. store *database.CloudStorage
  15. idAddr sync.Map
  16. }
  17. func New(store *database.CloudStorage, adapterId string) (*Cloud, error) {
  18. if store == nil {
  19. return nil, errors.New("store cannot be nil")
  20. }
  21. a := &Cloud{
  22. store: store,
  23. }
  24. css, err := store.GetClustersByAdapterId(adapterId)
  25. if err != nil {
  26. return nil, fmt.Errorf("failed to get clusters: %w", err)
  27. }
  28. for _, info := range css.List {
  29. a.idAddr.Store(info.Id, info.Server)
  30. }
  31. InitClient()
  32. return a, nil
  33. }
  34. func (c *Cloud) ContainerCreate(platformId string, param *CreateParam) (resp *Resp, err error) {
  35. addr, ok := c.GetServerAddrById(platformId)
  36. if !ok {
  37. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  38. }
  39. respErr := &RespErr{}
  40. _, err = Request(addr+CreateContainer, http.MethodPost, func(req *resty.Request) {
  41. req.SetQueryParams(map[string]string{
  42. "pfId": platformId,
  43. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  44. })
  45. if err != nil {
  46. return nil, err
  47. }
  48. return resp, nil
  49. }
  50. func (c *Cloud) ContainerDelete(platformId string, param *DeleteParam) (resp *Resp, err error) {
  51. addr, ok := c.GetServerAddrById(platformId)
  52. if !ok {
  53. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  54. }
  55. respErr := &RespErr{}
  56. _, err = Request(addr+DeleteContainer, http.MethodDelete, func(req *resty.Request) {
  57. req.SetQueryParams(map[string]string{
  58. "pfId": platformId,
  59. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  60. })
  61. if err != nil {
  62. return nil, err
  63. }
  64. return
  65. }
  66. func (c *Cloud) ContainerGet(platformId string, param *GetParam) (resp *Resp, err error) {
  67. addr, ok := c.GetServerAddrById(platformId)
  68. if !ok {
  69. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  70. }
  71. respErr := &RespErr{}
  72. _, err = Request(addr+GetContainer, http.MethodGet, func(req *resty.Request) {
  73. req.SetQueryParams(map[string]string{
  74. "pfId": platformId,
  75. "name": param.Name,
  76. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  77. })
  78. if err != nil {
  79. return nil, err
  80. }
  81. return resp, nil
  82. }
  83. func (c *Cloud) GetServerAddrById(id string) (string, bool) {
  84. val, ok := c.idAddr.Load(id)
  85. if !ok {
  86. return "", false
  87. }
  88. addr, ok := val.(string)
  89. if !ok {
  90. return "", false
  91. }
  92. return addr, true
  93. }

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.