Skip to content

Commit 6a7391d

Browse files
Add CustomDiffFunc for health in sql_endpoint resources (#3227)
* update * update * update * update * update * update * update * update * update * update
1 parent df57a8a commit 6a7391d

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

qa/testing.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ type ResourceFixture struct {
104104
CommandMock common.CommandMock
105105

106106
// Set one of them to true to test the corresponding CRUD function for the
107-
// terraform resource.
108-
Create bool
109-
Read bool
110-
Update bool
111-
Delete bool
107+
// terraform resource. Or set ExpectedDiff to skip execution and only test
108+
// that the diff is expected.
109+
Create bool
110+
Read bool
111+
Update bool
112+
Delete bool
113+
ExpectedDiff map[string]*terraform.ResourceAttrDiff
112114

113115
Removed bool
114116
ID string
@@ -171,8 +173,10 @@ func (f ResourceFixture) prepareExecution(r *schema.Resource) (resourceCRUD, err
171173
return nil, fmt.Errorf("ID must be set for Delete")
172174
}
173175
return resourceCRUD(r.DeleteContext).withId(f.ID), nil
176+
case f.ExpectedDiff != nil:
177+
return nil, nil
174178
}
175-
return nil, fmt.Errorf("no `Create|Read|Update|Delete: true` specificed")
179+
return nil, fmt.Errorf("no `Create|Read|Update|Delete: true` or `ExpectedDiff` specified")
176180
}
177181

178182
func (f ResourceFixture) setDatabricksEnvironmentForTest(client *common.DatabricksClient, host string) {
@@ -304,6 +308,10 @@ func (f ResourceFixture) Apply(t *testing.T) (*schema.ResourceData, error) {
304308
}
305309
ctx := context.Background()
306310
diff, err := resource.Diff(ctx, is, resourceConfig, client)
311+
if f.ExpectedDiff != nil {
312+
assert.Equal(t, f.ExpectedDiff, diff.Attributes)
313+
return nil, err
314+
}
307315
// TODO: f.Resource.Data(is) - check why it doesn't work
308316
if err != nil {
309317
return nil, err

qa/testing_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var noopContextResource = common.Resource{
106106

107107
func TestResourceFixture_ID(t *testing.T) {
108108
_, err := ResourceFixture{}.prepareExecution(nil)
109-
assert.EqualError(t, err, "no `Create|Read|Update|Delete: true` specificed")
109+
assert.EqualError(t, err, "no `Create|Read|Update|Delete: true` or `ExpectedDiff` specified")
110110

111111
f := ResourceFixture{
112112
Resource: noopResource,

sql/resource_sql_endpoint.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,8 @@ func ResourceSqlEndpoint() common.Resource {
145145
return w.Warehouses.DeleteById(ctx, d.Id())
146146
},
147147
Schema: s,
148+
CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff) error {
149+
return d.Clear("health")
150+
},
148151
}
149152
}

sql/resource_sql_endpoint_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/databricks/databricks-sdk-go/service/sql"
1212
"github.com/databricks/terraform-provider-databricks/common"
1313
"github.com/databricks/terraform-provider-databricks/qa"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1415

1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/mock"
@@ -239,6 +240,37 @@ func TestResourceSQLEndpointUpdate(t *testing.T) {
239240
assert.Equal(t, "d7c9d05c-7496-4c69-b089-48823edad40c", d.Get("data_source_id"))
240241
}
241242

243+
// Testing the customizeDiff on clearing "health" diff is working as expected.
244+
func TestResourceSQLEndpointUpdateHealthNoDiff(t *testing.T) {
245+
qa.ResourceFixture{
246+
Resource: ResourceSqlEndpoint(),
247+
ID: "abc",
248+
InstanceState: map[string]string{
249+
"name": "foo",
250+
"cluster_size": "Small",
251+
"auto_stop_mins": "120",
252+
"enable_photon": "true",
253+
"max_num_clusters": "1",
254+
"spot_instance_policy": "COST_OPTIMIZED",
255+
},
256+
ExpectedDiff: map[string]*terraform.ResourceAttrDiff{
257+
"state": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
258+
"odbc_params.#": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
259+
"num_clusters": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
260+
"num_active_sessions": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
261+
"jdbc_url": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
262+
"id": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
263+
"enable_serverless_compute": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
264+
"data_source_id": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
265+
"creator_name": {Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false},
266+
},
267+
HCL: `
268+
name = "foo"
269+
cluster_size = "Small"
270+
`,
271+
}.ApplyNoError(t)
272+
}
273+
242274
func TestResourceSQLEndpointDelete(t *testing.T) {
243275
d, err := qa.ResourceFixture{
244276
MockWorkspaceClientFunc: func(mwc *mocks.MockWorkspaceClient) {

0 commit comments

Comments
 (0)