/
pipeline_template.go
167 lines (141 loc) · 5.04 KB
/
pipeline_template.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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 dbclient
import (
"github.com/jinzhu/gorm"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/database/dbengine"
)
type DicePipelineTemplate struct {
dbengine.BaseModel
Name string `json:"name" gorm:"type:varchar(255)"`
LogoUrl string `json:"logoUrl" gorm:"type:varchar(255)"`
Desc string `json:"desc" gorm:"type:varchar(255)"`
ScopeType string `json:"scope_type" gorm:"type:varchar(255)"`
ScopeId string `json:"scope_id" gorm:"type:bigint(20)"`
DefaultVersion string `json:"default_version" gorm:"type:varchar(255)"`
}
func (ext *DicePipelineTemplate) ToApiData() *apistructs.PipelineTemplate {
return &apistructs.PipelineTemplate{
ID: ext.ID,
Name: ext.Name,
Desc: ext.Desc,
LogoUrl: ext.LogoUrl,
CreatedAt: ext.CreatedAt,
UpdatedAt: ext.UpdatedAt,
ScopeID: ext.ScopeId,
ScopeType: ext.ScopeType,
Version: ext.DefaultVersion,
}
}
type DicePipelineTemplateVersion struct {
dbengine.BaseModel
TemplateId uint64 `json:"template_id"`
Name string `json:"name" gorm:"type:varchar(255);"`
Version string `json:"version" gorm:"type:varchar(128);"`
Spec string `json:"spec" gorm:"type:text"`
Readme string `json:"readme" gorm:"type:longtext"`
}
func (ext *DicePipelineTemplateVersion) ToApiData() *apistructs.PipelineTemplateVersion {
return &apistructs.PipelineTemplateVersion{
ID: ext.ID,
Name: ext.Name,
TemplateId: ext.TemplateId,
Version: ext.Version,
Spec: ext.Spec,
Readme: ext.Readme,
CreatedAt: ext.CreatedAt,
UpdatedAt: ext.UpdatedAt,
}
}
func (client *DBClient) CreatePipelineTemplate(template *DicePipelineTemplate) error {
err := client.Create(template).Error
return err
}
func (client *DBClient) UpdatePipelineTemplate(template *DicePipelineTemplate) error {
err := client.Model(template).Save(template).Error
return err
}
func (client *DBClient) GetPipelineTemplate(name string, scopeType string, scopeId string) (*DicePipelineTemplate, error) {
var result DicePipelineTemplate
err := client.Model(&DicePipelineTemplate{}).Where("name = ? and scope_type = ? and scope_id = ?", name, scopeType, scopeId).Find(&result).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &result, err
}
func (client *DBClient) GetPipelineTemplateVersion(version string, templateId uint64) (*DicePipelineTemplateVersion, error) {
var result DicePipelineTemplateVersion
err := client.Model(&DicePipelineTemplate{}).Where(" template_id = ? and version = ? ", templateId, version).Find(&result).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &result, err
}
func (client *DBClient) CreatePipelineTemplateVersion(version *DicePipelineTemplateVersion) error {
err := client.Create(version).Error
return err
}
func (client *DBClient) UpdatePipelineTemplateVersion(version *DicePipelineTemplateVersion) error {
err := client.Model(version).Save(version).Error
return err
}
func (client *DBClient) QueryByPipelineTemplates(template *DicePipelineTemplate, pageSize int, pageNo int) ([]DicePipelineTemplate, int, error) {
var result []DicePipelineTemplate
var total int
query := client.Model(&DicePipelineTemplate{})
if template != nil {
if template.Name != "" {
query = query.Where(" name like ? ", "%"+template.Name+"%")
}
if template.ScopeType != "" {
query = query.Where(" scope_type = ? ", template.ScopeType)
}
if template.ScopeId != "" {
query = query.Where(" scope_id = ? ", template.ScopeId)
}
}
if pageNo > 0 && pageSize > 0 {
if query.Count(&total).Error == gorm.ErrRecordNotFound {
return nil, total, nil
}
err := query.Offset((pageNo - 1) * pageSize).Limit(pageSize).Find(&result).Error
if err == gorm.ErrRecordNotFound {
return nil, 0, nil
}
return result, total, nil
} else {
err := query.Find(&result).Error
if err == gorm.ErrRecordNotFound {
return nil, 0, nil
}
return result, 0, err
}
}
func (client *DBClient) QueryPipelineTemplateVersions(version *DicePipelineTemplateVersion) ([]DicePipelineTemplateVersion, error) {
query := client.Model(&DicePipelineTemplateVersion{})
if version != nil {
if version.TemplateId > 0 {
query = query.Where(" template_id = ? ", version.TemplateId)
}
if version.Name != "" {
query = query.Where(" name = ? ", version.Name)
}
}
var result []DicePipelineTemplateVersion
err := query.Find(&result).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return result, err
}