Skip to content

Commit 65463b5

Browse files
committed
fmt
1 parent c0b300e commit 65463b5

File tree

5 files changed

+120
-29
lines changed

5 files changed

+120
-29
lines changed

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(sVersion)
30+
body, diags := planModel.toAPICreateModel(ctx, 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(policy, sVersion)
43+
diags = planModel.populateFromAPI(ctx, policy, sVersion)
4444
resp.Diagnostics.Append(diags...)
4545
if resp.Diagnostics.HasError() {
4646
return

internal/fleet/agent_policy/models.go

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package agent_policy
22

33
import (
4-
"encoding/json"
4+
"context"
55
"slices"
66

77
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
88
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
99

1010
"github.com/hashicorp/go-version"
1111
"github.com/hashicorp/terraform-plugin-framework/diag"
12+
"github.com/hashicorp/terraform-plugin-framework/path"
1213
"github.com/hashicorp/terraform-plugin-framework/types"
1314
)
1415

@@ -26,10 +27,10 @@ type agentPolicyModel struct {
2627
MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
2728
SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
2829
SkipDestroy types.Bool `tfsdk:"skip_destroy"`
29-
GlobalDataTags types.String `tfsdk:"global_data_tags"`
30+
GlobalDataTags types.Map `tfsdk:"global_data_tags"`
3031
}
3132

32-
func (model *agentPolicyModel) populateFromAPI(data *kbapi.AgentPolicy, serverVersion *version.Version) diag.Diagnostics {
33+
func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy, serverVersion *version.Version) diag.Diagnostics {
3334
if data == nil {
3435
return nil
3536
}
@@ -59,21 +60,36 @@ func (model *agentPolicyModel) populateFromAPI(data *kbapi.AgentPolicy, serverVe
5960
model.MonitoringOutputId = types.StringPointerValue(data.MonitoringOutputId)
6061
model.Name = types.StringValue(data.Name)
6162
model.Namespace = types.StringValue(data.Namespace)
62-
if serverVersion.GreaterThanOrEqual(MinVersionGlobalDataTags) && utils.Deref(data.GlobalDataTags) != nil {
63+
if utils.Deref(data.GlobalDataTags) != nil {
6364
diags := diag.Diagnostics{}
64-
d, err := json.Marshal(utils.Deref(data.GlobalDataTags))
65-
if err != nil {
66-
diags.AddError("Failed to marshal global data tags", err.Error())
65+
var map0 = make(map[string]any)
66+
for _, v := range utils.Deref(data.GlobalDataTags) {
67+
maybeFloat, error := v.Value.AsAgentPolicyGlobalDataTagsItemValue1()
68+
if error != nil {
69+
maybeString, error := v.Value.AsAgentPolicyGlobalDataTagsItemValue0()
70+
if error != nil {
71+
diags.AddError("Failed to unmarshal global data tags", error.Error())
72+
}
73+
map0[v.Name] = map[string]string{
74+
"string_value": string(maybeString),
75+
}
76+
} else {
77+
map0[v.Name] = map[string]float32{
78+
"number_value": float32(maybeFloat),
79+
}
80+
}
81+
}
82+
gdt := utils.MapValueFrom(ctx, map0, getGlobalDataTagsAttrType(), path.Root("global_data_tags"), &diags)
83+
if diags.HasError() {
6784
return diags
6885
}
69-
strD := string(d)
70-
model.GlobalDataTags = types.StringPointerValue(&strD)
86+
model.GlobalDataTags = gdt
7187
}
7288

7389
return nil
7490
}
7591

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

7995
if model.MonitorLogs.ValueBool() {
@@ -95,29 +111,50 @@ func (model *agentPolicyModel) toAPICreateModel(serverVersion *version.Version)
95111
Namespace: model.Namespace.ValueString(),
96112
}
97113

98-
if len(model.GlobalDataTags.ValueString()) > 0 {
114+
if len(model.GlobalDataTags.Elements()) > 0 {
99115
var diags diag.Diagnostics
100116
if serverVersion.LessThan(MinVersionGlobalDataTags) {
101117
diags.AddError("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above")
102118
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
103119
}
104120

105-
str := model.GlobalDataTags.ValueStringPointer()
106121
var items []kbapi.AgentPolicyGlobalDataTagsItem
107-
108-
err := json.Unmarshal([]byte(*str), &items)
109-
if err != nil {
110-
diags.AddError(err.Error(), "")
122+
itemsMap := utils.MapTypeAs[struct {
123+
string_value *string
124+
number_value *float32
125+
}](ctx, model.GlobalDataTags, path.Root("global_data_tags"), &diags)
126+
if diags.HasError() {
111127
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
112128
}
129+
for k, v := range itemsMap {
130+
if (v.string_value != nil && v.number_value != nil) || (v.string_value == nil && v.number_value == nil) {
131+
diags.AddError("global_data_tags ES version error", "Global data tags must have exactly one of string_value or number_value")
132+
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
133+
}
134+
var value kbapi.AgentPolicyGlobalDataTagsItem_Value
135+
var err error
136+
if v.string_value != nil {
137+
err = value.FromAgentPolicyGlobalDataTagsItemValue0(*v.string_value)
138+
} else {
139+
err = value.FromAgentPolicyGlobalDataTagsItemValue1(*v.number_value)
140+
}
141+
if err != nil {
142+
diags.AddError("global_data_tags ES version error", "could not convert global data tags value")
143+
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
144+
}
145+
items = append(items, kbapi.AgentPolicyGlobalDataTagsItem{
146+
Name: k,
147+
Value: value,
148+
})
149+
}
113150

114151
body.GlobalDataTags = &items
115152
}
116153

117154
return body, nil
118155
}
119156

120-
func (model *agentPolicyModel) toAPIUpdateModel(serverVersion *version.Version) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody, diag.Diagnostics) {
157+
func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersion *version.Version) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody, diag.Diagnostics) {
121158
monitoring := make([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled, 0, 2)
122159
if model.MonitorLogs.ValueBool() {
123160
monitoring = append(monitoring, kbapi.Logs)
@@ -137,21 +174,43 @@ func (model *agentPolicyModel) toAPIUpdateModel(serverVersion *version.Version)
137174
Namespace: model.Namespace.ValueString(),
138175
}
139176

140-
if len(model.GlobalDataTags.ValueString()) > 0 {
177+
if len(model.GlobalDataTags.Elements()) > 0 {
141178
var diags diag.Diagnostics
142179
if serverVersion.LessThan(MinVersionGlobalDataTags) {
143180
diags.AddError("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above")
144181
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
145182
}
146183

147-
str := model.GlobalDataTags.ValueStringPointer()
148184
var items []kbapi.AgentPolicyGlobalDataTagsItem
149-
150-
err := json.Unmarshal([]byte(*str), &items)
151-
if err != nil {
152-
diags.AddError(err.Error(), "")
185+
itemsMap := utils.MapTypeAs[struct {
186+
string_value *string
187+
number_value *float32
188+
}](ctx, model.GlobalDataTags, path.Root("global_data_tags"), &diags)
189+
if diags.HasError() {
153190
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
154191
}
192+
for k, v := range itemsMap {
193+
if (v.string_value != nil && v.number_value != nil) || (v.string_value == nil && v.number_value == nil) {
194+
diags.AddError("global_data_tags ES version error", "Global data tags must have exactly one of string_value or number_value")
195+
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
196+
}
197+
var value kbapi.AgentPolicyGlobalDataTagsItem_Value
198+
var err error
199+
if v.string_value != nil {
200+
// s := *v.string_value
201+
err = value.FromAgentPolicyGlobalDataTagsItemValue0(*v.string_value)
202+
} else {
203+
err = value.FromAgentPolicyGlobalDataTagsItemValue1(*v.number_value)
204+
}
205+
if err != nil {
206+
diags.AddError("global_data_tags ES version error", "could not convert global data tags value")
207+
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
208+
}
209+
items = append(items, kbapi.AgentPolicyGlobalDataTagsItem{
210+
Name: k,
211+
Value: value,
212+
})
213+
}
155214

156215
body.GlobalDataTags = &items
157216
}

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(policy, sVersion)
42+
diags = stateModel.populateFromAPI(ctx, policy, sVersion)
4343
resp.Diagnostics.Append(diags...)
4444
if resp.Diagnostics.HasError() {
4545
return

internal/fleet/agent_policy/schema.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ package agent_policy
33
import (
44
"context"
55

6+
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
7+
"github.com/hashicorp/terraform-plugin-framework/attr"
8+
"github.com/hashicorp/terraform-plugin-framework/path"
69
"github.com/hashicorp/terraform-plugin-framework/resource"
710
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
811
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
912
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
13+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
1014
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1115
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
16+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1217
)
1318

1419
func (r *agentPolicyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
@@ -86,7 +91,30 @@ func getSchema() schema.Schema {
8691
boolplanmodifier.RequiresReplace(),
8792
},
8893
},
89-
"global_data_tags": schema.StringAttribute{
94+
"global_data_tags": schema.MapNestedAttribute{
95+
Description: "user-defined data tags to apply to all inputs. values can be strings (string_value) or numbers (number_value).",
96+
NestedObject: schema.NestedAttributeObject{
97+
Attributes: map[string]schema.Attribute{
98+
"string_value": schema.StringAttribute{
99+
Description: "Custom label for the field.",
100+
Optional: true,
101+
},
102+
"number_value": schema.Float32Attribute{
103+
Description: "Popularity count for the field.",
104+
Optional: true,
105+
},
106+
},
107+
Validators: []validator.Object{
108+
objectvalidator.ExactlyOneOf(path.MatchRoot("string_value"), path.MatchRoot("number_value")),
109+
},
110+
},
111+
Optional: true,
112+
PlanModifiers: []planmodifier.Map{
113+
mapplanmodifier.RequiresReplace(),
114+
},
115+
},
116+
117+
"global_data_tags_old": schema.StringAttribute{
90118
Description: "JSON encoded user-defined data tags to apply to all inputs.",
91119
Optional: true,
92120
PlanModifiers: []planmodifier.String{
@@ -95,3 +123,7 @@ func getSchema() schema.Schema {
95123
},
96124
}}
97125
}
126+
127+
func getGlobalDataTagsAttrType() attr.Type {
128+
return getSchema().Attributes["global_data_tags"].GetType()
129+
}

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(sVersion)
30+
body, diags := planModel.toAPIUpdateModel(ctx, 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(policy, sVersion)
44+
planModel.populateFromAPI(ctx, policy, sVersion)
4545

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

0 commit comments

Comments
 (0)