|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- package pcmsdk
-
- import (
- "fmt"
- "net/url"
- "strings"
-
- schsdk "gitlink.org.cn/cloudream/common/sdks/scheduler"
- uopsdk "gitlink.org.cn/cloudream/common/sdks/unifyops"
- myhttp "gitlink.org.cn/cloudream/common/utils/http"
- "gitlink.org.cn/cloudream/common/utils/serder"
- )
-
- const CORRECT_CODE int = 200
-
- type UploadImageReq struct {
- SlwNodeID uopsdk.SlwNodeID `json:"slwNodeID"`
- ImagePath string `json:"imagePath"`
- }
-
- type UploadImageResp struct {
- Result string `json:"result"`
- ImageID uopsdk.SlwNodeImageID `json:"imageID"`
- }
-
- func (c *Client) UploadImage(req UploadImageReq) (*UploadImageResp, error) {
- url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/uploadImage")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: req,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[UploadImageResp]
- if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
- return nil, fmt.Errorf("parsing response: %w", err)
- }
-
- if codeResp.Code == CORRECT_CODE {
- return &codeResp.Data, nil
- }
-
- return nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- type GetImageListReq struct {
- SlwNodeID int64 `json:"slwNodeID"`
- }
-
- type GetImageListResp struct {
- ImageIDs []int64 `json:"imageIDs"`
- }
-
- func (c *Client) GetImageList(req GetImageListReq) (*GetImageListResp, error) {
- url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/getImageList")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: req,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[GetImageListResp]
- if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
- return nil, fmt.Errorf("parsing response: %w", err)
- }
-
- if codeResp.Code == CORRECT_CODE {
- return &codeResp.Data, nil
- }
-
- return nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- type DeleteImageReq struct {
- SlwNodeID int64 `json:"slwNodeID"`
- PCMJobID int64 `json:"pcmJobID"`
- }
-
- type DeleteImageResp struct {
- Result string `json:"result"`
- }
-
- func (c *Client) DeleteImage(req DeleteImageReq) (*DeleteImageResp, error) {
- url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/deleteImage")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: req,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[DeleteImageResp]
- if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
- return nil, fmt.Errorf("parsing response: %w", err)
- }
-
- if codeResp.Code == CORRECT_CODE {
- return &codeResp.Data, nil
- }
-
- return nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- type ScheduleTaskReq struct {
- SlwNodeID uopsdk.SlwNodeID `json:"slwNodeID"`
- Envs []schsdk.EnvVar `json:"envs"`
- ImageID uopsdk.SlwNodeImageID `json:"imageID"`
- CMDLine string `json:"cmdLine"`
- }
-
- type ScheduleTaskResp struct {
- Result string `json:"result"`
- PCMJobID int64 `json:"pcmJobID"`
- }
-
- func (c *Client) ScheduleTask(req ScheduleTaskReq) (*ScheduleTaskResp, error) {
- url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/scheduleTask")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: req,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[ScheduleTaskResp]
- if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
- return nil, fmt.Errorf("parsing response: %w", err)
- }
-
- if codeResp.Code == CORRECT_CODE {
- return &codeResp.Data, nil
- }
-
- return nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- type GetTaskStatusReq struct {
- SlwNodeID uopsdk.SlwNodeID `json:"slwNodeID"`
- PCMJobID int64 `json:"pcmJobID"`
- }
-
- type GetTaskStatusResp struct {
- Result string `json:"result"`
- Status string `json:"status"`
- }
-
- func (c *Client) GetTaskStatus(req GetTaskStatusReq) (*GetTaskStatusResp, error) {
- url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/getTaskStatus")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: req,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[GetTaskStatusResp]
- if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
- return nil, fmt.Errorf("parsing response: %w", err)
- }
-
- if codeResp.Code == CORRECT_CODE {
- return &codeResp.Data, nil
- }
-
- return nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- type DeleteTaskReq struct {
- SlwNodeID int64 `json:"slwNodeID"`
- PCMJobID int64 `json:"pcmJobID"`
- }
-
- type DeleteTaskResp struct {
- Result string `json:"result"`
- }
-
- func (c *Client) DeleteTask(req DeleteTaskReq) (*DeleteTaskResp, error) {
- url, err := url.JoinPath(c.baseURL, "/pcm/v1/core/deleteTask")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: req,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[DeleteTaskResp]
- if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
- return nil, fmt.Errorf("parsing response: %w", err)
- }
-
- if codeResp.Code == CORRECT_CODE {
- return &codeResp.Data, nil
- }
-
- return nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
|