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.

pcm.go 6.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package pcmsdk
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. schsdk "gitlink.org.cn/cloudream/common/sdks/scheduler"
  7. uopsdk "gitlink.org.cn/cloudream/common/sdks/unifyops"
  8. myhttp "gitlink.org.cn/cloudream/common/utils/http"
  9. "gitlink.org.cn/cloudream/common/utils/serder"
  10. )
  11. const CORRECT_CODE int = 200
  12. type UploadImageReq struct {
  13. SlwNodeID uopsdk.SlwNodeID `json:"slwNodeID"`
  14. ImagePath string `json:"imagePath"`
  15. }
  16. type UploadImageResp struct {
  17. Result string `json:"result"`
  18. ImageID uopsdk.SlwNodeImageID `json:"imageID"`
  19. }
  20. func (c *Client) UploadImage(req UploadImageReq) (*UploadImageResp, error) {
  21. url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/uploadImage")
  22. if err != nil {
  23. return nil, err
  24. }
  25. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  26. Body: req,
  27. })
  28. if err != nil {
  29. return nil, err
  30. }
  31. contType := resp.Header.Get("Content-Type")
  32. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  33. var codeResp response[UploadImageResp]
  34. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  35. return nil, fmt.Errorf("parsing response: %w", err)
  36. }
  37. if codeResp.Code == CORRECT_CODE {
  38. return &codeResp.Data, nil
  39. }
  40. return nil, codeResp.ToError()
  41. }
  42. return nil, fmt.Errorf("unknow response content type: %s", contType)
  43. }
  44. type GetImageListReq struct {
  45. SlwNodeID int64 `json:"slwNodeID"`
  46. }
  47. type GetImageListResp struct {
  48. ImageIDs []int64 `json:"imageIDs"`
  49. }
  50. func (c *Client) GetImageList(req GetImageListReq) (*GetImageListResp, error) {
  51. url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/getImageList")
  52. if err != nil {
  53. return nil, err
  54. }
  55. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  56. Body: req,
  57. })
  58. if err != nil {
  59. return nil, err
  60. }
  61. contType := resp.Header.Get("Content-Type")
  62. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  63. var codeResp response[GetImageListResp]
  64. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  65. return nil, fmt.Errorf("parsing response: %w", err)
  66. }
  67. if codeResp.Code == CORRECT_CODE {
  68. return &codeResp.Data, nil
  69. }
  70. return nil, codeResp.ToError()
  71. }
  72. return nil, fmt.Errorf("unknow response content type: %s", contType)
  73. }
  74. type DeleteImageReq struct {
  75. SlwNodeID int64 `json:"slwNodeID"`
  76. PCMJobID int64 `json:"pcmJobID"`
  77. }
  78. type DeleteImageResp struct {
  79. Result string `json:"result"`
  80. }
  81. func (c *Client) DeleteImage(req DeleteImageReq) (*DeleteImageResp, error) {
  82. url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/deleteImage")
  83. if err != nil {
  84. return nil, err
  85. }
  86. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  87. Body: req,
  88. })
  89. if err != nil {
  90. return nil, err
  91. }
  92. contType := resp.Header.Get("Content-Type")
  93. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  94. var codeResp response[DeleteImageResp]
  95. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  96. return nil, fmt.Errorf("parsing response: %w", err)
  97. }
  98. if codeResp.Code == CORRECT_CODE {
  99. return &codeResp.Data, nil
  100. }
  101. return nil, codeResp.ToError()
  102. }
  103. return nil, fmt.Errorf("unknow response content type: %s", contType)
  104. }
  105. type ScheduleTaskReq struct {
  106. SlwNodeID uopsdk.SlwNodeID `json:"slwNodeID"`
  107. Envs []schsdk.EnvVar `json:"envs"`
  108. ImageID uopsdk.SlwNodeImageID `json:"imageID"`
  109. CMDLine string `json:"cmdLine"`
  110. }
  111. type ScheduleTaskResp struct {
  112. Result string `json:"result"`
  113. PCMJobID int64 `json:"pcmJobID"`
  114. }
  115. func (c *Client) ScheduleTask(req ScheduleTaskReq) (*ScheduleTaskResp, error) {
  116. url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/scheduleTask")
  117. if err != nil {
  118. return nil, err
  119. }
  120. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  121. Body: req,
  122. })
  123. if err != nil {
  124. return nil, err
  125. }
  126. contType := resp.Header.Get("Content-Type")
  127. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  128. var codeResp response[ScheduleTaskResp]
  129. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  130. return nil, fmt.Errorf("parsing response: %w", err)
  131. }
  132. if codeResp.Code == CORRECT_CODE {
  133. return &codeResp.Data, nil
  134. }
  135. return nil, codeResp.ToError()
  136. }
  137. return nil, fmt.Errorf("unknow response content type: %s", contType)
  138. }
  139. type GetTaskStatusReq struct {
  140. SlwNodeID uopsdk.SlwNodeID `json:"slwNodeID"`
  141. PCMJobID int64 `json:"pcmJobID"`
  142. }
  143. type GetTaskStatusResp struct {
  144. Result string `json:"result"`
  145. Status string `json:"status"`
  146. }
  147. func (c *Client) GetTaskStatus(req GetTaskStatusReq) (*GetTaskStatusResp, error) {
  148. url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/getTaskStatus")
  149. if err != nil {
  150. return nil, err
  151. }
  152. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  153. Body: req,
  154. })
  155. if err != nil {
  156. return nil, err
  157. }
  158. contType := resp.Header.Get("Content-Type")
  159. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  160. var codeResp response[GetTaskStatusResp]
  161. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  162. return nil, fmt.Errorf("parsing response: %w", err)
  163. }
  164. if codeResp.Code == CORRECT_CODE {
  165. return &codeResp.Data, nil
  166. }
  167. return nil, codeResp.ToError()
  168. }
  169. return nil, fmt.Errorf("unknow response content type: %s", contType)
  170. }
  171. type DeleteTaskReq struct {
  172. SlwNodeID int64 `json:"slwNodeID"`
  173. PCMJobID int64 `json:"pcmJobID"`
  174. }
  175. type DeleteTaskResp struct {
  176. Result string `json:"result"`
  177. }
  178. func (c *Client) DeleteTask(req DeleteTaskReq) (*DeleteTaskResp, error) {
  179. url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/deleteTask")
  180. if err != nil {
  181. return nil, err
  182. }
  183. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  184. Body: req,
  185. })
  186. if err != nil {
  187. return nil, err
  188. }
  189. contType := resp.Header.Get("Content-Type")
  190. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  191. var codeResp response[DeleteTaskResp]
  192. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  193. return nil, fmt.Errorf("parsing response: %w", err)
  194. }
  195. if codeResp.Code == CORRECT_CODE {
  196. return &codeResp.Data, nil
  197. }
  198. return nil, codeResp.ToError()
  199. }
  200. return nil, fmt.Errorf("unknow response content type: %s", contType)
  201. }