-
Notifications
You must be signed in to change notification settings - Fork 429
/
v2_plugin.go
74 lines (65 loc) · 1.95 KB
/
v2_plugin.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
package api
import (
"context"
"net/http"
"github.com/gorilla/mux"
"github.com/ovh/cds/engine/api/plugin"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
)
func (api *API) getPluginHandler() ([]service.RbacChecker, service.Handler) {
return service.RBAC(api.pluginRead),
func(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
vars := mux.Vars(req)
pluginName := vars["name"]
pl, err := plugin.LoadByName(ctx, api.mustDB(), pluginName)
if err != nil {
return err
}
return service.WriteMarshal(w, req, pl, http.StatusOK)
}
}
func (api *API) postImportPluginHandler() ([]service.RbacChecker, service.Handler) {
return service.RBAC(api.globalPluginManage),
func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
force := service.FormBool(r, "force")
var p sdk.GRPCPlugin
db := api.mustDB()
if err := service.UnmarshalBody(r, &p); err != nil {
return sdk.WithStack(err)
}
if err := p.Validate(); err != nil {
return sdk.WithStack(err)
}
p.Binaries = nil
tx, err := db.Begin()
if err != nil {
return sdk.WrapError(err, "cannot start transaction")
}
defer tx.Rollback() //nolint
oldP, err := plugin.LoadByName(ctx, db, p.Name)
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) {
return err
}
// If plugin does not exist, create it
if err != nil && sdk.ErrorIs(err, sdk.ErrNotFound) {
if err := plugin.Insert(tx, &p); err != nil {
return sdk.WrapError(err, "unable to insert plugin")
}
} else {
// If plugin exist and --force, update it
if force {
p.ID = oldP.ID
if err := plugin.Update(tx, &p); err != nil {
return sdk.WrapError(err, "unable to insert plugin")
}
} else {
return sdk.NewErrorFrom(sdk.ErrConflictData, "plugin already exist")
}
}
if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}
return service.WriteJSON(w, p, http.StatusOK)
}
}