|
- package cloud
-
- import (
- "fmt"
- schsdk "gitlink.org.cn/cloudream/common/sdks/scheduler"
- "gitlink.org.cn/cloudream/common/utils/http2"
- "gitlink.org.cn/cloudream/common/utils/serder"
- "net/url"
- "strings"
- )
-
- //type CreateParam struct {
- // ClusterId string `json:"clusterId,omitempty"`
- // ContainerGroupName string `json:"containerGroupName"`
- // Name string `json:"name"`
- // Image string `json:"image"`
- // Cpu string `json:"cpu,omitempty"`
- // Memory string `json:"memory,omitempty"`
- // Port int32 `json:"port,omitempty"`
- // NodePort int32 `json:"nodePort,omitempty"`
- // MountPath string `json:"mountPath,omitempty"`
- // Args []string `json:"args,omitempty"`
- // Envs []struct {
- // Name string `json:"name,omitempty"`
- // Value string `json:"value,omitempty"`
- // } `json:"envs,omitempty"`
- // ContainerCreateParameter ContainerCreateParameter `json:"containerCreateParameter,omitempty"`
- //}
-
- type CreateCloudJobReq struct {
- ClusterId schsdk.ClusterID `json:"clusterId,omitempty"`
- ContainerGroupName string `json:"containerGroupName"`
- Name string `json:"name"`
- Description string `json:"description"`
- Image string `json:"image"`
- Cpu string `json:"cpu,omitempty"`
- Memory string `json:"memory,omitempty"`
- Port int32 `json:"port,omitempty"`
- NodePort int32 `json:"nodePort,omitempty"`
- MountPath string `json:"mountPath,omitempty"`
- Args []string `json:"args,omitempty"`
- Envs []interface{} `json:"envs,omitempty"`
- }
-
- // type T2 struct {
- // Code int `json:"code"`
- // Msg string `json:"msg"`
- // Data struct {
- // Command interface{} `json:"Command"`
- // Args []string `json:"args"`
- // ContainerPorts struct {
- // NodePort int `json:"nodePort"`
- // Port int `json:"port"`
- // } `json:"containerPorts"`
- // Image string `json:"image"`
- // Limits struct {
- // Cpu string `json:"cpu"`
- // Memory string `json:"memory"`
- // } `json:"limits"`
- // Name string `json:"name"`
- // } `json:"data"`
- // TraceId string `json:"traceId"`
- // }
- type CreateCloudResp struct {
- TaskID TaskID `json:"taskId"`
- }
-
- func (c *Client) CreateJob(req CreateCloudJobReq, token string) (*CreateCloudResp, error) {
- targetUrl, err := url.JoinPath(c.baseURL, "/cloud/container/create")
- if err != nil {
- return nil, err
- }
-
- resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
- Body: req,
- Header: map[string]string{
- "Authorization": token,
- },
- })
- if err != nil {
- return nil, err
- }
-
- // 打印resp.Body内容
- //body, err := io.ReadAll(resp.Body)
- //if err != nil {
- // println("Error reading response body:", err)
- //}
- //println("Response Body:", string(body))
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, http2.ContentTypeJSON) {
- var codeResp respons2[CreateCloudResp]
- if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
- return nil, fmt.Errorf("parsing response: %w", err)
- }
-
- if codeResp.Code == ResponseCodeOK {
- return &codeResp.Data, nil
- }
-
- return nil, fmt.Errorf("error: %s", codeResp.Message)
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
-
- }
|