Skip to content

Commit 4b9cf76

Browse files
committed
fix tests
1 parent a7adf49 commit 4b9cf76

File tree

2 files changed

+34
-56
lines changed

2 files changed

+34
-56
lines changed

internal/fleet/integration_policy/models.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,35 @@ func (model integrationPolicyModel) toAPIModel(ctx context.Context, isUpdate boo
125125
Name: model.IntegrationName.ValueString(),
126126
Version: model.IntegrationVersion.ValueString(),
127127
},
128-
PolicyId: model.AgentPolicyID.ValueStringPointer(),
129-
PolicyIds: func() *[]string {
130-
if !model.AgentPolicyIDs.IsNull() && !model.AgentPolicyIDs.IsUnknown() {
131-
var policyIDs []string
132-
d := model.AgentPolicyIDs.ElementsAs(ctx, &policyIDs, false)
133-
diags.Append(d...)
134-
return &policyIDs
135-
}
136-
// Return empty array instead of nil when agent_policy_ids is not defined
137-
emptyArray := []string{}
138-
return &emptyArray
139-
}(),
140128
Vars: utils.MapRef(utils.NormalizedTypeToMap[any](model.VarsJson, path.Root("vars_json"), &diags)),
141129
}
142130

131+
// Handle agent policy ID vs IDs - only set one field, not both
132+
hasAgentPolicyIDs := !model.AgentPolicyIDs.IsNull() && !model.AgentPolicyIDs.IsUnknown()
133+
hasAgentPolicyID := !model.AgentPolicyID.IsNull() && !model.AgentPolicyID.IsUnknown()
134+
135+
if hasAgentPolicyIDs {
136+
var policyIDs []string
137+
d := model.AgentPolicyIDs.ElementsAs(ctx, &policyIDs, false)
138+
diags.Append(d...)
139+
body.PolicyIds = &policyIDs
140+
} else if hasAgentPolicyID {
141+
body.PolicyId = model.AgentPolicyID.ValueStringPointer()
142+
}
143+
144+
// Ensure at least one policy ID is provided
145+
if !hasAgentPolicyIDs && !hasAgentPolicyID {
146+
diags.AddError(
147+
"Missing Agent Policy",
148+
"Either agent_policy_id or agent_policy_ids must be provided",
149+
)
150+
} else if hasAgentPolicyIDs && body.PolicyIds != nil && len(*body.PolicyIds) == 0 {
151+
diags.AddError(
152+
"Empty Agent Policy IDs",
153+
"agent_policy_ids cannot be empty when provided",
154+
)
155+
}
156+
143157
if isUpdate {
144158
body.Id = model.ID.ValueStringPointer()
145159
}

internal/fleet/integration_policy/resource_test.go

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
)
2222

2323
var minVersionIntegrationPolicy = version.Must(version.NewVersion("8.10.0"))
24+
var minVersionPolicyIds = version.Must(version.NewVersion("8.16.0"))
2425

2526
func TestAccResourceIntegrationPolicyMultipleAgentPolicies(t *testing.T) {
2627
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
@@ -31,7 +32,7 @@ func TestAccResourceIntegrationPolicyMultipleAgentPolicies(t *testing.T) {
3132
ProtoV6ProviderFactories: acctest.Providers,
3233
Steps: []resource.TestStep{
3334
{
34-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegrationPolicy),
35+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionPolicyIds),
3536
Config: testAccResourceIntegrationPolicyCreateMultipleAgentPolicies(policyName),
3637
Check: resource.ComposeTestCheckFunc(
3738
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "name", policyName),
@@ -45,22 +46,6 @@ func TestAccResourceIntegrationPolicyMultipleAgentPolicies(t *testing.T) {
4546
})
4647
}
4748

48-
func TestAccResourceIntegrationPolicyValidation(t *testing.T) {
49-
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
50-
51-
resource.Test(t, resource.TestCase{
52-
PreCheck: func() { acctest.PreCheck(t) },
53-
ProtoV6ProviderFactories: acctest.Providers,
54-
Steps: []resource.TestStep{
55-
{
56-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegrationPolicy),
57-
Config: testAccResourceIntegrationPolicyCreateWithNoAgentPolicyFields(policyName),
58-
ExpectError: regexp.MustCompile("Either 'agent_policy_id' or 'agent_policy_ids' must be provided"),
59-
},
60-
},
61-
})
62-
}
63-
6449
func TestAccResourceIntegrationPolicyBothAgentPolicyFields(t *testing.T) {
6550
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
6651

@@ -70,7 +55,7 @@ func TestAccResourceIntegrationPolicyBothAgentPolicyFields(t *testing.T) {
7055
ProtoV6ProviderFactories: acctest.Providers,
7156
Steps: []resource.TestStep{
7257
{
73-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegrationPolicy),
58+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionPolicyIds),
7459
Config: testAccResourceIntegrationPolicyCreateWithBothAgentPolicyFields(policyName),
7560
Check: resource.ComposeTestCheckFunc(
7661
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "name", policyName),
@@ -554,13 +539,17 @@ resource "elasticstack_fleet_integration_policy" "test_policy" {
554539
integration_version = elasticstack_fleet_integration.test_policy.version
555540
input {
556541
input_id = "tcp-tcp"
557-
enabled = true
558542
streams_json = jsonencode({
559543
"tcp.generic": {
560544
"enabled": true
561545
"vars": {
562546
"listen_address": "localhost"
563547
"listen_port": 8080
548+
"data_stream.dataset": "tcp.generic"
549+
"tags": []
550+
"syslog_options": "field: message"
551+
"ssl": ""
552+
"custom": ""
564553
}
565554
}
566555
})
@@ -603,28 +592,3 @@ resource "elasticstack_fleet_integration_policy" "test_policy" {
603592
}
604593
`, id, id)
605594
}
606-
607-
func testAccResourceIntegrationPolicyCreateWithNoAgentPolicyFields(id string) string {
608-
return fmt.Sprintf(`
609-
provider "elasticstack" {
610-
elasticsearch {}
611-
kibana {}
612-
}
613-
resource "elasticstack_fleet_integration" "test_policy" {
614-
name = "tcp"
615-
version = "1.16.0"
616-
force = true
617-
}
618-
resource "elasticstack_fleet_integration_policy" "test_policy" {
619-
name = "%s"
620-
namespace = "default"
621-
description = "IntegrationPolicyTest Policy"
622-
integration_name = elasticstack_fleet_integration.test_policy.name
623-
integration_version = elasticstack_fleet_integration.test_policy.version
624-
input {
625-
input_id = "tcp-tcp"
626-
enabled = true
627-
}
628-
}
629-
`, id)
630-
}

0 commit comments

Comments
 (0)