|
| 1 | +package security_exception_list_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/elastic/terraform-provider-elasticstack/internal/acctest" |
| 8 | + "github.com/elastic/terraform-provider-elasticstack/internal/versionutils" |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/hashicorp/go-version" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/config" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 13 | +) |
| 14 | + |
| 15 | +var minExceptionListAPISupport = version.Must(version.NewVersion("7.9.0")) |
| 16 | + |
| 17 | +func TestAccResourceExceptionList(t *testing.T) { |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 20 | + Steps: []resource.TestStep{ |
| 21 | + { |
| 22 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), |
| 23 | + ProtoV6ProviderFactories: acctest.Providers, |
| 24 | + ConfigDirectory: acctest.NamedTestCaseDirectory("create"), |
| 25 | + ConfigVariables: config.Variables{ |
| 26 | + "list_id": config.StringVariable("test-exception-list"), |
| 27 | + "name": config.StringVariable("Test Exception List"), |
| 28 | + "description": config.StringVariable("Test exception list for acceptance tests"), |
| 29 | + "type": config.StringVariable("detection"), |
| 30 | + "namespace_type": config.StringVariable("single"), |
| 31 | + "tags": config.ListVariable(config.StringVariable("test")), |
| 32 | + }, |
| 33 | + Check: resource.ComposeTestCheckFunc( |
| 34 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "list_id", "test-exception-list"), |
| 35 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "name", "Test Exception List"), |
| 36 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "description", "Test exception list for acceptance tests"), |
| 37 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "type", "detection"), |
| 38 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "namespace_type", "single"), |
| 39 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "tags.0", "test"), |
| 40 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_exception_list.test", "id"), |
| 41 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_exception_list.test", "created_at"), |
| 42 | + resource.TestCheckResourceAttrSet("elasticstack_kibana_security_exception_list.test", "created_by"), |
| 43 | + ), |
| 44 | + }, |
| 45 | + { |
| 46 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), |
| 47 | + ProtoV6ProviderFactories: acctest.Providers, |
| 48 | + ConfigDirectory: acctest.NamedTestCaseDirectory("update"), |
| 49 | + ConfigVariables: config.Variables{ |
| 50 | + "list_id": config.StringVariable("test-exception-list"), |
| 51 | + "name": config.StringVariable("Test Exception List Updated"), |
| 52 | + "description": config.StringVariable("Updated description"), |
| 53 | + "type": config.StringVariable("detection"), |
| 54 | + "namespace_type": config.StringVariable("single"), |
| 55 | + "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("updated")), |
| 56 | + }, |
| 57 | + Check: resource.ComposeTestCheckFunc( |
| 58 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "name", "Test Exception List Updated"), |
| 59 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "description", "Updated description"), |
| 60 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "tags.0", "test"), |
| 61 | + resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "tags.1", "updated"), |
| 62 | + ), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestAccResourceExceptionListWithSpace(t *testing.T) { |
| 69 | + resourceName := "elasticstack_kibana_security_exception_list.test" |
| 70 | + spaceResourceName := "elasticstack_kibana_space.test" |
| 71 | + spaceID := fmt.Sprintf("test-space-%s", uuid.New().String()[:8]) |
| 72 | + |
| 73 | + resource.Test(t, resource.TestCase{ |
| 74 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 75 | + ProtoV6ProviderFactories: acctest.Providers, |
| 76 | + Steps: []resource.TestStep{ |
| 77 | + { |
| 78 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), |
| 79 | + ConfigDirectory: acctest.NamedTestCaseDirectory("create"), |
| 80 | + ConfigVariables: config.Variables{ |
| 81 | + "space_id": config.StringVariable(spaceID), |
| 82 | + "list_id": config.StringVariable("test-exception-list-space"), |
| 83 | + "name": config.StringVariable("Test Exception List in Space"), |
| 84 | + "description": config.StringVariable("Test exception list in custom space"), |
| 85 | + "type": config.StringVariable("detection"), |
| 86 | + "namespace_type": config.StringVariable("single"), |
| 87 | + "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("space")), |
| 88 | + }, |
| 89 | + Check: resource.ComposeTestCheckFunc( |
| 90 | + // Check space attributes |
| 91 | + resource.TestCheckResourceAttr(spaceResourceName, "space_id", spaceID), |
| 92 | + resource.TestCheckResourceAttr(spaceResourceName, "name", "Test Space for Exception Lists"), |
| 93 | + |
| 94 | + // Check exception list attributes |
| 95 | + resource.TestCheckResourceAttr(resourceName, "space_id", spaceID), |
| 96 | + resource.TestCheckResourceAttr(resourceName, "list_id", "test-exception-list-space"), |
| 97 | + resource.TestCheckResourceAttr(resourceName, "name", "Test Exception List in Space"), |
| 98 | + resource.TestCheckResourceAttr(resourceName, "description", "Test exception list in custom space"), |
| 99 | + resource.TestCheckResourceAttr(resourceName, "type", "detection"), |
| 100 | + resource.TestCheckResourceAttr(resourceName, "namespace_type", "single"), |
| 101 | + resource.TestCheckResourceAttr(resourceName, "tags.0", "test"), |
| 102 | + resource.TestCheckResourceAttr(resourceName, "tags.1", "space"), |
| 103 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 104 | + resource.TestCheckResourceAttrSet(resourceName, "created_at"), |
| 105 | + resource.TestCheckResourceAttrSet(resourceName, "created_by"), |
| 106 | + ), |
| 107 | + }, |
| 108 | + { |
| 109 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), |
| 110 | + ConfigDirectory: acctest.NamedTestCaseDirectory("update"), |
| 111 | + ConfigVariables: config.Variables{ |
| 112 | + "space_id": config.StringVariable(spaceID), |
| 113 | + "list_id": config.StringVariable("test-exception-list-space"), |
| 114 | + "name": config.StringVariable("Test Exception List in Space Updated"), |
| 115 | + "description": config.StringVariable("Updated description in space"), |
| 116 | + "type": config.StringVariable("detection"), |
| 117 | + "namespace_type": config.StringVariable("single"), |
| 118 | + "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("space"), config.StringVariable("updated")), |
| 119 | + }, |
| 120 | + Check: resource.ComposeTestCheckFunc( |
| 121 | + // Check space attributes remain the same |
| 122 | + resource.TestCheckResourceAttr(spaceResourceName, "space_id", spaceID), |
| 123 | + resource.TestCheckResourceAttr(spaceResourceName, "name", "Test Space for Exception Lists"), |
| 124 | + |
| 125 | + // Check updated exception list attributes |
| 126 | + resource.TestCheckResourceAttr(resourceName, "space_id", spaceID), |
| 127 | + resource.TestCheckResourceAttr(resourceName, "name", "Test Exception List in Space Updated"), |
| 128 | + resource.TestCheckResourceAttr(resourceName, "description", "Updated description in space"), |
| 129 | + resource.TestCheckResourceAttr(resourceName, "tags.0", "test"), |
| 130 | + resource.TestCheckResourceAttr(resourceName, "tags.1", "space"), |
| 131 | + resource.TestCheckResourceAttr(resourceName, "tags.2", "updated"), |
| 132 | + ), |
| 133 | + }, |
| 134 | + }, |
| 135 | + }) |
| 136 | +} |
0 commit comments