machine_create.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/machine"
  12. )
  13. // MachineCreate is the builder for creating a Machine entity.
  14. type MachineCreate struct {
  15. config
  16. mutation *MachineMutation
  17. hooks []Hook
  18. }
  19. // SetCreatedAt sets the "created_at" field.
  20. func (mc *MachineCreate) SetCreatedAt(t time.Time) *MachineCreate {
  21. mc.mutation.SetCreatedAt(t)
  22. return mc
  23. }
  24. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  25. func (mc *MachineCreate) SetNillableCreatedAt(t *time.Time) *MachineCreate {
  26. if t != nil {
  27. mc.SetCreatedAt(*t)
  28. }
  29. return mc
  30. }
  31. // SetUpdatedAt sets the "updated_at" field.
  32. func (mc *MachineCreate) SetUpdatedAt(t time.Time) *MachineCreate {
  33. mc.mutation.SetUpdatedAt(t)
  34. return mc
  35. }
  36. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  37. func (mc *MachineCreate) SetNillableUpdatedAt(t *time.Time) *MachineCreate {
  38. if t != nil {
  39. mc.SetUpdatedAt(*t)
  40. }
  41. return mc
  42. }
  43. // SetLastPush sets the "last_push" field.
  44. func (mc *MachineCreate) SetLastPush(t time.Time) *MachineCreate {
  45. mc.mutation.SetLastPush(t)
  46. return mc
  47. }
  48. // SetNillableLastPush sets the "last_push" field if the given value is not nil.
  49. func (mc *MachineCreate) SetNillableLastPush(t *time.Time) *MachineCreate {
  50. if t != nil {
  51. mc.SetLastPush(*t)
  52. }
  53. return mc
  54. }
  55. // SetMachineId sets the "machineId" field.
  56. func (mc *MachineCreate) SetMachineId(s string) *MachineCreate {
  57. mc.mutation.SetMachineId(s)
  58. return mc
  59. }
  60. // SetPassword sets the "password" field.
  61. func (mc *MachineCreate) SetPassword(s string) *MachineCreate {
  62. mc.mutation.SetPassword(s)
  63. return mc
  64. }
  65. // SetIpAddress sets the "ipAddress" field.
  66. func (mc *MachineCreate) SetIpAddress(s string) *MachineCreate {
  67. mc.mutation.SetIpAddress(s)
  68. return mc
  69. }
  70. // SetScenarios sets the "scenarios" field.
  71. func (mc *MachineCreate) SetScenarios(s string) *MachineCreate {
  72. mc.mutation.SetScenarios(s)
  73. return mc
  74. }
  75. // SetNillableScenarios sets the "scenarios" field if the given value is not nil.
  76. func (mc *MachineCreate) SetNillableScenarios(s *string) *MachineCreate {
  77. if s != nil {
  78. mc.SetScenarios(*s)
  79. }
  80. return mc
  81. }
  82. // SetVersion sets the "version" field.
  83. func (mc *MachineCreate) SetVersion(s string) *MachineCreate {
  84. mc.mutation.SetVersion(s)
  85. return mc
  86. }
  87. // SetNillableVersion sets the "version" field if the given value is not nil.
  88. func (mc *MachineCreate) SetNillableVersion(s *string) *MachineCreate {
  89. if s != nil {
  90. mc.SetVersion(*s)
  91. }
  92. return mc
  93. }
  94. // SetIsValidated sets the "isValidated" field.
  95. func (mc *MachineCreate) SetIsValidated(b bool) *MachineCreate {
  96. mc.mutation.SetIsValidated(b)
  97. return mc
  98. }
  99. // SetNillableIsValidated sets the "isValidated" field if the given value is not nil.
  100. func (mc *MachineCreate) SetNillableIsValidated(b *bool) *MachineCreate {
  101. if b != nil {
  102. mc.SetIsValidated(*b)
  103. }
  104. return mc
  105. }
  106. // SetStatus sets the "status" field.
  107. func (mc *MachineCreate) SetStatus(s string) *MachineCreate {
  108. mc.mutation.SetStatus(s)
  109. return mc
  110. }
  111. // SetNillableStatus sets the "status" field if the given value is not nil.
  112. func (mc *MachineCreate) SetNillableStatus(s *string) *MachineCreate {
  113. if s != nil {
  114. mc.SetStatus(*s)
  115. }
  116. return mc
  117. }
  118. // AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
  119. func (mc *MachineCreate) AddAlertIDs(ids ...int) *MachineCreate {
  120. mc.mutation.AddAlertIDs(ids...)
  121. return mc
  122. }
  123. // AddAlerts adds the "alerts" edges to the Alert entity.
  124. func (mc *MachineCreate) AddAlerts(a ...*Alert) *MachineCreate {
  125. ids := make([]int, len(a))
  126. for i := range a {
  127. ids[i] = a[i].ID
  128. }
  129. return mc.AddAlertIDs(ids...)
  130. }
  131. // Mutation returns the MachineMutation object of the builder.
  132. func (mc *MachineCreate) Mutation() *MachineMutation {
  133. return mc.mutation
  134. }
  135. // Save creates the Machine in the database.
  136. func (mc *MachineCreate) Save(ctx context.Context) (*Machine, error) {
  137. var (
  138. err error
  139. node *Machine
  140. )
  141. mc.defaults()
  142. if len(mc.hooks) == 0 {
  143. if err = mc.check(); err != nil {
  144. return nil, err
  145. }
  146. node, err = mc.sqlSave(ctx)
  147. } else {
  148. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  149. mutation, ok := m.(*MachineMutation)
  150. if !ok {
  151. return nil, fmt.Errorf("unexpected mutation type %T", m)
  152. }
  153. if err = mc.check(); err != nil {
  154. return nil, err
  155. }
  156. mc.mutation = mutation
  157. if node, err = mc.sqlSave(ctx); err != nil {
  158. return nil, err
  159. }
  160. mutation.id = &node.ID
  161. mutation.done = true
  162. return node, err
  163. })
  164. for i := len(mc.hooks) - 1; i >= 0; i-- {
  165. if mc.hooks[i] == nil {
  166. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  167. }
  168. mut = mc.hooks[i](mut)
  169. }
  170. if _, err := mut.Mutate(ctx, mc.mutation); err != nil {
  171. return nil, err
  172. }
  173. }
  174. return node, err
  175. }
  176. // SaveX calls Save and panics if Save returns an error.
  177. func (mc *MachineCreate) SaveX(ctx context.Context) *Machine {
  178. v, err := mc.Save(ctx)
  179. if err != nil {
  180. panic(err)
  181. }
  182. return v
  183. }
  184. // Exec executes the query.
  185. func (mc *MachineCreate) Exec(ctx context.Context) error {
  186. _, err := mc.Save(ctx)
  187. return err
  188. }
  189. // ExecX is like Exec, but panics if an error occurs.
  190. func (mc *MachineCreate) ExecX(ctx context.Context) {
  191. if err := mc.Exec(ctx); err != nil {
  192. panic(err)
  193. }
  194. }
  195. // defaults sets the default values of the builder before save.
  196. func (mc *MachineCreate) defaults() {
  197. if _, ok := mc.mutation.CreatedAt(); !ok {
  198. v := machine.DefaultCreatedAt()
  199. mc.mutation.SetCreatedAt(v)
  200. }
  201. if _, ok := mc.mutation.UpdatedAt(); !ok {
  202. v := machine.DefaultUpdatedAt()
  203. mc.mutation.SetUpdatedAt(v)
  204. }
  205. if _, ok := mc.mutation.LastPush(); !ok {
  206. v := machine.DefaultLastPush()
  207. mc.mutation.SetLastPush(v)
  208. }
  209. if _, ok := mc.mutation.IsValidated(); !ok {
  210. v := machine.DefaultIsValidated
  211. mc.mutation.SetIsValidated(v)
  212. }
  213. }
  214. // check runs all checks and user-defined validators on the builder.
  215. func (mc *MachineCreate) check() error {
  216. if _, ok := mc.mutation.MachineId(); !ok {
  217. return &ValidationError{Name: "machineId", err: errors.New(`ent: missing required field "machineId"`)}
  218. }
  219. if _, ok := mc.mutation.Password(); !ok {
  220. return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "password"`)}
  221. }
  222. if _, ok := mc.mutation.IpAddress(); !ok {
  223. return &ValidationError{Name: "ipAddress", err: errors.New(`ent: missing required field "ipAddress"`)}
  224. }
  225. if v, ok := mc.mutation.Scenarios(); ok {
  226. if err := machine.ScenariosValidator(v); err != nil {
  227. return &ValidationError{Name: "scenarios", err: fmt.Errorf(`ent: validator failed for field "scenarios": %w`, err)}
  228. }
  229. }
  230. if _, ok := mc.mutation.IsValidated(); !ok {
  231. return &ValidationError{Name: "isValidated", err: errors.New(`ent: missing required field "isValidated"`)}
  232. }
  233. return nil
  234. }
  235. func (mc *MachineCreate) sqlSave(ctx context.Context) (*Machine, error) {
  236. _node, _spec := mc.createSpec()
  237. if err := sqlgraph.CreateNode(ctx, mc.driver, _spec); err != nil {
  238. if sqlgraph.IsConstraintError(err) {
  239. err = &ConstraintError{err.Error(), err}
  240. }
  241. return nil, err
  242. }
  243. id := _spec.ID.Value.(int64)
  244. _node.ID = int(id)
  245. return _node, nil
  246. }
  247. func (mc *MachineCreate) createSpec() (*Machine, *sqlgraph.CreateSpec) {
  248. var (
  249. _node = &Machine{config: mc.config}
  250. _spec = &sqlgraph.CreateSpec{
  251. Table: machine.Table,
  252. ID: &sqlgraph.FieldSpec{
  253. Type: field.TypeInt,
  254. Column: machine.FieldID,
  255. },
  256. }
  257. )
  258. if value, ok := mc.mutation.CreatedAt(); ok {
  259. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  260. Type: field.TypeTime,
  261. Value: value,
  262. Column: machine.FieldCreatedAt,
  263. })
  264. _node.CreatedAt = &value
  265. }
  266. if value, ok := mc.mutation.UpdatedAt(); ok {
  267. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  268. Type: field.TypeTime,
  269. Value: value,
  270. Column: machine.FieldUpdatedAt,
  271. })
  272. _node.UpdatedAt = &value
  273. }
  274. if value, ok := mc.mutation.LastPush(); ok {
  275. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  276. Type: field.TypeTime,
  277. Value: value,
  278. Column: machine.FieldLastPush,
  279. })
  280. _node.LastPush = &value
  281. }
  282. if value, ok := mc.mutation.MachineId(); ok {
  283. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  284. Type: field.TypeString,
  285. Value: value,
  286. Column: machine.FieldMachineId,
  287. })
  288. _node.MachineId = value
  289. }
  290. if value, ok := mc.mutation.Password(); ok {
  291. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  292. Type: field.TypeString,
  293. Value: value,
  294. Column: machine.FieldPassword,
  295. })
  296. _node.Password = value
  297. }
  298. if value, ok := mc.mutation.IpAddress(); ok {
  299. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  300. Type: field.TypeString,
  301. Value: value,
  302. Column: machine.FieldIpAddress,
  303. })
  304. _node.IpAddress = value
  305. }
  306. if value, ok := mc.mutation.Scenarios(); ok {
  307. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  308. Type: field.TypeString,
  309. Value: value,
  310. Column: machine.FieldScenarios,
  311. })
  312. _node.Scenarios = value
  313. }
  314. if value, ok := mc.mutation.Version(); ok {
  315. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  316. Type: field.TypeString,
  317. Value: value,
  318. Column: machine.FieldVersion,
  319. })
  320. _node.Version = value
  321. }
  322. if value, ok := mc.mutation.IsValidated(); ok {
  323. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  324. Type: field.TypeBool,
  325. Value: value,
  326. Column: machine.FieldIsValidated,
  327. })
  328. _node.IsValidated = value
  329. }
  330. if value, ok := mc.mutation.Status(); ok {
  331. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  332. Type: field.TypeString,
  333. Value: value,
  334. Column: machine.FieldStatus,
  335. })
  336. _node.Status = value
  337. }
  338. if nodes := mc.mutation.AlertsIDs(); len(nodes) > 0 {
  339. edge := &sqlgraph.EdgeSpec{
  340. Rel: sqlgraph.O2M,
  341. Inverse: false,
  342. Table: machine.AlertsTable,
  343. Columns: []string{machine.AlertsColumn},
  344. Bidi: false,
  345. Target: &sqlgraph.EdgeTarget{
  346. IDSpec: &sqlgraph.FieldSpec{
  347. Type: field.TypeInt,
  348. Column: alert.FieldID,
  349. },
  350. },
  351. }
  352. for _, k := range nodes {
  353. edge.Target.Nodes = append(edge.Target.Nodes, k)
  354. }
  355. _spec.Edges = append(_spec.Edges, edge)
  356. }
  357. return _node, _spec
  358. }
  359. // MachineCreateBulk is the builder for creating many Machine entities in bulk.
  360. type MachineCreateBulk struct {
  361. config
  362. builders []*MachineCreate
  363. }
  364. // Save creates the Machine entities in the database.
  365. func (mcb *MachineCreateBulk) Save(ctx context.Context) ([]*Machine, error) {
  366. specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
  367. nodes := make([]*Machine, len(mcb.builders))
  368. mutators := make([]Mutator, len(mcb.builders))
  369. for i := range mcb.builders {
  370. func(i int, root context.Context) {
  371. builder := mcb.builders[i]
  372. builder.defaults()
  373. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  374. mutation, ok := m.(*MachineMutation)
  375. if !ok {
  376. return nil, fmt.Errorf("unexpected mutation type %T", m)
  377. }
  378. if err := builder.check(); err != nil {
  379. return nil, err
  380. }
  381. builder.mutation = mutation
  382. nodes[i], specs[i] = builder.createSpec()
  383. var err error
  384. if i < len(mutators)-1 {
  385. _, err = mutators[i+1].Mutate(root, mcb.builders[i+1].mutation)
  386. } else {
  387. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  388. // Invoke the actual operation on the latest mutation in the chain.
  389. if err = sqlgraph.BatchCreate(ctx, mcb.driver, spec); err != nil {
  390. if sqlgraph.IsConstraintError(err) {
  391. err = &ConstraintError{err.Error(), err}
  392. }
  393. }
  394. }
  395. if err != nil {
  396. return nil, err
  397. }
  398. mutation.id = &nodes[i].ID
  399. mutation.done = true
  400. if specs[i].ID.Value != nil {
  401. id := specs[i].ID.Value.(int64)
  402. nodes[i].ID = int(id)
  403. }
  404. return nodes[i], nil
  405. })
  406. for i := len(builder.hooks) - 1; i >= 0; i-- {
  407. mut = builder.hooks[i](mut)
  408. }
  409. mutators[i] = mut
  410. }(i, ctx)
  411. }
  412. if len(mutators) > 0 {
  413. if _, err := mutators[0].Mutate(ctx, mcb.builders[0].mutation); err != nil {
  414. return nil, err
  415. }
  416. }
  417. return nodes, nil
  418. }
  419. // SaveX is like Save, but panics if an error occurs.
  420. func (mcb *MachineCreateBulk) SaveX(ctx context.Context) []*Machine {
  421. v, err := mcb.Save(ctx)
  422. if err != nil {
  423. panic(err)
  424. }
  425. return v
  426. }
  427. // Exec executes the query.
  428. func (mcb *MachineCreateBulk) Exec(ctx context.Context) error {
  429. _, err := mcb.Save(ctx)
  430. return err
  431. }
  432. // ExecX is like Exec, but panics if an error occurs.
  433. func (mcb *MachineCreateBulk) ExecX(ctx context.Context) {
  434. if err := mcb.Exec(ctx); err != nil {
  435. panic(err)
  436. }
  437. }