/
apitest.go
130 lines (109 loc) · 4.09 KB
/
apitest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package apitest
import (
"context"
"fmt"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/pipeline/dbclient"
"github.com/erda-project/erda/modules/pipeline/pipengine/actionexecutor/plugins/apitest/logic"
"github.com/erda-project/erda/modules/pipeline/pipengine/actionexecutor/types"
"github.com/erda-project/erda/modules/pipeline/spec"
)
var Kind = types.Kind(spec.PipelineTaskExecutorKindAPITest)
type define struct {
name types.Name
options map[string]string
dbClient *dbclient.Client
}
func (d *define) Kind() types.Kind { return Kind }
func (d *define) Name() types.Name { return d.name }
func (d *define) Exist(ctx context.Context, task *spec.PipelineTask) (created bool, started bool, err error) {
status := task.Status
switch true {
case status == apistructs.PipelineStatusAnalyzed, status == apistructs.PipelineStatusBorn:
return false, false, nil
case status == apistructs.PipelineStatusCreated:
return true, false, nil
case status == apistructs.PipelineStatusQueue, status == apistructs.PipelineStatusRunning:
return true, true, nil
case status.IsEndStatus():
return true, true, nil
default:
return false, false, fmt.Errorf("invalid status when query task exist")
}
}
func (d *define) Create(ctx context.Context, task *spec.PipelineTask) (interface{}, error) {
return nil, nil
}
func (d *define) Start(ctx context.Context, task *spec.PipelineTask) (interface{}, error) {
logic.Do(ctx, task)
return nil, nil
}
func (d *define) Update(ctx context.Context, task *spec.PipelineTask) (interface{}, error) {
return nil, nil
}
func (d *define) Status(ctx context.Context, task *spec.PipelineTask) (apistructs.PipelineStatusDesc, error) {
latestTask, err := d.dbClient.GetPipelineTask(task.ID)
if err != nil {
return apistructs.PipelineStatusDesc{}, fmt.Errorf("failed to query latest task, err: %v", err)
}
*task = latestTask
if task.Status.IsEndStatus() {
return apistructs.PipelineStatusDesc{Status: task.Status}, nil
}
created, _, err := d.Exist(ctx, task)
if err != nil {
return apistructs.PipelineStatusDesc{}, err
}
if !created {
return apistructs.PipelineStatusDesc{Status: apistructs.PipelineStatusAnalyzed}, nil
}
// status according to api success or not
meta := latestTask.Result.Metadata
for _, metaField := range meta {
if metaField.Name == logic.MetaKeyResult {
if metaField.Value == logic.ResultSuccess {
return apistructs.PipelineStatusDesc{Status: apistructs.PipelineStatusSuccess}, nil
}
return apistructs.PipelineStatusDesc{Status: apistructs.PipelineStatusFailed}, nil
}
}
// return created status to do start step
return apistructs.PipelineStatusDesc{Status: apistructs.PipelineStatusCreated}, nil
}
func (d *define) Inspect(ctx context.Context, task *spec.PipelineTask) (apistructs.TaskInspect, error) {
return apistructs.TaskInspect{}, nil
}
func (d *define) Cancel(ctx context.Context, task *spec.PipelineTask) (interface{}, error) {
return nil, nil
}
func (d *define) Remove(ctx context.Context, task *spec.PipelineTask) (interface{}, error) {
return nil, nil
}
func (d *define) BatchDelete(ctx context.Context, actions []*spec.PipelineTask) (interface{}, error) {
return nil, nil
}
func init() {
types.MustRegister(Kind, func(name types.Name, options map[string]string) (types.ActionExecutor, error) {
dbClient, err := dbclient.New()
if err != nil {
return nil, fmt.Errorf("failed to init dbclient, err: %v", err)
}
return &define{
name: name,
options: options,
dbClient: dbClient,
}, nil
})
}