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

[ISSUE-154] Add Watches #155

Merged
merged 154 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 139 commits
Commits
Show all changes
154 commits
Select commit Hold shift + click to select a range
bfc26ee
add watcher - need to test
RobsonSutton Oct 15, 2022
e8150bd
rebasing
RobsonSutton Dec 6, 2022
d8f2a01
rebasing
RobsonSutton Dec 6, 2022
1c07e79
generate docs
RobsonSutton Dec 6, 2022
ceca05a
fmt
RobsonSutton Dec 6, 2022
73f3660
regenerate docs
RobsonSutton Dec 6, 2022
4ad9784
update errors
RobsonSutton Dec 22, 2022
a9ba3e0
docs-generate
RobsonSutton Dec 22, 2022
91cf1d7
update to work with new package structure
RobsonSutton Dec 22, 2022
89bd9b9
test enabling xpack watcher
RobsonSutton Dec 22, 2022
7f445aa
missed license
RobsonSutton Dec 22, 2022
41c1076
test updating xpack license value
RobsonSutton Dec 22, 2022
2a32701
rebase
RobsonSutton Feb 3, 2023
713e7e1
testing
RobsonSutton Mar 1, 2023
ac1c6f2
tidy files
RobsonSutton Mar 1, 2023
1bd809e
missed full stop
RobsonSutton Mar 2, 2023
8eb28cf
revert container memory settings
RobsonSutton Mar 2, 2023
60f66d8
update PUT approach
RobsonSutton Mar 2, 2023
b0d35bb
marshal entire object
RobsonSutton Mar 2, 2023
bd744a2
testing
RobsonSutton Mar 2, 2023
2ced4e8
testing
RobsonSutton Mar 2, 2023
cef2fd3
testing
RobsonSutton Mar 2, 2023
ec2c0dc
testing
RobsonSutton Mar 2, 2023
948aff5
teting
RobsonSutton Mar 2, 2023
60ba98e
testing
RobsonSutton Mar 2, 2023
d7da805
testing
RobsonSutton Mar 2, 2023
faeacf1
testing
RobsonSutton Mar 2, 2023
4e1b23f
testing
RobsonSutton Mar 2, 2023
a6631d9
testing
RobsonSutton Mar 2, 2023
241c5b6
testing
RobsonSutton Mar 2, 2023
2085c1f
testing
RobsonSutton Mar 2, 2023
fa991b8
remove comment
RobsonSutton Mar 2, 2023
e7ab2ee
unflatten active
RobsonSutton Mar 2, 2023
e303b44
testing
RobsonSutton Mar 2, 2023
33b3603
nest structs
RobsonSutton Mar 2, 2023
7848801
testing
RobsonSutton Mar 3, 2023
ebf101a
testing
RobsonSutton Mar 3, 2023
867eb90
testing
RobsonSutton Mar 3, 2023
ac6e973
testing
RobsonSutton Mar 3, 2023
b0279c2
testing
RobsonSutton Mar 3, 2023
5090822
testing
RobsonSutton Mar 3, 2023
3a510c1
testing
RobsonSutton Mar 3, 2023
ab5f1b0
testing
RobsonSutton Mar 3, 2023
7b18d2f
silly mistake while testing
RobsonSutton Mar 3, 2023
f3c6c6a
Correctly handle the GET watch API response
tobio Mar 5, 2023
e4759ec
testing
RobsonSutton Mar 3, 2023
5657768
revert local to match remote
RobsonSutton Mar 6, 2023
5823e5d
testing defining empty defaults for nested json
RobsonSutton Mar 6, 2023
c92b9ad
testing
RobsonSutton Mar 6, 2023
584515d
testing previous approach
RobsonSutton Mar 6, 2023
ad53bf8
testing
RobsonSutton Mar 6, 2023
6dab80d
testing
RobsonSutton Mar 6, 2023
fd70883
testing
RobsonSutton Mar 6, 2023
a72d02c
testing
RobsonSutton Mar 6, 2023
c06059d
testing
RobsonSutton Mar 6, 2023
8ef4617
testing
RobsonSutton Mar 7, 2023
9ea259e
testing
RobsonSutton Mar 8, 2023
e4092c0
testing
RobsonSutton Mar 8, 2023
8e0956f
testing
RobsonSutton Mar 8, 2023
c316cda
testing
RobsonSutton Mar 8, 2023
b635b83
revert
RobsonSutton Mar 8, 2023
4b70d63
testing
RobsonSutton Mar 8, 2023
a76baf5
testing
RobsonSutton Mar 8, 2023
a58134d
testing
RobsonSutton Mar 8, 2023
47be70f
testing
RobsonSutton Mar 8, 2023
48a280b
testing
RobsonSutton Mar 8, 2023
89482ef
testing
RobsonSutton Mar 8, 2023
0196f79
testing defaults
RobsonSutton Mar 8, 2023
cd00dbe
testing
RobsonSutton Mar 8, 2023
fb54383
testing
RobsonSutton Mar 8, 2023
e0708f6
revert testing
RobsonSutton Mar 8, 2023
51bef53
testing
RobsonSutton Mar 8, 2023
d8c502e
testing
RobsonSutton Mar 9, 2023
5a51fbc
testing
RobsonSutton Mar 9, 2023
25a3a85
update types
RobsonSutton Mar 9, 2023
feefbdd
testing
RobsonSutton Mar 13, 2023
df7caa5
testing
RobsonSutton Mar 13, 2023
0cdebd3
testing
RobsonSutton Mar 13, 2023
7b0ac27
testing
RobsonSutton Mar 13, 2023
bec3449
testing
RobsonSutton Mar 13, 2023
8f4bc92
testing
RobsonSutton Mar 13, 2023
5e45140
testing
RobsonSutton Mar 13, 2023
6b19e9d
testing
RobsonSutton Mar 13, 2023
120aed6
testing
RobsonSutton Mar 13, 2023
a8ae03f
testing
RobsonSutton Mar 13, 2023
a29d891
testing
RobsonSutton Mar 13, 2023
e1fd04b
testing
RobsonSutton Mar 13, 2023
76f4db6
testing
RobsonSutton Mar 13, 2023
c6087e1
testing
RobsonSutton Mar 13, 2023
04f711c
testing
RobsonSutton Mar 13, 2023
c04098e
testing
RobsonSutton Mar 13, 2023
543eb89
testing
RobsonSutton Mar 13, 2023
7a722b9
testing
RobsonSutton Mar 13, 2023
ed4f369
testing
RobsonSutton Mar 13, 2023
7721c4e
testing
RobsonSutton Mar 13, 2023
8eccf1e
testing
RobsonSutton Mar 13, 2023
07f5500
testing
RobsonSutton Mar 13, 2023
3743937
testing
RobsonSutton Mar 13, 2023
04887fe
testing
RobsonSutton Mar 13, 2023
5dba763
testing
RobsonSutton Mar 13, 2023
6ba0da1
testing
RobsonSutton Mar 13, 2023
5254fd8
testing
RobsonSutton Mar 13, 2023
9ac8513
testing
RobsonSutton Mar 13, 2023
ea06802
testing
RobsonSutton Mar 13, 2023
979a728
testing
RobsonSutton Mar 13, 2023
426cb27
testing
RobsonSutton Mar 13, 2023
c20dff4
testing
RobsonSutton Mar 13, 2023
6a9e5e3
testing
RobsonSutton Mar 13, 2023
ef85cd2
testing
RobsonSutton Mar 13, 2023
a308fc7
testing
RobsonSutton Mar 13, 2023
775fdd0
testing
RobsonSutton Mar 13, 2023
3e499b0
testing
RobsonSutton Mar 13, 2023
7b84eaf
testing
RobsonSutton Mar 13, 2023
c5851dd
testing
RobsonSutton Mar 13, 2023
6cfe418
testing
RobsonSutton Mar 13, 2023
39a8f85
testing
RobsonSutton Mar 13, 2023
f9030ea
testing
RobsonSutton Mar 13, 2023
c588fc5
testing
RobsonSutton Mar 13, 2023
4f8aab7
testing
RobsonSutton Mar 13, 2023
8848d03
testing
RobsonSutton Mar 13, 2023
da24fc3
testing
RobsonSutton Mar 13, 2023
dcc32a5
testing
RobsonSutton Mar 13, 2023
225b78a
testing
RobsonSutton Mar 13, 2023
3a9fd44
tidy up
RobsonSutton Mar 13, 2023
0ec2c46
update tests
RobsonSutton Mar 13, 2023
b5c146c
reorder struct
RobsonSutton Mar 13, 2023
b04555d
blasted space...
RobsonSutton Mar 13, 2023
d7ab40d
update check
RobsonSutton Mar 13, 2023
f9596b2
try again
RobsonSutton Mar 13, 2023
0354071
testing
RobsonSutton Mar 13, 2023
c989089
testing
RobsonSutton Mar 13, 2023
142c0fc
Merge branch 'elastic:main' into issue-154
RobsonSutton Mar 13, 2023
849e10f
validate omitempty on metadata field
RobsonSutton Mar 13, 2023
acd06cf
remove error
RobsonSutton Mar 13, 2023
d64297b
fmt
RobsonSutton Mar 13, 2023
21f0c1d
fmt
RobsonSutton Mar 13, 2023
f6e2552
update docs
RobsonSutton Mar 13, 2023
3bc46ec
generate docs
RobsonSutton Mar 13, 2023
e61aae1
remove testing tf files
RobsonSutton Mar 13, 2023
ac229cc
remove connection schema
RobsonSutton Mar 16, 2023
f77042c
update docs
RobsonSutton Mar 16, 2023
b4e9bcf
add models.PutWatch
RobsonSutton Mar 16, 2023
88c2a26
rename resource to elasticstack_elasticsearch_watch
RobsonSutton Mar 16, 2023
ecf4c2b
update approach to use individual fields
RobsonSutton Mar 17, 2023
a8291ce
gen docs
RobsonSutton Mar 17, 2023
36858d7
gen docs
RobsonSutton Mar 17, 2023
27bc1f7
marshal into individual fields
RobsonSutton Mar 17, 2023
38a7f74
restructure body to individual fields
RobsonSutton Mar 21, 2023
a15b494
missed some renaming
RobsonSutton Mar 21, 2023
d861e6b
fix missing braces in watcher_test
RobsonSutton Mar 21, 2023
f7770cb
match test to default from API
RobsonSutton Mar 21, 2023
47347b9
Merge branch 'main' into issue-154
RobsonSutton Mar 21, 2023
e8004d5
fmt
RobsonSutton Mar 21, 2023
e3b4407
update CHANGELOG.md
RobsonSutton Mar 22, 2023
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
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ jobs:
image: docker.elastic.co/elasticsearch/elasticsearch:${{ matrix.version }}
env:
discovery.type: single-node
xpack.license.self_generated.type: trial
xpack.security.enabled: true
xpack.watcher.enabled: true
repositories.url.allowed_urls: https://example.com/*
path.repo: /tmp
ELASTIC_PASSWORD: ${{ env.ELASTIC_PASSWORD }}
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ docker-elasticsearch: docker-network ## Start Elasticsearch single node cluster
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e "xpack.security.enabled=true" \
-e "xpack.watcher.enabled=true" \
-e "xpack.license.self_generated.type=trial" \
-e "repositories.url.allowed_urls=https://example.com/*" \
-e "path.repo=/tmp" \
-e ELASTIC_PASSWORD=$(ELASTICSEARCH_PASSWORD) \
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/elasticsearch_logstash_pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ Optional:
Import is supported using the following syntax:

