Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor "cscli decisions" #3061

Merged
merged 8 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 57 additions & 54 deletions cmd/crowdsec-cli/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
}
apiURL, err := url.Parse(cfg.API.Client.Credentials.URL)
if err != nil {
return fmt.Errorf("parsing api url %s: %w", apiURL, err)
return fmt.Errorf("parsing api url: %w", err)

Check warning on line 207 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L207

Added line #L207 was not covered by tests
}

cli.client, err = apiclient.NewClient(&apiclient.Config{
Expand All @@ -215,7 +215,7 @@
VersionPrefix: "v1",
})
if err != nil {
return fmt.Errorf("new api client: %w", err)
return fmt.Errorf("creating api client: %w", err)

Check warning on line 218 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L218

Added line #L218 was not covered by tests
}

return nil
Expand Down Expand Up @@ -370,6 +370,60 @@
return cmd
}

func (cli *cliAlerts) delete(alertDeleteFilter apiclient.AlertsDeleteOpts, ActiveDecision *bool, AlertDeleteAll bool, delAlertByID string, contained *bool) error {
var err error

Check warning on line 375 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L375

Added line #L375 was not covered by tests
if !AlertDeleteAll {
if err = manageCliDecisionAlerts(alertDeleteFilter.IPEquals, alertDeleteFilter.RangeEquals,
alertDeleteFilter.ScopeEquals, alertDeleteFilter.ValueEquals); err != nil {
return err
}

Check warning on line 380 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L379-L380

Added lines #L379 - L380 were not covered by tests
if ActiveDecision != nil {
alertDeleteFilter.ActiveDecisionEquals = ActiveDecision
}

Check warning on line 383 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L382-L383

Added lines #L382 - L383 were not covered by tests

if *alertDeleteFilter.ScopeEquals == "" {
alertDeleteFilter.ScopeEquals = nil
}

Check warning on line 387 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L387

Added line #L387 was not covered by tests
if *alertDeleteFilter.ValueEquals == "" {
alertDeleteFilter.ValueEquals = nil
}

Check warning on line 390 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L390

Added line #L390 was not covered by tests
if *alertDeleteFilter.ScenarioEquals == "" {
alertDeleteFilter.ScenarioEquals = nil
}

Check warning on line 393 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L393

Added line #L393 was not covered by tests
if *alertDeleteFilter.IPEquals == "" {
alertDeleteFilter.IPEquals = nil
}

Check warning on line 396 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L396

Added line #L396 was not covered by tests
if *alertDeleteFilter.RangeEquals == "" {
alertDeleteFilter.RangeEquals = nil
}

Check warning on line 399 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L399

Added line #L399 was not covered by tests
if contained != nil && *contained {
alertDeleteFilter.Contains = new(bool)
}

Check warning on line 402 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L401-L402

Added lines #L401 - L402 were not covered by tests
limit := 0
alertDeleteFilter.Limit = &limit
} else {
limit := 0
alertDeleteFilter = apiclient.AlertsDeleteOpts{Limit: &limit}
}

Check warning on line 408 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L408

Added line #L408 was not covered by tests

var alerts *models.DeleteAlertsResponse
if delAlertByID == "" {
alerts, _, err = cli.client.Alerts.Delete(context.Background(), alertDeleteFilter)
if err != nil {
return fmt.Errorf("unable to delete alerts: %w", err)
}

Check warning on line 415 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L414-L415

Added lines #L414 - L415 were not covered by tests
} else {
alerts, _, err = cli.client.Alerts.DeleteOne(context.Background(), delAlertByID)
if err != nil {
return fmt.Errorf("unable to delete alert: %w", err)
}

Check warning on line 420 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L420

Added line #L420 was not covered by tests
}
log.Infof("%s alert(s) deleted", alerts.NbDeleted)

Check warning on line 423 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L423

Added line #L423 was not covered by tests
return nil
}

func (cli *cliAlerts) NewDeleteCmd() *cobra.Command {
var (
ActiveDecision *bool
Expand Down Expand Up @@ -411,58 +465,7 @@
return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
var err error

if !AlertDeleteAll {
if err = manageCliDecisionAlerts(alertDeleteFilter.IPEquals, alertDeleteFilter.RangeEquals,
alertDeleteFilter.ScopeEquals, alertDeleteFilter.ValueEquals); err != nil {
printHelp(cmd)
return err
}
if ActiveDecision != nil {
alertDeleteFilter.ActiveDecisionEquals = ActiveDecision
}

if *alertDeleteFilter.ScopeEquals == "" {
alertDeleteFilter.ScopeEquals = nil
}
if *alertDeleteFilter.ValueEquals == "" {
alertDeleteFilter.ValueEquals = nil
}
if *alertDeleteFilter.ScenarioEquals == "" {
alertDeleteFilter.ScenarioEquals = nil
}
if *alertDeleteFilter.IPEquals == "" {
alertDeleteFilter.IPEquals = nil
}
if *alertDeleteFilter.RangeEquals == "" {
alertDeleteFilter.RangeEquals = nil
}
if contained != nil && *contained {
alertDeleteFilter.Contains = new(bool)
}
limit := 0
alertDeleteFilter.Limit = &limit
} else {
limit := 0
alertDeleteFilter = apiclient.AlertsDeleteOpts{Limit: &limit}
}

var alerts *models.DeleteAlertsResponse
if delAlertByID == "" {
alerts, _, err = cli.client.Alerts.Delete(context.Background(), alertDeleteFilter)
if err != nil {
return fmt.Errorf("unable to delete alerts: %w", err)
}
} else {
alerts, _, err = cli.client.Alerts.DeleteOne(context.Background(), delAlertByID)
if err != nil {
return fmt.Errorf("unable to delete alert: %w", err)
}
}
log.Infof("%s alert(s) deleted", alerts.NbDeleted)

return nil
return cli.delete(alertDeleteFilter, ActiveDecision, AlertDeleteAll, delAlertByID, contained)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func QueryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri
return errors.New("no scenarios installed, abort")
}

Client, err = apiclient.NewDefaultClient(apiURL,
client, err := apiclient.NewDefaultClient(apiURL,
CAPIURLPrefix,
cwversion.UserAgent(),
nil)
Expand All @@ -180,7 +180,7 @@ func QueryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri
Scenarios: scenarios,
}

_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
_, _, err = client.Auth.AuthenticateWatcher(context.Background(), t)
if err != nil {
return err
}
Expand Down
Loading
Loading