|
| 1 | +package enrich_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/elastic/terraform-provider-elasticstack/internal/acctest" |
| 10 | + "github.com/elastic/terraform-provider-elasticstack/internal/clients" |
| 11 | + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccResourceEnrichPolicyFW(t *testing.T) { |
| 17 | + name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 20 | + CheckDestroy: checkEnrichPolicyDestroyFW(name), |
| 21 | + ProtoV6ProviderFactories: acctest.Providers, |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + { |
| 24 | + Config: testAccEnrichPolicyFW(name), |
| 25 | + Check: resource.ComposeTestCheckFunc( |
| 26 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "name", name), |
| 27 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "policy_type", "match"), |
| 28 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "match_field", `email`), |
| 29 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "indices.0", name), |
| 30 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "enrich_fields.0", "first_name"), |
| 31 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "enrich_fields.1", "last_name"), |
| 32 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "query", "{\"match_all\": {}}\n"), |
| 33 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "execute", "true"), |
| 34 | + ), |
| 35 | + }, |
| 36 | + }, |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func TestAccDataSourceEnrichPolicyFW(t *testing.T) { |
| 41 | + name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) |
| 42 | + resource.Test(t, resource.TestCase{ |
| 43 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 44 | + ProtoV6ProviderFactories: acctest.Providers, |
| 45 | + Steps: []resource.TestStep{ |
| 46 | + { |
| 47 | + Config: testAccEnrichPolicyDataSourceFW(name), |
| 48 | + Check: resource.ComposeTestCheckFunc( |
| 49 | + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "name", name), |
| 50 | + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "policy_type", "match"), |
| 51 | + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "match_field", "email"), |
| 52 | + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "indices.0", name), |
| 53 | + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "enrich_fields.0", "first_name"), |
| 54 | + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "enrich_fields.1", "last_name"), |
| 55 | + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_enrich_policy.test", "query", "{\"match_all\":{}}"), |
| 56 | + ), |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func TestAccResourceEnrichPolicyFromSDK(t *testing.T) { |
| 63 | + name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) |
| 64 | + resource.Test(t, resource.TestCase{ |
| 65 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 66 | + Steps: []resource.TestStep{ |
| 67 | + { |
| 68 | + // Create the enrich policy with the last provider version where the enrich policy resource was built on the SDK |
| 69 | + ExternalProviders: map[string]resource.ExternalProvider{ |
| 70 | + "elasticstack": { |
| 71 | + Source: "elastic/elasticstack", |
| 72 | + VersionConstraint: "0.11.17", |
| 73 | + }, |
| 74 | + }, |
| 75 | + Config: testAccEnrichPolicyFW(name), |
| 76 | + Check: resource.ComposeTestCheckFunc( |
| 77 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "name", name), |
| 78 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "policy_type", "match"), |
| 79 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "execute", "true"), |
| 80 | + ), |
| 81 | + }, |
| 82 | + { |
| 83 | + ProtoV6ProviderFactories: acctest.Providers, |
| 84 | + Config: testAccEnrichPolicyFW(name), |
| 85 | + Check: resource.ComposeTestCheckFunc( |
| 86 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "name", name), |
| 87 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "policy_type", "match"), |
| 88 | + resource.TestCheckResourceAttr("elasticstack_elasticsearch_enrich_policy.policy", "execute", "true"), |
| 89 | + ), |
| 90 | + }, |
| 91 | + }, |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func testAccEnrichPolicyFW(name string) string { |
| 96 | + return fmt.Sprintf(` |
| 97 | +provider "elasticstack" { |
| 98 | + elasticsearch {} |
| 99 | +} |
| 100 | +
|
| 101 | +resource "elasticstack_elasticsearch_index" "my_index" { |
| 102 | + name = "%s" |
| 103 | +
|
| 104 | + mappings = jsonencode({ |
| 105 | + properties = { |
| 106 | + email = { type = "text" } |
| 107 | + first_name = { type = "text" } |
| 108 | + last_name = { type = "text" } |
| 109 | + } |
| 110 | + }) |
| 111 | + deletion_protection = false |
| 112 | +} |
| 113 | +
|
| 114 | +resource "elasticstack_elasticsearch_enrich_policy" "policy" { |
| 115 | + name = "%s" |
| 116 | + policy_type = "match" |
| 117 | + indices = [elasticstack_elasticsearch_index.my_index.name] |
| 118 | + match_field = "email" |
| 119 | + enrich_fields = ["first_name", "last_name"] |
| 120 | + query = <<-EOD |
| 121 | + {"match_all": {}} |
| 122 | + EOD |
| 123 | +} |
| 124 | + `, name, name) |
| 125 | +} |
| 126 | + |
| 127 | +func testAccEnrichPolicyDataSourceFW(name string) string { |
| 128 | + return fmt.Sprintf(` |
| 129 | +provider "elasticstack" { |
| 130 | + elasticsearch {} |
| 131 | +} |
| 132 | +
|
| 133 | +resource "elasticstack_elasticsearch_index" "my_index" { |
| 134 | + name = "%s" |
| 135 | +
|
| 136 | + mappings = jsonencode({ |
| 137 | + properties = { |
| 138 | + email = { type = "text" } |
| 139 | + first_name = { type = "text" } |
| 140 | + last_name = { type = "text" } |
| 141 | + } |
| 142 | + }) |
| 143 | + deletion_protection = false |
| 144 | +} |
| 145 | +
|
| 146 | +resource "elasticstack_elasticsearch_enrich_policy" "policy" { |
| 147 | + name = "%s" |
| 148 | + policy_type = "match" |
| 149 | + indices = [elasticstack_elasticsearch_index.my_index.name] |
| 150 | + match_field = "email" |
| 151 | + enrich_fields = ["first_name", "last_name"] |
| 152 | + query = <<-EOD |
| 153 | + {"match_all": {}} |
| 154 | + EOD |
| 155 | +} |
| 156 | +
|
| 157 | +data "elasticstack_elasticsearch_enrich_policy" "test" { |
| 158 | + name = elasticstack_elasticsearch_enrich_policy.policy.name |
| 159 | +} |
| 160 | + `, name, name) |
| 161 | +} |
| 162 | + |
| 163 | +func checkEnrichPolicyDestroyFW(name string) func(s *terraform.State) error { |
| 164 | + return func(s *terraform.State) error { |
| 165 | + client, err := clients.NewAcceptanceTestingClient() |
| 166 | + if err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + |
| 170 | + for _, rs := range s.RootModule().Resources { |
| 171 | + if rs.Type != "elasticstack_elasticsearch_enrich_policy" { |
| 172 | + continue |
| 173 | + } |
| 174 | + compId, _ := clients.CompositeIdFromStr(rs.Primary.ID) |
| 175 | + if compId.ResourceId != name { |
| 176 | + return fmt.Errorf("Found unexpectedly enrich policy: %s", compId.ResourceId) |
| 177 | + } |
| 178 | + esClient, err := client.GetESClient() |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + req := esClient.EnrichGetPolicy.WithName(compId.ResourceId) |
| 183 | + res, err := esClient.EnrichGetPolicy(req) |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + defer res.Body.Close() |
| 188 | + if res.StatusCode == http.StatusFound { |
| 189 | + var policiesResponse map[string]any |
| 190 | + if err := json.NewDecoder(res.Body).Decode(&policiesResponse); err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + if len(policiesResponse["policies"].([]any)) != 0 { |
| 194 | + return fmt.Errorf("Enrich policy (%s) still exists", compId.ResourceId) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + return nil |
| 199 | + } |
| 200 | +} |
0 commit comments