|
| 1 | +package security_list_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/elastic/terraform-provider-elasticstack/internal/acctest" |
| 8 | + "github.com/elastic/terraform-provider-elasticstack/internal/clients" |
| 9 | + "github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi" |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/config" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 13 | +) |
| 14 | + |
| 15 | +func ensureListIndexExists(t *testing.T) { |
| 16 | + client, err := clients.NewAcceptanceTestingClient() |
| 17 | + if err != nil { |
| 18 | + t.Fatalf("Failed to create client: %v", err) |
| 19 | + } |
| 20 | + |
| 21 | + kibanaClient, err := client.GetKibanaOapiClient() |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("Failed to get Kibana client: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + diags := kibana_oapi.CreateListIndex(context.Background(), kibanaClient, "default") |
| 27 | + if diags.HasError() { |
| 28 | + // It's OK if it already exists, we'll only fail on other errors |
| 29 | + for _, d := range diags { |
| 30 | + if d.Summary() != "Unexpected status code from server: got HTTP 409" { |
| 31 | + t.Fatalf("Failed to create list index: %v", d.Detail()) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestAccResourceSecurityList(t *testing.T) { |
| 38 | + listID := "test-list-" + uuid.New().String() |
| 39 | + resource.Test(t, resource.TestCase{ |
| 40 | + PreCheck: func() { |
| 41 | + acctest.PreCheck(t) |
| 42 | + ensureListIndexExists(t) |
| 43 | + }, |
| 44 | + ProtoV6ProviderFactories: acctest.Providers, |
| 45 | + Steps: []resource.TestStep{ |
| 46 | + { // Create |
| 47 | + ConfigDirectory: acctest.NamedTestCaseDirectory("create"), |
| 48 | + ConfigVariables: config.Variables{ |
| 49 | + "list_id": config.StringVariable(listID), |
| 50 | + "name": config.StringVariable("Test Security List"), |
| 51 | + "description": config.StringVariable("A test security list for IP addresses"), |
| 52 | + "type": config.StringVariable("ip"), |
| 53 | + }, |
| 54 | + Check: resource.ComposeTestCheckFunc( |
| 55 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list.test", "id"), |
| 56 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "name", "Test Security List"), |
| 57 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "description", "A test security list for IP addresses"), |
| 58 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "type", "ip"), |
| 59 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list.test", "created_at"), |
| 60 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list.test", "created_by"), |
| 61 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list.test", "updated_at"), |
| 62 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list.test", "updated_by"), |
| 63 | + ), |
| 64 | + }, |
| 65 | + { // Update |
| 66 | + ConfigDirectory: acctest.NamedTestCaseDirectory("update"), |
| 67 | + ConfigVariables: config.Variables{ |
| 68 | + "list_id": config.StringVariable(listID), |
| 69 | + "name": config.StringVariable("Updated Security List"), |
| 70 | + "description": config.StringVariable("An updated test security list"), |
| 71 | + "type": config.StringVariable("ip"), |
| 72 | + }, |
| 73 | + Check: resource.ComposeTestCheckFunc( |
| 74 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "name", "Updated Security List"), |
| 75 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "description", "An updated test security list"), |
| 76 | + ), |
| 77 | + }, |
| 78 | + }, |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func TestAccResourceSecurityList_KeywordType(t *testing.T) { |
| 83 | + listID := "keyword-list-" + uuid.New().String() |
| 84 | + resource.Test(t, resource.TestCase{ |
| 85 | + PreCheck: func() { |
| 86 | + acctest.PreCheck(t) |
| 87 | + ensureListIndexExists(t) |
| 88 | + }, |
| 89 | + ProtoV6ProviderFactories: acctest.Providers, |
| 90 | + Steps: []resource.TestStep{ |
| 91 | + { |
| 92 | + ConfigDirectory: acctest.NamedTestCaseDirectory("keyword_type"), |
| 93 | + ConfigVariables: config.Variables{ |
| 94 | + "list_id": config.StringVariable(listID), |
| 95 | + "name": config.StringVariable("Keyword Security List"), |
| 96 | + "description": config.StringVariable("A test security list for keywords"), |
| 97 | + "type": config.StringVariable("keyword"), |
| 98 | + }, |
| 99 | + Check: resource.ComposeTestCheckFunc( |
| 100 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "type", "keyword"), |
| 101 | + ), |
| 102 | + }, |
| 103 | + }, |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func TestAccResourceSecurityList_SerializerDeserializer(t *testing.T) { |
| 108 | + listID := "serializer-list-" + uuid.New().String() |
| 109 | + resource.Test(t, resource.TestCase{ |
| 110 | + PreCheck: func() { |
| 111 | + acctest.PreCheck(t) |
| 112 | + ensureListIndexExists(t) |
| 113 | + }, |
| 114 | + ProtoV6ProviderFactories: acctest.Providers, |
| 115 | + Steps: []resource.TestStep{ |
| 116 | + { // Create with serializer and deserializer |
| 117 | + ConfigDirectory: acctest.NamedTestCaseDirectory("create"), |
| 118 | + ConfigVariables: config.Variables{ |
| 119 | + "list_id": config.StringVariable(listID), |
| 120 | + "name": config.StringVariable("Custom Serializer List"), |
| 121 | + "description": config.StringVariable("A test list with custom serializer and deserializer"), |
| 122 | + "type": config.StringVariable("ip"), |
| 123 | + "serializer": config.StringVariable("{{ip}}"), |
| 124 | + "deserializer": config.StringVariable("{{ip}}"), |
| 125 | + }, |
| 126 | + Check: resource.ComposeTestCheckFunc( |
| 127 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list.test", "id"), |
| 128 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "name", "Custom Serializer List"), |
| 129 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "type", "ip"), |
| 130 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "serializer", "{{ip}}"), |
| 131 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "deserializer", "{{ip}}"), |
| 132 | + ), |
| 133 | + }, |
| 134 | + { // Update name and description (serializer/deserializer are immutable) |
| 135 | + ConfigDirectory: acctest.NamedTestCaseDirectory("update"), |
| 136 | + ConfigVariables: config.Variables{ |
| 137 | + "list_id": config.StringVariable(listID), |
| 138 | + "name": config.StringVariable("Updated Serializer List"), |
| 139 | + "description": config.StringVariable("Updated test list description"), |
| 140 | + "type": config.StringVariable("ip"), |
| 141 | + "serializer": config.StringVariable("{{ip}}"), |
| 142 | + "deserializer": config.StringVariable("{{ip}}"), |
| 143 | + }, |
| 144 | + Check: resource.ComposeTestCheckFunc( |
| 145 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "name", "Updated Serializer List"), |
| 146 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "description", "Updated test list description"), |
| 147 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "serializer", "{{ip}}"), |
| 148 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "deserializer", "{{ip}}"), |
| 149 | + ), |
| 150 | + }, |
| 151 | + }, |
| 152 | + }) |
| 153 | +} |
0 commit comments