Skip to content

Commit 2b08097

Browse files
tobiodimuon
andauthored
Add alert rule management (#292)
* Generate Kibana Alerting API client * Add kibana encryption key to support alerts * Add Alerting client to the ApiClient * Add alerting rule resource * Add docs template * Update docs * Fix acceptance tests * Apply suggestions from code review Co-authored-by: Dmitry Onishchenko <[email protected]> * PR feedback * CHANGELOG.md * Fix acceptance tests --------- Co-authored-by: Dmitry Onishchenko <[email protected]>
1 parent c311bbe commit 2b08097

File tree

127 files changed

+31268
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+31268
-35
lines changed

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ jobs:
6262
discovery.type: single-node
6363
xpack.license.self_generated.type: trial
6464
xpack.security.enabled: true
65+
xpack.security.authc.api_key.enabled: true
6566
xpack.watcher.enabled: true
6667
repositories.url.allowed_urls: https://example.com/*
6768
path.repo: /tmp
@@ -76,6 +77,7 @@ jobs:
7677
ELASTICSEARCH_HOSTS: http://elasticsearch:9200
7778
ELASTICSEARCH_USERNAME: ${{ env.KIBANA_SYSTEM_USERNAME }}
7879
ELASTICSEARCH_PASSWORD: ${{ env.KIBANA_SYSTEM_PASSWORD }}
80+
XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY: a7a6311933d3503b89bc2dbc36572c33a6c10925682e591bffcab6911c06786d
7981
ports:
8082
- 5601:5601
8183

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Add `elasticstack_kibana_space` for managing Kibana spaces ([#272](https://github.com./elastic/terraform-provider-elasticstack/pull/272))
2222
- Add `elasticstack_elasticsearch_transform` for managing Elasticsearch transforms ([#284](https://github.com./elastic/terraform-provider-elasticstack/pull/284))
2323
- Add `elasticstack_elasticsearch_watch` for managing Elasticsearch Watches ([#155](https://github.com./elastic/terraform-provider-elasticstack/pull/155))
24+
- Add `elasticstack_kibana_alerting_rule` for managing Kibana alerting rules ([#292](https://github.com./elastic/terraform-provider-elasticstack/pull/292))
2425

2526
### Fixed
2627
- Respect `ignore_unavailable` and `include_global_state` values when configuring SLM policies ([#224](https://github.com./elastic/terraform-provider-elasticstack/pull/224))

Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ACCTEST_PARALLELISM ?= 10
1111
ACCTEST_TIMEOUT = 120m
1212
ACCTEST_COUNT = 1
1313
TEST ?= ./...
14+
SWAGGER_VERSION ?= 8.7
1415

1516
GOVERSION ?= 1.19
1617

@@ -86,6 +87,7 @@ docker-elasticsearch: docker-network ## Start Elasticsearch single node cluster
8687
-p 9200:9200 -p 9300:9300 \
8788
-e "discovery.type=single-node" \
8889
-e "xpack.security.enabled=true" \
90+
-e "xpack.security.authc.api_key.enabled=true" \
8991
-e "xpack.watcher.enabled=true" \
9092
-e "xpack.license.self_generated.type=trial" \
9193
-e "repositories.url.allowed_urls=https://example.com/*" \
@@ -105,6 +107,7 @@ docker-kibana: docker-network docker-elasticsearch set-kibana-password ## Start
105107
-e ELASTICSEARCH_HOSTS=$(ELASTICSEARCH_ENDPOINTS) \
106108
-e ELASTICSEARCH_USERNAME=$(KIBANA_SYSTEM_USERNAME) \
107109
-e ELASTICSEARCH_PASSWORD=$(KIBANA_SYSTEM_PASSWORD) \
110+
-e XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY=a7a6311933d3503b89bc2dbc36572c33a6c10925682e591bffcab6911c06786d \
108111
-e "logging.root.level=debug" \
109112
--name $(KIBANA_NAME) \
110113
--network $(ELASTICSEARCH_NETWORK) \
@@ -229,3 +232,18 @@ release-notes: ## greps UNRELEASED notes from the CHANGELOG
229232
.PHONY: help
230233
help: ## this help
231234
@ awk 'BEGIN {FS = ":.*##"; printf "Usage: make \033[36m<target>\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-10s\033[0m\t%s\n", $$1, $$2 }' $(MAKEFILE_LIST) | column -s$$'\t' -t
235+
236+
.PHONY: generate-alerting-client
237+
generate-alerting-client: ## generate Kibana alerting client
238+
@ docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
239+
-i https://raw.githubusercontent.com/elastic/kibana/$(SWAGGER_VERSION)/x-pack/plugins/alerting/docs/openapi/bundled.json \
240+
--skip-validate-spec \
241+
--git-repo-id terraform-provider-elasticstack \
242+
--git-user-id elastic \
243+
-p isGoSubmodule=true \
244+
-p packageName=alerting \
245+
-p generateInterfaces=true \
246+
-g go \
247+
-o /local/generated/alerting
248+
@ rm -rf generated/alerting/go.mod generated/alerting/go.sum generated/alerting/test
249+
@ go fmt ./generated/...
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
subcategory: "Kibana"
3+
layout: ""
4+
page_title: "Elasticstack: elasticstack_kibana_alerting_rule Resource"
5+
description: |-
6+
Creates or updates a Kibana alerting rule.
7+
---
8+
9+
# Resource: elasticstack_kibana_alerting_rule
10+
11+
Creates or updates a Kibana alerting rule. See https://www.elastic.co/guide/en/kibana/8.6/create-and-manage-rules.html
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider "elasticstack" {
17+
elasticsearch {}
18+
}
19+
20+
resource "elasticstack_kibana_alerting_rule" "example" {
21+
name = "%s"
22+
consumer = "alerts"
23+
notify_when = "onActiveAlert"
24+
params = jsonencode({
25+
aggType = "avg"
26+
groupBy = "top"
27+
termSize = 10
28+
timeWindowSize = 10
29+
timeWindowUnit = "s"
30+
threshold = [10]
31+
thresholdComparator = ">"
32+
index = ["test-index"]
33+
timeField = "@timestamp"
34+
aggField = "version"
35+
termField = "name"
36+
})
37+
rule_type_id = ".index-threshold"
38+
interval = "1m"
39+
enabled = true
40+
}
41+
```
42+
43+
<!-- schema generated by tfplugindocs -->
44+
## Schema
45+
46+
### Required
47+
48+
- `consumer` (String) The name of the application or feature that owns the rule.
49+
- `interval` (String) The check interval, which specifies how frequently the rule conditions are checked. The interval must be specified in seconds, minutes, hours or days.
50+
- `name` (String) The name of the rule. While this name does not have to be unique, a distinctive name can help you identify a rule.
51+
- `notify_when` (String) Defines how often alerts generate actions. One of `onActionGroupChange`, `onActiveAlert`, or `onThrottleInterval`
52+
- `params` (String) The parameters to pass to the rule type executor params value. This will also validate against the rule type params validator, if defined.
53+
- `rule_type_id` (String) The ID of the rule type that you want to call when the rule is scheduled to run. For more information, refer to Rule types documentation (https://www.elastic.co/guide/en/kibana/master/rule-types.html).
54+
55+
### Optional
56+
57+
- `actions` (Block List) An array of action objects (see [below for nested schema](#nestedblock--actions))
58+
- `enabled` (Boolean) Indicates if you want to run the rule on an interval basis
59+
- `rule_id` (String) A UUID v1 or v4 to use instead of a randomly generated ID.
60+
- `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used.
61+
- `tags` (List of String) A list of tag names that are applied to the rule
62+
- `throttle` (String) Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is used only if notify_when is onThrottleInterval.
63+
64+
### Read-Only
65+
66+
- `id` (String) The ID of this resource.
67+
- `last_execution_date` (String) Date of the last execution of this rule.
68+
- `last_execution_status` (String) Status of the last execution of this rule.
69+
- `scheduled_task_id` (String) ID of the scheduled task that will execute the alert.
70+
71+
<a id="nestedblock--actions"></a>
72+
### Nested Schema for `actions`
73+
74+
Required:
75+
76+
- `id` (String) The ID of the connector saved object.
77+
- `params` (String) The map to the `params` that the connector type will receive.
78+
79+
Optional:
80+
81+
- `group` (String) Grouping actions is recommended for escalations for different types of alerts.
82+
83+
## Import
84+
85+
Import is supported using the following syntax:
86+
87+
```shell
88+
terraform import elasticstack_kibana_alerting_rule.my_rule <space id>/<rule id>
89+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform import elasticstack_kibana_alerting_rule.my_rule <space id>/<rule id>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
provider "elasticstack" {
2+
elasticsearch {}
3+
}
4+
5+
resource "elasticstack_kibana_alerting_rule" "example" {
6+
name = "%s"
7+
consumer = "alerts"
8+
notify_when = "onActiveAlert"
9+
params = jsonencode({
10+
aggType = "avg"
11+
groupBy = "top"
12+
termSize = 10
13+
timeWindowSize = 10
14+
timeWindowUnit = "s"
15+
threshold = [10]
16+
thresholdComparator = ">"
17+
index = ["test-index"]
18+
timeField = "@timestamp"
19+
aggField = "version"
20+
termField = "name"
21+
})
22+
rule_type_id = ".index-threshold"
23+
interval = "1m"
24+
enabled = true
25+
}

generated/alerting/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
10+
# Architecture specific extensions/prefixes
11+
*.[568vq]
12+
[568vq].out
13+
14+
*.cgo1.go
15+
*.cgo2.c
16+
_cgo_defun.c
17+
_cgo_gotypes.go
18+
_cgo_export.*
19+
20+
_testmain.go
21+
22+
*.exe
23+
*.test
24+
*.prof
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com./openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
.gitignore
2+
.travis.yml
3+
README.md
4+
api/openapi.yaml
5+
api_alerting.go
6+
client.go
7+
configuration.go
8+
docs/ActionsInner.md
9+
docs/ActionsInnerFrequency.md
10+
docs/AlertResponseProperties.md
11+
docs/AlertResponsePropertiesExecutionStatus.md
12+
docs/AlertResponsePropertiesSchedule.md
13+
docs/AlertingApi.md
14+
docs/CreateRuleRequest.md
15+
docs/FindRules200Response.md
16+
docs/FindRulesHasReferenceParameter.md
17+
docs/FindRulesSearchFieldsParameter.md
18+
docs/GetAlertingHealth200Response.md
19+
docs/GetAlertingHealth200ResponseAlertingFrameworkHealth.md
20+
docs/GetAlertingHealth200ResponseAlertingFrameworkHealthDecryptionHealth.md
21+
docs/GetAlertingHealth200ResponseAlertingFrameworkHealthExecutionHealth.md
22+
docs/GetAlertingHealth200ResponseAlertingFrameworkHealthReadHealth.md
23+
docs/GetAlertingHealth200ResponseAlertingFrameworkHeath.md
24+
docs/GetAlertingHealth200ResponseAlertingFrameworkHeathDecryptionHealth.md
25+
docs/GetRuleTypes200ResponseInner.md
26+
docs/GetRuleTypes200ResponseInnerActionGroupsInner.md
27+
docs/GetRuleTypes200ResponseInnerActionVariables.md
28+
docs/GetRuleTypes200ResponseInnerActionVariablesContextInner.md
29+
docs/GetRuleTypes200ResponseInnerActionVariablesParamsInner.md
30+
docs/GetRuleTypes200ResponseInnerAuthorizedConsumers.md
31+
docs/GetRuleTypes200ResponseInnerAuthorizedConsumersAlerts.md
32+
docs/GetRuleTypes200ResponseInnerRecoveryActionGroup.md
33+
docs/LegacyCreateAlertRequestProperties.md
34+
docs/LegacyCreateAlertRequestPropertiesSchedule.md
35+
docs/LegacyFindAlerts200Response.md
36+
docs/LegacyGetAlertTypes200ResponseInner.md
37+
docs/LegacyGetAlertTypes200ResponseInnerActionVariables.md
38+
docs/LegacyGetAlertTypes200ResponseInnerActionVariablesContextInner.md
39+
docs/LegacyGetAlertTypes200ResponseInnerRecoveryActionGroup.md
40+
docs/LegacyGetAlertingHealth200Response.md
41+
docs/LegacyGetAlertingHealth200ResponseAlertingFrameworkHealth.md
42+
docs/LegacyGetAlertingHealth200ResponseAlertingFrameworkHealthDecryptionHealth.md
43+
docs/LegacyGetAlertingHealth200ResponseAlertingFrameworkHealthExecutionHealth.md
44+
docs/LegacyGetAlertingHealth200ResponseAlertingFrameworkHealthReadHealth.md
45+
docs/LegacyUpdateAlertRequestProperties.md
46+
docs/LegacyUpdateAlertRequestPropertiesActionsInner.md
47+
docs/LegacyUpdateAlertRequestPropertiesSchedule.md
48+
docs/Model401Response.md
49+
docs/Model404Response.md
50+
docs/NotifyWhen.md
51+
docs/RuleResponseProperties.md
52+
docs/RuleResponsePropertiesExecutionStatus.md
53+
docs/RuleResponsePropertiesLastRun.md
54+
docs/RuleResponsePropertiesLastRunAlertsCount.md
55+
docs/Schedule.md
56+
docs/UpdateRuleRequest.md
57+
git_push.sh
58+
go.mod
59+
go.sum
60+
model_401_response.go
61+
model_404_response.go
62+
model_actions_inner.go
63+
model_actions_inner_frequency.go
64+
model_alert_response_properties.go
65+
model_alert_response_properties_execution_status.go
66+
model_alert_response_properties_schedule.go
67+
model_create_rule_request.go
68+
model_find_rules_200_response.go
69+
model_find_rules_has_reference_parameter.go
70+
model_find_rules_search_fields_parameter.go
71+
model_get_alerting_health_200_response.go
72+
model_get_alerting_health_200_response_alerting_framework_health.go
73+
model_get_alerting_health_200_response_alerting_framework_health_decryption_health.go
74+
model_get_alerting_health_200_response_alerting_framework_health_execution_health.go
75+
model_get_alerting_health_200_response_alerting_framework_health_read_health.go
76+
model_get_alerting_health_200_response_alerting_framework_heath.go
77+
model_get_alerting_health_200_response_alerting_framework_heath_decryption_health.go
78+
model_get_rule_types_200_response_inner.go
79+
model_get_rule_types_200_response_inner_action_groups_inner.go
80+
model_get_rule_types_200_response_inner_action_variables.go
81+
model_get_rule_types_200_response_inner_action_variables_context_inner.go
82+
model_get_rule_types_200_response_inner_action_variables_params_inner.go
83+
model_get_rule_types_200_response_inner_authorized_consumers.go
84+
model_get_rule_types_200_response_inner_authorized_consumers_alerts.go
85+
model_get_rule_types_200_response_inner_recovery_action_group.go
86+
model_legacy_create_alert_request_properties.go
87+
model_legacy_create_alert_request_properties_schedule.go
88+
model_legacy_find_alerts_200_response.go
89+
model_legacy_get_alert_types_200_response_inner.go
90+
model_legacy_get_alert_types_200_response_inner_action_variables.go
91+
model_legacy_get_alert_types_200_response_inner_action_variables_context_inner.go
92+
model_legacy_get_alert_types_200_response_inner_recovery_action_group.go
93+
model_legacy_get_alerting_health_200_response.go
94+
model_legacy_get_alerting_health_200_response_alerting_framework_health.go
95+
model_legacy_get_alerting_health_200_response_alerting_framework_health_decryption_health.go
96+
model_legacy_get_alerting_health_200_response_alerting_framework_health_execution_health.go
97+
model_legacy_get_alerting_health_200_response_alerting_framework_health_read_health.go
98+
model_legacy_update_alert_request_properties.go
99+
model_legacy_update_alert_request_properties_actions_inner.go
100+
model_legacy_update_alert_request_properties_schedule.go
101+
model_notify_when.go
102+
model_rule_response_properties.go
103+
model_rule_response_properties_execution_status.go
104+
model_rule_response_properties_last_run.go
105+
model_rule_response_properties_last_run_alerts_count.go
106+
model_schedule.go
107+
model_update_rule_request.go
108+
response.go
109+
test/api_alerting_test.go
110+
utils.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.5.0-SNAPSHOT

generated/alerting/.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: go
2+
3+
install:
4+
- go get -d -v .
5+
6+
script:
7+
- go build -v ./
8+

0 commit comments

Comments
 (0)