Skip to content

Commit a0c6336

Browse files
Copilottobio
andcommitted
Fix IsNull vs IsKnown checks and reorganize attribute type definitions
Co-authored-by: tobio <[email protected]>
1 parent e053b2c commit a0c6336

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

docs/resources/elasticsearch_security_role.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ output "role" {
5555

5656
### Optional
5757

58-
- `applications` (Attributes Set) A list of application privilege entries. (see [below for nested schema](#nestedatt--applications))
58+
- `applications` (Block Set) A list of application privilege entries. (see [below for nested schema](#nestedblock--applications))
5959
- `cluster` (Set of String) A list of cluster privileges. These privileges define the cluster level actions that users with this role are able to execute.
6060
- `description` (String) The description of the role.
6161
- `elasticsearch_connection` (Block List, Deprecated) Elasticsearch connection configuration block. (see [below for nested schema](#nestedblock--elasticsearch_connection))
6262
- `global` (String) An object defining global privileges.
63-
- `indices` (Attributes Set) A list of indices permissions entries. (see [below for nested schema](#nestedatt--indices))
63+
- `indices` (Block Set) A list of indices permissions entries. (see [below for nested schema](#nestedblock--indices))
6464
- `metadata` (String) Optional meta-data.
65-
- `remote_indices` (Attributes Set) A list of remote indices permissions entries. Remote indices are effective for remote clusters configured with the API key based model. They have no effect for remote clusters configured with the certificate based model. (see [below for nested schema](#nestedatt--remote_indices))
65+
- `remote_indices` (Block Set) A list of remote indices permissions entries. Remote indices are effective for remote clusters configured with the API key based model. They have no effect for remote clusters configured with the certificate based model. (see [below for nested schema](#nestedblock--remote_indices))
6666
- `run_as` (Set of String) A list of users that the owners of this role can impersonate.
6767

6868
### Read-Only
6969

7070
- `id` (String) Internal identifier of the resource
7171

72-
<a id="nestedatt--applications"></a>
72+
<a id="nestedblock--applications"></a>
7373
### Nested Schema for `applications`
7474

7575
Required:
@@ -100,7 +100,7 @@ Optional:
100100
- `username` (String) Username to use for API authentication to Elasticsearch.
101101

102102

103-
<a id="nestedatt--indices"></a>
103+
<a id="nestedblock--indices"></a>
104104
### Nested Schema for `indices`
105105

106106
Required:
@@ -124,7 +124,7 @@ Optional:
124124

125125

126126

127-
<a id="nestedatt--remote_indices"></a>
127+
<a id="nestedblock--remote_indices"></a>
128128
### Nested Schema for `remote_indices`
129129

130130
Required:

internal/elasticsearch/security/role/models.go

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,42 @@ import (
66
"fmt"
77

88
"github.com/elastic/terraform-provider-elasticstack/internal/models"
9+
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
910
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1011
"github.com/hashicorp/terraform-plugin-framework/attr"
1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-framework/types"
1314
)
1415

16+
var (
17+
applicationAttrTypes = map[string]attr.Type{
18+
"application": types.StringType,
19+
"privileges": types.SetType{ElemType: types.StringType},
20+
"resources": types.SetType{ElemType: types.StringType},
21+
}
22+
23+
fieldSecurityAttrTypes = map[string]attr.Type{
24+
"grant": types.SetType{ElemType: types.StringType},
25+
"except": types.SetType{ElemType: types.StringType},
26+
}
27+
28+
indexPermsAttrTypes = map[string]attr.Type{
29+
"field_security": types.ListType{ElemType: types.ObjectType{AttrTypes: fieldSecurityAttrTypes}},
30+
"names": types.SetType{ElemType: types.StringType},
31+
"privileges": types.SetType{ElemType: types.StringType},
32+
"query": jsontypes.NormalizedType{},
33+
"allow_restricted_indices": types.BoolType,
34+
}
35+
36+
remoteIndexPermsAttrTypes = map[string]attr.Type{
37+
"clusters": types.SetType{ElemType: types.StringType},
38+
"field_security": types.ListType{ElemType: types.ObjectType{AttrTypes: fieldSecurityAttrTypes}},
39+
"query": jsontypes.NormalizedType{},
40+
"names": types.SetType{ElemType: types.StringType},
41+
"privileges": types.SetType{ElemType: types.StringType},
42+
}
43+
)
44+
1545
type RoleData struct {
1646
Id types.String `tfsdk:"id"`
1747
ElasticsearchConnection types.List `tfsdk:"elasticsearch_connection"`
@@ -61,13 +91,13 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
6191
role.Name = data.Name.ValueString()
6292

6393
// Description
64-
if !data.Description.IsNull() {
94+
if utils.IsKnown(data.Description) {
6595
description := data.Description.ValueString()
6696
role.Description = &description
6797
}
6898

6999
// Applications
70-
if !data.Applications.IsNull() {
100+
if utils.IsKnown(data.Applications) {
71101
var applicationsList []ApplicationData
72102
diags.Append(data.Applications.ElementsAs(ctx, &applicationsList, false)...)
73103
if diags.HasError() {
@@ -93,7 +123,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
93123
}
94124

95125
// Global
96-
if !data.Global.IsNull() {
126+
if utils.IsKnown(data.Global) {
97127
var global map[string]interface{}
98128
if err := json.Unmarshal([]byte(data.Global.ValueString()), &global); err != nil {
99129
diags.AddError("Invalid JSON", fmt.Sprintf("Error parsing global JSON: %s", err))
@@ -103,7 +133,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
103133
}
104134

105135
// Cluster
106-
if !data.Cluster.IsNull() {
136+
if utils.IsKnown(data.Cluster) {
107137
var cluster []string
108138
diags.Append(data.Cluster.ElementsAs(ctx, &cluster, false)...)
109139
if diags.HasError() {
@@ -113,7 +143,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
113143
}
114144

115145
// Indices
116-
if !data.Indices.IsNull() {
146+
if utils.IsKnown(data.Indices) {
117147
var indicesList []IndexPermsData
118148
diags.Append(data.Indices.ElementsAs(ctx, &indicesList, false)...)
119149
if diags.HasError() {
@@ -134,13 +164,13 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
134164
Privileges: privileges,
135165
}
136166

137-
if !idx.Query.IsNull() {
167+
if utils.IsKnown(idx.Query) {
138168
query := idx.Query.ValueString()
139169
newIndex.Query = &query
140170
}
141171

142172
// Field Security
143-
if !idx.FieldSecurity.IsNull() {
173+
if utils.IsKnown(idx.FieldSecurity) {
144174
var fieldSecList []FieldSecurityData
145175
diags.Append(idx.FieldSecurity.ElementsAs(ctx, &fieldSecList, false)...)
146176
if diags.HasError() {
@@ -151,7 +181,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
151181
fieldSec := fieldSecList[0]
152182
fieldSecurity := models.FieldSecurity{}
153183

154-
if !fieldSec.Grant.IsNull() {
184+
if utils.IsKnown(fieldSec.Grant) {
155185
var grants []string
156186
diags.Append(fieldSec.Grant.ElementsAs(ctx, &grants, false)...)
157187
if diags.HasError() {
@@ -160,7 +190,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
160190
fieldSecurity.Grant = grants
161191
}
162192

163-
if !fieldSec.Except.IsNull() {
193+
if utils.IsKnown(fieldSec.Except) {
164194
var excepts []string
165195
diags.Append(fieldSec.Except.ElementsAs(ctx, &excepts, false)...)
166196
if diags.HasError() {
@@ -173,7 +203,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
173203
}
174204
}
175205

176-
if !idx.AllowRestrictedIndices.IsNull() {
206+
if utils.IsKnown(idx.AllowRestrictedIndices) {
177207
allowRestrictedIndices := idx.AllowRestrictedIndices.ValueBool()
178208
newIndex.AllowRestrictedIndices = &allowRestrictedIndices
179209
}
@@ -184,7 +214,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
184214
}
185215

186216
// Remote Indices
187-
if !data.RemoteIndices.IsNull() {
217+
if utils.IsKnown(data.RemoteIndices) {
188218
var remoteIndicesList []RemoteIndexPermsData
189219
diags.Append(data.RemoteIndices.ElementsAs(ctx, &remoteIndicesList, false)...)
190220
if diags.HasError() {
@@ -207,13 +237,13 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
207237
Privileges: privileges,
208238
}
209239

210-
if !remoteIdx.Query.IsNull() {
240+
if utils.IsKnown(remoteIdx.Query) {
211241
query := remoteIdx.Query.ValueString()
212242
newRemoteIndex.Query = &query
213243
}
214244

215245
// Field Security
216-
if !remoteIdx.FieldSecurity.IsNull() {
246+
if utils.IsKnown(remoteIdx.FieldSecurity) {
217247
var fieldSecList []FieldSecurityData
218248
diags.Append(remoteIdx.FieldSecurity.ElementsAs(ctx, &fieldSecList, false)...)
219249
if diags.HasError() {
@@ -224,7 +254,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
224254
fieldSec := fieldSecList[0]
225255
remoteFieldSecurity := models.FieldSecurity{}
226256

227-
if !fieldSec.Grant.IsNull() {
257+
if utils.IsKnown(fieldSec.Grant) {
228258
var grants []string
229259
diags.Append(fieldSec.Grant.ElementsAs(ctx, &grants, false)...)
230260
if diags.HasError() {
@@ -233,7 +263,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
233263
remoteFieldSecurity.Grant = grants
234264
}
235265

236-
if !fieldSec.Except.IsNull() {
266+
if utils.IsKnown(fieldSec.Except) {
237267
var excepts []string
238268
diags.Append(fieldSec.Except.ElementsAs(ctx, &excepts, false)...)
239269
if diags.HasError() {
@@ -252,7 +282,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
252282
}
253283

254284
// Metadata
255-
if !data.Metadata.IsNull() {
285+
if utils.IsKnown(data.Metadata) {
256286
var metadata map[string]interface{}
257287
if err := json.Unmarshal([]byte(data.Metadata.ValueString()), &metadata); err != nil {
258288
diags.AddError("Invalid JSON", fmt.Sprintf("Error parsing metadata JSON: %s", err))
@@ -262,7 +292,7 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
262292
}
263293

264294
// Run As
265-
if !data.RunAs.IsNull() {
295+
if utils.IsKnown(data.RunAs) {
266296
var runAs []string
267297
diags.Append(data.RunAs.ElementsAs(ctx, &runAs, false)...)
268298
if diags.HasError() {
@@ -278,34 +308,6 @@ func (data *RoleData) toAPIModel(ctx context.Context) (*models.Role, diag.Diagno
278308
func (data *RoleData) fromAPIModel(ctx context.Context, role *models.Role) diag.Diagnostics {
279309
var diags diag.Diagnostics
280310

281-
// Define attribute type maps
282-
applicationAttrTypes := map[string]attr.Type{
283-
"application": types.StringType,
284-
"privileges": types.SetType{ElemType: types.StringType},
285-
"resources": types.SetType{ElemType: types.StringType},
286-
}
287-
288-
fieldSecurityAttrTypes := map[string]attr.Type{
289-
"grant": types.SetType{ElemType: types.StringType},
290-
"except": types.SetType{ElemType: types.StringType},
291-
}
292-
293-
indexPermsAttrTypes := map[string]attr.Type{
294-
"field_security": types.ListType{ElemType: types.ObjectType{AttrTypes: fieldSecurityAttrTypes}},
295-
"names": types.SetType{ElemType: types.StringType},
296-
"privileges": types.SetType{ElemType: types.StringType},
297-
"query": jsontypes.NormalizedType{},
298-
"allow_restricted_indices": types.BoolType,
299-
}
300-
301-
remoteIndexPermsAttrTypes := map[string]attr.Type{
302-
"clusters": types.SetType{ElemType: types.StringType},
303-
"field_security": types.ListType{ElemType: types.ObjectType{AttrTypes: fieldSecurityAttrTypes}},
304-
"query": jsontypes.NormalizedType{},
305-
"names": types.SetType{ElemType: types.StringType},
306-
"privileges": types.SetType{ElemType: types.StringType},
307-
}
308-
309311
data.Name = types.StringValue(role.Name)
310312

311313
// Description

0 commit comments

Comments
 (0)