-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add elasticsearch enrich policy datasource (#293)
* Initial enrich policy data source. * Add docs and examples of enrich policy data source. * Update CHANGELOG for enrich policy data source. * Update internal/elasticsearch/enrich/policy_data_source.go Co-authored-by: Toby Brain <[email protected]> * Update PR number in CHANGELOG * Fix docs and docs template. * make fmt changes * Update CHANGELOG.md --------- Co-authored-by: jonjitsu <[email protected]> Co-authored-by: Toby Brain <[email protected]>
- Loading branch information
1 parent
0c8d916
commit c311bbe
Showing
7 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,84 @@ | ||
--- | ||
subcategory: "Enrich" | ||
page_title: "elasticstack_elasticsearch_enrich_policy Data Source - terraform-provider-elasticstack" | ||
description: |- | ||
Returns information about an enrich policy. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/get-enrich-policy-api.html | ||
--- | ||
|
||
# Data Source: elasticstack_elasticsearch_enrich_policy | ||
|
||
Returns information about an enrich policy. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/get-enrich-policy-api.html | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
provider "elasticstack" { | ||
elasticsearch {} | ||
} | ||
resource "elasticstack_elasticsearch_index" "my_index" { | ||
name = "my-index" | ||
mappings = jsonencode({ | ||
properties = { | ||
email = { type = "text" } | ||
first_name = { type = "text" } | ||
last_name = { type = "text" } | ||
} | ||
}) | ||
deletion_protection = false | ||
} | ||
resource "elasticstack_elasticsearch_enrich_policy" "policy1" { | ||
name = "policy1" | ||
policy_type = "match" | ||
indices = [elasticstack_elasticsearch_index.my_index.name] | ||
match_field = "email" | ||
enrich_fields = ["first_name", "last_name"] | ||
query = jsonencode({ | ||
bool = { | ||
must = [{ term = { b = "A" } }] | ||
must_not = [{ term = { a = "B" } }] | ||
} | ||
}) | ||
} | ||
data "elasticstack_elasticsearch_enrich_policy" "policy" { | ||
name = "policy1" | ||
} | ||
output "name" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.name | ||
} | ||
output "match_field" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.match_field | ||
} | ||
output "indices" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.indices | ||
} | ||
output "policy_type" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.policy_type | ||
} | ||
output "enrich_fields" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.enrich_fields | ||
} | ||
output "query" { | ||
value = jsondecode(data.elasticstack_elasticsearch_enrich_policy.policy.query) | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the policy. | ||
|
||
### Read-Only | ||
|
||
- `enrich_fields` (Set of String) Fields to add to matching incoming documents. These fields must be present in the source indices. | ||
- `id` (String) Internal identifier of the resource | ||
- `indices` (Set of String) Array of one or more source indices used to create the enrich index. | ||
- `match_field` (String) Field in source indices used to match incoming documents. | ||
- `policy_type` (String) The type of enrich policy, can be one of geo_match, match, range. | ||
- `query` (String) Query used to filter documents in the enrich index. The policy only uses documents matching this query to enrich incoming documents. Defaults to a match_all query. |
53 changes: 53 additions & 0 deletions
53
examples/data-sources/elasticstack_elasticsearch_enrich_policy/data-source.tf
This file contains 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,53 @@ | ||
provider "elasticstack" { | ||
elasticsearch {} | ||
} | ||
|
||
resource "elasticstack_elasticsearch_index" "my_index" { | ||
name = "my-index" | ||
|
||
mappings = jsonencode({ | ||
properties = { | ||
email = { type = "text" } | ||
first_name = { type = "text" } | ||
last_name = { type = "text" } | ||
} | ||
}) | ||
deletion_protection = false | ||
} | ||
|
||
resource "elasticstack_elasticsearch_enrich_policy" "policy1" { | ||
name = "policy1" | ||
policy_type = "match" | ||
indices = [elasticstack_elasticsearch_index.my_index.name] | ||
match_field = "email" | ||
enrich_fields = ["first_name", "last_name"] | ||
query = jsonencode({ | ||
bool = { | ||
must = [{ term = { b = "A" } }] | ||
must_not = [{ term = { a = "B" } }] | ||
} | ||
}) | ||
} | ||
|
||
data "elasticstack_elasticsearch_enrich_policy" "policy" { | ||
name = "policy1" | ||
} | ||
|
||
output "name" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.name | ||
} | ||
output "match_field" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.match_field | ||
} | ||
output "indices" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.indices | ||
} | ||
output "policy_type" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.policy_type | ||
} | ||
output "enrich_fields" { | ||
value = data.elasticstack_elasticsearch_enrich_policy.policy.enrich_fields | ||
} | ||
output "query" { | ||
value = jsondecode(data.elasticstack_elasticsearch_enrich_policy.policy.query) | ||
} |
This file contains 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,76 @@ | ||
package enrich | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/elastic/terraform-provider-elasticstack/internal/clients" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceEnrichPolicy() *schema.Resource { | ||
policySchema := map[string]*schema.Schema{ | ||
"id": { | ||
Description: "Internal identifier of the resource", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Description: "The name of the policy.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"policy_type": { | ||
Description: "The type of enrich policy, can be one of geo_match, match, range.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"indices": { | ||
Description: "Array of one or more source indices used to create the enrich index.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"match_field": { | ||
Description: "Field in source indices used to match incoming documents.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"enrich_fields": { | ||
Description: "Fields to add to matching incoming documents. These fields must be present in the source indices.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"query": { | ||
Description: "Query used to filter documents in the enrich index. The policy only uses documents matching this query to enrich incoming documents. Defaults to a match_all query.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
} | ||
|
||
return &schema.Resource{ | ||
Description: "Returns information about an enrich policy. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/get-enrich-policy-api.html", | ||
ReadContext: dataSourceEnrichPolicyRead, | ||
Schema: policySchema, | ||
} | ||
} | ||
|
||
func dataSourceEnrichPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client, diags := clients.NewApiClient(d, meta) | ||
if diags.HasError() { | ||
return diags | ||
} | ||
|
||
policyId := d.Get("name").(string) | ||
id, diags := client.ID(ctx, policyId) | ||
if diags.HasError() { | ||
return diags | ||
} | ||
d.SetId(id.String()) | ||
return resourceEnrichPolicyRead(ctx, d, meta) | ||
} |
This file contains 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,68 @@ | ||
package enrich_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/elastic/terraform-provider-elasticstack/internal/acctest" | ||
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceEnrichPolicy(t *testing.T) { | ||
name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.Providers, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccEnrichPolicyDataSource(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "name", name), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "policy_type", "match"), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "match_field", "email"), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "indices.0", name), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "enrich_fields.0", "first_name"), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "enrich_fields.1", "last_name"), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "query", "{\"match_all\":{}}"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccEnrichPolicyDataSource(name string) string { | ||
return fmt.Sprintf(` | ||
provider "elasticstack" { | ||
elasticsearch {} | ||
} | ||
resource "elasticstack_elasticsearch_index" "my_index" { | ||
name = "%s" | ||
mappings = jsonencode({ | ||
properties = { | ||
email = { type = "text" } | ||
first_name = { type = "text" } | ||
last_name = { type = "text" } | ||
} | ||
}) | ||
deletion_protection = false | ||
} | ||
resource "elasticstack_elasticsearch_enrich_policy" "policy" { | ||
name = "%s" | ||
policy_type = "match" | ||
indices = [elasticstack_elasticsearch_index.my_index.name] | ||
match_field = "email" | ||
enrich_fields = ["first_name", "last_name"] | ||
query = <<-EOD | ||
{"match_all": {}} | ||
EOD | ||
} | ||
data "elasticstack_elasticsearch_enrich_policy" "test" { | ||
name = elasticstack_elasticsearch_enrich_policy.policy.name | ||
} | ||
`, name, name) | ||
} |
This file contains 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
16 changes: 16 additions & 0 deletions
16
templates/data-sources/elasticsearch_enrich_policy.md.tmpl
This file contains 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,16 @@ | ||
--- | ||
subcategory: "Enrich" | ||
page_title: "elasticstack_elasticsearch_enrich_policy Data Source - terraform-provider-elasticstack" | ||
description: |- | ||
Returns information about an enrich policy. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/get-enrich-policy-api.html | ||
--- | ||
|
||
# Data Source: elasticstack_elasticsearch_enrich_policy | ||
|
||
Returns information about an enrich policy. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/get-enrich-policy-api.html | ||
|
||
## Example Usage | ||
|
||
{{ tffile "examples/data-sources/elasticstack_elasticsearch_enrich_policy/data-source.tf" }} | ||
|
||
{{ .SchemaMarkdown | trimspace }} |