-
Notifications
You must be signed in to change notification settings - Fork 39
/
connector.go
157 lines (143 loc) · 4.33 KB
/
connector.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
package schema
import (
"context"
"fmt"
"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/crypto"
"github.com/seal-io/walrus/pkg/dao/types/object"
)
type Connector struct {
ent.Schema
}
func (Connector) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.Metadata(),
mixin.Status(),
}
}
func (Connector) Indexes() []ent.Index {
return []ent.Index{
// NB(thxCode): since null project connector belongs to the organization(beyond any project),
// single unique constraint index cannot cover null column value,
// so we leverage conditional indexes to run this case.
index.Fields("project_id", "name").
Unique().
Annotations(
entsql.IndexWhere("project_id IS NOT NULL")),
index.Fields("name").
Unique().
Annotations(
entsql.IndexWhere("project_id IS NULL")),
}
}
func (Connector) Fields() []ent.Field {
return []ent.Field{
object.IDField("project_id").
Comment("ID of the project to belong, empty means for all projects.").
Immutable().
Optional(),
field.String("category").
Comment("Category of the connector.").
NotEmpty().
Immutable().
StructTag(`json:"category,omitempty,cli-table-column"`).
Annotations(
entx.Input()),
field.String("type").
Comment("Type of the connector.").
NotEmpty().
Immutable().
StructTag(`json:"type,omitempty,cli-table-column"`).
Annotations(
entx.Input()),
field.String("applicable_environment_type").
Comment("Environment type of the connector to apply.").
NotEmpty().
Immutable().
Annotations(
entx.Input(entx.WithQuery())),
field.String("config_version").
Comment("Connector config version.").
NotEmpty(),
crypto.PropertiesField("config_data").
Comment("Connector config data.").
Default(crypto.Properties{}).
Optional().
Annotations(
entx.SkipClearingOptionalField()),
field.Bool("enable_fin_ops").
Comment("Config whether enable finOps, will install prometheus and opencost while enable."),
field.JSON("fin_ops_custom_pricing", &types.FinOpsCustomPricing{}).
Comment("Custom pricing user defined.").
Optional().
Annotations(
entx.SkipClearingOptionalField()),
}
}
func (Connector) Edges() []ent.Edge {
return []ent.Edge{
// Project 1-* Connectors.
edge.From("project", Project.Type).
Ref("connectors").
Field("project_id").
Comment("Project to which the connector belongs.").
Unique().
Immutable().
Annotations(
entx.ValidateContext(intercept.WithProjectInterceptor)),
// Environments *-* Connectors.
edge.From("environments", Environment.Type).
Ref("connectors").
Comment("Environments to which the connector configures.").
Through("environment_connector_relationships", EnvironmentConnectorRelationship.Type).
Annotations(
entx.SkipIO()),
// Connector 1-* ResourceComponents.
edge.To("resource_components", ResourceComponent.Type).
Comment("ResourceComponents that use the connector.").
Annotations(
entsql.OnDelete(entsql.Restrict),
entx.SkipIO()),
edge.To("cost_reports", CostReport.Type).
Comment("CostReports that linked to the connection.").
Annotations(
entsql.OnDelete(entsql.Cascade),
entx.SkipIO()),
}
}
func (Connector) Hooks() []ent.Hook {
// Set default pricing for Kubernetes connector.
defaultK8sFinOps := func(n ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if !m.Op().Is(ent.OpCreate) {
return n.Mutate(ctx, m)
}
if v, ok := m.Field("type"); !ok || v.(string) != types.ConnectorTypeKubernetes {
return n.Mutate(ctx, m)
}
if v, ok := m.Field("fin_ops_custom_pricing"); !ok || v.(*types.FinOpsCustomPricing).IsZero() {
err := m.SetField("fin_ops_custom_pricing", types.DefaultFinOpsCustomPricing())
if err != nil {
return nil, fmt.Errorf("error setting default finOps custom pricing: %w", err)
}
}
return n.Mutate(ctx, m)
})
}
return []ent.Hook{
defaultK8sFinOps,
}
}
func (Connector) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
intercept.ByProjectOptional("project_id"),
}
}