|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + graphql "github.com/cli/shurcooL-graphql" |
| 6 | + "github.com/humio/cli/api/internal/humiographql" |
| 7 | +) |
| 8 | + |
| 9 | +type AggregateAlert struct { |
| 10 | + ID string `graphql:"id" yaml:"-" json:"id"` |
| 11 | + Name string `graphql:"name" yaml:"name" json:"name"` |
| 12 | + Description string `graphql:"description" yaml:"description,omitempty" json:"description,omitempty"` |
| 13 | + QueryString string `graphql:"queryString" yaml:"queryString" json:"queryString"` |
| 14 | + SearchIntervalSeconds int `graphql:"searchIntervalSeconds" yaml:"searchIntervalSeconds" json:"searchIntervalSeconds"` |
| 15 | + ActionNames []string `graphql:"actionNames" yaml:"actionNames" json:"actionNames"` |
| 16 | + Labels []string `graphql:"labels" yaml:"labels" json:"labels"` |
| 17 | + Enabled bool `graphql:"enabled" yaml:"enabled" json:"enabled"` |
| 18 | + ThrottleField string `graphql:"throttleField" yaml:"throttleField,omitempty" json:"throttleField,omitempty"` |
| 19 | + ThrottleTimeSeconds int `graphql:"throttleTimeSeconds" yaml:"throttleTimeSeconds" json:"throttleTimeSeconds"` |
| 20 | + QueryOwnershipType string `graphql:"queryOwnership" yaml:"queryOwnershipType" json:"queryOwnershipType"` |
| 21 | + TriggerMode string `graphql:"triggerMode" yaml:"triggerMode" json:"triggerMode"` |
| 22 | + QueryTimestampType string `graphql:"queryTimestampType" yaml:"queryTimestampType" json:"queryTimestampType"` |
| 23 | + RunAsUserID string `graphql:"runAsUserId" yaml:"runAsUserId,omitempty" json:"runAsUserId,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +type AggregateAlerts struct { |
| 27 | + client *Client |
| 28 | +} |
| 29 | + |
| 30 | +func (c *Client) AggregateAlerts() *AggregateAlerts { return &AggregateAlerts{client: c} } |
| 31 | + |
| 32 | +func (a *AggregateAlerts) List(viewName string) ([]AggregateAlert, error) { |
| 33 | + var query struct { |
| 34 | + SearchDomain struct { |
| 35 | + AggregateAlerts []humiographql.AggregateAlert `graphql:"aggregateAlerts"` |
| 36 | + } `graphql:"searchDomain(name: $viewName)"` |
| 37 | + } |
| 38 | + |
| 39 | + variables := map[string]any{ |
| 40 | + "viewName": graphql.String(viewName), |
| 41 | + } |
| 42 | + |
| 43 | + err := a.client.Query(&query, variables) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + var aggregateAlerts = make([]AggregateAlert, len(query.SearchDomain.AggregateAlerts)) |
| 49 | + for i := range query.SearchDomain.AggregateAlerts { |
| 50 | + aggregateAlerts[i] = mapHumioGraphqlAggregateAlertToAggregateAlert(query.SearchDomain.AggregateAlerts[i]) |
| 51 | + } |
| 52 | + |
| 53 | + return aggregateAlerts, err |
| 54 | +} |
| 55 | + |
| 56 | +func (a *AggregateAlerts) Update(viewName string, updatedAggregateAlert *AggregateAlert) (*AggregateAlert, error) { |
| 57 | + if updatedAggregateAlert == nil { |
| 58 | + return nil, fmt.Errorf("updatedAggregateAlert must not be nil") |
| 59 | + } |
| 60 | + |
| 61 | + if updatedAggregateAlert.ID == "" { |
| 62 | + return nil, fmt.Errorf("updatedAggregateAlert must have non-empty ID") |
| 63 | + } |
| 64 | + |
| 65 | + var mutation struct { |
| 66 | + humiographql.AggregateAlert `graphql:"updateAggregateAlert(input: $input)"` |
| 67 | + } |
| 68 | + |
| 69 | + actionNames := make([]graphql.String, len(updatedAggregateAlert.ActionNames)) |
| 70 | + for i, actionName := range updatedAggregateAlert.ActionNames { |
| 71 | + actionNames[i] = graphql.String(actionName) |
| 72 | + } |
| 73 | + |
| 74 | + labels := make([]graphql.String, len(updatedAggregateAlert.Labels)) |
| 75 | + for i, label := range updatedAggregateAlert.Labels { |
| 76 | + labels[i] = graphql.String(label) |
| 77 | + } |
| 78 | + |
| 79 | + updateAlert := humiographql.UpdateAggregateAlert{ |
| 80 | + ViewName: humiographql.RepoOrViewName(viewName), |
| 81 | + ID: graphql.String(updatedAggregateAlert.ID), |
| 82 | + Name: graphql.String(updatedAggregateAlert.Name), |
| 83 | + Description: graphql.String(updatedAggregateAlert.Description), |
| 84 | + QueryString: graphql.String(updatedAggregateAlert.QueryString), |
| 85 | + SearchIntervalSeconds: humiographql.Long(updatedAggregateAlert.SearchIntervalSeconds), |
| 86 | + ActionIdsOrNames: actionNames, |
| 87 | + Labels: labels, |
| 88 | + Enabled: graphql.Boolean(updatedAggregateAlert.Enabled), |
| 89 | + RunAsUserID: graphql.String(updatedAggregateAlert.RunAsUserID), |
| 90 | + ThrottleField: graphql.String(updatedAggregateAlert.ThrottleField), |
| 91 | + ThrottleTimeSeconds: humiographql.Long(updatedAggregateAlert.ThrottleTimeSeconds), |
| 92 | + TriggerMode: humiographql.TriggerMode(updatedAggregateAlert.TriggerMode), |
| 93 | + QueryTimestampType: humiographql.QueryTimestampType(updatedAggregateAlert.QueryTimestampType), |
| 94 | + QueryOwnershipType: humiographql.QueryOwnershipType(updatedAggregateAlert.QueryOwnershipType), |
| 95 | + } |
| 96 | + |
| 97 | + variables := map[string]any{ |
| 98 | + "input": updateAlert, |
| 99 | + } |
| 100 | + |
| 101 | + err := a.client.Mutate(&mutation, variables) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(mutation.AggregateAlert) |
| 107 | + |
| 108 | + return &aggregateAlert, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (a *AggregateAlerts) Create(viewName string, newAggregateAlert *AggregateAlert) (*AggregateAlert, error) { |
| 112 | + if newAggregateAlert == nil { |
| 113 | + return nil, fmt.Errorf("newAggregateAlert must not be nil") |
| 114 | + } |
| 115 | + |
| 116 | + var mutation struct { |
| 117 | + humiographql.AggregateAlert `graphql:"createAggregateAlert(input: $input)"` |
| 118 | + } |
| 119 | + |
| 120 | + actionNames := make([]graphql.String, len(newAggregateAlert.ActionNames)) |
| 121 | + for i, actionName := range newAggregateAlert.ActionNames { |
| 122 | + actionNames[i] = graphql.String(actionName) |
| 123 | + } |
| 124 | + |
| 125 | + labels := make([]graphql.String, len(newAggregateAlert.Labels)) |
| 126 | + for i, label := range newAggregateAlert.Labels { |
| 127 | + labels[i] = graphql.String(label) |
| 128 | + } |
| 129 | + |
| 130 | + createAggregateAlert := humiographql.CreateAggregateAlert{ |
| 131 | + ViewName: humiographql.RepoOrViewName(viewName), |
| 132 | + Name: graphql.String(newAggregateAlert.Name), |
| 133 | + Description: graphql.String(newAggregateAlert.Description), |
| 134 | + QueryString: graphql.String(newAggregateAlert.QueryString), |
| 135 | + SearchIntervalSeconds: humiographql.Long(newAggregateAlert.SearchIntervalSeconds), |
| 136 | + ActionIdsOrNames: actionNames, |
| 137 | + Labels: labels, |
| 138 | + Enabled: graphql.Boolean(newAggregateAlert.Enabled), |
| 139 | + ThrottleField: graphql.String(newAggregateAlert.ThrottleField), |
| 140 | + ThrottleTimeSeconds: humiographql.Long(newAggregateAlert.ThrottleTimeSeconds), |
| 141 | + RunAsUserID: graphql.String(newAggregateAlert.RunAsUserID), |
| 142 | + TriggerMode: humiographql.TriggerMode(newAggregateAlert.TriggerMode), |
| 143 | + QueryTimestampType: humiographql.QueryTimestampType(newAggregateAlert.QueryTimestampType), |
| 144 | + QueryOwnershipType: humiographql.QueryOwnershipType(newAggregateAlert.QueryOwnershipType), |
| 145 | + } |
| 146 | + |
| 147 | + variables := map[string]any{ |
| 148 | + "input": createAggregateAlert, |
| 149 | + } |
| 150 | + |
| 151 | + err := a.client.Mutate(&mutation, variables) |
| 152 | + if err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + |
| 156 | + aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(mutation.AggregateAlert) |
| 157 | + |
| 158 | + return &aggregateAlert, nil |
| 159 | +} |
| 160 | + |
| 161 | +func (a *AggregateAlerts) Delete(viewName, aggregateAlertID string) error { |
| 162 | + if aggregateAlertID == "" { |
| 163 | + return fmt.Errorf("aggregateAlertID is empty") |
| 164 | + } |
| 165 | + |
| 166 | + var mutation struct { |
| 167 | + DidDelete bool `graphql:"deleteAggregateAlert(input: { viewName: $viewName, id: $id })"` |
| 168 | + } |
| 169 | + |
| 170 | + variables := map[string]any{ |
| 171 | + "viewName": humiographql.RepoOrViewName(viewName), |
| 172 | + "id": graphql.String(aggregateAlertID), |
| 173 | + } |
| 174 | + |
| 175 | + err := a.client.Mutate(&mutation, variables) |
| 176 | + |
| 177 | + if !mutation.DidDelete { |
| 178 | + return fmt.Errorf("unable to remove aggregate alert in repo/view '%s' with id '%s'", viewName, aggregateAlertID) |
| 179 | + } |
| 180 | + |
| 181 | + return err |
| 182 | +} |
| 183 | + |
| 184 | +func (a *AggregateAlerts) Get(viewName string, aggregateAlertID string) (*AggregateAlert, error) { |
| 185 | + var query struct { |
| 186 | + SearchDomain struct { |
| 187 | + AggregateAlert humiographql.AggregateAlert `graphql:"aggregateAlert(id: $aggregateAlertId)"` |
| 188 | + } `graphql:"searchDomain(name: $viewName) "` |
| 189 | + } |
| 190 | + |
| 191 | + variables := map[string]any{ |
| 192 | + "viewName": graphql.String(viewName), |
| 193 | + "aggregateAlertId": graphql.String(aggregateAlertID), |
| 194 | + } |
| 195 | + |
| 196 | + err := a.client.Query(&query, variables) |
| 197 | + if err != nil { |
| 198 | + return nil, err |
| 199 | + } |
| 200 | + |
| 201 | + aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(query.SearchDomain.AggregateAlert) |
| 202 | + |
| 203 | + return &aggregateAlert, nil |
| 204 | +} |
| 205 | + |
| 206 | +func mapHumioGraphqlAggregateAlertToAggregateAlert(input humiographql.AggregateAlert) AggregateAlert { |
| 207 | + var queryOwnershipType, runAsUserID string |
| 208 | + switch input.QueryOwnership.QueryOwnershipTypeName { |
| 209 | + case humiographql.QueryOwnershipTypeNameOrganization: |
| 210 | + queryOwnershipType = QueryOwnershipTypeOrganization |
| 211 | + case humiographql.QueryOwnershipTypeNameUser: |
| 212 | + queryOwnershipType = QueryOwnershipTypeUser |
| 213 | + runAsUserID = string(input.QueryOwnership.ID) |
| 214 | + } |
| 215 | + |
| 216 | + var actionNames = make([]string, len(input.Actions)) |
| 217 | + for i := range input.Actions { |
| 218 | + actionNames[i] = string(input.Actions[i].Name) |
| 219 | + } |
| 220 | + |
| 221 | + var labels = make([]string, len(input.Labels)) |
| 222 | + for i := range input.Labels { |
| 223 | + labels[i] = string(input.Labels[i]) |
| 224 | + } |
| 225 | + |
| 226 | + return AggregateAlert{ |
| 227 | + ID: string(input.ID), |
| 228 | + Name: string(input.Name), |
| 229 | + Description: string(input.Description), |
| 230 | + QueryString: string(input.QueryString), |
| 231 | + SearchIntervalSeconds: int(input.SearchIntervalSeconds), |
| 232 | + ActionNames: actionNames, |
| 233 | + Labels: labels, |
| 234 | + Enabled: bool(input.Enabled), |
| 235 | + ThrottleField: string(input.ThrottleField), |
| 236 | + ThrottleTimeSeconds: int(input.ThrottleTimeSeconds), |
| 237 | + QueryOwnershipType: queryOwnershipType, |
| 238 | + TriggerMode: string(input.TriggerMode), |
| 239 | + QueryTimestampType: string(input.QueryTimestampType), |
| 240 | + RunAsUserID: runAsUserID, |
| 241 | + } |
| 242 | +} |
0 commit comments