Skip to content

Commit

Permalink
Add --runAsUserId and QueryOwnershipType to alerts (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjerlov-cs committed Mar 1, 2024
1 parent 2bcc3af commit 1d7631a
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 85 deletions.
187 changes: 106 additions & 81 deletions api/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ package api
import (
"fmt"
graphql "github.com/cli/shurcooL-graphql"
"github.com/humio/cli/api/internal/humiographql"
)

type Alert struct {
ID string `graphql:"id" yaml:"-" json:"id"`
Name string `graphql:"name" yaml:"name" json:"name"`
QueryString string `graphql:"queryString" yaml:"queryString" json:"queryString"`
QueryStart string `graphql:"queryStart" yaml:"queryStart" json:"queryStart"`
ThrottleField string `graphql:"throttleField" yaml:"throttleField" json:"throttleField"`
TimeOfLastTrigger int `graphql:"timeOfLastTrigger" yaml:"timeOfLastTrigger" json:"timeOfLastTrigger"`
IsStarred bool `graphql:"isStarred" yaml:"isStarred" json:"isStarred"`
Description string `graphql:"description" yaml:"description,omitempty" json:"description"`
ThrottleTimeMillis int `graphql:"throttleTimeMillis" yaml:"throttleTimeMillis" json:"throttleTimeMillis"`
Enabled bool `graphql:"enabled" yaml:"enabled" json:"enabled"`
Actions []string `graphql:"actions" yaml:"actions" json:"actions"`
Labels []string `graphql:"labels" yaml:"labels,omitempty" json:"labels,omitempty"`
LastError string `graphql:"lastError" yaml:"lastError" json:"lastError"`
ID string `graphql:"id" yaml:"-" json:"id"`
Name string `graphql:"name" yaml:"name" json:"name"`
QueryString string `graphql:"queryString" yaml:"queryString" json:"queryString"`
QueryStart string `graphql:"queryStart" yaml:"queryStart" json:"queryStart"`
ThrottleField string `graphql:"throttleField" yaml:"throttleField" json:"throttleField"`
TimeOfLastTrigger int `graphql:"timeOfLastTrigger" yaml:"timeOfLastTrigger" json:"timeOfLastTrigger"`
IsStarred bool `graphql:"isStarred" yaml:"isStarred" json:"isStarred"`
Description string `graphql:"description" yaml:"description,omitempty" json:"description"`
ThrottleTimeMillis int `graphql:"throttleTimeMillis" yaml:"throttleTimeMillis" json:"throttleTimeMillis"`
Enabled bool `graphql:"enabled" yaml:"enabled" json:"enabled"`
Actions []string `graphql:"actions" yaml:"actions" json:"actions"`
Labels []string `graphql:"labels" yaml:"labels,omitempty" json:"labels,omitempty"`
LastError string `graphql:"lastError" yaml:"lastError" json:"lastError"`
RunAsUserID string `graphql:"runAsUserId" yaml:"runAsUserId,omitempty" json:"runAsUserID,omitempty"`
QueryOwnershipType string `graphql:"queryOwnershipType" yaml:"queryOwnershipType,omitempty" json:"queryOwnershipType,omitempty"`
}

type Long int64
const (
QueryOwnershipTypeUser string = "User"
QueryOwnershipTypeOrganization string = "Organization"
)

type Alerts struct {
client *Client
Expand All @@ -32,7 +38,7 @@ func (c *Client) Alerts() *Alerts { return &Alerts{client: c} }
func (a *Alerts) List(viewName string) ([]Alert, error) {
var query struct {
SearchDomain struct {
Alerts []Alert `graphql:"alerts"`
Alerts []humiographql.Alert `graphql:"alerts"`
} `graphql:"searchDomain(name: $viewName)"`
}

Expand All @@ -41,7 +47,12 @@ func (a *Alerts) List(viewName string) ([]Alert, error) {
}

err := a.client.Query(&query, variables)
return query.SearchDomain.Alerts, err

var alerts []Alert
for _, humioGraphqlAlert := range query.SearchDomain.Alerts {
alerts = append(alerts, mapHumioGraphqlAlertToAlert(humioGraphqlAlert))
}
return alerts, err
}

func (a *Alerts) Update(viewName string, newAlert *Alert) (*Alert, error) {
Expand All @@ -54,56 +65,45 @@ func (a *Alerts) Update(viewName string, newAlert *Alert) (*Alert, error) {
}

var mutation struct {
Alert `graphql:"updateAlert(input: { id: $id, viewName: $viewName, name: $alertName, description: $description, queryString: $queryString, queryStart: $queryStart, throttleTimeMillis: $throttleTimeMillis, throttleField: $throttleField, enabled: $enabled, actions: $actions, labels: $labels })"`
humiographql.Alert `graphql:"updateAlert(input: $input)"`
}

actions := make([]graphql.String, len(newAlert.Actions))
labels := make([]graphql.String, len(newAlert.Labels))
var throttleField *graphql.String
for i, action := range newAlert.Actions {
actions[i] = graphql.String(action)
}

labels := make([]graphql.String, len(newAlert.Labels))
for i, label := range newAlert.Labels {
labels[i] = graphql.String(label)
}
if newAlert.ThrottleField != "" {
field := graphql.String(newAlert.ThrottleField)
throttleField = &field

updateAlert := humiographql.UpdateAlert{
ID: graphql.String(newAlert.ID),
ViewName: graphql.String(viewName),
Name: graphql.String(newAlert.Name),
Description: graphql.String(newAlert.Description),
QueryString: graphql.String(newAlert.QueryString),
QueryStart: graphql.String(newAlert.QueryStart),
ThrottleTimeMillis: humiographql.Long(newAlert.ThrottleTimeMillis),
Enabled: graphql.Boolean(newAlert.Enabled),
Actions: actions,
Labels: labels,
RunAsUserID: graphql.String(newAlert.RunAsUserID),
QueryOwnershipType: humiographql.QueryOwnershipType(newAlert.QueryOwnershipType),
ThrottleField: graphql.String(newAlert.ThrottleField),
}

variables := map[string]interface{}{
"id": graphql.String(newAlert.ID),
"viewName": graphql.String(viewName),
"alertName": graphql.String(newAlert.Name),
"description": graphql.String(newAlert.Description),
"queryString": graphql.String(newAlert.QueryString),
"queryStart": graphql.String(newAlert.QueryStart),
"throttleField": throttleField,
"throttleTimeMillis": Long(newAlert.ThrottleTimeMillis),
"enabled": graphql.Boolean(newAlert.Enabled),
"actions": actions,
"labels": labels,
"input": updateAlert,
}

err := a.client.Mutate(&mutation, variables)
if err != nil {
return nil, err
}

alert := Alert{
ID: mutation.Alert.ID,
Name: mutation.Alert.Name,
QueryString: mutation.Alert.QueryString,
QueryStart: mutation.Alert.QueryStart,
ThrottleField: mutation.Alert.ThrottleField,
TimeOfLastTrigger: mutation.Alert.TimeOfLastTrigger,
IsStarred: mutation.Alert.IsStarred,
Description: mutation.Alert.Description,
ThrottleTimeMillis: mutation.Alert.ThrottleTimeMillis,
Enabled: mutation.Alert.Enabled,
Actions: mutation.Alert.Actions,
Labels: mutation.Alert.Labels,
LastError: mutation.Alert.LastError,
}
alert := mapHumioGraphqlAlertToAlert(mutation.Alert)

return &alert, nil
}
Expand All @@ -112,58 +112,46 @@ func (a *Alerts) Add(viewName string, newAlert *Alert) (*Alert, error) {
if newAlert == nil {
return nil, fmt.Errorf("newAlert must not be nil")
}
var alert Alert

var mutation struct {
Alert `graphql:"createAlert(input: { viewName: $viewName, name: $alertName, description: $description, queryString: $queryString, queryStart: $queryStart, throttleTimeMillis: $throttleTimeMillis, throttleField: $throttleField, enabled: $enabled, actions: $actions, labels: $labels })"`
humiographql.Alert `graphql:"createAlert(input: $input)"`
}

actions := make([]graphql.String, len(newAlert.Actions))
labels := make([]graphql.String, len(newAlert.Labels))
var throttleField *graphql.String
for i, action := range newAlert.Actions {
actions[i] = graphql.String(action)
}

labels := make([]graphql.String, len(newAlert.Labels))
for i, label := range newAlert.Labels {
labels[i] = graphql.String(label)
}
if newAlert.ThrottleField != "" {
field := graphql.String(newAlert.ThrottleField)
throttleField = &field

createAlert := humiographql.CreateAlert{
ViewName: graphql.String(viewName),
Name: graphql.String(newAlert.Name),
Description: graphql.String(newAlert.Description),
QueryString: graphql.String(newAlert.QueryString),
QueryStart: graphql.String(newAlert.QueryStart),
ThrottleTimeMillis: humiographql.Long(newAlert.ThrottleTimeMillis),
Enabled: graphql.Boolean(newAlert.Enabled),
Actions: actions,
Labels: labels,
RunAsUserID: graphql.String(newAlert.RunAsUserID),
QueryOwnershipType: humiographql.QueryOwnershipType(newAlert.QueryOwnershipType),
ThrottleField: graphql.String(newAlert.ThrottleField),
}

variables := map[string]interface{}{
"viewName": graphql.String(viewName),
"alertName": graphql.String(newAlert.Name),
"description": graphql.String(newAlert.Description),
"queryString": graphql.String(newAlert.QueryString),
"queryStart": graphql.String(newAlert.QueryStart),
"throttleTimeMillis": Long(newAlert.ThrottleTimeMillis),
"throttleField": throttleField,
"enabled": graphql.Boolean(newAlert.Enabled),
"actions": actions,
"labels": labels,
"input": createAlert,
}

err := a.client.Mutate(&mutation, variables)
if err != nil {
return nil, err
}

alert = Alert{
ID: mutation.Alert.ID,
Name: mutation.Alert.Name,
QueryString: mutation.Alert.QueryString,
QueryStart: mutation.Alert.QueryStart,
ThrottleField: mutation.Alert.ThrottleField,
TimeOfLastTrigger: mutation.Alert.TimeOfLastTrigger,
IsStarred: mutation.Alert.IsStarred,
Description: mutation.Alert.Description,
ThrottleTimeMillis: mutation.Alert.ThrottleTimeMillis,
Enabled: mutation.Alert.Enabled,
Actions: mutation.Alert.Actions,
Labels: mutation.Alert.Labels,
LastError: mutation.Alert.LastError,
}
alert := mapHumioGraphqlAlertToAlert(mutation.Alert)
return &alert, nil
}

Expand All @@ -179,7 +167,6 @@ func (a *Alerts) Get(viewName, alertName string) (*Alert, error) {
}

return nil, AlertNotFound(alertName)

}

func (a *Alerts) Delete(viewName, alertName string) error {
Expand Down Expand Up @@ -209,3 +196,41 @@ func (a *Alerts) Delete(viewName, alertName string) error {

return a.client.Mutate(&mutation, variables)
}

func mapHumioGraphqlAlertToAlert(input humiographql.Alert) Alert {
var queryOwnershipType string
switch input.QueryOwnership.QueryOwnershipTypeName {
case humiographql.QueryOwnershipTypeNameOrganization:
queryOwnershipType = QueryOwnershipTypeOrganization
case humiographql.QueryOwnershipTypeNameUser:
queryOwnershipType = QueryOwnershipTypeUser
}

var actions []string
for _, action := range input.Actions {
actions = append(actions, string(action))
}

var labels []string
for _, label := range input.Labels {
labels = append(labels, string(label))
}

return Alert{
ID: string(input.ID),
Name: string(input.Name),
QueryString: string(input.QueryString),
QueryStart: string(input.QueryStart),
ThrottleField: string(input.ThrottleField),
TimeOfLastTrigger: int(input.TimeOfLastTrigger),
IsStarred: bool(input.IsStarred),
Description: string(input.Description),
ThrottleTimeMillis: int(input.ThrottleTimeMillis),
Enabled: bool(input.Enabled),
Actions: actions,
Labels: labels,
LastError: string(input.LastError),
RunAsUserID: string(input.RunAsUser.ID),
QueryOwnershipType: queryOwnershipType,
}
}
76 changes: 76 additions & 0 deletions api/internal/humiographql/alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package humiographql

import graphql "github.com/cli/shurcooL-graphql"

type Alert struct {
ID graphql.String `graphql:"id"`
Name graphql.String `graphql:"name"`
QueryString graphql.String `graphql:"queryString"`
QueryStart graphql.String `graphql:"queryStart"`
ThrottleField graphql.String `graphql:"throttleField"`
TimeOfLastTrigger Long `graphql:"timeOfLastTrigger"`
IsStarred graphql.Boolean `graphql:"isStarred"`
Description graphql.String `graphql:"description"`
ThrottleTimeMillis Long `graphql:"throttleTimeMillis"`
Enabled graphql.Boolean `graphql:"enabled"`
Actions []graphql.String `graphql:"actions"`
Labels []graphql.String `graphql:"labels"`
LastError graphql.String `graphql:"lastError"`
QueryOwnership struct {
ID graphql.String `graphql:"id"`
QueryOwnershipTypeName QueryOwnershipTypeName `graphql:"__typename"`
} `graphql:"queryOwnership"`
RunAsUser struct {
ID graphql.String `graphql:"id"`
} `graphql:"runAsUser"`
}

type QueryOwnership struct {
}

type CreateAlert struct {
ViewName graphql.String `json:"viewName"`
Name graphql.String `json:"name"`
Description graphql.String `json:"description,omitempty"`
QueryString graphql.String `json:"queryString"`
QueryStart graphql.String `json:"queryStart"`
ThrottleTimeMillis Long `json:"throttleTimeMillis"`
ThrottleField graphql.String `json:"throttleField,omitempty"`
RunAsUserID graphql.String `json:"runAsUserId,omitempty"`
Enabled graphql.Boolean `json:"enabled"`
Actions []graphql.String `json:"actions"`
Labels []graphql.String `json:"labels"`
QueryOwnershipType QueryOwnershipType `json:"queryOwnershipType,omitempty"`
}

type UpdateAlert struct {
ViewName graphql.String `json:"viewName"`
ID graphql.String `json:"id"`
Name graphql.String `json:"name"`
Description graphql.String `json:"description,omitempty"`
QueryString graphql.String `json:"queryString"`
QueryStart graphql.String `json:"queryStart"`
ThrottleTimeMillis Long `json:"throttleTimeMillis"`
ThrottleField graphql.String `json:"throttleField,omitempty"`
RunAsUserID graphql.String `json:"runAsUserId,omitempty"`
Enabled graphql.Boolean `json:"enabled"`
Actions []graphql.String `json:"actions"`
Labels []graphql.String `json:"labels"`
QueryOwnershipType QueryOwnershipType `json:"queryOwnershipType,omitempty"`
}

type Long int64

type QueryOwnershipTypeName string

const (
QueryOwnershipTypeNameOrganization QueryOwnershipTypeName = "OrganizationOwnership"
QueryOwnershipTypeNameUser QueryOwnershipTypeName = "UserOwnership"
)

type QueryOwnershipType string

const (
QueryOwnershipTypeUser QueryOwnershipType = "User"
QueryOwnershipTypeOrganization QueryOwnershipType = "Organization"
)
5 changes: 1 addition & 4 deletions cmd/humioctl/alerts_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ The install command allows you to install alerts from a URL or from a local file
$ humioctl alerts install viewName --name alertName --url=https://example.com/acme/alert.yaml
$ humioctl alerts install viewName --name alertName --file=./parser.yaml
$ humioctl alerts install viewName --name alertName --file=./alert.yaml
$ humioctl alerts install viewName --file=./alert.yaml
By default 'install' will not override existing alerts with the same name.
Use the --force flag to update existing alerts with conflicting names.
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/humioctl/alerts_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func newAlertsShowCmd() *cobra.Command {
{format.String("Last Error"), format.String(alert.LastError)},
{format.String("Throttle Field"), format.String(alert.ThrottleField)},
{format.String("Time Of Last Trigger"), format.Int(alert.TimeOfLastTrigger)},
{format.String("Run As User ID"), format.String(alert.RunAsUserID)},
{format.String("Query Ownership Type"), format.String(alert.QueryOwnershipType)},
}

printDetailsTable(cmd, details)
Expand Down

0 comments on commit 1d7631a

Please sign in to comment.