Skip to content

Commit 7f3aed7

Browse files
committed
minimum version
1 parent 8764537 commit 7f3aed7

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

internal/fleet/agent_policy/create.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ func (r *agentPolicyResource) Create(ctx context.Context, req resource.CreateReq
2222
return
2323
}
2424

25-
body := planModel.toAPICreateModel(ctx)
25+
sVersion, e := r.client.ServerVersion(ctx)
26+
if e != nil {
27+
return
28+
}
29+
30+
body, diags := planModel.toAPICreateModel(ctx, sVersion)
31+
resp.Diagnostics.Append(diags...)
32+
if resp.Diagnostics.HasError() {
33+
return
34+
}
2635

2736
sysMonitoring := planModel.SysMonitoring.ValueBool()
2837
policy, diags := fleet.CreateAgentPolicy(ctx, client, body, sysMonitoring)

internal/fleet/agent_policy/models.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,8 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
8787
return
8888
}
8989

90-
func (model agentPolicyModel) toAPICreateModel(ctx context.Context) kbapi.PostFleetAgentPoliciesJSONRequestBody {
90+
func (model agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersion *version.Version) (kbapi.PostFleetAgentPoliciesJSONRequestBody, diag.Diagnostics) {
9191
monitoring := make([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled, 0, 2)
92-
tags := make([]struct {
93-
Name string `json:"name"`
94-
Value kbapi.PostFleetAgentPoliciesJSONBody_GlobalDataTags_Value `json:"value"`
95-
}, 0, len(model.GlobalDataTags.ElementsAs(ctx, getGlobalDataTagsType, false)))
9692

9793
if model.MonitorLogs.ValueBool() {
9894
monitoring = append(monitoring, kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabledLogs)
@@ -113,14 +109,23 @@ func (model agentPolicyModel) toAPICreateModel(ctx context.Context) kbapi.PostFl
113109
Namespace: model.Namespace.ValueString(),
114110
}
115111

116-
if len(tags) > 0 {
117-
body.GlobalDataTags = &tags
112+
if len(model.GlobalDataTags.Elements()) > 0 {
113+
if serverVersion.LessThan(MinVersionGlobalDataTags) {
114+
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diag.Diagnostics{
115+
diag.NewErrorDiagnostic("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above"),
116+
}
117+
118+
}
119+
diags := model.GlobalDataTags.ElementsAs(ctx, body.GlobalDataTags, false)
120+
if diags.HasError() {
121+
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
122+
}
118123
}
119124

120-
return body
125+
return body, nil
121126
}
122127

123-
func (model agentPolicyModel) toAPIUpdateModel(ctx context.Context) kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {
128+
func (model agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersion *version.Version) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody, diag.Diagnostics) {
124129
monitoring := make([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled, 0, 2)
125130
if model.MonitorLogs.ValueBool() {
126131
monitoring = append(monitoring, kbapi.Logs)
@@ -129,11 +134,6 @@ func (model agentPolicyModel) toAPIUpdateModel(ctx context.Context) kbapi.PutFle
129134
monitoring = append(monitoring, kbapi.Metrics)
130135
}
131136

132-
tags := make([]struct {
133-
Name string `json:"name"`
134-
Value kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBody_GlobalDataTags_Value `json:"value"`
135-
}, 0, len(model.GlobalDataTags.ElementsAs(ctx, getGlobalDataTagsType, false)))
136-
137137
body := kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{
138138
DataOutputId: model.DataOutputId.ValueStringPointer(),
139139
Description: model.Description.ValueStringPointer(),
@@ -145,9 +145,18 @@ func (model agentPolicyModel) toAPIUpdateModel(ctx context.Context) kbapi.PutFle
145145
Namespace: model.Namespace.ValueString(),
146146
}
147147

148-
if len(tags) > 0 {
149-
body.GlobalDataTags = &tags
148+
if len(model.GlobalDataTags.Elements()) > 0 {
149+
if serverVersion.LessThan(MinVersionGlobalDataTags) {
150+
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diag.Diagnostics{
151+
diag.NewErrorDiagnostic("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above"),
152+
}
153+
154+
}
155+
diags := model.GlobalDataTags.ElementsAs(ctx, body.GlobalDataTags, false)
156+
if diags.HasError() {
157+
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
158+
}
150159
}
151160

152-
return body
161+
return body, nil
153162
}

internal/fleet/agent_policy/resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
8+
"github.com/hashicorp/go-version"
89
"github.com/hashicorp/terraform-plugin-framework/path"
910
"github.com/hashicorp/terraform-plugin-framework/resource"
1011
)
@@ -15,6 +16,8 @@ var (
1516
_ resource.ResourceWithImportState = &agentPolicyResource{}
1617
)
1718

19+
var MinVersionGlobalDataTags = version.Must(version.NewVersion("8.15.0"))
20+
1821
// NewResource is a helper function to simplify the provider implementation.
1922
func NewResource() resource.Resource {
2023
return &agentPolicyResource{}

internal/fleet/agent_policy/update.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ func (r *agentPolicyResource) Update(ctx context.Context, req resource.UpdateReq
2222
return
2323
}
2424

25-
body := planModel.toAPIUpdateModel(ctx)
25+
sVersion, e := r.client.ServerVersion(ctx)
26+
if e != nil {
27+
return
28+
}
29+
30+
body, diags := planModel.toAPIUpdateModel(ctx, sVersion)
31+
32+
resp.Diagnostics.Append(diags...)
33+
if resp.Diagnostics.HasError() {
34+
return
35+
}
2636

2737
policyID := planModel.PolicyID.ValueString()
2838
policy, diags := fleet.UpdateAgentPolicy(ctx, client, policyID, body)

0 commit comments

Comments
 (0)