Skip to content

Commit 5f77aed

Browse files
committed
Add description to kibana role resource
1 parent 1015d90 commit 5f77aed

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Add `headers` for the provider connection ([#1057](https://github.com/elastic/terraform-provider-elasticstack/pull/1057))
44
- Add custom `endpoint` configuration support for snapshot repository setup ([#1158](https://github.com/elastic/terraform-provider-elasticstack/pull/1158))
5+
- Add `description` to `elasticstack_kibana_security_role` ([#1172](https://github.com/elastic/terraform-provider-elasticstack/issues/1172))
56

67
## [0.11.15] - 2025-04-23
78

internal/kibana/role.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import (
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1616
)
1717

18-
var minSupportedRemoteIndicesVersion = version.Must(version.NewVersion("8.10.0"))
18+
var (
19+
minSupportedRemoteIndicesVersion = version.Must(version.NewVersion("8.10.0"))
20+
minSupportedDescriptionVersion = version.Must(version.NewVersion("8.15.0"))
21+
)
1922

2023
func ResourceRole() *schema.Resource {
2124
roleSchema := map[string]*schema.Schema{
@@ -232,6 +235,11 @@ func ResourceRole() *schema.Resource {
232235
ValidateFunc: validation.StringIsJSON,
233236
DiffSuppressFunc: utils.DiffJsonSuppress,
234237
},
238+
"description": {
239+
Description: "Optional description for the role",
240+
Type: schema.TypeString,
241+
Optional: true,
242+
},
235243
}
236244

237245
return &schema.Resource{
@@ -293,6 +301,14 @@ func resourceRoleUpsert(ctx context.Context, d *schema.ResourceData, meta interf
293301
}
294302
}
295303

304+
if v, ok := d.GetOk("description"); ok {
305+
if serverVersion.LessThan(minSupportedDescriptionVersion) {
306+
return diag.FromErr(fmt.Errorf("'description' is supported only for Kibana v%s and above", minSupportedDescriptionVersion.String()))
307+
}
308+
309+
kibanaRole.Description = v.(string)
310+
}
311+
296312
roleManageResponse, err := kibana.KibanaRoleManagement.CreateOrUpdate(&kibanaRole)
297313
if err != nil {
298314
return diag.FromErr(err)
@@ -334,6 +350,9 @@ func resourceRoleRead(ctx context.Context, d *schema.ResourceData, meta interfac
334350
if err := d.Set("kibana", flattenKibanaRoleKibanaData(&role.Kibana)); err != nil {
335351
return diag.FromErr(err)
336352
}
353+
if err := d.Set("description", role.Description); err != nil {
354+
return diag.FromErr(err)
355+
}
337356
if role.Metadata != nil {
338357
metadata, err := json.Marshal(role.Metadata)
339358
if err != nil {

internal/kibana/role_data_source.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ func DataSourceRole() *schema.Resource {
217217
ValidateFunc: validation.StringIsJSON,
218218
DiffSuppressFunc: utils.DiffJsonSuppress,
219219
},
220+
"description": {
221+
Description: "Description for the role",
222+
Type: schema.TypeString,
223+
Optional: true,
224+
},
220225
}
221226

222227
return &schema.Resource{

internal/kibana/role_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestAccResourceKibanaSecurityRole(t *testing.T) {
1919
roleName := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
2020
roleNameRemoteIndices := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
2121
minSupportedRemoteIndicesVersion := version.Must(version.NewSemver("8.10.0"))
22+
minSupportedDescriptionVersion := version.Must(version.NewVersion("8.15.0"))
2223

2324
resource.Test(t, resource.TestCase{
2425
PreCheck: func() { acctest.PreCheck(t) },
@@ -48,6 +49,19 @@ func TestAccResourceKibanaSecurityRole(t *testing.T) {
4849
utils.TestCheckResourceListAttr("elasticstack_kibana_security_role.test", "kibana.0.spaces", []string{"default"}),
4950
),
5051
},
52+
{
53+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minSupportedDescriptionVersion),
54+
Config: testAccResourceSecurityRoleWithDescription(roleName),
55+
Check: resource.ComposeTestCheckFunc(
56+
resource.TestCheckResourceAttr("elasticstack_kibana_security_role.test", "name", roleName),
57+
resource.TestCheckNoResourceAttr("elasticstack_kibana_security_role.test", "kibana.0.feature.#"),
58+
resource.TestCheckNoResourceAttr("elasticstack_kibana_security_role.test", "elasticsearch.0.indices.0.field_security.#"),
59+
utils.TestCheckResourceListAttr("elasticstack_kibana_security_role.test", "elasticsearch.0.run_as", []string{"elastic", "kibana"}),
60+
utils.TestCheckResourceListAttr("elasticstack_kibana_security_role.test", "kibana.0.base", []string{"all"}),
61+
utils.TestCheckResourceListAttr("elasticstack_kibana_security_role.test", "kibana.0.spaces", []string{"default"}),
62+
resource.TestCheckResourceAttr("elasticstack_kibana_security_role.test", "description", "Role description"),
63+
),
64+
},
5165
{
5266
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minSupportedRemoteIndicesVersion),
5367
Config: testAccResourceSecurityRoleRemoteIndicesCreate(roleNameRemoteIndices),
@@ -174,6 +188,32 @@ resource "elasticstack_kibana_security_role" "test" {
174188
`, roleName)
175189
}
176190

191+
func testAccResourceSecurityRoleWithDescription(roleName string) string {
192+
return fmt.Sprintf(`
193+
provider "elasticstack" {
194+
elasticsearch {}
195+
kibana {}
196+
}
197+
198+
resource "elasticstack_kibana_security_role" "test" {
199+
name = "%s"
200+
description = "Role description"
201+
elasticsearch {
202+
cluster = [ "create_snapshot" ]
203+
indices {
204+
names = ["sample"]
205+
privileges = ["create", "read", "write"]
206+
}
207+
run_as = ["kibana", "elastic"]
208+
}
209+
kibana {
210+
base = [ "all" ]
211+
spaces = ["default"]
212+
}
213+
}
214+
`, roleName)
215+
}
216+
177217
func testAccResourceSecurityRoleRemoteIndicesCreate(roleName string) string {
178218
return fmt.Sprintf(`
179219
provider "elasticstack" {

libs/go-kibana-rest/kbapi/api.kibana_role_management.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ const (
1414

1515
// KibanaRole is the API role object
1616
type KibanaRole struct {
17-
Name string `json:"name,omitempty"`
18-
Metadata map[string]interface{} `json:"metadata,omitempty"`
19-
TransientMedata *KibanaRoleTransientMetadata `json:"transient_metadata,omitempty"`
20-
Elasticsearch *KibanaRoleElasticsearch `json:"elasticsearch,omitempty"`
21-
Kibana []KibanaRoleKibana `json:"kibana,omitempty"`
22-
CreateOnly bool `json:"-"`
17+
Name string `json:"name,omitempty"`
18+
Metadata map[string]interface{} `json:"metadata,omitempty"`
19+
TransientMetadata *KibanaRoleTransientMetadata `json:"transient_metadata,omitempty"`
20+
Elasticsearch *KibanaRoleElasticsearch `json:"elasticsearch,omitempty"`
21+
Kibana []KibanaRoleKibana `json:"kibana,omitempty"`
22+
Description string `json:"description,omitempty"`
23+
CreateOnly bool `json:"-"`
2324
}
2425

2526
// KibanaRoleTransientMetadata is the API TransientMedata object

0 commit comments

Comments
 (0)