Skip to content

Commit 5934e78

Browse files
committed
to jsonencode
1 parent d33762f commit 5934e78

File tree

6 files changed

+65
-78
lines changed

6 files changed

+65
-78
lines changed

.devcontainer/devcontainer.json

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

internal/fleet/agent_policy/create.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ func (r *agentPolicyResource) Create(ctx context.Context, req resource.CreateReq
4040
return
4141
}
4242

43-
planModel.populateFromAPI(ctx, policy, sVersion)
43+
diags = planModel.populateFromAPI(ctx, policy, sVersion)
44+
resp.Diagnostics.Append(diags...)
45+
if resp.Diagnostics.HasError() {
46+
return
47+
}
4448

4549
resp.State.Set(ctx, planModel)
4650
resp.Diagnostics.Append(diags...)

internal/fleet/agent_policy/models.go

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

33
import (
44
"context"
5+
"encoding/json"
56
"slices"
67

78
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
@@ -12,24 +13,24 @@ import (
1213
"github.com/hashicorp/terraform-plugin-framework/types"
1314
)
1415

15-
type globalDataTagModel struct {
16-
Name types.String `tfsdk:"name"`
17-
Value types.String `tfsdk:"value"`
18-
}
19-
20-
func newGlobalDataTagModel(data struct {
21-
Name string "json:\"name\""
22-
Value kbapi.AgentPolicy_GlobalDataTags_Value "json:\"value\""
23-
}) globalDataTagModel {
24-
val, err := data.Value.AsAgentPolicyGlobalDataTagsValue0()
25-
if err != nil {
26-
panic(err)
27-
}
28-
return globalDataTagModel{
29-
Name: types.StringValue(data.Name),
30-
Value: types.StringValue(val),
31-
}
32-
}
16+
// type globalDataTagModel struct {
17+
// Name types.String `tfsdk:"name"`
18+
// Value types.String `tfsdk:"value"`
19+
// }
20+
21+
// func newGlobalDataTagModel(data struct {
22+
// Name string "json:\"name\""
23+
// Value kbapi.AgentPolicy_GlobalDataTags_Value "json:\"value\""
24+
// }) globalDataTagModel {
25+
// val, err := data.Value.AsAgentPolicyGlobalDataTagsValue0()
26+
// if err != nil {
27+
// panic(err)
28+
// }
29+
// return globalDataTagModel{
30+
// Name: types.StringValue(data.Name),
31+
// Value: types.StringValue(val),
32+
// }
33+
// }
3334

3435
type agentPolicyModel struct {
3536
ID types.String `tfsdk:"id"`
@@ -45,12 +46,12 @@ type agentPolicyModel struct {
4546
MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
4647
SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
4748
SkipDestroy types.Bool `tfsdk:"skip_destroy"`
48-
GlobalDataTags types.List `tfsdk:"global_data_tags"`
49+
GlobalDataTags types.String `tfsdk:"global_data_tags"`
4950
}
5051

51-
func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy, serverVersion *version.Version) {
52+
func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy, serverVersion *version.Version) diag.Diagnostics {
5253
if data == nil {
53-
return
54+
return nil
5455
}
5556

5657
model.ID = types.StringValue(data.Id)
@@ -79,17 +80,16 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
7980
model.Name = types.StringValue(data.Name)
8081
model.Namespace = types.StringValue(data.Namespace)
8182
if serverVersion.GreaterThanOrEqual(MinVersionGlobalDataTags) && utils.Deref(data.GlobalDataTags) != nil {
82-
83-
gdt := []globalDataTagModel{}
84-
for _, val := range utils.Deref(data.GlobalDataTags) {
85-
gdt = append(gdt, newGlobalDataTagModel(val))
83+
diags := diag.Diagnostics{}
84+
d, err := json.Marshal(data.GlobalDataTags)
85+
if err != nil {
86+
diags.AddError("Failed to marshal global data tags", err.Error())
87+
return diags
8688
}
87-
gdtList, diags := types.ListValueFrom(ctx, getGlobalDataTagsType(), gdt)
88-
if diags.HasError() {
89-
return
90-
}
91-
model.GlobalDataTags = gdtList
89+
model.GlobalDataTags = types.StringValue(string(d))
9290
}
91+
92+
return nil
9393
}
9494

9595
func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersion *version.Version) (kbapi.PostFleetAgentPoliciesJSONRequestBody, diag.Diagnostics) {
@@ -114,19 +114,20 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
114114
Namespace: model.Namespace.ValueString(),
115115
}
116116

117-
if len(model.GlobalDataTags.Elements()) > 0 {
117+
if len(model.GlobalDataTags.ValueString()) > 0 {
118+
var diags diag.Diagnostics
118119
if serverVersion.LessThan(MinVersionGlobalDataTags) {
119-
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diag.Diagnostics{
120-
diag.NewErrorDiagnostic("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above"),
121-
}
122-
120+
diags.AddError("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above")
121+
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
123122
}
124-
diags := model.GlobalDataTags.ElementsAs(ctx, body.GlobalDataTags, false)
125-
if diags.HasError() {
123+
124+
str := model.GlobalDataTags.ValueString()
125+
err := json.Unmarshal([]byte(str), body.GlobalDataTags)
126+
if err != nil {
127+
diags.AddError(err.Error(), "")
126128
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
127129
}
128130
}
129-
130131
return body, nil
131132
}
132133

@@ -150,17 +151,20 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
150151
Namespace: model.Namespace.ValueString(),
151152
}
152153

153-
if len(model.GlobalDataTags.Elements()) > 0 {
154+
if len(model.GlobalDataTags.ValueString()) > 0 {
155+
var diags diag.Diagnostics
154156
if serverVersion.LessThan(MinVersionGlobalDataTags) {
155-
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diag.Diagnostics{
156-
diag.NewErrorDiagnostic("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above"),
157-
}
158-
157+
diags.AddError("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above")
158+
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
159159
}
160-
diags := model.GlobalDataTags.ElementsAs(ctx, body.GlobalDataTags, false)
161-
if diags.HasError() {
160+
161+
str := model.GlobalDataTags.ValueString()
162+
err := json.Unmarshal([]byte(str), body.GlobalDataTags)
163+
if err != nil {
164+
diags.AddError(err.Error(), "")
162165
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
163166
}
167+
164168
}
165169

166170
return body, nil

internal/fleet/agent_policy/read.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ func (r *agentPolicyResource) Read(ctx context.Context, req resource.ReadRequest
3939
return
4040
}
4141

42-
stateModel.populateFromAPI(ctx, policy, sVersion)
42+
diags = stateModel.populateFromAPI(ctx, policy, sVersion)
43+
resp.Diagnostics.Append(diags...)
44+
if resp.Diagnostics.HasError() {
45+
return
46+
}
4347

4448
resp.State.Set(ctx, stateModel)
4549
resp.Diagnostics.Append(diags...)

internal/fleet/agent_policy/resource_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ resource "elasticstack_fleet_agent_policy" "test_policy" {
230230
monitor_logs = true
231231
monitor_metrics = false
232232
skip_destroy = %t
233-
global_data_tags = [
233+
global_data_tags = jsonencode([
234234
{
235235
name = "tag1"
236236
value = "value1"
@@ -241,7 +241,7 @@ resource "elasticstack_fleet_agent_policy" "test_policy" {
241241
name = "tag3"
242242
value = "value3"
243243
}
244-
]
244+
])
245245
}
246246
247247
data "elasticstack_fleet_enrollment_tokens" "test_policy" {
@@ -265,15 +265,15 @@ resource "elasticstack_fleet_agent_policy" "test_policy" {
265265
monitor_logs = false
266266
monitor_metrics = true
267267
skip_destroy = %t
268-
global_data_tags = [
268+
global_data_tags = jsonencode([
269269
{
270270
name = "tag1"
271271
value = "value1a"
272272
},{
273273
name = "tag2"
274274
value = "value2a"
275275
}
276-
]
276+
])
277277
}
278278
279279
data "elasticstack_fleet_enrollment_tokens" "test_policy" {

internal/fleet/agent_policy/schema.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package agent_policy
33
import (
44
"context"
55

6-
"github.com/hashicorp/terraform-plugin-framework/attr"
76
"github.com/hashicorp/terraform-plugin-framework/resource"
87
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
98
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
@@ -87,26 +86,9 @@ func getSchema() schema.Schema {
8786
boolplanmodifier.RequiresReplace(),
8887
},
8988
},
90-
"global_data_tags": schema.ListNestedAttribute{
91-
Description: "User defined data tags to apply to all inputs.",
89+
"global_data_tags": schema.StringAttribute{
90+
Description: "JSON encoded defined data tags to apply to all inputs.",
9291
Optional: true,
93-
94-
NestedObject: schema.NestedAttributeObject{
95-
Attributes: map[string]schema.Attribute{
96-
"name": schema.StringAttribute{
97-
Description: "The name of the data tag.",
98-
Required: true,
99-
},
100-
"value": schema.StringAttribute{
101-
Description: "The string value of the data tag.",
102-
Required: true,
103-
},
104-
},
105-
},
10692
},
10793
}}
10894
}
109-
110-
func getGlobalDataTagsType() attr.Type {
111-
return getSchema().Attributes["global_data_tags"].GetType().(attr.TypeWithElementType).ElementType()
112-
}

0 commit comments

Comments
 (0)