@@ -42,6 +42,52 @@ func TestAccResourceSecurityApiKey(t *testing.T) {
42
42
return err
43
43
}
44
44
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
+
45
91
expectedRoleDescriptor := map [string ]models.ApiKeyRoleDescriptor {
46
92
"role-a" : {
47
93
Cluster : []string {"all" },
@@ -161,6 +207,31 @@ provider "elasticstack" {
161
207
elasticsearch {}
162
208
}
163
209
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
+
164
235
resource "elasticstack_elasticsearch_security_api_key" "test" {
165
236
name = "%s"
166
237
0 commit comments