-
Notifications
You must be signed in to change notification settings - Fork 25
Aggregate alerts #164
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
Merged
danielamkaer
merged 2 commits into
humio:master
from
fjerlov-cs:fjerlov/aggregate-alert
Jul 12, 2024
Merged
Aggregate alerts #164
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "fmt" | ||
| graphql "github.com/cli/shurcooL-graphql" | ||
| "github.com/humio/cli/api/internal/humiographql" | ||
| ) | ||
|
|
||
| type AggregateAlert struct { | ||
| ID string `graphql:"id" yaml:"-" json:"id"` | ||
| Name string `graphql:"name" yaml:"name" json:"name"` | ||
| Description string `graphql:"description" yaml:"description,omitempty" json:"description,omitempty"` | ||
| QueryString string `graphql:"queryString" yaml:"queryString" json:"queryString"` | ||
| SearchIntervalSeconds int `graphql:"searchIntervalSeconds" yaml:"searchIntervalSeconds" json:"searchIntervalSeconds"` | ||
| ActionNames []string `graphql:"actionNames" yaml:"actionNames" json:"actionNames"` | ||
| Labels []string `graphql:"labels" yaml:"labels" json:"labels"` | ||
| Enabled bool `graphql:"enabled" yaml:"enabled" json:"enabled"` | ||
| ThrottleField string `graphql:"throttleField" yaml:"throttleField,omitempty" json:"throttleField,omitempty"` | ||
| ThrottleTimeSeconds int `graphql:"throttleTimeSeconds" yaml:"throttleTimeSeconds" json:"throttleTimeSeconds"` | ||
| QueryOwnershipType string `graphql:"queryOwnership" yaml:"queryOwnershipType" json:"queryOwnershipType"` | ||
| TriggerMode string `graphql:"triggerMode" yaml:"triggerMode" json:"triggerMode"` | ||
| QueryTimestampType string `graphql:"queryTimestampType" yaml:"queryTimestampType" json:"queryTimestampType"` | ||
| RunAsUserID string `graphql:"runAsUserId" yaml:"runAsUserId,omitempty" json:"runAsUserId,omitempty"` | ||
| } | ||
|
|
||
| type AggregateAlerts struct { | ||
| client *Client | ||
| } | ||
|
|
||
| func (c *Client) AggregateAlerts() *AggregateAlerts { return &AggregateAlerts{client: c} } | ||
|
|
||
| func (a *AggregateAlerts) List(viewName string) ([]AggregateAlert, error) { | ||
| var query struct { | ||
| SearchDomain struct { | ||
| AggregateAlerts []humiographql.AggregateAlert `graphql:"aggregateAlerts"` | ||
| } `graphql:"searchDomain(name: $viewName)"` | ||
| } | ||
|
|
||
| variables := map[string]any{ | ||
| "viewName": graphql.String(viewName), | ||
| } | ||
|
|
||
| err := a.client.Query(&query, variables) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var aggregateAlerts = make([]AggregateAlert, len(query.SearchDomain.AggregateAlerts)) | ||
| for i := range query.SearchDomain.AggregateAlerts { | ||
| aggregateAlerts[i] = mapHumioGraphqlAggregateAlertToAggregateAlert(query.SearchDomain.AggregateAlerts[i]) | ||
| } | ||
|
|
||
| return aggregateAlerts, err | ||
fjerlov-cs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (a *AggregateAlerts) Update(viewName string, updatedAggregateAlert *AggregateAlert) (*AggregateAlert, error) { | ||
| if updatedAggregateAlert == nil { | ||
| return nil, fmt.Errorf("updatedAggregateAlert must not be nil") | ||
| } | ||
|
|
||
| if updatedAggregateAlert.ID == "" { | ||
| return nil, fmt.Errorf("updatedAggregateAlert must have non-empty ID") | ||
| } | ||
|
|
||
| var mutation struct { | ||
| humiographql.AggregateAlert `graphql:"updateAggregateAlert(input: $input)"` | ||
| } | ||
|
|
||
| actionNames := make([]graphql.String, len(updatedAggregateAlert.ActionNames)) | ||
| for i, actionName := range updatedAggregateAlert.ActionNames { | ||
| actionNames[i] = graphql.String(actionName) | ||
| } | ||
|
|
||
| labels := make([]graphql.String, len(updatedAggregateAlert.Labels)) | ||
| for i, label := range updatedAggregateAlert.Labels { | ||
| labels[i] = graphql.String(label) | ||
| } | ||
|
|
||
| updateAlert := humiographql.UpdateAggregateAlert{ | ||
| ViewName: humiographql.RepoOrViewName(viewName), | ||
| ID: graphql.String(updatedAggregateAlert.ID), | ||
| Name: graphql.String(updatedAggregateAlert.Name), | ||
| Description: graphql.String(updatedAggregateAlert.Description), | ||
| QueryString: graphql.String(updatedAggregateAlert.QueryString), | ||
| SearchIntervalSeconds: humiographql.Long(updatedAggregateAlert.SearchIntervalSeconds), | ||
| ActionIdsOrNames: actionNames, | ||
| Labels: labels, | ||
| Enabled: graphql.Boolean(updatedAggregateAlert.Enabled), | ||
| RunAsUserID: graphql.String(updatedAggregateAlert.RunAsUserID), | ||
| ThrottleField: graphql.String(updatedAggregateAlert.ThrottleField), | ||
| ThrottleTimeSeconds: humiographql.Long(updatedAggregateAlert.ThrottleTimeSeconds), | ||
| TriggerMode: humiographql.TriggerMode(updatedAggregateAlert.TriggerMode), | ||
| QueryTimestampType: humiographql.QueryTimestampType(updatedAggregateAlert.QueryTimestampType), | ||
| QueryOwnershipType: humiographql.QueryOwnershipType(updatedAggregateAlert.QueryOwnershipType), | ||
| } | ||
|
|
||
| variables := map[string]any{ | ||
| "input": updateAlert, | ||
| } | ||
|
|
||
| err := a.client.Mutate(&mutation, variables) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(mutation.AggregateAlert) | ||
|
|
||
| return &aggregateAlert, nil | ||
| } | ||
|
|
||
| func (a *AggregateAlerts) Create(viewName string, newAggregateAlert *AggregateAlert) (*AggregateAlert, error) { | ||
| if newAggregateAlert == nil { | ||
| return nil, fmt.Errorf("newAggregateAlert must not be nil") | ||
| } | ||
|
|
||
| var mutation struct { | ||
| humiographql.AggregateAlert `graphql:"createAggregateAlert(input: $input)"` | ||
| } | ||
|
|
||
| actionNames := make([]graphql.String, len(newAggregateAlert.ActionNames)) | ||
| for i, actionName := range newAggregateAlert.ActionNames { | ||
| actionNames[i] = graphql.String(actionName) | ||
| } | ||
|
|
||
| labels := make([]graphql.String, len(newAggregateAlert.Labels)) | ||
| for i, label := range newAggregateAlert.Labels { | ||
| labels[i] = graphql.String(label) | ||
| } | ||
|
|
||
| createAggregateAlert := humiographql.CreateAggregateAlert{ | ||
| ViewName: humiographql.RepoOrViewName(viewName), | ||
| Name: graphql.String(newAggregateAlert.Name), | ||
| Description: graphql.String(newAggregateAlert.Description), | ||
| QueryString: graphql.String(newAggregateAlert.QueryString), | ||
| SearchIntervalSeconds: humiographql.Long(newAggregateAlert.SearchIntervalSeconds), | ||
| ActionIdsOrNames: actionNames, | ||
| Labels: labels, | ||
| Enabled: graphql.Boolean(newAggregateAlert.Enabled), | ||
| ThrottleField: graphql.String(newAggregateAlert.ThrottleField), | ||
| ThrottleTimeSeconds: humiographql.Long(newAggregateAlert.ThrottleTimeSeconds), | ||
| RunAsUserID: graphql.String(newAggregateAlert.RunAsUserID), | ||
| TriggerMode: humiographql.TriggerMode(newAggregateAlert.TriggerMode), | ||
| QueryTimestampType: humiographql.QueryTimestampType(newAggregateAlert.QueryTimestampType), | ||
| QueryOwnershipType: humiographql.QueryOwnershipType(newAggregateAlert.QueryOwnershipType), | ||
| } | ||
|
|
||
| variables := map[string]any{ | ||
| "input": createAggregateAlert, | ||
| } | ||
|
|
||
| err := a.client.Mutate(&mutation, variables) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(mutation.AggregateAlert) | ||
|
|
||
| return &aggregateAlert, nil | ||
| } | ||
|
|
||
| func (a *AggregateAlerts) Delete(viewName, aggregateAlertID string) error { | ||
| if aggregateAlertID == "" { | ||
fjerlov-cs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return fmt.Errorf("aggregateAlertID is empty") | ||
| } | ||
|
|
||
| var mutation struct { | ||
| DidDelete bool `graphql:"deleteAggregateAlert(input: { viewName: $viewName, id: $id })"` | ||
| } | ||
|
|
||
| variables := map[string]any{ | ||
| "viewName": humiographql.RepoOrViewName(viewName), | ||
| "id": graphql.String(aggregateAlertID), | ||
| } | ||
|
|
||
| err := a.client.Mutate(&mutation, variables) | ||
|
|
||
| if !mutation.DidDelete { | ||
| return fmt.Errorf("unable to remove aggregate alert in repo/view '%s' with id '%s'", viewName, aggregateAlertID) | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func (a *AggregateAlerts) Get(viewName string, aggregateAlertID string) (*AggregateAlert, error) { | ||
| var query struct { | ||
| SearchDomain struct { | ||
| AggregateAlert humiographql.AggregateAlert `graphql:"aggregateAlert(id: $aggregateAlertId)"` | ||
| } `graphql:"searchDomain(name: $viewName) "` | ||
| } | ||
|
|
||
| variables := map[string]any{ | ||
| "viewName": graphql.String(viewName), | ||
| "aggregateAlertId": graphql.String(aggregateAlertID), | ||
| } | ||
|
|
||
| err := a.client.Query(&query, variables) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(query.SearchDomain.AggregateAlert) | ||
|
|
||
| return &aggregateAlert, nil | ||
| } | ||
|
|
||
| func mapHumioGraphqlAggregateAlertToAggregateAlert(input humiographql.AggregateAlert) AggregateAlert { | ||
| var queryOwnershipType, runAsUserID string | ||
| switch input.QueryOwnership.QueryOwnershipTypeName { | ||
| case humiographql.QueryOwnershipTypeNameOrganization: | ||
| queryOwnershipType = QueryOwnershipTypeOrganization | ||
| case humiographql.QueryOwnershipTypeNameUser: | ||
| queryOwnershipType = QueryOwnershipTypeUser | ||
| runAsUserID = string(input.QueryOwnership.ID) | ||
| } | ||
|
|
||
| var actionNames = make([]string, len(input.Actions)) | ||
| for i := range input.Actions { | ||
| actionNames[i] = string(input.Actions[i].Name) | ||
| } | ||
|
|
||
| var labels = make([]string, len(input.Labels)) | ||
| for i := range input.Labels { | ||
| labels[i] = string(input.Labels[i]) | ||
| } | ||
|
|
||
| return AggregateAlert{ | ||
| ID: string(input.ID), | ||
| Name: string(input.Name), | ||
| Description: string(input.Description), | ||
| QueryString: string(input.QueryString), | ||
| SearchIntervalSeconds: int(input.SearchIntervalSeconds), | ||
| ActionNames: actionNames, | ||
| Labels: labels, | ||
| Enabled: bool(input.Enabled), | ||
| ThrottleField: string(input.ThrottleField), | ||
| ThrottleTimeSeconds: int(input.ThrottleTimeSeconds), | ||
| QueryOwnershipType: queryOwnershipType, | ||
| TriggerMode: string(input.TriggerMode), | ||
| QueryTimestampType: string(input.QueryTimestampType), | ||
| RunAsUserID: runAsUserID, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package humiographql | ||
|
|
||
| import graphql "github.com/cli/shurcooL-graphql" | ||
|
|
||
| type Action struct { | ||
| Name graphql.String `graphql:"name"` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package humiographql | ||
|
|
||
| import graphql "github.com/cli/shurcooL-graphql" | ||
|
|
||
| type AggregateAlert struct { | ||
| ID graphql.String `graphql:"id"` | ||
fjerlov-cs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Name graphql.String `graphql:"name"` | ||
fjerlov-cs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Description graphql.String `graphql:"description"` | ||
| QueryString graphql.String `graphql:"queryString"` | ||
| SearchIntervalSeconds Long `graphql:"searchIntervalSeconds"` | ||
| ThrottleTimeSeconds Long `graphql:"throttleTimeSeconds"` | ||
| ThrottleField graphql.String `graphql:"throttleField"` | ||
| Actions []Action `graphql:"actions"` | ||
| Labels []graphql.String `graphql:"labels"` | ||
| Enabled graphql.Boolean `graphql:"enabled"` | ||
| QueryOwnership QueryOwnership `graphql:"queryOwnership"` | ||
| TriggerMode TriggerMode `graphql:"triggerMode"` | ||
| QueryTimestampType QueryTimestampType `graphql:"queryTimestampType"` | ||
| } | ||
|
|
||
| type CreateAggregateAlert struct { | ||
| ViewName RepoOrViewName `json:"viewName"` | ||
| Name graphql.String `json:"name"` | ||
| Description graphql.String `json:"description,omitempty"` | ||
| QueryString graphql.String `json:"queryString"` | ||
| SearchIntervalSeconds Long `json:"searchIntervalSeconds"` | ||
| ThrottleTimeSeconds Long `json:"throttleTimeSeconds"` | ||
| ThrottleField graphql.String `json:"throttleField,omitempty"` | ||
| ActionIdsOrNames []graphql.String `json:"actionIdsOrNames"` | ||
| Labels []graphql.String `json:"labels"` | ||
| Enabled graphql.Boolean `json:"enabled"` | ||
| RunAsUserID graphql.String `json:"runAsUserId,omitempty"` | ||
| QueryOwnershipType QueryOwnershipType `json:"queryOwnershipType"` | ||
| TriggerMode TriggerMode `json:"triggerMode,omitempty"` | ||
| QueryTimestampType QueryTimestampType `json:"queryTimestampType"` | ||
| } | ||
|
|
||
| type UpdateAggregateAlert struct { | ||
| ViewName RepoOrViewName `json:"viewName"` | ||
| ID graphql.String `json:"id"` | ||
| Name graphql.String `json:"name"` | ||
| Description graphql.String `json:"description,omitempty"` | ||
| QueryString graphql.String `json:"queryString"` | ||
| SearchIntervalSeconds Long `json:"searchIntervalSeconds"` | ||
| ThrottleTimeSeconds Long `json:"throttleTimeSeconds"` | ||
| ThrottleField graphql.String `json:"throttleField,omitempty"` | ||
| ActionIdsOrNames []graphql.String `json:"actionIdsOrNames"` | ||
| Labels []graphql.String `json:"labels"` | ||
| Enabled graphql.Boolean `json:"enabled"` | ||
| RunAsUserID graphql.String `json:"runAsUserId,omitempty"` | ||
| QueryOwnershipType QueryOwnershipType `json:"queryOwnershipType"` | ||
| TriggerMode TriggerMode `json:"triggerMode"` | ||
| QueryTimestampType QueryTimestampType `json:"queryTimestampType"` | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package humiographql | ||
|
|
||
| type QueryTimestampType string | ||
|
|
||
| const ( | ||
| QueryTimestampTypeIngestTimestamp QueryTimestampType = "IngestTimestamp" | ||
| QueryTimestampTypeEventTimestamp QueryTimestampType = "EventTimestamp" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package humiographql | ||
|
|
||
| type TriggerMode string | ||
|
|
||
| const ( | ||
| TriggerModeCompleteMode TriggerMode = "CompleteMode" | ||
| TriggerModeImmediateMode TriggerMode = "ImmediateMode" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright © 2024 CrowdStrike | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newAggregateAlertsCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "aggregate-alerts", | ||
| Short: "Manage aggregate alerts", | ||
| } | ||
|
|
||
| cmd.AddCommand(newAggregateAlertsListCmd()) | ||
| cmd.AddCommand(newAggregateAlertsInstallCmd()) | ||
| cmd.AddCommand(newAggregateAlertsExportCmd()) | ||
| cmd.AddCommand(newAggregateAlertsRemoveCmd()) | ||
| cmd.AddCommand(newAggregateAlertsShowCmd()) | ||
|
|
||
| return cmd | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.