Skip to content

Commit a19c981

Browse files
committed
Use normalized json type for "meta"
1 parent 7b2f1d0 commit a19c981

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

internal/kibana/security_exception_list/create.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
99
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
1010
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
11+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-framework/resource"
1314
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -76,8 +77,9 @@ func (r *ExceptionListResource) Create(ctx context.Context, req resource.CreateR
7677
// Set optional meta
7778
if utils.IsKnown(plan.Meta) {
7879
var meta kbapi.SecurityExceptionsAPIExceptionListMeta
79-
if err := json.Unmarshal([]byte(plan.Meta.ValueString()), &meta); err != nil {
80-
resp.Diagnostics.AddError("Failed to parse meta JSON", err.Error())
80+
unmarshalDiags := plan.Meta.Unmarshal(&meta)
81+
resp.Diagnostics.Append(unmarshalDiags...)
82+
if resp.Diagnostics.HasError() {
8183
return
8284
}
8385
body.Meta = &meta
@@ -177,9 +179,9 @@ func (r *ExceptionListResource) updateStateFromAPIResponse(ctx context.Context,
177179
diags.AddError("Failed to serialize meta", err.Error())
178180
return diags
179181
}
180-
model.Meta = types.StringValue(string(metaJSON))
182+
model.Meta = jsontypes.NewNormalizedValue(string(metaJSON))
181183
} else {
182-
model.Meta = types.StringNull()
184+
model.Meta = jsontypes.NewNormalizedNull()
183185
}
184186

185187
return diags
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package security_exception_list
22

33
import (
4+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
45
"github.com/hashicorp/terraform-plugin-framework/types"
56
)
67

78
type ExceptionListModel struct {
8-
ID types.String `tfsdk:"id"`
9-
SpaceID types.String `tfsdk:"space_id"`
10-
ListID types.String `tfsdk:"list_id"`
11-
Name types.String `tfsdk:"name"`
12-
Description types.String `tfsdk:"description"`
13-
Type types.String `tfsdk:"type"`
14-
NamespaceType types.String `tfsdk:"namespace_type"`
15-
OsTypes types.List `tfsdk:"os_types"`
16-
Tags types.List `tfsdk:"tags"`
17-
Meta types.String `tfsdk:"meta"`
18-
CreatedAt types.String `tfsdk:"created_at"`
19-
CreatedBy types.String `tfsdk:"created_by"`
20-
UpdatedAt types.String `tfsdk:"updated_at"`
21-
UpdatedBy types.String `tfsdk:"updated_by"`
22-
Immutable types.Bool `tfsdk:"immutable"`
23-
TieBreakerID types.String `tfsdk:"tie_breaker_id"`
9+
ID types.String `tfsdk:"id"`
10+
SpaceID types.String `tfsdk:"space_id"`
11+
ListID types.String `tfsdk:"list_id"`
12+
Name types.String `tfsdk:"name"`
13+
Description types.String `tfsdk:"description"`
14+
Type types.String `tfsdk:"type"`
15+
NamespaceType types.String `tfsdk:"namespace_type"`
16+
OsTypes types.List `tfsdk:"os_types"`
17+
Tags types.List `tfsdk:"tags"`
18+
Meta jsontypes.Normalized `tfsdk:"meta"`
19+
CreatedAt types.String `tfsdk:"created_at"`
20+
CreatedBy types.String `tfsdk:"created_by"`
21+
UpdatedAt types.String `tfsdk:"updated_at"`
22+
UpdatedBy types.String `tfsdk:"updated_by"`
23+
Immutable types.Bool `tfsdk:"immutable"`
24+
TieBreakerID types.String `tfsdk:"tie_breaker_id"`
2425
}

internal/kibana/security_exception_list/schema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
_ "embed"
66

7+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
78
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -94,6 +95,7 @@ func (r *ExceptionListResource) Schema(_ context.Context, _ resource.SchemaReque
9495
"meta": schema.StringAttribute{
9596
MarkdownDescription: "Placeholder for metadata about the list container as JSON string.",
9697
Optional: true,
98+
CustomType: jsontypes.NormalizedType{},
9799
},
98100
"created_at": schema.StringAttribute{
99101
MarkdownDescription: "The timestamp of when the exception list was created.",

internal/kibana/security_exception_list/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package security_exception_list
22

33
import (
44
"context"
5-
"encoding/json"
65

76
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
87
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
@@ -84,8 +83,9 @@ func (r *ExceptionListResource) Update(ctx context.Context, req resource.UpdateR
8483
// Set optional meta
8584
if utils.IsKnown(plan.Meta) {
8685
var meta kbapi.SecurityExceptionsAPIExceptionListMeta
87-
if err := json.Unmarshal([]byte(plan.Meta.ValueString()), &meta); err != nil {
88-
resp.Diagnostics.AddError("Failed to parse meta JSON", err.Error())
86+
unmarshalDiags := plan.Meta.Unmarshal(&meta)
87+
resp.Diagnostics.Append(unmarshalDiags...)
88+
if resp.Diagnostics.HasError() {
8989
return
9090
}
9191
body.Meta = &meta

0 commit comments

Comments
 (0)