```shell
terraform import elasticstack_elasticsearch_security_logstash_pipeline.my_pipeline <cluster_uuid>/<pipeline ID>
terraform import elasticstack_elasticsearch_logstash_pipeline.my_pipeline <cluster_uuid>/<pipeline ID>
```
89 changes: 89 additions & 0 deletions docs/resources/elasticsearch_watcher_watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
subcategory: "Watcher"
layout: ""
page_title: "Elasticstack: elasticstack_elasticsearch_watcher_watch Resource"
description: |-
Adds and manages a Watch.
---

# Resource: elasticstack_elasticsearch_watcher_watch

Adds and manages a Watch. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api.html

## Example Usage

```terraform
provider "elasticstack" {
elasticsearch {}
}

resource "elasticstack_elasticsearch_watcher_watch" "example" {
watch_id = "test_watch"
active = true

body = jsonencode({
"trigger" = {
"schedule" = {
"cron" = "0 0/1 * * * ?"
}
},
"input" = {
"none" = {}
},
"condition" = {
"always" = {}
},
"actions" = {},
"metadata" = {
"example_key" = "example_value"
},
"throttle_period_in_millis" = 10000
})
}

output "watch" {
value = elasticstack_elasticsearch_watcher_watch.example.watch_id
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `body` (String) Configuration for the pipeline.
- `watch_id` (String) Identifier for the watch.

### Optional

- `active` (Boolean) Defines whether the watch is active or inactive by default. The default value is true, which means the watch is active by default.
- `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection))

