-
Notifications
You must be signed in to change notification settings - Fork 39
/
template_version.go
131 lines (123 loc) · 3.74 KB
/
template_version.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
package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"github.com/seal-io/walrus/pkg/dao/entx"
"github.com/seal-io/walrus/pkg/dao/schema/intercept"
"github.com/seal-io/walrus/pkg/dao/schema/mixin"
"github.com/seal-io/walrus/pkg/dao/types"
"github.com/seal-io/walrus/pkg/dao/types/object"
)
type TemplateVersion struct {
ent.Schema
}
func (TemplateVersion) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.ID(),
mixin.Time(),
}
}
func (TemplateVersion) Indexes() []ent.Index {
return []ent.Index{
index.Fields("name", "version", "project_id").
Unique().
Annotations(
entsql.IndexWhere("project_id IS NOT NULL")),
index.Fields("name", "version").
Unique().
Annotations(
entsql.IndexWhere("project_id IS NULL")),
}
}
func (TemplateVersion) Fields() []ent.Field {
return []ent.Field{
object.IDField("template_id").
Comment("ID of the template.").
NotEmpty().
Immutable(),
// Redundant template name reduce the number of queries.
field.String("name").
Comment("Name of the template.").
NotEmpty().
Immutable(),
field.String("version").
Comment("Version of the template.").
NotEmpty().
Immutable().
StructTag(`json:"version,omitempty,cli-table-column"`),
// This is the normalized terraform module source that can be directly applied to terraform configuration.
// For example, when we store multiple versions of a module in a mono repo,
// Template.Source = "github.com/foo/bar"
// TemplateVersion.Source = "github.com/foo/bar/1.0.0"
field.String("source").
Comment("Source of the template.").
NotEmpty().
Immutable(),
field.JSON("schema", types.TemplateVersionSchema{}).
Comment("Generated schema and data of the template.").
Default(types.TemplateVersionSchema{}),
field.JSON("original_ui_schema", types.UISchema{}).
Comment("store the original ui schema of the template.").
Default(types.UISchema{}).
Annotations(
entx.SkipIO()),
field.JSON("ui_schema", types.UISchema{}).
Comment("ui schema of the template.").
Default(types.UISchema{}).
Annotations(
entx.Input(entx.WithUpdate())),
field.Bytes("schema_default_value").
Comment("Default value generated from schema and ui schema").
Optional().
Annotations(
entx.SkipIO()),
object.IDField("project_id").
Comment("ID of the project to belong, empty means for all projects.").
Immutable().
Optional(),
}
}
func (TemplateVersion) Edges() []ent.Edge {
return []ent.Edge{
// Template 1-* TemplateVersions.
edge.From("template", Template.Type).
Ref("versions").
Field("template_id").
Comment("Template to which the template version belongs.").
Unique().
Required().
Immutable().
Annotations(
entx.SkipInput()),
// TemplateVersion 1-* Resources.
edge.To("resources", Resource.Type).
Comment("Resources that belong to the template version.").
Annotations(
entsql.OnDelete(entsql.Restrict),
entx.SkipIO()),
// TemplateVersion *-* ResourceDefinitions.
edge.From("resource_definitions", ResourceDefinition.Type).
Ref("matching_rules").
Comment("Resource definitions that use the template version.").
Through("resource_definition_matching_rules", ResourceDefinitionMatchingRule.Type).
Annotations(
entx.SkipIO()),
// Project 1-* TemplateVersions.
edge.From("project", Project.Type).
Ref("template_versions").
Field("project_id").
Comment("Project to which the template version belongs.").
Unique().
Immutable().
Annotations(
entx.ValidateContext(intercept.WithProjectInterceptor)),
}
}
func (TemplateVersion) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
intercept.ByProjectOptional("project_id"),
}
}