|
| 1 | +package security_list_item_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 TestAccResourceSecurityListItem(t *testing.T) { |
| 38 | + listID := "test-list-items-" + 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 | + "value": config.StringVariable("test-value-1"), |
| 51 | + }, |
| 52 | + Check: resource.ComposeTestCheckFunc( |
| 53 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "id"), |
| 54 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "value", "test-value-1"), |
| 55 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "created_at"), |
| 56 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "created_by"), |
| 57 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "updated_at"), |
| 58 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "updated_by"), |
| 59 | + ), |
| 60 | + }, |
| 61 | + { // Update |
| 62 | + ConfigDirectory: acctest.NamedTestCaseDirectory("update"), |
| 63 | + ConfigVariables: config.Variables{ |
| 64 | + "list_id": config.StringVariable(listID), |
| 65 | + "value": config.StringVariable("test-value-updated"), |
| 66 | + }, |
| 67 | + Check: resource.ComposeTestCheckFunc( |
| 68 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "value", "test-value-updated"), |
| 69 | + ), |
| 70 | + }, |
| 71 | + }, |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func TestAccResourceSecurityListItem_Space(t *testing.T) { |
| 76 | + spaceID := "test-space-" + uuid.New().String() |
| 77 | + listID := "test-list-" + uuid.New().String() |
| 78 | + |
| 79 | + resource.Test(t, resource.TestCase{ |
| 80 | + PreCheck: func() { |
| 81 | + acctest.PreCheck(t) |
| 82 | + ensureListIndexExistsInSpace(t, spaceID) |
| 83 | + }, |
| 84 | + ProtoV6ProviderFactories: acctest.Providers, |
| 85 | + Steps: []resource.TestStep{ |
| 86 | + { // Create space, list, and list item |
| 87 | + ConfigDirectory: acctest.NamedTestCaseDirectory("space_create"), |
| 88 | + ConfigVariables: config.Variables{ |
| 89 | + "space_id": config.StringVariable(spaceID), |
| 90 | + "list_id": config.StringVariable(listID), |
| 91 | + "value": config.StringVariable("192.168.1.1"), |
| 92 | + }, |
| 93 | + Check: resource.ComposeTestCheckFunc( |
| 94 | + // Check space |
| 95 | + resource.TestCheckResourceAttr("elasticstack_kibana_space.test", "space_id", spaceID), |
| 96 | + resource.TestCheckResourceAttr("elasticstack_kibana_space.test", "name", "Test Security Lists Space"), |
| 97 | + // Check list |
| 98 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list.test", "id"), |
| 99 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "space_id", spaceID), |
| 100 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "list_id", listID), |
| 101 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "name", "IP Blocklist"), |
| 102 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list.test", "type", "ip"), |
| 103 | + // Check list item |
| 104 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "id"), |
| 105 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "space_id", spaceID), |
| 106 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "list_id", listID), |
| 107 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "value", "192.168.1.1"), |
| 108 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "created_at"), |
| 109 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "created_by"), |
| 110 | + ), |
| 111 | + }, |
| 112 | + { // Update list item |
| 113 | + ConfigDirectory: acctest.NamedTestCaseDirectory("space_update"), |
| 114 | + ConfigVariables: config.Variables{ |
| 115 | + "space_id": config.StringVariable(spaceID), |
| 116 | + "list_id": config.StringVariable(listID), |
| 117 | + "value": config.StringVariable("10.0.0.1"), |
| 118 | + }, |
| 119 | + Check: resource.ComposeTestCheckFunc( |
| 120 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "value", "10.0.0.1"), |
| 121 | + ), |
| 122 | + }, |
| 123 | + }, |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +func ensureListIndexExistsInSpace(t *testing.T, spaceID string) { |
| 128 | + client, err := clients.NewAcceptanceTestingClient() |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("Failed to create client: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + kibanaClient, err := client.GetKibanaOapiClient() |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("Failed to get Kibana client: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + diags := kibana_oapi.CreateListIndex(context.Background(), kibanaClient, spaceID) |
| 139 | + if diags.HasError() { |
| 140 | + // It's OK if it already exists, we'll only fail on other errors |
| 141 | + for _, d := range diags { |
| 142 | + if d.Summary() != "Unexpected status code from server: got HTTP 409" { |
| 143 | + t.Fatalf("Failed to create list index in space %s: %v", spaceID, d.Detail()) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments