Skip to content

Commit a01a80c

Browse files
committed
amazing, types arent screaming and tests pass
1 parent 75977b1 commit a01a80c

File tree

7 files changed

+49
-78
lines changed

7 files changed

+49
-78
lines changed

docs/resources/fleet_agent_policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ resource "elasticstack_fleet_agent_policy" "test_policy" {
4141
- `description` (String) The description of the agent policy.
4242
- `download_source_id` (String) The identifier for the Elastic Agent binary download server.
4343
- `fleet_server_host_id` (String) The identifier for the Fleet server host.
44-
- `global_data_tags` (String) JSON encoded defined data tags to apply to all inputs.
44+
- `global_data_tags` (String) JSON encoded user-defined data tags to apply to all inputs.
4545
- `monitor_logs` (Boolean) Enable collection of agent logs.
4646
- `monitor_metrics` (Boolean) Enable collection of agent metrics.
4747
- `monitoring_output_id` (String) The identifier for monitoring output.

internal/fleet/agent_policy/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (r *agentPolicyResource) Create(ctx context.Context, req resource.CreateReq
2727
return
2828
}
2929

30-
body, diags := planModel.toAPICreateModel(ctx, sVersion)
30+
body, diags := planModel.toAPICreateModel(sVersion)
3131
resp.Diagnostics.Append(diags...)
3232
if resp.Diagnostics.HasError() {
3333
return
@@ -40,7 +40,7 @@ func (r *agentPolicyResource) Create(ctx context.Context, req resource.CreateReq
4040
return
4141
}
4242

43-
diags = planModel.populateFromAPI(ctx, policy, sVersion)
43+
diags = planModel.populateFromAPI(policy, sVersion)
4444
resp.Diagnostics.Append(diags...)
4545
if resp.Diagnostics.HasError() {
4646
return

internal/fleet/agent_policy/models.go

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package agent_policy
22

33
import (
4-
"context"
54
"encoding/json"
65
"slices"
76

@@ -13,25 +12,6 @@ import (
1312
"github.com/hashicorp/terraform-plugin-framework/types"
1413
)
1514

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-
// }
34-
3515
type agentPolicyModel struct {
3616
ID types.String `tfsdk:"id"`
3717
PolicyID types.String `tfsdk:"policy_id"`
@@ -49,7 +29,7 @@ type agentPolicyModel struct {
4929
GlobalDataTags types.String `tfsdk:"global_data_tags"`
5030
}
5131

52-
func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy, serverVersion *version.Version) diag.Diagnostics {
32+
func (model *agentPolicyModel) populateFromAPI(data *kbapi.AgentPolicy, serverVersion *version.Version) diag.Diagnostics {
5333
if data == nil {
5434
return nil
5535
}
@@ -81,18 +61,19 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
8161
model.Namespace = types.StringValue(data.Namespace)
8262
if serverVersion.GreaterThanOrEqual(MinVersionGlobalDataTags) && utils.Deref(data.GlobalDataTags) != nil {
8363
diags := diag.Diagnostics{}
84-
d, err := json.Marshal(data.GlobalDataTags)
64+
d, err := json.Marshal(utils.Deref(data.GlobalDataTags))
8565
if err != nil {
8666
diags.AddError("Failed to marshal global data tags", err.Error())
8767
return diags
8868
}
89-
model.GlobalDataTags = types.StringValue(string(d))
69+
strD := string(d)
70+
model.GlobalDataTags = types.StringPointerValue(&strD)
9071
}
9172

9273
return nil
9374
}
9475

95-
func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersion *version.Version) (kbapi.PostFleetAgentPoliciesJSONRequestBody, diag.Diagnostics) {
76+
func (model *agentPolicyModel) toAPICreateModel(serverVersion *version.Version) (kbapi.PostFleetAgentPoliciesJSONRequestBody, diag.Diagnostics) {
9677
monitoring := make([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled, 0, 2)
9778

9879
if model.MonitorLogs.ValueBool() {
@@ -122,22 +103,21 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
122103
}
123104

124105
str := model.GlobalDataTags.ValueStringPointer()
125-
var items []struct {
126-
Name string `json:"name"`
127-
Value kbapi.PostFleetAgentPoliciesJSONBody_GlobalDataTags_Value `json:"value"`
128-
}
106+
var items []kbapi.AgentPolicyGlobalDataTagsItem
129107

130-
err := json.Unmarshal([]byte(utils.Deref(str)), &items)
108+
err := json.Unmarshal([]byte(*str), &items)
131109
if err != nil {
132110
diags.AddError(err.Error(), "")
133111
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
134112
}
135-
*body.GlobalDataTags = items
113+
114+
body.GlobalDataTags = &items
136115
}
116+
137117
return body, nil
138118
}
139119

140-
func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersion *version.Version) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody, diag.Diagnostics) {
120+
func (model *agentPolicyModel) toAPIUpdateModel(serverVersion *version.Version) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody, diag.Diagnostics) {
141121
monitoring := make([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled, 0, 2)
142122
if model.MonitorLogs.ValueBool() {
143123
monitoring = append(monitoring, kbapi.Logs)
@@ -163,17 +143,17 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
163143
diags.AddError("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above")
164144
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
165145
}
146+
166147
str := model.GlobalDataTags.ValueStringPointer()
167-
var items []struct {
168-
Name string `json:"name"`
169-
Value kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBody_GlobalDataTags_Value `json:"value"`
170-
}
171-
err := json.Unmarshal([]byte(utils.Deref(str)), &items)
148+
var items []kbapi.AgentPolicyGlobalDataTagsItem
149+
150+
err := json.Unmarshal([]byte(*str), &items)
172151
if err != nil {
173152
diags.AddError(err.Error(), "")
174153
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
175154
}
176-
*body.GlobalDataTags = items
155+
156+
body.GlobalDataTags = &items
177157
}
178158

179159
return body, nil

internal/fleet/agent_policy/read.go

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

42-
diags = stateModel.populateFromAPI(ctx, policy, sVersion)
42+
diags = stateModel.populateFromAPI(policy, sVersion)
4343
resp.Diagnostics.Append(diags...)
4444
if resp.Diagnostics.HasError() {
4545
return

internal/fleet/agent_policy/resource_test.go

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ func TestAccResourceAgentPolicy(t *testing.T) {
131131
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "true"),
132132
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "false"),
133133
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"),
134-
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag1", "value1"),
135-
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag2", "value2"),
136-
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag3", "value3"),
134+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags", `[{"name":"tag1","value":"value1"},{"name":"tag2","value":1.1}]`),
137135
),
138136
},
139137
{
@@ -146,10 +144,7 @@ func TestAccResourceAgentPolicy(t *testing.T) {
146144
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "false"),
147145
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "true"),
148146
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"),
149-
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag1", "value1a"),
150-
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag2", "value2a"),
151-
resource.TestCheckNoResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag3"),
152-
),
147+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags", `[{"name":"tag1","value":"value1a"}]`)),
153148
},
154149
{
155150
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionGlobalDataTags),
@@ -161,9 +156,7 @@ func TestAccResourceAgentPolicy(t *testing.T) {
161156
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "false"),
162157
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "true"),
163158
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"),
164-
resource.TestCheckNoResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag1"),
165-
resource.TestCheckNoResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag2"),
166-
resource.TestCheckNoResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags.tag3"),
159+
resource.TestCheckNoResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags"),
167160
),
168161
},
169162
},
@@ -231,17 +224,15 @@ resource "elasticstack_fleet_agent_policy" "test_policy" {
231224
monitor_metrics = false
232225
skip_destroy = %t
233226
global_data_tags = jsonencode([
234-
{
235-
name = "tag1"
236-
value = "value1"
237-
},{
238-
name = "tag2"
239-
value = "value2"
240-
},{
241-
name = "tag3"
242-
value = "value3"
243-
}
244-
])
227+
{
228+
name = "tag1"
229+
value = "value1"
230+
},
231+
{
232+
name = "tag2"
233+
value = 1.1
234+
}
235+
])
245236
}
246237
247238
data "elasticstack_fleet_enrollment_tokens" "test_policy" {
@@ -259,21 +250,18 @@ provider "elasticstack" {
259250
}
260251
261252
resource "elasticstack_fleet_agent_policy" "test_policy" {
262-
name = "%s"
263-
namespace = "default"
264-
description = "This policy was updated"
265-
monitor_logs = false
253+
name = "%s"
254+
namespace = "default"
255+
description = "This policy was updated"
256+
monitor_logs = false
266257
monitor_metrics = true
267-
skip_destroy = %t
258+
skip_destroy = %t
268259
global_data_tags = jsonencode([
269-
{
270-
name = "tag1"
271-
value = "value1a"
272-
},{
273-
name = "tag2"
274-
value = "value2a"
275-
}
276-
])
260+
{
261+
name = "tag1"
262+
value = "value1a"
263+
}
264+
])
277265
}
278266
279267
data "elasticstack_fleet_enrollment_tokens" "test_policy" {

internal/fleet/agent_policy/schema.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ func getSchema() schema.Schema {
8787
},
8888
},
8989
"global_data_tags": schema.StringAttribute{
90-
Description: "JSON encoded defined data tags to apply to all inputs.",
90+
Description: "JSON encoded user-defined data tags to apply to all inputs.",
9191
Optional: true,
92+
PlanModifiers: []planmodifier.String{
93+
stringplanmodifier.RequiresReplace(),
94+
},
9295
},
9396
}}
9497
}

internal/fleet/agent_policy/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (r *agentPolicyResource) Update(ctx context.Context, req resource.UpdateReq
2727
return
2828
}
2929

30-
body, diags := planModel.toAPIUpdateModel(ctx, sVersion)
30+
body, diags := planModel.toAPIUpdateModel(sVersion)
3131

3232
resp.Diagnostics.Append(diags...)
3333
if resp.Diagnostics.HasError() {
@@ -41,7 +41,7 @@ func (r *agentPolicyResource) Update(ctx context.Context, req resource.UpdateReq
4141
return
4242
}
4343

44-
planModel.populateFromAPI(ctx, policy, sVersion)
44+
planModel.populateFromAPI(policy, sVersion)
4545

4646
diags = resp.State.Set(ctx, planModel)
4747
resp.Diagnostics.Append(diags...)

0 commit comments

Comments
 (0)