Skip to content

Commit d7e3bd9

Browse files
committed
All resource imports are now making call to corresponding Databricks API by default
1 parent 196af97 commit d7e3bd9

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Added validation for secret scope name in `databricks_secret`, `databricks_secret_scope` and `databricks_secret_acl`. Non-compliant names may cause errors.
1515
* Added [databricks_spark_version](https://github.com/databrickslabs/terraform-provider-databricks/issues/347) data source.
1616
* Fixed support for [single node clusters](https://docs.databricks.com/clusters/single-node.html) support by allowing [`num_workers` to be `0`](https://github.com/databrickslabs/terraform-provider-databricks/pull/454).
17-
* Fixed import of resources, such as, `databricks_user` ([#471](https://github.com/databrickslabs/terraform-provider-databricks/issues/471))
17+
* All resource imports are now making call to corresponding Databricks API by default ([#471](https://github.com/databrickslabs/terraform-provider-databricks/issues/471))
1818

1919
**Behavior changes**
2020

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ github.com/hashicorp/terraform-json v0.8.0 h1:XObQ3PgqU52YLQKEaJ08QtUshAfN3yu4u8
227227
github.com/hashicorp/terraform-json v0.8.0/go.mod h1:3defM4kkMfttwiE7VakJDwCd4R+umhSQnvJwORXbprE=
228228
github.com/hashicorp/terraform-plugin-go v0.1.0 h1:kyXZ0nkHxiRev/q18N40IbRRk4AV0zE/MDJkDM3u8dY=
229229
github.com/hashicorp/terraform-plugin-go v0.1.0/go.mod h1:10V6F3taeDWVAoLlkmArKttR3IULlRWFAGtQIQTIDr4=
230-
github.com/hashicorp/terraform-plugin-sdk v1.16.0 h1:NrkXMRjHErUPPTHQkZ6JIn6bByiJzGnlJzH1rVdNEuE=
231230
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.2-0.20200828083434-d39628234432 h1:IABUWxC+Atoelqy5Wk1GtbG6OR1DwIC5N0RPvePfvfw=
232231
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.2-0.20200828083434-d39628234432/go.mod h1:BRz6UtYmksQJU0eMfahQR8fcJf8tIe77gn7YVm6rGD4=
233232
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.0 h1:2c+vG46celrDCsfYEIzaXxvBaAXCqlVG77LwtFz8cfs=

internal/util/common_resource.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (r CommonResource) ToResource() *schema.Resource {
4343
v.ForceNew = true
4444
}
4545
}
46-
readFunc := func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
46+
read := func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
4747
err := r.Read(ctx, d, m.(*common.DatabricksClient))
4848
if e, ok := err.(common.APIError); ok && e.IsMissing() {
4949
log.Printf("[INFO] %s[id=%s] is removed on backend",
@@ -56,7 +56,6 @@ func (r CommonResource) ToResource() *schema.Resource {
5656
}
5757
return nil
5858
}
59-
6059
return &schema.Resource{
6160
Schema: r.Schema,
6261
SchemaVersion: r.SchemaVersion,
@@ -78,7 +77,7 @@ func (r CommonResource) ToResource() *schema.Resource {
7877
}
7978
return nil
8079
},
81-
ReadContext: readFunc,
80+
ReadContext: read,
8281
UpdateContext: update,
8382
DeleteContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
8483
if err := r.Delete(ctx, d, m.(*common.DatabricksClient)); err != nil {
@@ -89,7 +88,7 @@ func (r CommonResource) ToResource() *schema.Resource {
8988
Importer: &schema.ResourceImporter{
9089
StateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) (data []*schema.ResourceData, e error) {
9190
d.MarkNewResource()
92-
diags := readFunc(ctx, d, m)
91+
diags := read(ctx, d, m)
9392
var err error
9493
if diags.HasError() {
9594
err = diags[0].Validate()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package util
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/databrickslabs/databricks-terraform/common"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestImportingCallsRead(t *testing.T) {
14+
r := CommonResource{
15+
Read: func(ctx context.Context,
16+
d *schema.ResourceData,
17+
c *common.DatabricksClient) error {
18+
d.SetId("abc")
19+
return d.Set("foo", 1)
20+
},
21+
Schema: map[string]*schema.Schema{
22+
"foo": {
23+
Type: schema.TypeInt,
24+
Required: true,
25+
},
26+
},
27+
}.ToResource()
28+
29+
d := r.TestResourceData()
30+
datas, err := r.Importer.StateContext(
31+
context.Background(), d,
32+
&common.DatabricksClient{})
33+
require.NoError(t, err)
34+
assert.Len(t, datas, 1)
35+
assert.Equal(t, "abc", d.Id())
36+
assert.Equal(t, 1, d.Get("foo"))
37+
}

0 commit comments

Comments
 (0)