-
Notifications
You must be signed in to change notification settings - Fork 346
/
service.go
78 lines (70 loc) · 1.91 KB
/
service.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
package templates
import (
"context"
"database/sql"
"fmt"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/determined-ai/determined/master/internal/api"
"github.com/determined-ai/determined/master/internal/db"
"github.com/determined-ai/determined/master/pkg/model"
)
// TemplateByName looks up a config template by name in a database.
func TemplateByName(ctx context.Context, name string) (model.Template, error) {
var dest model.Template
err := db.Bun().NewSelect().Table("templates").
ColumnExpr("*").
Where("name = ?", name).
Scan(ctx, &dest)
switch {
case errors.Is(err, sql.ErrNoRows):
return model.Template{}, db.ErrNotFound
case err != nil:
return dest, fmt.Errorf("fetching template %s from database: %w", name, err)
}
return dest, nil
}
// UnmarshalTemplateConfig unmarshals the template config into `o` and returns api-ready errors.
func UnmarshalTemplateConfig(
ctx context.Context,
name string,
user *model.User,
out interface{},
disallowUnknownFields bool,
) error {
tpl, err := TemplateByName(ctx, name)
switch {
case errors.Is(err, db.ErrNotFound):
return api.NotFoundErrs("template", name, true)
case err != nil:
return err
}
permErr, err := AuthZProvider.Get().CanViewTemplate(
ctx,
user,
model.AccessScopeID(tpl.WorkspaceID),
)
switch {
case err != nil:
return err
case permErr != nil:
return api.NotFoundErrs("template", name, true)
}
var opts []yaml.JSONOpt
if disallowUnknownFields {
opts = append(opts, yaml.DisallowUnknownFields)
}
err = yaml.Unmarshal(tpl.Config, out, opts...)
if err != nil {
return fmt.Errorf("yaml.Unmarshal(template=%s): %w", name, err)
}
return nil
}
// DeleteWorkspaceTemplates deletes all the templates in a workspace.
func DeleteWorkspaceTemplates(ctx context.Context, workspaceID int) error {
_, err := db.Bun().NewDelete().
Model(&model.Template{}).
Where("workspace_id = ?", workspaceID).
Exec(ctx)
return err
}