Skip to content

Commit 5942359

Browse files
committed
Ignore disabled inputs/streams from plan diff
1 parent be66c97 commit 5942359

File tree

13 files changed

+905
-19
lines changed

13 files changed

+905
-19
lines changed

internal/fleet/integration_policy/acc_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,75 @@ func TestAccResourceIntegrationPolicySecrets(t *testing.T) {
333333
})
334334
}
335335

336+
func TestAccIntegrationPolicyInputs(t *testing.T) {
337+
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
338+
339+
resource.Test(t, resource.TestCase{
340+
PreCheck: func() { acctest.PreCheck(t) },
341+
CheckDestroy: checkResourceIntegrationPolicyDestroy,
342+
Steps: []resource.TestStep{
343+
{
344+
ProtoV6ProviderFactories: acctest.Providers,
345+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegrationPolicy),
346+
ConfigDirectory: acctest.NamedTestCaseDirectory("create"),
347+
ConfigVariables: config.Variables{
348+
"policy_name": config.StringVariable(policyName),
349+
},
350+
Check: resource.ComposeTestCheckFunc(
351+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "name", policyName),
352+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "description", "Kafka Integration Policy"),
353+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "integration_name", "kafka"),
354+
// Check enabled inputs
355+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-logfile.enabled", "true"),
356+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.enabled", "true"),
357+
// Check enabled streams
358+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-logfile.streams.kafka.log.enabled", "true"),
359+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.streams.kafka.broker.enabled", "true"),
360+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.streams.kafka.consumergroup.enabled", "true"),
361+
// Check disabled stream
362+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.streams.kafka.partition.enabled", "false"),
363+
// Check vars
364+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.vars", `{"hosts":["localhost:9092"],"period":"10s","ssl.certificate_authorities":[]}`),
365+
// Check unspecified, disabled by default input
366+
resource.TestCheckNoResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-jolokia/metrics"),
367+
),
368+
},
369+
{
370+
ProtoV6ProviderFactories: acctest.Providers,
371+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegrationPolicy),
372+
ConfigDirectory: acctest.NamedTestCaseDirectory("update_disabled_input"),
373+
ConfigVariables: config.Variables{
374+
"policy_name": config.StringVariable(policyName),
375+
},
376+
Check: resource.ComposeTestCheckFunc(
377+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "name", policyName),
378+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "description", "Kafka Integration Policy - Updated"),
379+
// Check that disabling an input works correctly
380+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-logfile.enabled", "false"),
381+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.enabled", "true"),
382+
// Vars should remain the same since we didn't change them
383+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.vars", `{"hosts":["localhost:9092"],"period":"10s","ssl.certificate_authorities":[]}`),
384+
),
385+
},
386+
{
387+
ProtoV6ProviderFactories: acctest.Providers,
388+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegrationPolicy),
389+
ConfigDirectory: acctest.NamedTestCaseDirectory("update_enabled_input"),
390+
ConfigVariables: config.Variables{
391+
"policy_name": config.StringVariable(policyName),
392+
},
393+
Check: resource.ComposeTestCheckFunc(
394+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "name", policyName),
395+
// Check that updating an enabled input's vars triggers a change
396+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-kafka/metrics.streams.kafka.consumergroup.vars", `{"topics":["don't mention the war, I mentioned it once but I think I got away with it"]}`),
397+
// Disabled input should remain disabled
398+
resource.TestCheckResourceAttr("elasticstack_fleet_integration_policy.test_policy", "inputs.kafka-logfile.enabled", "false"),
399+
),
400+
},
401+
},
402+
})
403+
}
404+
336405
func checkResourceIntegrationPolicyDestroy(s *terraform.State) error {
337406
client, err := clients.NewAcceptanceTestingClient()
338407
if err != nil {

internal/fleet/integration_policy/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *integrationPolicyResource) Create(ctx context.Context, req resource.Cre
7676
// If plan didn't have input configured, ensure we don't add it now
7777
// This prevents "Provider produced inconsistent result" errors
7878
if !planHadInput {
79-
planModel.Inputs = types.MapNull(getInputsTypes())
79+
planModel.Inputs = NewInputsNull(getInputsElementType())
8080
}
8181

8282
diags = resp.State.Set(ctx, planModel)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package integration_policy
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/attr"
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
9+
"github.com/hashicorp/terraform-plugin-go/tftypes"
10+
)
11+
12+
var (
13+
_ basetypes.MapTypable = (*InputsType)(nil)
14+
_ basetypes.MapValuableWithSemanticEquals = (*InputsValue)(nil)
15+
)
16+
17+
// InputsType is a custom type for the inputs map that supports semantic equality
18+
type InputsType struct {
19+
basetypes.MapType
20+
}
21+
22+
// String returns a human readable string of the type name.
23+
func (t InputsType) String() string {
24+
return "integration_policy.InputsType"
25+
}
26+
27+
// ValueType returns the Value type.
28+
func (t InputsType) ValueType(ctx context.Context) attr.Value {
29+
return InputsValue{
30+
MapValue: basetypes.NewMapUnknown(t.ElementType()),
31+
}
32+
}
33+
34+
// Equal returns true if the given type is equivalent.
35+
func (t InputsType) Equal(o attr.Type) bool {
36+
other, ok := o.(InputsType)
37+
if !ok {
38+
return false
39+
}
40+
return t.MapType.Equal(other.MapType)
41+
}
42+
43+
// ValueFromMap returns a MapValuable type given a basetypes.MapValue.
44+
func (t InputsType) ValueFromMap(ctx context.Context, in basetypes.MapValue) (basetypes.MapValuable, diag.Diagnostics) {
45+
return InputsValue{
46+
MapValue: in,
47+
}, nil
48+
}
49+
50+
// ValueFromTerraform returns a Value given a tftypes.Value.
51+
func (t InputsType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
52+
attrValue, err := t.MapType.ValueFromTerraform(ctx, in)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
mapValue, ok := attrValue.(basetypes.MapValue)
58+
if !ok {
59+
return nil, err
60+
}
61+
62+
return InputsValue{
63+
MapValue: mapValue,
64+
}, nil
65+
}
66+
67+
// NewInputsType creates a new InputsType with the given element type
68+
func NewInputsType(elemType attr.Type) InputsType {
69+
return InputsType{
70+
MapType: basetypes.MapType{
71+
ElemType: elemType,
72+
},
73+
}
74+
}

0 commit comments

Comments
 (0)