### Read-Only

- `id` (String) Internal identifier of the resource.

<a id="nestedblock--elasticsearch_connection"></a>
### Nested Schema for `elasticsearch_connection`

Optional:

- `api_key` (String, Sensitive) API Key to use for authentication to Elasticsearch
- `ca_data` (String) PEM-encoded custom Certificate Authority certificate
- `ca_file` (String) Path to a custom Certificate Authority certificate
- `cert_data` (String) PEM encoded certificate for client auth
- `cert_file` (String) Path to a file containing the PEM encoded certificate for client auth
- `endpoints` (List of String, Sensitive) A list of endpoints where the terraform provider will point to, this must include the http(s) schema and port number.
- `insecure` (Boolean) Disable TLS certificate validation
- `key_data` (String, Sensitive) PEM encoded private key for client auth
- `key_file` (String) Path to a file containing the PEM encoded private key for client auth
- `password` (String, Sensitive) Password to use for API authentication to Elasticsearch.
- `username` (String) Username to use for API authentication to Elasticsearch.

## Import

Import is supported using the following syntax:

```shell
terraform import elasticstack_elasticsearch_watcher_watch.watch_id <cluster_uuid>/<watch ID>
```
Original file line number Diff line number Diff line change
@@ -1 +1 @@
terraform import elasticstack_elasticsearch_security_logstash_pipeline.my_pipeline <cluster_uuid>/<pipeline ID>
terraform import elasticstack_elasticsearch_logstash_pipeline.my_pipeline <cluster_uuid>/<pipeline ID>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import elasticstack_elasticsearch_watcher_watch.watch_id <cluster_uuid>/<watch ID>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
provider "elasticstack" {
elasticsearch {}
}

