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