Skip to content

Commit

Permalink
implement remove check
Browse files Browse the repository at this point in the history
  • Loading branch information
techiepriyansh committed Oct 28, 2022
1 parent 6a1e78a commit 61130f9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
71 changes: 69 additions & 2 deletions pkg/components/central/central.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package central

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -62,13 +63,26 @@ func Run(ctx *appcontext.Context) error {
}

func RunApply(ctx *appcontext.Context, checkdiff *configfile.CheckDiff) error {
allErrors := ""

for i := range checkdiff.Additions {
err := AddCheck(ctx, &checkdiff.Additions[i])
if err != nil {
return err
allErrors = fmt.Sprintf("%serror in applying addition: %s\n", allErrors, err.Error())
}
}

for i := range checkdiff.Removals {
err := RemoveCheck(ctx, checkdiff.Removals[i])
if err != nil {
allErrors = fmt.Sprintf("%serror in applying removal: %s\n", allErrors, err.Error())
}
}

if allErrors != "" {
return errors.New(allErrors)
}

return nil
}

Expand All @@ -95,7 +109,7 @@ func AddCheck(ctx *appcontext.Context, check *config.Check) error {
client := agentProto.NewAgentClient(conn)

protoCheck := config.CheckToProto(check)
res, err := client.PushCheck(context.Background(), &protoCheck)
res, err := client.PushCheck(context.Background(), &protoCheck) // is using context.Background here correct?
if err != nil {
return err
}
Expand All @@ -104,6 +118,59 @@ func AddCheck(ctx *appcontext.Context, check *config.Check) error {
return fmt.Errorf("could not add check: %s", res.GetError())
}

// Also make use of res.GetSuccessful()

return nil
}

func RemoveCheck(ctx *appcontext.Context, checkID string) error {
agents, err := getAllAgents(ctx)
if err != nil {
return err
}

for _, agent := range agents {
err := removeCheckFromAgent(ctx, checkID, agent)
if err != nil {
ctx.Logger().
WithError(err).
Errorln("could not broadcast remove check message to an agent")
}
}

return nil
}

func removeCheckFromAgent(ctx *appcontext.Context, checkID string, agent string) error {
conn, err := grpc.Dial(agent, grpc.WithInsecure())
if err != nil {
return fmt.Errorf("cannot connect to agent with tcp address %s: %w", agent, err)
}

defer func() {
_err := conn.Close()
if _err != nil {
ctx.Logger().
WithError(_err).
Errorln("could not close connection to agent's grpc server")
}
}()

client := agentProto.NewAgentClient(conn)

res, err := client.RemoveCheck(context.Background(), &agentProto.CheckID{
ID: checkID,
}) // is using context.Background here correct?
if err != nil {
return err
}

// Also make use of res.GetSuccessful()

if res.GetError() != "" {
return fmt.Errorf("could not add check: %s", res.GetError())
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/configfile/checkdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (

type CheckDiff struct {
Additions []config.Check `mapstructure:"additions" json:"additions"`
Removals []config.Check `mapstructure:"removals" json:"removals"`
Removals []string `mapstructure:"removals" json:"removals"`
}

0 comments on commit 61130f9

Please sign in to comment.