mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 20:36:12 +02:00
context propagation: pkg/database/bouncers (#3249)
This commit is contained in:
parent
fee3debdcc
commit
4a2a663227
6 changed files with 51 additions and 41 deletions
|
@ -41,8 +41,8 @@ func (c *Client) BouncerUpdateBaseMetrics(ctx context.Context, bouncerName strin
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) SelectBouncer(apiKeyHash string) (*ent.Bouncer, error) {
|
||||
result, err := c.Ent.Bouncer.Query().Where(bouncer.APIKeyEQ(apiKeyHash)).First(c.CTX)
|
||||
func (c *Client) SelectBouncer(ctx context.Context, apiKeyHash string) (*ent.Bouncer, error) {
|
||||
result, err := c.Ent.Bouncer.Query().Where(bouncer.APIKeyEQ(apiKeyHash)).First(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -50,8 +50,8 @@ func (c *Client) SelectBouncer(apiKeyHash string) (*ent.Bouncer, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
func (c *Client) SelectBouncerByName(bouncerName string) (*ent.Bouncer, error) {
|
||||
result, err := c.Ent.Bouncer.Query().Where(bouncer.NameEQ(bouncerName)).First(c.CTX)
|
||||
func (c *Client) SelectBouncerByName(ctx context.Context, bouncerName string) (*ent.Bouncer, error) {
|
||||
result, err := c.Ent.Bouncer.Query().Where(bouncer.NameEQ(bouncerName)).First(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -68,14 +68,14 @@ func (c *Client) ListBouncers(ctx context.Context) ([]*ent.Bouncer, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateBouncer(name string, ipAddr string, apiKey string, authType string) (*ent.Bouncer, error) {
|
||||
func (c *Client) CreateBouncer(ctx context.Context, name string, ipAddr string, apiKey string, authType string) (*ent.Bouncer, error) {
|
||||
bouncer, err := c.Ent.Bouncer.
|
||||
Create().
|
||||
SetName(name).
|
||||
SetAPIKey(apiKey).
|
||||
SetRevoked(false).
|
||||
SetAuthType(authType).
|
||||
Save(c.CTX)
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
if ent.IsConstraintError(err) {
|
||||
return nil, fmt.Errorf("bouncer %s already exists", name)
|
||||
|
@ -87,11 +87,11 @@ func (c *Client) CreateBouncer(name string, ipAddr string, apiKey string, authTy
|
|||
return bouncer, nil
|
||||
}
|
||||
|
||||
func (c *Client) DeleteBouncer(name string) error {
|
||||
func (c *Client) DeleteBouncer(ctx context.Context, name string) error {
|
||||
nbDeleted, err := c.Ent.Bouncer.
|
||||
Delete().
|
||||
Where(bouncer.NameEQ(name)).
|
||||
Exec(c.CTX)
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -103,13 +103,13 @@ func (c *Client) DeleteBouncer(name string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) BulkDeleteBouncers(bouncers []*ent.Bouncer) (int, error) {
|
||||
func (c *Client) BulkDeleteBouncers(ctx context.Context, bouncers []*ent.Bouncer) (int, error) {
|
||||
ids := make([]int, len(bouncers))
|
||||
for i, b := range bouncers {
|
||||
ids[i] = b.ID
|
||||
}
|
||||
|
||||
nbDeleted, err := c.Ent.Bouncer.Delete().Where(bouncer.IDIn(ids...)).Exec(c.CTX)
|
||||
nbDeleted, err := c.Ent.Bouncer.Delete().Where(bouncer.IDIn(ids...)).Exec(ctx)
|
||||
if err != nil {
|
||||
return nbDeleted, fmt.Errorf("unable to delete bouncers: %w", err)
|
||||
}
|
||||
|
@ -117,10 +117,10 @@ func (c *Client) BulkDeleteBouncers(bouncers []*ent.Bouncer) (int, error) {
|
|||
return nbDeleted, nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateBouncerLastPull(lastPull time.Time, id int) error {
|
||||
func (c *Client) UpdateBouncerLastPull(ctx context.Context, lastPull time.Time, id int) error {
|
||||
_, err := c.Ent.Bouncer.UpdateOneID(id).
|
||||
SetLastPull(lastPull).
|
||||
Save(c.CTX)
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to update machine last pull in database: %w", err)
|
||||
}
|
||||
|
@ -128,8 +128,8 @@ func (c *Client) UpdateBouncerLastPull(lastPull time.Time, id int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateBouncerIP(ipAddr string, id int) error {
|
||||
_, err := c.Ent.Bouncer.UpdateOneID(id).SetIPAddress(ipAddr).Save(c.CTX)
|
||||
func (c *Client) UpdateBouncerIP(ctx context.Context, ipAddr string, id int) error {
|
||||
_, err := c.Ent.Bouncer.UpdateOneID(id).SetIPAddress(ipAddr).Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to update bouncer ip address in database: %w", err)
|
||||
}
|
||||
|
@ -137,8 +137,8 @@ func (c *Client) UpdateBouncerIP(ipAddr string, id int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateBouncerTypeAndVersion(bType string, version string, id int) error {
|
||||
_, err := c.Ent.Bouncer.UpdateOneID(id).SetVersion(version).SetType(bType).Save(c.CTX)
|
||||
func (c *Client) UpdateBouncerTypeAndVersion(ctx context.Context, bType string, version string, id int) error {
|
||||
_, err := c.Ent.Bouncer.UpdateOneID(id).SetVersion(version).SetType(bType).Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to update bouncer type and version in database: %w", err)
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ func (c *Client) UpdateBouncerTypeAndVersion(bType string, version string, id in
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) QueryBouncersInactiveSince(t time.Time) ([]*ent.Bouncer, error) {
|
||||
func (c *Client) QueryBouncersInactiveSince(ctx context.Context, t time.Time) ([]*ent.Bouncer, error) {
|
||||
return c.Ent.Bouncer.Query().Where(
|
||||
// poor man's coalesce
|
||||
bouncer.Or(
|
||||
|
@ -156,5 +156,5 @@ func (c *Client) QueryBouncersInactiveSince(t time.Time) ([]*ent.Bouncer, error)
|
|||
bouncer.CreatedAtLT(t),
|
||||
),
|
||||
),
|
||||
).All(c.CTX)
|
||||
).All(ctx)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue