Skip to content

Commit 0baefee

Browse files
Copilottobio
andauthored
Migrate Elasticsearch role mapping resource and data source to Plugin Framework (#1280)
* Initial plan * Implement Plugin Framework role mapping resource and data source Co-authored-by: tobio <444668+tobio@users.noreply.github.com> * Apply code formatting * Remove old SDKv2 registrations and generate docs * Address review comments: extract read logic, use normalized JSON types, use framework diagnostics, and cleanup Co-authored-by: tobio <444668+tobio@users.noreply.github.com> * Address review comments: simplify diagnostics handling and add normalized JSON types to data source Co-authored-by: tobio <444668+tobio@users.noreply.github.com> * Address review feedback: use utils.SetValueFrom and utils.SetTypeAs for role handling Co-authored-by: tobio <444668+tobio@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tobio <444668+tobio@users.noreply.github.com>
1 parent b33ee50 commit 0baefee

File tree

18 files changed

+676
-355
lines changed

18 files changed

+676
-355
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Add support for managing cross_cluster API keys in `elasticstack_elasticsearch_security_api_key` ([#1252](https://github.com/elastic/terraform-provider-elasticstack/pull/1252))
99
- Allow version changes without a destroy/create cycle with `elasticstack_fleet_integration` ([#1255](https://github.com/elastic/terraform-provider-elasticstack/pull/1255)). This fixes an issue where it was impossible to upgrade integrations which are used by an integration policy.
1010
- Add `namespace` attribute to `elasticstack_kibana_synthetics_monitor` resource to support setting data stream namespace independently from `space_id` ([#1247](https://github.com/elastic/terraform-provider-elasticstack/pull/1247))
11+
- Migrate `elasticstack_elasticsearch_security_role_mapping` resource and data source to Terraform Plugin Framework ([#1279](https://github.com/elastic/terraform-provider-elasticstack/pull/1279))
1112

1213
## [0.11.17] - 2025-07-21
1314

docs/data-sources/elasticsearch_security_role_mapping.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ output "user" {
3535

3636
### Optional
3737

38-
- `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection))
38+
- `elasticsearch_connection` (Block List, Deprecated) Elasticsearch connection configuration block. (see [below for nested schema](#nestedblock--elasticsearch_connection))
3939

4040
### Read-Only
4141

docs/resources/elasticsearch_security_role_mapping.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ output "role" {
4646

4747
### Optional
4848

49-
- `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection))
49+
- `elasticsearch_connection` (Block List, Deprecated) Elasticsearch connection configuration block. (see [below for nested schema](#nestedblock--elasticsearch_connection))
5050
- `enabled` (Boolean) Mappings that have `enabled` set to `false` are ignored when role mapping is performed.
5151
- `metadata` (String) Additional metadata that helps define which roles are assigned to each user. Keys beginning with `_` are reserved for system usage.
5252
- `role_templates` (String) A list of mustache templates that will be evaluated to determine the roles names that should granted to the users that match the role mapping rules.

internal/clients/elasticsearch/security.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,73 +252,88 @@ func DeleteRole(ctx context.Context, apiClient *clients.ApiClient, rolename stri
252252
return diags
253253
}
254254

255-
func PutRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMapping *models.RoleMapping) diag.Diagnostics {
255+
func PutRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMapping *models.RoleMapping) fwdiag.Diagnostics {
256+
var diags fwdiag.Diagnostics
256257
roleMappingBytes, err := json.Marshal(roleMapping)
257258
if err != nil {
258-
return diag.FromErr(err)
259+
diags.AddError("Unable to marshal role mapping", err.Error())
260+
return diags
259261
}
260262
esClient, err := apiClient.GetESClient()
261263
if err != nil {
262-
return diag.FromErr(err)
264+
diags.AddError("Unable to get Elasticsearch client", err.Error())
265+
return diags
263266
}
264267
res, err := esClient.Security.PutRoleMapping(roleMapping.Name, bytes.NewReader(roleMappingBytes), esClient.Security.PutRoleMapping.WithContext(ctx))
265268
if err != nil {
266-
return diag.FromErr(err)
269+
diags.AddError("Unable to put role mapping", err.Error())
270+
return diags
267271
}
268272
defer res.Body.Close()
269-
if diags := utils.CheckError(res, "Unable to put role mapping"); diags.HasError() {
273+
if sdkDiags := utils.CheckError(res, "Unable to put role mapping"); sdkDiags.HasError() {
274+
diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...)
270275
return diags
271276
}
272277

273-
return nil
278+
return diags
274279
}
275280

276-
func GetRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) (*models.RoleMapping, diag.Diagnostics) {
281+
func GetRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) (*models.RoleMapping, fwdiag.Diagnostics) {
282+
var diags fwdiag.Diagnostics
277283
esClient, err := apiClient.GetESClient()
278284
if err != nil {
279-
return nil, diag.FromErr(err)
285+
diags.AddError("Unable to get Elasticsearch client", err.Error())
286+
return nil, diags
280287
}
281288
req := esClient.Security.GetRoleMapping.WithName(roleMappingName)
282289
res, err := esClient.Security.GetRoleMapping(req, esClient.Security.GetRoleMapping.WithContext(ctx))
283290
if err != nil {
284-
return nil, diag.FromErr(err)
291+
diags.AddError("Unable to get role mapping", err.Error())
292+
return nil, diags
285293
}
286294
defer res.Body.Close()
287295

288296
if res.StatusCode == http.StatusNotFound {
289-
return nil, nil
297+
return nil, diags
290298
}
291-
if diags := utils.CheckError(res, "Unable to get a role mapping."); diags.HasError() {
299+
if sdkDiags := utils.CheckError(res, "Unable to get a role mapping."); sdkDiags.HasError() {
300+
diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...)
292301
return nil, diags
293302
}
294303
roleMappings := make(map[string]models.RoleMapping)
295304
if err := json.NewDecoder(res.Body).Decode(&roleMappings); err != nil {
296-
return nil, diag.FromErr(err)
305+
diags.AddError("Unable to decode role mapping response", err.Error())
306+
return nil, diags
297307

298308
}
299309
if roleMapping, ok := roleMappings[roleMappingName]; ok {
300310
roleMapping.Name = roleMappingName
301-
return &roleMapping, nil
311+
return &roleMapping, diags
302312
}
303313

304-
return nil, diag.Errorf("unable to find role mapping '%s' in the cluster", roleMappingName)
314+
diags.AddError("Role mapping not found", fmt.Sprintf("unable to find role mapping '%s' in the cluster", roleMappingName))
315+
return nil, diags
305316
}
306317

307-
func DeleteRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) diag.Diagnostics {
318+
func DeleteRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) fwdiag.Diagnostics {
319+
var diags fwdiag.Diagnostics
308320
esClient, err := apiClient.GetESClient()
309321
if err != nil {
310-
return diag.FromErr(err)
322+
diags.AddError("Unable to get Elasticsearch client", err.Error())
323+
return diags
311324
}
312325
res, err := esClient.Security.DeleteRoleMapping(roleMappingName, esClient.Security.DeleteRoleMapping.WithContext(ctx))
313326
if err != nil {
314-
return diag.FromErr(err)
327+
diags.AddError("Unable to delete role mapping", err.Error())
328+
return diags
315329
}
316330
defer res.Body.Close()
317-
if diags := utils.CheckError(res, "Unable to delete role mapping"); diags.HasError() {
331+
if sdkDiags := utils.CheckError(res, "Unable to delete role mapping"); sdkDiags.HasError() {
332+
diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...)
318333
return diags
319334
}
320335

321-
return nil
336+
return diags
322337
}
323338

324339
func CreateApiKey(apiClient *clients.ApiClient, apikey *models.ApiKey) (*models.ApiKeyCreateResponse, fwdiag.Diagnostics) {

internal/elasticsearch/security/role_mapping.go

Lines changed: 0 additions & 198 deletions
This file was deleted.

0 commit comments

Comments
 (0)