-
Notifications
You must be signed in to change notification settings - Fork 121
Migrate Elasticsearch enrich policy resource and data source to Terraform Plugin Framework #1220
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9c2bd8f
Initial plan
Copilot cb33c4d
Implement Plugin Framework migration for enrich policy resource and d…
Copilot b824577
Final validation - enrich policy migration complete and tested
Copilot 724c139
Address PR feedback: refactor code structure and remove old SDK imple…
Copilot d208b65
Fix code formatting for enrich policy data source
Copilot c9c91d1
Regenerate documentation for enrich policy migration
Copilot 3ca7bb2
Use jsontypes.NormalizedType for query field and extract duplicate re…
Copilot 1636489
Refactor EnrichPolicy models to separate resource and data source typ…
Copilot 80c97ef
Change indices and enrich_fields to SetAttribute, update data source …
Copilot a9aa97f
Update documentation after changing indices and enrich_fields to SetA…
Copilot fee9c8d
Merge branch 'main' into copilot/fix-1219
tobio 7bea4cd
PR feedback
tobio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,200 @@ | ||
| package enrich_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/elastic/terraform-provider-elasticstack/internal/acctest" | ||
| "github.com/elastic/terraform-provider-elasticstack/internal/clients" | ||
| sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
| ) | ||
|
|
||
| func TestAccResourceEnrichPolicyFW(t *testing.T) { | ||
| name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { acctest.PreCheck(t) }, | ||
| CheckDestroy: checkEnrichPolicyDestroyFW(name), | ||
| ProtoV6ProviderFactories: acctest.Providers, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testAccEnrichPolicyFW(name), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "name", name), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "policy_type", "match"), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "match_field", `email`), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "indices.0", name), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "enrich_fields.0", "first_name"), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "enrich_fields.1", "last_name"), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "query", "{\"match_all\":{}}"), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "execute", "true"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAccDataSourceEnrichPolicyFW(t *testing.T) { | ||
| name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { acctest.PreCheck(t) }, | ||
| ProtoV6ProviderFactories: acctest.Providers, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testAccEnrichPolicyDataSourceFW(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 TestAccResourceEnrichPolicyFromSDK(t *testing.T) { | ||
| name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { acctest.PreCheck(t) }, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| // Create the enrich policy with the last provider version where the enrich policy resource was built on the SDK | ||
| ExternalProviders: map[string]resource.ExternalProvider{ | ||
| "elasticstack": { | ||
| Source: "elastic/elasticstack", | ||
| VersionConstraint: "0.11.17", | ||
| }, | ||
| }, | ||
| Config: testAccEnrichPolicyFW(name), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "name", name), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "policy_type", "match"), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "execute", "true"), | ||
| ), | ||
| }, | ||
| { | ||
| ProtoV6ProviderFactories: acctest.Providers, | ||
| Config: testAccEnrichPolicyFW(name), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "name", name), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "policy_type", "match"), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "execute", "true"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func testAccEnrichPolicyFW(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 | ||
| } | ||
| `, name, name) | ||
| } | ||
|
|
||
| func testAccEnrichPolicyDataSourceFW(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) | ||
| } | ||
|
|
||
| func checkEnrichPolicyDestroyFW(name string) func(s *terraform.State) error { | ||
| return func(s *terraform.State) error { | ||
| client, err := clients.NewAcceptanceTestingClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, rs := range s.RootModule().Resources { | ||
| if rs.Type != "elasticstack_elasticsearch_enrich_policy" { | ||
| continue | ||
| } | ||
| compId, _ := clients.CompositeIdFromStr(rs.Primary.ID) | ||
| if compId.ResourceId != name { | ||
| return fmt.Errorf("Found unexpectedly enrich policy: %s", compId.ResourceId) | ||
| } | ||
| esClient, err := client.GetESClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| req := esClient.EnrichGetPolicy.WithName(compId.ResourceId) | ||
| res, err := esClient.EnrichGetPolicy(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer res.Body.Close() | ||
| if res.StatusCode == http.StatusFound { | ||
| var policiesResponse map[string]any | ||
| if err := json.NewDecoder(res.Body).Decode(&policiesResponse); err != nil { | ||
| return err | ||
| } | ||
| if len(policiesResponse["policies"].([]any)) != 0 { | ||
| return fmt.Errorf("Enrich policy (%s) still exists", compId.ResourceId) | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
This file contains hidden or 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,94 @@ | ||
| package enrich | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/elastic/terraform-provider-elasticstack/internal/clients" | ||
| "github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch" | ||
| "github.com/elastic/terraform-provider-elasticstack/internal/models" | ||
| "github.com/elastic/terraform-provider-elasticstack/internal/utils" | ||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| "github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| ) | ||
|
|
||
| func (r *enrichPolicyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
| diags := r.upsert(ctx, req.Plan, &resp.State) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func (r *enrichPolicyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
| diags := r.upsert(ctx, req.Plan, &resp.State) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func (r *enrichPolicyResource) upsert(ctx context.Context, plan tfsdk.Plan, state *tfsdk.State) diag.Diagnostics { | ||
| var data EnrichPolicyData | ||
| var diags diag.Diagnostics | ||
| diags.Append(plan.Get(ctx, &data)...) | ||
| if diags.HasError() { | ||
| return diags | ||
| } | ||
|
|
||
| policyName := data.Name.ValueString() | ||
| id, sdkDiags := r.client.ID(ctx, policyName) | ||
| diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...) | ||
| if diags.HasError() { | ||
| return diags | ||
| } | ||
|
|
||
| client, diags := clients.MaybeNewApiClientFromFrameworkResource(ctx, data.ElasticsearchConnection, r.client) | ||
| diags.Append(diags...) | ||
| if diags.HasError() { | ||
| return diags | ||
| } | ||
|
|
||
| // Convert framework types to model | ||
| indices := utils.ListTypeToSlice_String(ctx, data.Indices, path.Empty(), &diags) | ||
| if diags.HasError() { | ||
| return diags | ||
| } | ||
|
|
||
| enrichFields := utils.ListTypeToSlice_String(ctx, data.EnrichFields, path.Empty(), &diags) | ||
| if diags.HasError() { | ||
| return diags | ||
| } | ||
|
|
||
| policy := &models.EnrichPolicy{ | ||
| Type: data.PolicyType.ValueString(), | ||
| Name: policyName, | ||
| Indices: indices, | ||
| MatchField: data.MatchField.ValueString(), | ||
| EnrichFields: enrichFields, | ||
| } | ||
|
|
||
| if !data.Query.IsNull() && !data.Query.IsUnknown() { | ||
| policy.Query = data.Query.ValueString() | ||
| } | ||
|
|
||
| if sdkDiags := elasticsearch.PutEnrichPolicy(ctx, client, policy); sdkDiags.HasError() { | ||
| diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...) | ||
| return diags | ||
| } | ||
|
|
||
| data.Id = types.StringValue(id.String()) | ||
|
|
||
| // Execute policy if requested | ||
| if !data.Execute.IsNull() && !data.Execute.IsUnknown() && data.Execute.ValueBool() { | ||
| if sdkDiags := elasticsearch.ExecuteEnrichPolicy(ctx, client, policyName); sdkDiags.HasError() { | ||
| diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...) | ||
| return diags | ||
| } | ||
| } | ||
|
|
||
| diags.Append(state.Set(ctx, &data)...) | ||
| return diags | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.