|
|
@@ -1,5 +1,10 @@
|
|
|
package client
|
|
|
|
|
|
+import (
|
|
|
+ "github.com/pkg/errors"
|
|
|
+ "net/http"
|
|
|
+)
|
|
|
+
|
|
|
const (
|
|
|
createWorkflowRelativeUrl = "/api/v1/workflows/{namespace}"
|
|
|
deleteWorkflowRelativeUrl = "/api/v1/workflows/{namespace}/{name}"
|
|
|
@@ -24,7 +29,58 @@ type CreateWorkflowParams struct {
|
|
|
}
|
|
|
|
|
|
func (c *Client) CreateWorkflow(params CreateWorkflowParams) (string, error) {
|
|
|
- return "", nil
|
|
|
+ responseMap := make(map[string]any)
|
|
|
+
|
|
|
+ resp, err := c.restyClient.R().
|
|
|
+ SetHeader("Content-Type", "application/json").
|
|
|
+ SetAuthToken(c.token).
|
|
|
+ SetPathParams(map[string]string{
|
|
|
+ "namespace": params.Namespace,
|
|
|
+ }).
|
|
|
+ SetBody(map[string]any{
|
|
|
+ "namespace": params.Namespace,
|
|
|
+ "workflow": params.WorkflowDefinition,
|
|
|
+ }).
|
|
|
+ SetResult(&responseMap).
|
|
|
+ SetError(&responseMap).
|
|
|
+ Post(createWorkflowRelativeUrl)
|
|
|
+ if err != nil {
|
|
|
+ return "", errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ switch resp.StatusCode() {
|
|
|
+ case http.StatusOK:
|
|
|
+ metadata, ok := responseMap["metadata"]
|
|
|
+ if !ok {
|
|
|
+ return "", errors.New("metadata为空")
|
|
|
+ }
|
|
|
+
|
|
|
+ metadataMap, ok := metadata.(map[string]any)
|
|
|
+ if !ok {
|
|
|
+ return "", errors.New("metadata不是map")
|
|
|
+ }
|
|
|
+
|
|
|
+ workflowName, ok := metadataMap["name"]
|
|
|
+ if !ok {
|
|
|
+ return "", errors.New("metadata中没有工作流名称")
|
|
|
+ }
|
|
|
+
|
|
|
+ workflowNameStr, ok := workflowName.(string)
|
|
|
+ if !ok {
|
|
|
+ return "", errors.New("工作流名称不是字符串")
|
|
|
+ }
|
|
|
+
|
|
|
+ return workflowNameStr, nil
|
|
|
+ case http.StatusConflict:
|
|
|
+ return "", errors.New("工作流已存在")
|
|
|
+ default:
|
|
|
+ message, ok := responseMap["message"]
|
|
|
+ if !ok {
|
|
|
+ return "", errors.Errorf("%v", resp.Status())
|
|
|
+ }
|
|
|
+
|
|
|
+ return "", errors.Errorf("%v, %v", resp.Status(), message)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type DeleteWorkflowParams struct {
|
|
|
@@ -33,7 +89,33 @@ type DeleteWorkflowParams struct {
|
|
|
}
|
|
|
|
|
|
func (c *Client) DeleteWorkflow(params DeleteWorkflowParams) error {
|
|
|
- return nil
|
|
|
+ responseMap := make(map[string]any)
|
|
|
+
|
|
|
+ resp, err := c.restyClient.R().
|
|
|
+ SetHeader("Content-Type", "application/json").
|
|
|
+ SetAuthToken(c.token).
|
|
|
+ SetPathParams(map[string]string{
|
|
|
+ "namespace": params.Namespace,
|
|
|
+ "name": params.Name,
|
|
|
+ }).
|
|
|
+ SetResult(&responseMap).
|
|
|
+ SetError(&responseMap).
|
|
|
+ Delete(deleteWorkflowRelativeUrl)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ switch resp.StatusCode() {
|
|
|
+ case http.StatusOK:
|
|
|
+ return nil
|
|
|
+ default:
|
|
|
+ message, ok := responseMap["message"]
|
|
|
+ if !ok {
|
|
|
+ return errors.Errorf("%v", resp.Status())
|
|
|
+ }
|
|
|
+
|
|
|
+ return errors.Errorf("%v, %v", resp.Status(), message)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type GetWorkflowsInNamespaceParams struct {
|
|
|
@@ -41,8 +123,52 @@ type GetWorkflowsInNamespaceParams struct {
|
|
|
}
|
|
|
|
|
|
func (c *Client) GetWorkflowsInNamespace(params GetWorkflowsInNamespaceParams) ([]map[string]any, error) {
|
|
|
- workflowDefinitions := make([]map[string]any, 0)
|
|
|
- return workflowDefinitions, nil
|
|
|
+ responseMap := make(map[string]any)
|
|
|
+
|
|
|
+ resp, err := c.restyClient.R().
|
|
|
+ SetHeader("Content-Type", "application/json").
|
|
|
+ SetAuthToken(c.token).
|
|
|
+ SetPathParams(map[string]string{
|
|
|
+ "namespace": params.Namespace,
|
|
|
+ }).
|
|
|
+ SetResult(&responseMap).
|
|
|
+ SetError(&responseMap).
|
|
|
+ Get(getWorkflowsInNamespaceRelativeUrl)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ switch resp.StatusCode() {
|
|
|
+ case http.StatusOK:
|
|
|
+ itemsValue, ok := responseMap["items"]
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.New("没有获取到items参数")
|
|
|
+ }
|
|
|
+
|
|
|
+ items, ok := itemsValue.([]any)
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.New("items不是slice")
|
|
|
+ }
|
|
|
+
|
|
|
+ templateDefinitions := make([]map[string]any, len(items))
|
|
|
+ for i, item := range items {
|
|
|
+ templateDefinition, ok := item.(map[string]any)
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.New("item无法转换为map[string]any")
|
|
|
+ }
|
|
|
+
|
|
|
+ templateDefinitions[i] = templateDefinition
|
|
|
+ }
|
|
|
+
|
|
|
+ return templateDefinitions, nil
|
|
|
+ default:
|
|
|
+ message, ok := responseMap["message"]
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.Errorf("%v", resp.Status())
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, errors.Errorf("%v, %v", resp.Status(), message)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type GetWorkflowParams struct {
|
|
|
@@ -51,8 +177,33 @@ type GetWorkflowParams struct {
|
|
|
}
|
|
|
|
|
|
func (c *Client) GetWorkflow(params GetWorkflowParams) (map[string]any, error) {
|
|
|
- workflowDefinition := make(map[string]any)
|
|
|
- return workflowDefinition, nil
|
|
|
+ responseMap := make(map[string]any)
|
|
|
+
|
|
|
+ resp, err := c.restyClient.R().
|
|
|
+ SetHeader("Content-Type", "application/json").
|
|
|
+ SetAuthToken(c.token).
|
|
|
+ SetPathParams(map[string]string{
|
|
|
+ "namespace": params.Namespace,
|
|
|
+ "name": params.Name,
|
|
|
+ }).
|
|
|
+ SetResult(&responseMap).
|
|
|
+ SetError(&responseMap).
|
|
|
+ Get(getWorkflowRelativeUrl)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ switch resp.StatusCode() {
|
|
|
+ case http.StatusOK:
|
|
|
+ return responseMap, nil
|
|
|
+ default:
|
|
|
+ message, ok := responseMap["message"]
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.Errorf("%v", resp.Status())
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, errors.Errorf("%v, %v", resp.Status(), message)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type LintWorkflowParams struct {
|
|
|
@@ -61,7 +212,38 @@ type LintWorkflowParams struct {
|
|
|
}
|
|
|
|
|
|
func (c *Client) LintWorkflow(params LintWorkflowParams) error {
|
|
|
- return nil
|
|
|
+ responseMap := make(map[string]any)
|
|
|
+
|
|
|
+ resp, err := c.restyClient.R().
|
|
|
+ SetHeader("Content-Type", "application/json").
|
|
|
+ SetAuthToken(c.token).
|
|
|
+ SetPathParams(map[string]string{
|
|
|
+ "namespace": params.Namespace,
|
|
|
+ }).
|
|
|
+ SetBody(map[string]any{
|
|
|
+ "namespace": params.Namespace,
|
|
|
+ "workflow": params.WorkflowDefinition,
|
|
|
+ }).
|
|
|
+ SetResult(&responseMap).
|
|
|
+ SetError(&responseMap).
|
|
|
+ Post(lintWorkflowRelativeUrl)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ switch resp.StatusCode() {
|
|
|
+ case http.StatusOK:
|
|
|
+ return nil
|
|
|
+ case http.StatusConflict:
|
|
|
+ return errors.New("工作流已存在")
|
|
|
+ default:
|
|
|
+ message, ok := responseMap["message"]
|
|
|
+ if !ok {
|
|
|
+ return errors.Errorf("%v", resp.Status())
|
|
|
+ }
|
|
|
+
|
|
|
+ return errors.Errorf("%v, %v", resp.Status(), message)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type SubmitWorkflowParams struct {
|