Skip to content

Commit 85d0793

Browse files
committed
requested changes
1 parent b32eeed commit 85d0793

File tree

6 files changed

+51
-22
lines changed

6 files changed

+51
-22
lines changed

internal/fleet/agent_policy/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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(ctx, policy)
4444
resp.Diagnostics.Append(diags...)
4545
if resp.Diagnostics.HasError() {
4646
return

internal/fleet/agent_policy/models.go

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

33
import (
44
"context"
5+
"fmt"
56
"slices"
67

78
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
@@ -36,7 +37,7 @@ type agentPolicyModel struct {
3637
GlobalDataTags types.Map `tfsdk:"global_data_tags"` //> globalDataTagsModel
3738
}
3839

39-
func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy, serverVersion *version.Version) diag.Diagnostics {
40+
func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy) diag.Diagnostics {
4041
if data == nil {
4142
return nil
4243
}
@@ -121,18 +122,12 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
121122
if len(model.GlobalDataTags.Elements()) > 0 {
122123
var diags diag.Diagnostics
123124
if serverVersion.LessThan(MinVersionGlobalDataTags) {
124-
diags.AddError("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above")
125+
diags.AddError("global_data_tags ES version error", fmt.Sprintf("Global data tags are only supported in Elastic Stack %s and above", MinVersionGlobalDataTags))
125126
return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diags
126127
}
127128

128129
items := utils.MapTypeToMap(ctx, model.GlobalDataTags, path.Root("global_data_tags"), &diags,
129130
func(item globalDataTagsItemModel, meta utils.MapMeta) kbapi.AgentPolicyGlobalDataTagsItem {
130-
// do some checks
131-
if item.StringValue.ValueStringPointer() == nil && item.NumberValue.ValueFloat32Pointer() == nil || item.StringValue.ValueStringPointer() != nil && item.NumberValue.ValueFloat32Pointer() != nil {
132-
diags.AddError("global_data_tags validation_error", "Global data tags must have exactly one of string_value or number_value")
133-
return kbapi.AgentPolicyGlobalDataTagsItem{}
134-
}
135-
136131
var value kbapi.AgentPolicyGlobalDataTagsItem_Value
137132
var err error
138133
if item.StringValue.ValueStringPointer() != nil {
@@ -187,17 +182,12 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
187182
if len(model.GlobalDataTags.Elements()) > 0 {
188183
var diags diag.Diagnostics
189184
if serverVersion.LessThan(MinVersionGlobalDataTags) {
190-
diags.AddError("global_data_tags ES version error", "Global data tags are only supported in Elastic Stack 8.15.0 and above")
185+
diags.AddError("global_data_tags ES version error", fmt.Sprintf("Global data tags are only supported in Elastic Stack %s and above", MinVersionGlobalDataTags))
191186
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diags
192187
}
193188

194189
items := utils.MapTypeToMap(ctx, model.GlobalDataTags, path.Root("global_data_tags"), &diags,
195190
func(item globalDataTagsItemModel, meta utils.MapMeta) kbapi.AgentPolicyGlobalDataTagsItem {
196-
// do some checks
197-
if item.StringValue.ValueStringPointer() == nil && item.NumberValue.ValueFloat32Pointer() == nil || item.StringValue.ValueStringPointer() != nil && item.NumberValue.ValueFloat32Pointer() != nil {
198-
diags.AddError("global_data_tags validation_error", "Global data tags must have exactly one of string_value or number_value")
199-
return kbapi.AgentPolicyGlobalDataTagsItem{}
200-
}
201191

202192
var value kbapi.AgentPolicyGlobalDataTagsItem_Value
203193
var err error

internal/fleet/agent_policy/read.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ func (r *agentPolicyResource) Read(ctx context.Context, req resource.ReadRequest
2222
return
2323
}
2424

25-
sVersion, e := r.client.ServerVersion(ctx)
26-
if e != nil {
27-
return
28-
}
29-
3025
policyID := stateModel.PolicyID.ValueString()
3126
policy, diags := fleet.GetAgentPolicy(ctx, client, policyID)
3227
resp.Diagnostics.Append(diags...)
@@ -39,7 +34,7 @@ func (r *agentPolicyResource) Read(ctx context.Context, req resource.ReadRequest
3934
return
4035
}
4136

42-
diags = stateModel.populateFromAPI(ctx, policy, sVersion)
37+
diags = stateModel.populateFromAPI(ctx, policy)
4338
resp.Diagnostics.Append(diags...)
4439
if resp.Diagnostics.HasError() {
4540
return

internal/fleet/agent_policy/resource_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"regexp"
78
"testing"
89

910
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
@@ -161,6 +162,11 @@ func TestAccResourceAgentPolicy(t *testing.T) {
161162
resource.TestCheckNoResourceAttr("elasticstack_fleet_agent_policy.test_policy", "global_data_tags"),
162163
),
163164
},
165+
{
166+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionGlobalDataTags),
167+
Config: testAccResourceAgentPolicyUpdateWithBadGlobalDataTags(policyNameGlobalDataTags, false),
168+
ExpectError: regexp.MustCompile("conflicts with"),
169+
},
164170
},
165171
})
166172
}
@@ -269,6 +275,34 @@ data "elasticstack_fleet_enrollment_tokens" "test_policy" {
269275
`, fmt.Sprintf("Updated Policy %s", id), skipDestroy)
270276
}
271277

278+
func testAccResourceAgentPolicyUpdateWithBadGlobalDataTags(id string, skipDestroy bool) string {
279+
return fmt.Sprintf(`
280+
provider "elasticstack" {
281+
elasticsearch {}
282+
kibana {}
283+
}
284+
285+
resource "elasticstack_fleet_agent_policy" "test_policy" {
286+
name = "%s"
287+
namespace = "default"
288+
description = "This policy was updated"
289+
monitor_logs = false
290+
monitor_metrics = true
291+
skip_destroy = %t
292+
global_data_tags = {
293+
tag1 = {
294+
string_value = "value1a"
295+
number_value = 1.2
296+
}
297+
}
298+
}
299+
300+
data "elasticstack_fleet_enrollment_tokens" "test_policy" {
301+
policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
302+
}
303+
`, fmt.Sprintf("Updated Policy %s", id), skipDestroy)
304+
}
305+
272306
func testAccResourceAgentPolicyUpdateWithNoGlobalDataTags(id string, skipDestroy bool) string {
273307
return fmt.Sprintf(`
274308
provider "elasticstack" {

internal/fleet/agent_policy/schema.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ package agent_policy
33
import (
44
"context"
55

6+
"github.com/hashicorp/terraform-plugin-framework-validators/float32validator"
7+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
68
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-framework/path"
710
"github.com/hashicorp/terraform-plugin-framework/resource"
811
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
912
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
1013
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
1114
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
1215
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1316
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
17+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1418
)
1519

1620
func (r *agentPolicyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
@@ -95,10 +99,16 @@ func getSchema() schema.Schema {
9599
"string_value": schema.StringAttribute{
96100
Description: "String value for the field. If this is set, number_value must not be defined.",
97101
Optional: true,
102+
Validators: []validator.String{
103+
stringvalidator.ConflictsWith(path.MatchRelative().AtName("number_value")),
104+
},
98105
},
99106
"number_value": schema.Float32Attribute{
100107
Description: "Number value for the field. If this is set, string_value must not be defined.",
101108
Optional: true,
109+
Validators: []validator.Float32{
110+
float32validator.ConflictsWith(path.MatchRelative().AtName("string_value")),
111+
},
102112
},
103113
},
104114
},

internal/fleet/agent_policy/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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(ctx, policy)
4545

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

0 commit comments

Comments
 (0)