From 1efe5406e50e669aa815d4caf32a0e3578b4f2af Mon Sep 17 00:00:00 2001 From: qiwang <1364512070@qq.com> Date: Thu, 18 May 2023 16:51:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=88=9B=E5=BB=BA=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 80349a101566d63deeaecf56589ce4ab49ce028c --- .../logic/createprocessortasklogic.go | 62 ++++++++++++--- .../rpc/internal/logic/createservicelogic.go | 77 ++++++++++++++----- .../rpc/internal/logic/createtasklogic.go | 77 ++++++++++++++----- .../logic/createtrainingjobconfiglogic.go | 68 ++++++++++++---- .../logic/createvisualizationjoblogic.go | 65 +++++++++++++--- 5 files changed, 276 insertions(+), 73 deletions(-) diff --git a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createprocessortasklogic.go b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createprocessortasklogic.go index 5a67a60d..130a8042 100644 --- a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createprocessortasklogic.go +++ b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createprocessortasklogic.go @@ -6,10 +6,15 @@ package logic */ import ( + "APIGW-go-sdk/core" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/common" "PCM/common/tool" + "bytes" "context" + "fmt" + "io/ioutil" "k8s.io/apimachinery/pkg/util/json" + "net/http" "strings" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/svc" @@ -36,17 +41,52 @@ func NewCreateProcessorTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext func (l *CreateProcessorTaskLogic) CreateProcessorTask(in *modelarts.CreateProcessorTaskReq) (*modelarts.CreateProcessorTaskResp, error) { // todo: add your logic here and delete this line var resp modelarts.CreateProcessorTaskResp - url := "https://modelarts.cn-north-4.myhuaweicloud.com/v2/" + in.ProjectId + "/processor-tasks/" - reqByte, err := json.Marshal(in) - if err != nil { - return nil, err + //根据智算类型判断走华为智算还是南京智算 + modelArtsType := in.ModelArtsType + if modelArtsType == l.svcCtx.Config.HaweiModelArtsType { + modelArtsUrl := l.svcCtx.Config.ModelArtsUrl + url := modelArtsUrl + "v2/" + in.ProjectId + "/processor-tasks/" + reqByte, err := json.Marshal(in) + if err != nil { + return nil, err + } + payload := strings.NewReader(string(reqByte)) + token := common.GetToken() + body, err := tool.HttpClient(tool.POST, url, payload, token) + if err != nil { + return nil, err + } + json.Unmarshal(body, &resp) + } else if modelArtsType == l.svcCtx.Config.NanjingModelArtsType { + AK := l.svcCtx.Config.AK + SK := l.svcCtx.Config.SK + NanjingModelArtsUrl := l.svcCtx.Config.NanjingModelArtsUrl + XProjectId := l.svcCtx.Config.XProjectId + s := core.Signer{ + Key: AK, + Secret: SK, + } + reqByte, err := json.Marshal(in) + r, err := http.NewRequest("POST", NanjingModelArtsUrl+"v2/"+in.ProjectId+"/processor-tasks/", + bytes.NewBuffer(reqByte)) + if err != nil { + fmt.Println(err) + } + r.Header.Add("content-type", "application/json;charset=UTF-8") + r.Header.Add("X-Project-Id", XProjectId) + r.Header.Add("x-stage", "RELEASE") + s.Sign(r) + client := http.DefaultClient + res, err := client.Do(r) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + } + json.Unmarshal(body, &resp) } - payload := strings.NewReader(string(reqByte)) - token := common.GetToken() - body, err := tool.HttpClient(tool.POST, url, payload, token) - if err != nil { - return nil, err - } - json.Unmarshal(body, &resp) return &resp, nil } diff --git a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createservicelogic.go b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createservicelogic.go index 175ec9cc..d2d678aa 100644 --- a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createservicelogic.go +++ b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createservicelogic.go @@ -6,12 +6,17 @@ package logic */ import ( + "APIGW-go-sdk/core" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/common" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/svc" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/modelarts" "PCM/common/tool" + "bytes" + "fmt" "github.com/zeromicro/go-zero/core/logx" + "io/ioutil" "k8s.io/apimachinery/pkg/util/json" + "net/http" "strings" ) @@ -37,25 +42,61 @@ func NewCreateServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Cre func (l *CreateServiceLogic) CreateService(in *modelarts.CreateServiceReq) (*modelarts.CreateServiceResp, error) { // todo: add your logic here and delete this line var resp modelarts.CreateServiceResp - url := "https://modelarts.cn-north-4.myhuaweicloud.com/v1/" + in.ProjectId + "/services" - reqByte, err := json.Marshal(in) - if err != nil { - return nil, err - } - payload := strings.NewReader(string(reqByte)) - token := common.GetToken() - statusCode, body, err := tool.HttpClientWithBodyAndCode(tool.POST, url, payload, token) - if err != nil { - return nil, err - } - if statusCode == 200 { + //根据智算类型判断走华为智算还是南京智算 + modelArtsType := in.ModelArtsType + if modelArtsType == l.svcCtx.Config.HaweiModelArtsType { + modelArtsUrl := l.svcCtx.Config.ModelArtsUrl + url := modelArtsUrl + "v1/" + in.ProjectId + "/services" + reqByte, err := json.Marshal(in) + if err != nil { + return nil, err + } + payload := strings.NewReader(string(reqByte)) + token := common.GetToken() + statusCode, body, err := tool.HttpClientWithBodyAndCode(tool.POST, url, payload, token) + if err != nil { + return nil, err + } + if statusCode == 200 { + json.Unmarshal(body, &resp) + resp.Code = 200 + resp.Msg = "Success" + } else if statusCode != 200 { + json.Unmarshal(body, &resp) + resp.Code = 400 + resp.Msg = "Failure" + } + } else if modelArtsType == l.svcCtx.Config.NanjingModelArtsType { + AK := l.svcCtx.Config.AK + SK := l.svcCtx.Config.SK + NanjingModelArtsUrl := l.svcCtx.Config.NanjingModelArtsUrl + XProjectId := l.svcCtx.Config.XProjectId + s := core.Signer{ + Key: AK, + Secret: SK, + } + reqByte, err := json.Marshal(in) + r, err := http.NewRequest("POST", NanjingModelArtsUrl+"v1/"+in.ProjectId+"/services", + bytes.NewBuffer(reqByte)) + if err != nil { + fmt.Println(err) + } + r.Header.Add("content-type", "application/json;charset=UTF-8") + r.Header.Add("X-Project-Id", XProjectId) + r.Header.Add("x-stage", "RELEASE") + s.Sign(r) + client := http.DefaultClient + res, err := client.Do(r) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + } json.Unmarshal(body, &resp) - resp.Code = 200 - resp.Msg = "Success" - } else if statusCode != 200 { - json.Unmarshal(body, &resp) - resp.Code = 400 - resp.Msg = "Failure" } + return &resp, nil } diff --git a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtasklogic.go b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtasklogic.go index 60e7e832..84df6605 100644 --- a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtasklogic.go +++ b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtasklogic.go @@ -1,10 +1,15 @@ package logic import ( + "APIGW-go-sdk/core" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/common" "PCM/common/tool" + "bytes" "context" + "fmt" + "io/ioutil" "k8s.io/apimachinery/pkg/util/json" + "net/http" "strings" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/svc" @@ -30,25 +35,61 @@ func NewCreateTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create // creat task 创建导入任务 func (l *CreateTaskLogic) CreateTask(in *modelarts.ImportTaskDataReq) (*modelarts.ImportTaskDataResp, error) { var resp modelarts.ImportTaskDataResp - url := "https://modelarts.cn-north-4.myhuaweicloud.com/v2/" + in.ProjectId + "/datasets/" + in.DatasetId + "/import-tasks" - reqByte, err := json.Marshal(in) - if err != nil { - return nil, err - } - payload := strings.NewReader(string(reqByte)) - token := common.GetToken() - statusCode, body, err := tool.HttpClientWithBodyAndCode(tool.POST, url, payload, token) - if err != nil { - return nil, err - } - if statusCode == 200 { + //根据智算类型判断走华为智算还是南京智算 + modelArtsType := in.ModelArtsType + if modelArtsType == l.svcCtx.Config.HaweiModelArtsType { + modelArtsUrl := l.svcCtx.Config.ModelArtsUrl + url := modelArtsUrl + "v2/" + in.ProjectId + "/datasets/" + in.DatasetId + "/import-tasks" + reqByte, err := json.Marshal(in) + if err != nil { + return nil, err + } + payload := strings.NewReader(string(reqByte)) + token := common.GetToken() + statusCode, body, err := tool.HttpClientWithBodyAndCode(tool.POST, url, payload, token) + if err != nil { + return nil, err + } + if statusCode == 200 { + json.Unmarshal(body, &resp) + resp.Code = 200 + resp.Msg = "Success" + } else if statusCode != 200 { + json.Unmarshal(body, &resp) + resp.Code = 400 + resp.Msg = "Failure" + } + } else if modelArtsType == l.svcCtx.Config.NanjingModelArtsType { + AK := l.svcCtx.Config.AK + SK := l.svcCtx.Config.SK + NanjingModelArtsUrl := l.svcCtx.Config.NanjingModelArtsUrl + XProjectId := l.svcCtx.Config.XProjectId + s := core.Signer{ + Key: AK, + Secret: SK, + } + reqByte, err := json.Marshal(in) + r, err := http.NewRequest("POST", NanjingModelArtsUrl+"v2/"+in.ProjectId+"/datasets"+in.DatasetId+"/import-tasks", + bytes.NewBuffer(reqByte)) + if err != nil { + fmt.Println(err) + } + r.Header.Add("content-type", "application/json;charset=UTF-8") + r.Header.Add("X-Project-Id", XProjectId) + r.Header.Add("x-stage", "RELEASE") + s.Sign(r) + client := http.DefaultClient + res, err := client.Do(r) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + } json.Unmarshal(body, &resp) - resp.Code = 200 - resp.Msg = "Success" - } else if statusCode != 200 { - json.Unmarshal(body, &resp) - resp.Code = 400 - resp.Msg = "Failure" } + return &resp, nil } diff --git a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtrainingjobconfiglogic.go b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtrainingjobconfiglogic.go index 3f639a4e..971bb654 100644 --- a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtrainingjobconfiglogic.go +++ b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createtrainingjobconfiglogic.go @@ -1,10 +1,15 @@ package logic import ( + "APIGW-go-sdk/core" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/common" "PCM/common/tool" + "bytes" "context" + "fmt" + "io/ioutil" "k8s.io/apimachinery/pkg/util/json" + "net/http" "strings" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/svc" @@ -30,20 +35,55 @@ func NewCreateTrainingJobConfigLogic(ctx context.Context, svcCtx *svc.ServiceCon // CreateTrainingJobConfig 创建训练作业参数 func (l *CreateTrainingJobConfigLogic) CreateTrainingJobConfig(in *modelarts.CreateTrainingJobConfigReq) (*modelarts.CreateTrainingJobConfigResp, error) { var resp modelarts.CreateTrainingJobConfigResp - url := "https://modelarts.cn-north-4.myhuaweicloud.com/v1/" + in.ProjectId + "/training-job-configs" - reqByte, err := json.Marshal(in) - if err != nil { - return nil, err - } - payload := strings.NewReader(string(reqByte)) - token := common.GetToken() - body, err := tool.HttpClient(tool.POST, url, payload, token) - if err != nil { - return nil, err - } - json.Unmarshal(body, &resp) - if &resp == nil { - return nil, err + //根据智算类型判断走华为智算还是南京智算 + modelArtsType := in.ModelArtsType + if modelArtsType == l.svcCtx.Config.HaweiModelArtsType { + modelArtsUrl := l.svcCtx.Config.ModelArtsUrl + url := modelArtsUrl + "v1/" + in.ProjectId + "/training-job-configs" + reqByte, err := json.Marshal(in) + if err != nil { + return nil, err + } + payload := strings.NewReader(string(reqByte)) + token := common.GetToken() + body, err := tool.HttpClient(tool.POST, url, payload, token) + if err != nil { + return nil, err + } + json.Unmarshal(body, &resp) + if &resp == nil { + return nil, err + } + } else if modelArtsType == l.svcCtx.Config.NanjingModelArtsType { + AK := l.svcCtx.Config.AK + SK := l.svcCtx.Config.SK + NanjingModelArtsUrl := l.svcCtx.Config.NanjingModelArtsUrl + XProjectId := l.svcCtx.Config.XProjectId + s := core.Signer{ + Key: AK, + Secret: SK, + } + reqByte, err := json.Marshal(in) + r, err := http.NewRequest("POST", NanjingModelArtsUrl+"v1/"+in.ProjectId+"/training-job-configs", + bytes.NewBuffer(reqByte)) + if err != nil { + fmt.Println(err) + } + r.Header.Add("content-type", "application/json;charset=UTF-8") + r.Header.Add("X-Project-Id", XProjectId) + r.Header.Add("x-stage", "RELEASE") + s.Sign(r) + client := http.DefaultClient + res, err := client.Do(r) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + } + json.Unmarshal(body, &resp) } return &resp, nil } diff --git a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createvisualizationjoblogic.go b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createvisualizationjoblogic.go index 6396474f..c64e2467 100644 --- a/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createvisualizationjoblogic.go +++ b/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/logic/createvisualizationjoblogic.go @@ -1,10 +1,15 @@ package logic import ( + "APIGW-go-sdk/core" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/common" "PCM/common/tool" + "bytes" "context" "encoding/json" + "fmt" + "io/ioutil" + "net/http" "strings" "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/internal/svc" @@ -29,19 +34,55 @@ func NewCreateVisualizationJobLogic(ctx context.Context, svcCtx *svc.ServiceCont func (l *CreateVisualizationJobLogic) CreateVisualizationJob(in *modelarts.CreateVisualizationJobReq) (*modelarts.CreateVisualizationJobResp, error) { var resp modelarts.CreateVisualizationJobResp - createVisualJobUrl := "https://modelarts.cn-east-3.myhuaweicloud.com/v1/{project_id}/visualization-jobs" - createVisualJobUrl = strings.Replace(createVisualJobUrl, "{project_id}", in.ProjectId, -1) + //根据智算类型判断走华为智算还是南京智算 + modelArtsType := in.ModelArtsType + if modelArtsType == l.svcCtx.Config.HaweiModelArtsType { + modelArtsUrl := l.svcCtx.Config.ModelArtsUrl + createVisualJobUrl := modelArtsUrl + "/v1/{project_id}/visualization-jobs" + createVisualJobUrl = strings.Replace(createVisualJobUrl, "{project_id}", in.ProjectId, -1) - reqByte, err := json.Marshal(in.Param) - if err != nil { - panic(err.Error()) + reqByte, err := json.Marshal(in.Param) + if err != nil { + panic(err.Error()) + } + payload := strings.NewReader(string(reqByte)) + token := common.GetToken() + body, err := tool.HttpClient(tool.POST, createVisualJobUrl, payload, token) + if err != nil { + return nil, err + } + json.Unmarshal(body, &resp) + } else if modelArtsType == l.svcCtx.Config.NanjingModelArtsType { + AK := l.svcCtx.Config.AK + SK := l.svcCtx.Config.SK + NanjingModelArtsUrl := l.svcCtx.Config.NanjingModelArtsUrl + XProjectId := l.svcCtx.Config.XProjectId + s := core.Signer{ + Key: AK, + Secret: SK, + } + reqByte, err := json.Marshal(in) + r, err := http.NewRequest("POST", NanjingModelArtsUrl+"v1/"+in.ProjectId+"/visualization-jobs", + bytes.NewBuffer(reqByte)) + if err != nil { + fmt.Println(err) + } + r.Header.Add("content-type", "application/json;charset=UTF-8") + r.Header.Add("X-Project-Id", XProjectId) + r.Header.Add("x-stage", "RELEASE") + s.Sign(r) + client := http.DefaultClient + res, err := client.Do(r) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + } + json.Unmarshal(body, &resp) } - payload := strings.NewReader(string(reqByte)) - token := common.GetToken() - body, err := tool.HttpClient(tool.POST, createVisualJobUrl, payload, token) - if err != nil { - return nil, err - } - json.Unmarshal(body, &resp) + return &resp, nil }