decision_create.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "entgo.io/ent/dialect/sql/sqlgraph"
  9. "entgo.io/ent/schema/field"
  10. "github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
  11. "github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
  12. )
  13. // DecisionCreate is the builder for creating a Decision entity.
  14. type DecisionCreate struct {
  15. config
  16. mutation *DecisionMutation
  17. hooks []Hook
  18. }
  19. // SetCreatedAt sets the "created_at" field.
  20. func (dc *DecisionCreate) SetCreatedAt(t time.Time) *DecisionCreate {
  21. dc.mutation.SetCreatedAt(t)
  22. return dc
  23. }
  24. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  25. func (dc *DecisionCreate) SetNillableCreatedAt(t *time.Time) *DecisionCreate {
  26. if t != nil {
  27. dc.SetCreatedAt(*t)
  28. }
  29. return dc
  30. }
  31. // SetUpdatedAt sets the "updated_at" field.
  32. func (dc *DecisionCreate) SetUpdatedAt(t time.Time) *DecisionCreate {
  33. dc.mutation.SetUpdatedAt(t)
  34. return dc
  35. }
  36. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  37. func (dc *DecisionCreate) SetNillableUpdatedAt(t *time.Time) *DecisionCreate {
  38. if t != nil {
  39. dc.SetUpdatedAt(*t)
  40. }
  41. return dc
  42. }
  43. // SetUntil sets the "until" field.
  44. func (dc *DecisionCreate) SetUntil(t time.Time) *DecisionCreate {
  45. dc.mutation.SetUntil(t)
  46. return dc
  47. }
  48. // SetScenario sets the "scenario" field.
  49. func (dc *DecisionCreate) SetScenario(s string) *DecisionCreate {
  50. dc.mutation.SetScenario(s)
  51. return dc
  52. }
  53. // SetType sets the "type" field.
  54. func (dc *DecisionCreate) SetType(s string) *DecisionCreate {
  55. dc.mutation.SetType(s)
  56. return dc
  57. }
  58. // SetStartIP sets the "start_ip" field.
  59. func (dc *DecisionCreate) SetStartIP(i int64) *DecisionCreate {
  60. dc.mutation.SetStartIP(i)
  61. return dc
  62. }
  63. // SetNillableStartIP sets the "start_ip" field if the given value is not nil.
  64. func (dc *DecisionCreate) SetNillableStartIP(i *int64) *DecisionCreate {
  65. if i != nil {
  66. dc.SetStartIP(*i)
  67. }
  68. return dc
  69. }
  70. // SetEndIP sets the "end_ip" field.
  71. func (dc *DecisionCreate) SetEndIP(i int64) *DecisionCreate {
  72. dc.mutation.SetEndIP(i)
  73. return dc
  74. }
  75. // SetNillableEndIP sets the "end_ip" field if the given value is not nil.
  76. func (dc *DecisionCreate) SetNillableEndIP(i *int64) *DecisionCreate {
  77. if i != nil {
  78. dc.SetEndIP(*i)
  79. }
  80. return dc
  81. }
  82. // SetStartSuffix sets the "start_suffix" field.
  83. func (dc *DecisionCreate) SetStartSuffix(i int64) *DecisionCreate {
  84. dc.mutation.SetStartSuffix(i)
  85. return dc
  86. }
  87. // SetNillableStartSuffix sets the "start_suffix" field if the given value is not nil.
  88. func (dc *DecisionCreate) SetNillableStartSuffix(i *int64) *DecisionCreate {
  89. if i != nil {
  90. dc.SetStartSuffix(*i)
  91. }
  92. return dc
  93. }
  94. // SetEndSuffix sets the "end_suffix" field.
  95. func (dc *DecisionCreate) SetEndSuffix(i int64) *DecisionCreate {
  96. dc.mutation.SetEndSuffix(i)
  97. return dc
  98. }
  99. // SetNillableEndSuffix sets the "end_suffix" field if the given value is not nil.
  100. func (dc *DecisionCreate) SetNillableEndSuffix(i *int64) *DecisionCreate {
  101. if i != nil {
  102. dc.SetEndSuffix(*i)
  103. }
  104. return dc
  105. }
  106. // SetIPSize sets the "ip_size" field.
  107. func (dc *DecisionCreate) SetIPSize(i int64) *DecisionCreate {
  108. dc.mutation.SetIPSize(i)
  109. return dc
  110. }
  111. // SetNillableIPSize sets the "ip_size" field if the given value is not nil.
  112. func (dc *DecisionCreate) SetNillableIPSize(i *int64) *DecisionCreate {
  113. if i != nil {
  114. dc.SetIPSize(*i)
  115. }
  116. return dc
  117. }
  118. // SetScope sets the "scope" field.
  119. func (dc *DecisionCreate) SetScope(s string) *DecisionCreate {
  120. dc.mutation.SetScope(s)
  121. return dc
  122. }
  123. // SetValue sets the "value" field.
  124. func (dc *DecisionCreate) SetValue(s string) *DecisionCreate {
  125. dc.mutation.SetValue(s)
  126. return dc
  127. }
  128. // SetOrigin sets the "origin" field.
  129. func (dc *DecisionCreate) SetOrigin(s string) *DecisionCreate {
  130. dc.mutation.SetOrigin(s)
  131. return dc
  132. }
  133. // SetSimulated sets the "simulated" field.
  134. func (dc *DecisionCreate) SetSimulated(b bool) *DecisionCreate {
  135. dc.mutation.SetSimulated(b)
  136. return dc
  137. }
  138. // SetNillableSimulated sets the "simulated" field if the given value is not nil.
  139. func (dc *DecisionCreate) SetNillableSimulated(b *bool) *DecisionCreate {
  140. if b != nil {
  141. dc.SetSimulated(*b)
  142. }
  143. return dc
  144. }
  145. // SetOwnerID sets the "owner" edge to the Alert entity by ID.
  146. func (dc *DecisionCreate) SetOwnerID(id int) *DecisionCreate {
  147. dc.mutation.SetOwnerID(id)
  148. return dc
  149. }
  150. // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
  151. func (dc *DecisionCreate) SetNillableOwnerID(id *int) *DecisionCreate {
  152. if id != nil {
  153. dc = dc.SetOwnerID(*id)
  154. }
  155. return dc
  156. }
  157. // SetOwner sets the "owner" edge to the Alert entity.
  158. func (dc *DecisionCreate) SetOwner(a *Alert) *DecisionCreate {
  159. return dc.SetOwnerID(a.ID)
  160. }
  161. // Mutation returns the DecisionMutation object of the builder.
  162. func (dc *DecisionCreate) Mutation() *DecisionMutation {
  163. return dc.mutation
  164. }
  165. // Save creates the Decision in the database.
  166. func (dc *DecisionCreate) Save(ctx context.Context) (*Decision, error) {
  167. var (
  168. err error
  169. node *Decision
  170. )
  171. dc.defaults()
  172. if len(dc.hooks) == 0 {
  173. if err = dc.check(); err != nil {
  174. return nil, err
  175. }
  176. node, err = dc.sqlSave(ctx)
  177. } else {
  178. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  179. mutation, ok := m.(*DecisionMutation)
  180. if !ok {
  181. return nil, fmt.Errorf("unexpected mutation type %T", m)
  182. }
  183. if err = dc.check(); err != nil {
  184. return nil, err
  185. }
  186. dc.mutation = mutation
  187. if node, err = dc.sqlSave(ctx); err != nil {
  188. return nil, err
  189. }
  190. mutation.id = &node.ID
  191. mutation.done = true
  192. return node, err
  193. })
  194. for i := len(dc.hooks) - 1; i >= 0; i-- {
  195. if dc.hooks[i] == nil {
  196. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  197. }
  198. mut = dc.hooks[i](mut)
  199. }
  200. if _, err := mut.Mutate(ctx, dc.mutation); err != nil {
  201. return nil, err
  202. }
  203. }
  204. return node, err
  205. }
  206. // SaveX calls Save and panics if Save returns an error.
  207. func (dc *DecisionCreate) SaveX(ctx context.Context) *Decision {
  208. v, err := dc.Save(ctx)
  209. if err != nil {
  210. panic(err)
  211. }
  212. return v
  213. }
  214. // Exec executes the query.
  215. func (dc *DecisionCreate) Exec(ctx context.Context) error {
  216. _, err := dc.Save(ctx)
  217. return err
  218. }
  219. // ExecX is like Exec, but panics if an error occurs.
  220. func (dc *DecisionCreate) ExecX(ctx context.Context) {
  221. if err := dc.Exec(ctx); err != nil {
  222. panic(err)
  223. }
  224. }
  225. // defaults sets the default values of the builder before save.
  226. func (dc *DecisionCreate) defaults() {
  227. if _, ok := dc.mutation.CreatedAt(); !ok {
  228. v := decision.DefaultCreatedAt()
  229. dc.mutation.SetCreatedAt(v)
  230. }
  231. if _, ok := dc.mutation.UpdatedAt(); !ok {
  232. v := decision.DefaultUpdatedAt()
  233. dc.mutation.SetUpdatedAt(v)
  234. }
  235. if _, ok := dc.mutation.Simulated(); !ok {
  236. v := decision.DefaultSimulated
  237. dc.mutation.SetSimulated(v)
  238. }
  239. }
  240. // check runs all checks and user-defined validators on the builder.
  241. func (dc *DecisionCreate) check() error {
  242. if _, ok := dc.mutation.Until(); !ok {
  243. return &ValidationError{Name: "until", err: errors.New(`ent: missing required field "until"`)}
  244. }
  245. if _, ok := dc.mutation.Scenario(); !ok {
  246. return &ValidationError{Name: "scenario", err: errors.New(`ent: missing required field "scenario"`)}
  247. }
  248. if _, ok := dc.mutation.GetType(); !ok {
  249. return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "type"`)}
  250. }
  251. if _, ok := dc.mutation.Scope(); !ok {
  252. return &ValidationError{Name: "scope", err: errors.New(`ent: missing required field "scope"`)}
  253. }
  254. if _, ok := dc.mutation.Value(); !ok {
  255. return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "value"`)}
  256. }
  257. if _, ok := dc.mutation.Origin(); !ok {
  258. return &ValidationError{Name: "origin", err: errors.New(`ent: missing required field "origin"`)}
  259. }
  260. if _, ok := dc.mutation.Simulated(); !ok {
  261. return &ValidationError{Name: "simulated", err: errors.New(`ent: missing required field "simulated"`)}
  262. }
  263. return nil
  264. }
  265. func (dc *DecisionCreate) sqlSave(ctx context.Context) (*Decision, error) {
  266. _node, _spec := dc.createSpec()
  267. if err := sqlgraph.CreateNode(ctx, dc.driver, _spec); err != nil {
  268. if sqlgraph.IsConstraintError(err) {
  269. err = &ConstraintError{err.Error(), err}
  270. }
  271. return nil, err
  272. }
  273. id := _spec.ID.Value.(int64)
  274. _node.ID = int(id)
  275. return _node, nil
  276. }
  277. func (dc *DecisionCreate) createSpec() (*Decision, *sqlgraph.CreateSpec) {
  278. var (
  279. _node = &Decision{config: dc.config}
  280. _spec = &sqlgraph.CreateSpec{
  281. Table: decision.Table,
  282. ID: &sqlgraph.FieldSpec{
  283. Type: field.TypeInt,
  284. Column: decision.FieldID,
  285. },
  286. }
  287. )
  288. if value, ok := dc.mutation.CreatedAt(); ok {
  289. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  290. Type: field.TypeTime,
  291. Value: value,
  292. Column: decision.FieldCreatedAt,
  293. })
  294. _node.CreatedAt = &value
  295. }
  296. if value, ok := dc.mutation.UpdatedAt(); ok {
  297. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  298. Type: field.TypeTime,
  299. Value: value,
  300. Column: decision.FieldUpdatedAt,
  301. })
  302. _node.UpdatedAt = &value
  303. }
  304. if value, ok := dc.mutation.Until(); ok {
  305. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  306. Type: field.TypeTime,
  307. Value: value,
  308. Column: decision.FieldUntil,
  309. })
  310. _node.Until = value
  311. }
  312. if value, ok := dc.mutation.Scenario(); ok {
  313. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  314. Type: field.TypeString,
  315. Value: value,
  316. Column: decision.FieldScenario,
  317. })
  318. _node.Scenario = value
  319. }
  320. if value, ok := dc.mutation.GetType(); ok {
  321. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  322. Type: field.TypeString,
  323. Value: value,
  324. Column: decision.FieldType,
  325. })
  326. _node.Type = value
  327. }
  328. if value, ok := dc.mutation.StartIP(); ok {
  329. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  330. Type: field.TypeInt64,
  331. Value: value,
  332. Column: decision.FieldStartIP,
  333. })
  334. _node.StartIP = value
  335. }
  336. if value, ok := dc.mutation.EndIP(); ok {
  337. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  338. Type: field.TypeInt64,
  339. Value: value,
  340. Column: decision.FieldEndIP,
  341. })
  342. _node.EndIP = value
  343. }
  344. if value, ok := dc.mutation.StartSuffix(); ok {
  345. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  346. Type: field.TypeInt64,
  347. Value: value,
  348. Column: decision.FieldStartSuffix,
  349. })
  350. _node.StartSuffix = value
  351. }
  352. if value, ok := dc.mutation.EndSuffix(); ok {
  353. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  354. Type: field.TypeInt64,
  355. Value: value,
  356. Column: decision.FieldEndSuffix,
  357. })
  358. _node.EndSuffix = value
  359. }
  360. if value, ok := dc.mutation.IPSize(); ok {
  361. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  362. Type: field.TypeInt64,
  363. Value: value,
  364. Column: decision.FieldIPSize,
  365. })
  366. _node.IPSize = value
  367. }
  368. if value, ok := dc.mutation.Scope(); ok {
  369. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  370. Type: field.TypeString,
  371. Value: value,
  372. Column: decision.FieldScope,
  373. })
  374. _node.Scope = value
  375. }
  376. if value, ok := dc.mutation.Value(); ok {
  377. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  378. Type: field.TypeString,
  379. Value: value,
  380. Column: decision.FieldValue,
  381. })
  382. _node.Value = value
  383. }
  384. if value, ok := dc.mutation.Origin(); ok {
  385. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  386. Type: field.TypeString,
  387. Value: value,
  388. Column: decision.FieldOrigin,
  389. })
  390. _node.Origin = value
  391. }
  392. if value, ok := dc.mutation.Simulated(); ok {
  393. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  394. Type: field.TypeBool,
  395. Value: value,
  396. Column: decision.FieldSimulated,
  397. })
  398. _node.Simulated = value
  399. }
  400. if nodes := dc.mutation.OwnerIDs(); len(nodes) > 0 {
  401. edge := &sqlgraph.EdgeSpec{
  402. Rel: sqlgraph.M2O,
  403. Inverse: true,
  404. Table: decision.OwnerTable,
  405. Columns: []string{decision.OwnerColumn},
  406. Bidi: false,
  407. Target: &sqlgraph.EdgeTarget{
  408. IDSpec: &sqlgraph.FieldSpec{
  409. Type: field.TypeInt,
  410. Column: alert.FieldID,
  411. },
  412. },
  413. }
  414. for _, k := range nodes {
  415. edge.Target.Nodes = append(edge.Target.Nodes, k)
  416. }
  417. _node.alert_decisions = &nodes[0]
  418. _spec.Edges = append(_spec.Edges, edge)
  419. }
  420. return _node, _spec
  421. }
  422. // DecisionCreateBulk is the builder for creating many Decision entities in bulk.
  423. type DecisionCreateBulk struct {
  424. config
  425. builders []*DecisionCreate
  426. }
  427. // Save creates the Decision entities in the database.
  428. func (dcb *DecisionCreateBulk) Save(ctx context.Context) ([]*Decision, error) {
  429. specs := make([]*sqlgraph.CreateSpec, len(dcb.builders))
  430. nodes := make([]*Decision, len(dcb.builders))
  431. mutators := make([]Mutator, len(dcb.builders))
  432. for i := range dcb.builders {
  433. func(i int, root context.Context) {
  434. builder := dcb.builders[i]
  435. builder.defaults()
  436. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  437. mutation, ok := m.(*DecisionMutation)
  438. if !ok {
  439. return nil, fmt.Errorf("unexpected mutation type %T", m)
  440. }
  441. if err := builder.check(); err != nil {
  442. return nil, err
  443. }
  444. builder.mutation = mutation
  445. nodes[i], specs[i] = builder.createSpec()
  446. var err error
  447. if i < len(mutators)-1 {
  448. _, err = mutators[i+1].Mutate(root, dcb.builders[i+1].mutation)
  449. } else {
  450. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  451. // Invoke the actual operation on the latest mutation in the chain.
  452. if err = sqlgraph.BatchCreate(ctx, dcb.driver, spec); err != nil {
  453. if sqlgraph.IsConstraintError(err) {
  454. err = &ConstraintError{err.Error(), err}
  455. }
  456. }
  457. }
  458. if err != nil {
  459. return nil, err
  460. }
  461. mutation.id = &nodes[i].ID
  462. mutation.done = true
  463. if specs[i].ID.Value != nil {
  464. id := specs[i].ID.Value.(int64)
  465. nodes[i].ID = int(id)
  466. }
  467. return nodes[i], nil
  468. })
  469. for i := len(builder.hooks) - 1; i >= 0; i-- {
  470. mut = builder.hooks[i](mut)
  471. }
  472. mutators[i] = mut
  473. }(i, ctx)
  474. }
  475. if len(mutators) > 0 {
  476. if _, err := mutators[0].Mutate(ctx, dcb.builders[0].mutation); err != nil {
  477. return nil, err
  478. }
  479. }
  480. return nodes, nil
  481. }
  482. // SaveX is like Save, but panics if an error occurs.
  483. func (dcb *DecisionCreateBulk) SaveX(ctx context.Context) []*Decision {
  484. v, err := dcb.Save(ctx)
  485. if err != nil {
  486. panic(err)
  487. }
  488. return v
  489. }
  490. // Exec executes the query.
  491. func (dcb *DecisionCreateBulk) Exec(ctx context.Context) error {
  492. _, err := dcb.Save(ctx)
  493. return err
  494. }
  495. // ExecX is like Exec, but panics if an error occurs.
  496. func (dcb *DecisionCreateBulk) ExecX(ctx context.Context) {
  497. if err := dcb.Exec(ctx); err != nil {
  498. panic(err)
  499. }
  500. }