Skip to content

Commit 462e274

Browse files
committed
Only test remote indices when supported
1 parent 83747c6 commit 462e274

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

internal/elasticsearch/security/api_key_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,52 @@ func TestAccResourceSecurityApiKey(t *testing.T) {
4242
return err
4343
}
4444

45+
expectedRoleDescriptor := map[string]models.ApiKeyRoleDescriptor{
46+
"role-a": {
47+
Cluster: []string{"all"},
48+
Indices: []models.IndexPerms{{
49+
Names: []string{"index-a*"},
50+
Privileges: []string{"read"},
51+
AllowRestrictedIndices: utils.Pointer(false),
52+
}},
53+
},
54+
}
55+
56+
if !reflect.DeepEqual(testRoleDescriptor, expectedRoleDescriptor) {
57+
return fmt.Errorf("%v doesn't match %v", testRoleDescriptor, expectedRoleDescriptor)
58+
}
59+
60+
return nil
61+
}),
62+
resource.TestCheckResourceAttrSet("elasticstack_elasticsearch_security_api_key.test", "expiration"),
63+
resource.TestCheckResourceAttrSet("elasticstack_elasticsearch_security_api_key.test", "api_key"),
64+
resource.TestCheckResourceAttrSet("elasticstack_elasticsearch_security_api_key.test", "encoded"),
65+
),
66+
},
67+
},
68+
})
69+
}
70+
71+
func TestAccResourceSecurityApiKeyWithRemoteIndices(t *testing.T) {
72+
// generate a random name
73+
apiKeyName := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
74+
75+
resource.Test(t, resource.TestCase{
76+
PreCheck: func() { acctest.PreCheck(t) },
77+
CheckDestroy: checkResourceSecurityApiKeyDestroy,
78+
ProtoV6ProviderFactories: acctest.Providers,
79+
Steps: []resource.TestStep{
80+
{
81+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minSupportedRemoteIndicesVersion),
82+
Config: testAccResourceSecurityApiKeyRemoteIndices(apiKeyName),
83+
Check: resource.ComposeTestCheckFunc(
84+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_api_key.test", "name", apiKeyName),
85+
resource.TestCheckResourceAttrWith("elasticstack_elasticsearch_security_api_key.test", "role_descriptors", func(testValue string) error {
86+
var testRoleDescriptor map[string]models.ApiKeyRoleDescriptor
87+
if err := json.Unmarshal([]byte(testValue), &testRoleDescriptor); err != nil {
88+
return err
89+
}
90+
4591
expectedRoleDescriptor := map[string]models.ApiKeyRoleDescriptor{
4692
"role-a": {
4793
Cluster: []string{"all"},
@@ -161,6 +207,31 @@ provider "elasticstack" {
161207
elasticsearch {}
162208
}
163209
210+
resource "elasticstack_elasticsearch_security_api_key" "test" {
211+
name = "%s"
212+
213+
role_descriptors = jsonencode({
214+
role-a = {
215+
cluster = ["all"]
216+
indices = [{
217+
names = ["index-a*"]
218+
privileges = ["read"]
219+
allow_restricted_indices = false
220+
}]
221+
}
222+
})
223+
224+
expiration = "1d"
225+
}
226+
`, apiKeyName)
227+
}
228+
229+
func testAccResourceSecurityApiKeyRemoteIndices(apiKeyName string) string {
230+
return fmt.Sprintf(`
231+
provider "elasticstack" {
232+
elasticsearch {}
233+
}
234+
164235
resource "elasticstack_elasticsearch_security_api_key" "test" {
165236
name = "%s"
166237

internal/elasticsearch/security/role_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1414
)
1515

16+
var minSupportedRemoteIndicesVersion = version.Must(version.NewSemver("8.10.0"))
17+
1618
func TestAccResourceSecurityRole(t *testing.T) {
1719
// generate a random username
1820
roleName := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
1921
roleNameRemoteIndices := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
20-
minSupportedRemoteIndicesVersion := version.Must(version.NewSemver("8.10.0"))
2122

2223
resource.Test(t, resource.TestCase{
2324
PreCheck: func() { acctest.PreCheck(t) },
@@ -202,7 +203,7 @@ resource "elasticstack_elasticsearch_security_role" "test" {
202203
names = ["sample2"]
203204
privileges = ["create", "read", "write"]
204205
}
205-
206+
206207
metadata = jsonencode({
207208
version = 1
208209
})

0 commit comments

Comments
 (0)