resource "elasticstack_elasticsearch_watcher_watch" "example" {
watch_id = "test_watch"
active = true

body = jsonencode({
"trigger" = {
"schedule" = {
"cron" = "0 0/1 * * * ?"
}
},
"input" = {
"none" = {}
},
"condition" = {
"always" = {}
},
"actions" = {},
"metadata" = {
"example_key" = "example_value"
},
"throttle_period_in_millis" = 10000
})
}

output "watch" {
value = elasticstack_elasticsearch_watcher_watch.example.watch_id
}
84 changes: 84 additions & 0 deletions internal/clients/elasticsearch/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package elasticsearch

import (
"bytes"
"context"
"encoding/json"
"net/http"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/models"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)

func PutWatch(ctx context.Context, apiClient *clients.ApiClient, watch *models.Watch) diag.Diagnostics {
var diags diag.Diagnostics
watchBodyBytes, err := json.Marshal(watch.Body)
if err != nil {
return diag.FromErr(err)
}
esClient, err := apiClient.GetESClient()
if err != nil {
return diag.FromErr(err)
}
body := esClient.Watcher.PutWatch.WithBody(bytes.NewReader(watchBodyBytes))
active := esClient.Watcher.PutWatch.WithActive(watch.Status.State.Active)
RobsonSutton marked this conversation as resolved.
Show resolved Hide resolved
res, err := esClient.Watcher.PutWatch(watch.WatchID, active, body, esClient.Watcher.PutWatch.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
defer res.Body.Close()
if diags := utils.CheckError(res, "Unable to create or update watch"); diags.HasError() {
return diags
}

return diags
}

func GetWatch(ctx context.Context, apiClient *clients.ApiClient, watchID string) (*models.Watch, diag.Diagnostics) {
var diags diag.Diagnostics

esClient, err := apiClient.GetESClient()
if err != nil {
return nil, diag.FromErr(err)
}

res, err := esClient.Watcher.GetWatch(watchID, esClient.Watcher.GetWatch.WithContext(ctx))
if err != nil {
return nil, diag.FromErr(err)
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
return nil, nil
}
if diags := utils.CheckError(res, "Unable to find watch on cluster."); diags.HasError() {
return nil, diags
}

var watch models.Watch
if err := json.NewDecoder(res.Body).Decode(&watch); err != nil {
return nil, diag.FromErr(err)
}

watch.WatchID = watchID
return &watch, diags
}

func DeleteWatch(ctx context.Context, apiClient *clients.ApiClient, watchID string) diag.Diagnostics {
var diags diag.Diagnostics
esClient, err := apiClient.GetESClient()
if err != nil {
return diag.FromErr(err)
}
res, err := esClient.Watcher.DeleteWatch(watchID, esClient.Watcher.DeleteWatch.WithContext(ctx))

if err != nil && res.IsError() {
return diag.FromErr(err)
}
defer res.Body.Close()
if diags := utils.CheckError(res, "Unable to delete watch"); diags.HasError() {
return diags
}
return diags
}
Loading