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