-
Notifications
You must be signed in to change notification settings - Fork 414
/
integration.go
225 lines (188 loc) · 6.22 KB
/
integration.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package api
import (
"context"
"net/http"
"github.com/go-gorp/gorp"
"github.com/gorilla/mux"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/api/event"
"github.com/ovh/cds/engine/api/integration"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
)
func (api *API) getIntegrationModelsHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
p, err := integration.LoadModels(api.mustDB())
if err != nil {
return sdk.WrapError(err, "Cannot get integration models")
}
return service.WriteJSON(w, p, http.StatusOK)
}
}
func (api *API) getIntegrationModelHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
name := vars["name"]
p, err := integration.LoadModelByName(ctx, api.mustDB(), name)
if err != nil {
return sdk.WrapError(err, "Cannot get integration model")
}
return service.WriteJSON(w, p, http.StatusOK)
}
}
func (api *API) postIntegrationModelHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
m := new(sdk.IntegrationModel)
if err := service.UnmarshalBody(r, m); err != nil {
return sdk.WrapError(err, "postIntegrationModelHandler")
}
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WrapError(err, "Unable to start tx")
}
defer tx.Rollback() // nolint
if exist, err := integration.ModelExists(tx, m.Name); err != nil {
return sdk.WrapError(err, "unable to check if model %s exist", m.Name)
} else if exist {
return sdk.NewErrorFrom(sdk.ErrAlreadyExist, "integration model %s already exist", m.Name)
}
if err := integration.InsertModel(tx, m); err != nil {
return sdk.WrapError(err, "unable to insert model %s", m.Name)
}
if err := tx.Commit(); err != nil {
return sdk.WrapError(err, "unable to commit tx")
}
if m.Public {
go propagatePublicIntegrationModel(ctx, api.mustDB(), api.Cache, *m, getUserConsumer(ctx))
}
return service.WriteJSON(w, m, http.StatusCreated)
}
}
func (api *API) putIntegrationModelHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
name := vars["name"]
if name == "" {
return sdk.WithStack(sdk.ErrNotFound)
}
m := new(sdk.IntegrationModel)
if err := service.UnmarshalBody(r, m); err != nil {
return sdk.WrapError(err, "putIntegrationModelHandler")
}
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WrapError(err, "Unable to start tx")
}
defer tx.Rollback() // nolint
old, err := integration.LoadModelByName(ctx, tx, name)
if err != nil {
return sdk.WrapError(err, "Unable to load model")
}
if old.IsBuiltin() {
return sdk.WrapError(sdk.ErrForbidden, "putIntegrationModelHandler> Update builtin model is forbidden")
}
if m.Name != old.Name {
return sdk.WithStack(sdk.ErrWrongRequest)
}
m.ID = old.ID
if err := integration.UpdateModel(ctx, tx, m); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return sdk.WrapError(err, "Unable to commit tx")
}
if m.Public {
api.GoRoutines.Exec(ctx, "propagatePublicIntegrationModel", func(ctx context.Context) {
propagatePublicIntegrationModel(ctx, api.mustDB(), api.Cache, *m, getUserConsumer(ctx))
})
}
return service.WriteJSON(w, m, http.StatusOK)
}
}
func propagatePublicIntegrationModel(ctx context.Context, db *gorp.DbMap, store cache.Store, m sdk.IntegrationModel, u sdk.Identifiable) {
if !m.Public && len(m.PublicConfigurations) > 0 {
return
}
projs, err := project.LoadAll(context.Background(), db, store, nil, project.LoadOptions.WithClearIntegrations)
if err != nil {
log.Error(ctx, "propagatePublicIntegrationModel> Unable to retrieve all projects: %v", err)
return
}
for _, p := range projs {
tx, err := db.Begin()
if err != nil {
log.Error(ctx, "propagatePublicIntegrationModel> error: %v", err)
continue
}
if err := propagatePublicIntegrationModelOnProject(ctx, tx, store, m, p, u); err != nil {
log.Error(ctx, "propagatePublicIntegrationModel> error: %v", err)
_ = tx.Rollback()
continue
}
if err := tx.Commit(); err != nil {
log.Error(ctx, "propagatePublicIntegrationModel> unable to commit: %v", err)
}
}
}
func propagatePublicIntegrationModelOnProject(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.Store, m sdk.IntegrationModel, p sdk.Project, u sdk.Identifiable) error {
if !m.Public {
return nil
}
for pfName, immutableCfg := range m.PublicConfigurations {
cfg := immutableCfg.Clone()
oldPP, _ := integration.LoadProjectIntegrationByNameWithClearPassword(ctx, db, p.Key, pfName)
if oldPP.ID == 0 {
pp := sdk.ProjectIntegration{
Model: m,
IntegrationModelID: m.ID,
Name: pfName,
Config: cfg,
ProjectID: p.ID,
}
if err := integration.InsertIntegration(db, &pp); err != nil {
return sdk.WrapError(err, "Unable to insert integration %s", pp.Name)
}
event.PublishAddProjectIntegration(ctx, &p, pp, u)
continue
}
pp := sdk.ProjectIntegration{
ID: oldPP.ID,
Model: m,
IntegrationModelID: m.ID,
Name: pfName,
Config: cfg,
ProjectID: p.ID,
}
oldPP.Config = m.DefaultConfig
if err := integration.UpdateIntegration(ctx, db, pp); err != nil {
return err
}
event.PublishUpdateProjectIntegration(ctx, &p, oldPP, pp, u)
}
return nil
}
func (api *API) deleteIntegrationModelHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
name := vars["name"]
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WrapError(err, "Unable to start tx")
}
defer tx.Rollback() // nolint
old, err := integration.LoadModelByName(ctx, tx, name)
if err != nil {
return err
}
if err := integration.DeleteModel(ctx, tx, old.ID); err != nil {
return sdk.WithStack(err)
}
if err := tx.Commit(); err != nil {
return sdk.WrapError(err, "Unable to commit tx")
}
return nil
}
}