-
Notifications
You must be signed in to change notification settings - Fork 416
/
worker_model_export.go
54 lines (43 loc) · 1.24 KB
/
worker_model_export.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
package api
import (
"context"
"net/http"
"github.com/gorilla/mux"
"github.com/ovh/cds/engine/api/group"
"github.com/ovh/cds/engine/api/workermodel"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk/exportentities"
)
func (api *API) getWorkerModelExportHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
groupName := vars["permGroupName"]
modelName := vars["permModelName"]
format := FormString(r, "format")
if format == "" {
format = "yaml"
}
f, err := exportentities.GetFormat(format)
if err != nil {
return err
}
g, err := group.LoadByName(ctx, api.mustDB(), groupName)
if err != nil {
return err
}
wm, err := workermodel.LoadByNameAndGroupID(ctx, api.mustDB(), modelName, g.ID, workermodel.LoadOptions.Default)
if err != nil {
return err
}
opts := []exportentities.WorkerModelOption{}
if !(isMaintainer(ctx) || isAdmin(ctx)) && !wm.Restricted {
opts = append(opts, exportentities.WorkerModelLoadOptions.HideAdminFields)
}
if _, err := workermodel.Export(*wm, f, w, opts...); err != nil {
return err
}
w.Header().Add("Content-Type", f.ContentType())
w.WriteHeader(http.StatusOK)
return nil
}
}