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 7.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package pcmsdk
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. "time"
  7. "gitlink.org.cn/cloudream/common/sdks"
  8. schsdk "gitlink.org.cn/cloudream/common/sdks/scheduler"
  9. myhttp "gitlink.org.cn/cloudream/common/utils/http"
  10. "gitlink.org.cn/cloudream/common/utils/serder"
  11. )
  12. const CodeOK int = 200
  13. type UploadImageReq struct {
  14. PartID ParticipantID `json:"partID"`
  15. ImagePath string `json:"imagePath"`
  16. }
  17. type UploadImageResp struct {
  18. Result string `json:"result"`
  19. ImageID ImageID `json:"imageID"`
  20. Name string `json:"name"`
  21. }
  22. // TODO
  23. func (c *Client) UploadImage(req UploadImageReq) (*UploadImageResp, error) {
  24. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/uploadImage")
  25. if err != nil {
  26. return nil, err
  27. }
  28. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  29. Body: req,
  30. })
  31. if err != nil {
  32. return nil, err
  33. }
  34. contType := resp.Header.Get("Content-Type")
  35. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  36. var codeResp response[UploadImageResp]
  37. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  38. return nil, fmt.Errorf("parsing response: %w", err)
  39. }
  40. if codeResp.Code == CodeOK {
  41. return &codeResp.Data, nil
  42. }
  43. return nil, codeResp.ToError()
  44. }
  45. return nil, fmt.Errorf("unknow response content type: %s", contType)
  46. }
  47. type GetParticipantsResp struct {
  48. Participants []Participant
  49. }
  50. func (c *Client) GetParticipants() (*GetParticipantsResp, error) {
  51. type Resp struct {
  52. Code int `json:"code"`
  53. Message string `json:"message"`
  54. Participants []Participant `json:"participants"`
  55. }
  56. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/uploadImage")
  57. if err != nil {
  58. return nil, err
  59. }
  60. rawResp, err := myhttp.GetJSON(url, myhttp.RequestParam{})
  61. if err != nil {
  62. return nil, err
  63. }
  64. resp, err := myhttp.ParseJSONResponse[Resp](rawResp)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if resp.Code != CodeOK {
  69. return nil, &sdks.CodeMessageError{
  70. Code: fmt.Sprintf("%d", resp.Code),
  71. Message: resp.Message,
  72. }
  73. }
  74. return &GetParticipantsResp{
  75. Participants: resp.Participants,
  76. }, nil
  77. }
  78. type GetImageListReq struct {
  79. PartID ParticipantID `json:"partId"`
  80. }
  81. type GetImageListResp struct {
  82. Images []Image
  83. }
  84. func (c *Client) GetImageList(req GetImageListReq) (*GetImageListResp, error) {
  85. type Resp struct {
  86. Success bool `json:"success"`
  87. Images []Image `json:"images"`
  88. ErrorMsg string `json:"errorMsg"`
  89. }
  90. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/getImageList")
  91. if err != nil {
  92. return nil, err
  93. }
  94. rawResp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  95. Body: req,
  96. })
  97. if err != nil {
  98. return nil, err
  99. }
  100. resp, err := myhttp.ParseJSONResponse[Resp](rawResp)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if !resp.Success {
  105. return nil, fmt.Errorf(resp.ErrorMsg)
  106. }
  107. return &GetImageListResp{
  108. Images: resp.Images,
  109. }, nil
  110. }
  111. type DeleteImageReq struct {
  112. PartID ParticipantID `json:"partID"`
  113. ImageID ImageID `json:"imageID"`
  114. }
  115. func (c *Client) DeleteImage(req DeleteImageReq) error {
  116. type Resp struct {
  117. Success bool `json:"success"`
  118. ErrorMsg string `json:"errorMsg"`
  119. }
  120. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/deleteImage")
  121. if err != nil {
  122. return err
  123. }
  124. rawResp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  125. Body: req,
  126. })
  127. if err != nil {
  128. return err
  129. }
  130. resp, err := myhttp.ParseJSONResponse[Resp](rawResp)
  131. if err != nil {
  132. return err
  133. }
  134. if !resp.Success {
  135. return fmt.Errorf(resp.ErrorMsg)
  136. }
  137. return nil
  138. }
  139. type SubmitTaskReq struct {
  140. PartID ParticipantID `json:"partId"`
  141. ImageID ImageID `json:"imageId"`
  142. ResourceID ResourceID `json:"resourceId"`
  143. CMD string `json:"cmd"`
  144. Params []schsdk.KVPair `json:"params"`
  145. Envs []schsdk.KVPair `json:"envs"`
  146. }
  147. type SubmitTaskResp struct {
  148. TaskID TaskID
  149. }
  150. func (c *Client) SubmitTask(req SubmitTaskReq) (*SubmitTaskResp, error) {
  151. type Resp struct {
  152. Success bool `json:"success"`
  153. TaskID TaskID `json:"taskId"`
  154. ErrorMsg string `json:"errorMsg"`
  155. }
  156. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/submitTask")
  157. if err != nil {
  158. return nil, err
  159. }
  160. rawResp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  161. Body: req,
  162. })
  163. if err != nil {
  164. return nil, err
  165. }
  166. resp, err := myhttp.ParseJSONResponse[Resp](rawResp)
  167. if err != nil {
  168. return nil, err
  169. }
  170. if !resp.Success {
  171. return nil, fmt.Errorf(resp.ErrorMsg)
  172. }
  173. return &SubmitTaskResp{
  174. TaskID: resp.TaskID,
  175. }, nil
  176. }
  177. type GetTaskReq struct {
  178. PartID ParticipantID `json:"partId"`
  179. TaskID TaskID `json:"taskId"`
  180. }
  181. type GetTaskResp struct {
  182. TaskStatus TaskStatus
  183. TaskName string
  184. StartedAt time.Time
  185. CompletedAt time.Time
  186. }
  187. func (c *Client) GetTask(req GetTaskReq) (*GetTaskResp, error) {
  188. type Resp struct {
  189. Success bool `json:"success"`
  190. Task struct {
  191. TaskID TaskID `json:"taskId"`
  192. TaskStatus TaskStatus `json:"taskStatus"`
  193. TaskName string `json:"taskName"`
  194. StartedAt serder.TimestampSecond `json:"startedAt"`
  195. CompletedAt serder.TimestampSecond `json:"completedAt"`
  196. } `json:"task"`
  197. ErrorMsg string `json:"errorMsg"`
  198. }
  199. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/getTask")
  200. if err != nil {
  201. return nil, err
  202. }
  203. rawResp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  204. Body: req,
  205. })
  206. if err != nil {
  207. return nil, err
  208. }
  209. resp, err := myhttp.ParseJSONResponse[Resp](rawResp)
  210. if err != nil {
  211. return nil, err
  212. }
  213. if !resp.Success {
  214. return nil, fmt.Errorf(resp.ErrorMsg)
  215. }
  216. return &GetTaskResp{
  217. TaskStatus: resp.Task.TaskStatus,
  218. TaskName: resp.Task.TaskName,
  219. StartedAt: time.Time(resp.Task.StartedAt),
  220. CompletedAt: time.Time(resp.Task.CompletedAt),
  221. }, nil
  222. }
  223. type DeleteTaskReq struct {
  224. PartID ParticipantID `json:"partId"`
  225. TaskID TaskID `json:"taskId"`
  226. }
  227. func (c *Client) DeleteTask(req DeleteTaskReq) error {
  228. type Resp struct {
  229. Success bool `json:"success"`
  230. ErrorMsg string `json:"errorMsg"`
  231. }
  232. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/deleteTask")
  233. if err != nil {
  234. return err
  235. }
  236. rawResp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  237. Body: req,
  238. })
  239. if err != nil {
  240. return err
  241. }
  242. resp, err := myhttp.ParseJSONResponse[Resp](rawResp)
  243. if err != nil {
  244. return err
  245. }
  246. if !resp.Success {
  247. return fmt.Errorf(resp.ErrorMsg)
  248. }
  249. return nil
  250. }
  251. type GetResourceSpecs struct {
  252. PartID ParticipantID `json:"partId"`
  253. }
  254. type GetResourceSpecsResp struct {
  255. Resources []Resource
  256. }
  257. func (c *Client) GetResourceSpecs(req GetImageListReq) (*GetResourceSpecsResp, error) {
  258. type Resp struct {
  259. Success bool `json:"success"`
  260. ResourceSpecs []Resource `json:"resourceSpecs"`
  261. ErrorMsg string `json:"errorMsg"`
  262. }
  263. url, err := url.JoinPath(c.baseURL, "/pcm/v1/storelink/getResourceSpecs")
  264. if err != nil {
  265. return nil, err
  266. }
  267. rawResp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  268. Body: req,
  269. })
  270. if err != nil {
  271. return nil, err
  272. }
  273. resp, err := myhttp.ParseJSONResponse[Resp](rawResp)
  274. if err != nil {
  275. return nil, err
  276. }
  277. if !resp.Success {
  278. return nil, fmt.Errorf(resp.ErrorMsg)
  279. }
  280. return &GetResourceSpecsResp{
  281. Resources: resp.ResourceSpecs,
  282. }, nil
  283. }