Skip to content

Commit 6015daa

Browse files
authored
Do not suppress diff if it is explicitly changed to zero (#3611)
1 parent e1aec3b commit 6015daa

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

clusters/resource_cluster_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,3 +1691,81 @@ func TestRefreshOnRunningClusterWithFailedLibraryUninstallsIt(t *testing.T) {
16911691
ID: "foo",
16921692
}.ApplyNoError(t)
16931693
}
1694+
1695+
func TestResourceClusterUpdate_LocalSsdCount(t *testing.T) {
1696+
_, err := qa.ResourceFixture{
1697+
Fixtures: []qa.HTTPFixture{
1698+
{
1699+
Method: "GET",
1700+
Resource: "/api/2.0/clusters/get?cluster_id=abc",
1701+
ReuseRequest: true,
1702+
Response: compute.ClusterDetails{
1703+
ClusterId: "abc",
1704+
NumWorkers: 100,
1705+
ClusterName: "Non Autoscaling Cluster",
1706+
SparkVersion: "7.1-scala12",
1707+
NodeTypeId: "i3.xlarge",
1708+
AutoterminationMinutes: 15,
1709+
State: compute.StateTerminated,
1710+
GcpAttributes: &compute.GcpAttributes{
1711+
LocalSsdCount: 2,
1712+
},
1713+
},
1714+
},
1715+
{
1716+
Method: "POST",
1717+
Resource: "/api/2.0/clusters/events",
1718+
ExpectedRequest: compute.GetEvents{
1719+
ClusterId: "abc",
1720+
Limit: 1,
1721+
Order: compute.GetEventsOrderDesc,
1722+
EventTypes: []compute.EventType{compute.EventTypePinned, compute.EventTypeUnpinned},
1723+
},
1724+
Response: compute.GetEventsResponse{
1725+
Events: []compute.ClusterEvent{},
1726+
TotalCount: 0,
1727+
},
1728+
},
1729+
{
1730+
Method: "POST",
1731+
Resource: "/api/2.0/clusters/edit",
1732+
ExpectedRequest: compute.ClusterDetails{
1733+
AutoterminationMinutes: 15,
1734+
ClusterId: "abc",
1735+
NumWorkers: 100,
1736+
ClusterName: "Non Autoscaling Cluster",
1737+
SparkVersion: "7.1-scala12",
1738+
NodeTypeId: "i3.xlarge",
1739+
GcpAttributes: &compute.GcpAttributes{
1740+
LocalSsdCount: 0,
1741+
},
1742+
},
1743+
},
1744+
},
1745+
ID: "abc",
1746+
Update: true,
1747+
Resource: ResourceCluster(),
1748+
InstanceState: map[string]string{
1749+
"autotermination_minutes": "15",
1750+
"cluster_name": "Shared Autoscaling",
1751+
"spark_version": "7.1-scala12",
1752+
"node_type_id": "i3.xlarge",
1753+
"num_workers": "100",
1754+
"gcp_attributes": `"{
1755+
local_ssd_count = 2
1756+
}"`,
1757+
},
1758+
HCL: `
1759+
autotermination_minutes = 15,
1760+
cluster_name = "Non Autoscaling Cluster"
1761+
spark_version = "7.1-scala12"
1762+
node_type_id = "i3.xlarge"
1763+
num_workers = 100
1764+
gcp_attributes = {
1765+
local_ssd_count = 0
1766+
},
1767+
`,
1768+
}.Apply(t)
1769+
1770+
assert.NoError(t, err)
1771+
}

common/reflect_resource.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ func chooseFieldName(typeField reflect.StructField) string {
366366
func diffSuppressor(fieldName string, v *schema.Schema) func(k, old, new string, d *schema.ResourceData) bool {
367367
zero := fmt.Sprintf("%v", v.Type.Zero())
368368
return func(k, old, new string, d *schema.ResourceData) bool {
369-
if new == zero && old != zero {
369+
// HasChange allows to check if the field is explicitly changed in the configuration.
370+
// If the field is explicitly set in the configuration, we should not suppress the diff.
371+
if new == zero && old != zero && !d.HasChange(k) {
370372
log.Printf("[DEBUG] Suppressing diff for %v: platform=%#v config=%#v", k, old, new)
371373
return true
372374
}

common/reflect_resource_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,36 @@ func TestDiffSuppressor(t *testing.T) {
672672
assert.True(t, dsf("", "old", "", d))
673673
}
674674

675+
func TestDiffSuppressorWhenNumberExplicitlyChangedToZero(t *testing.T) {
676+
intSchema := &schema.Schema{
677+
Type: schema.TypeInt,
678+
}
679+
dsf := diffSuppressor("foo", intSchema)
680+
noChange := schema.TestResourceDataRaw(t, map[string]*schema.Schema{
681+
"foo": {
682+
Type: schema.TypeInt,
683+
Optional: true,
684+
},
685+
}, map[string]any{})
686+
// no suppress
687+
assert.False(t, dsf("foo", "1", "2", noChange))
688+
// suppress
689+
assert.True(t, dsf("foo", "1", "0", noChange))
690+
691+
change := schema.TestResourceDataRaw(t, map[string]*schema.Schema{
692+
"foo": {
693+
Type: schema.TypeInt,
694+
Optional: true,
695+
},
696+
}, map[string]any{
697+
"foo": 1,
698+
})
699+
700+
// no suppress
701+
assert.False(t, dsf("foo", "1", "2", change))
702+
assert.False(t, dsf("foo", "1", "0", change))
703+
}
704+
675705
func TestTypeToSchemaNoStruct(t *testing.T) {
676706
defer func() {
677707
p := recover()

0 commit comments

Comments
 (0)