decision_create.go 14 KB

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