Skip to content

Commit

Permalink
Add fake create method to customizediff test
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Feb 5, 2024
1 parent 782b210 commit 4ef15d3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 47 deletions.
102 changes: 55 additions & 47 deletions common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func recoverable(cb func(
}

func (r Resource) saferCustomizeDiff() schema.CustomizeDiffFunc {
if !r.isResource() {
return nil
}
return func(ctx context.Context, rd *schema.ResourceDiff, m any) (err error) {
defer func() {
// this is deliberate decision to convert a panic into error,
Expand All @@ -115,10 +118,11 @@ func (r Resource) saferCustomizeDiff() schema.CustomizeDiffFunc {
"customize diff for")
}
}()
c := m.(*DatabricksClient)
if r.WorkspaceIdField == WorkspaceId && rd.NewValueKnown("workspace_id") && !c.Config.IsAccountClient() {
// TODO: check that the provided workspace ID matches that for the provider
}
// TODO: check that the provided workspace ID matches that for the provider
// c := m.(*DatabricksClient)
// if r.WorkspaceIdField == WorkspaceId && rd.NewValueKnown("workspace_id") && !c.Config.IsAccountClient() {
// ...
// }
// we don't propagate instance of SDK client to the diff function, because
// authentication is not deterministic at this stage with the recent Terraform
// versions. Diff customization must be limited to hermetic checks only anyway.
Expand All @@ -140,8 +144,15 @@ func (r Resource) getSchema() map[string]*schema.Schema {
return r.Schema
}

func (r Resource) isResource() bool {
return r.Create != nil || r.Update != nil || r.Delete != nil
}

// ToResource converts to Terraform resource definition
func (r Resource) ToResource() *schema.Resource {
if r.WorkspaceIdField == OriginalWorkspaceId {
panic("OriginalWorkspaceId is not a valid value for WorkspaceIdField")
}
getClient := func(_ context.Context, m any, d *schema.ResourceData) (c *DatabricksClient, err error) {
return m.(*DatabricksClient), nil
}
Expand All @@ -154,49 +165,8 @@ func (r Resource) ToResource() *schema.Resource {
return m.(*DatabricksClient).InConfiguredWorkspace(ctx, d, OriginalWorkspaceId)
}
}
var update func(ctx context.Context, d *schema.ResourceData,
m any) diag.Diagnostics
if r.Update != nil {
update = func(ctx context.Context, d *schema.ResourceData,
m any) diag.Diagnostics {
c, err := getClient(ctx, m, d)
if err != nil {
return diag.FromErr(err)
}
if err := recoverable(r.Update)(ctx, d, c); err != nil {
err = nicerError(ctx, err, "update")
return diag.FromErr(err)
}
if err := recoverable(r.Read)(ctx, d, c); err != nil {
err = nicerError(ctx, err, "read")
return diag.FromErr(err)
}
return nil
}
} else {
// set ForceNew to all attributes with CRD
queue := []*schema.Resource{
{Schema: r.Schema},
}
for {
head := queue[0]
queue = queue[1:]
for _, v := range head.Schema {
if v.Computed {
continue
}
if nested, ok := v.Elem.(*schema.Resource); ok {
queue = append(queue, nested)
}
v.ForceNew = true
}
if len(queue) == 0 {
break
}
}
}
// Ignore missing for read for resources, but not for data sources.
ignoreMissingForRead := (r.Create != nil || r.Update != nil || r.Delete != nil)
ignoreMissingForRead := r.isResource()
generateReadFunc := func(ignoreMissing bool) func(ctx context.Context, d *schema.ResourceData,
m any) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData,
Expand Down Expand Up @@ -226,7 +196,6 @@ func (r Resource) ToResource() *schema.Resource {
StateUpgraders: r.StateUpgraders,
CustomizeDiff: r.saferCustomizeDiff(),
ReadContext: generateReadFunc(ignoreMissingForRead),
UpdateContext: update,
Importer: r.Importer,
Timeouts: r.Timeouts,
DeprecationMessage: r.DeprecationMessage,
Expand Down Expand Up @@ -286,6 +255,45 @@ func (r Resource) ToResource() *schema.Resource {
if r.WorkspaceIdField != NoWorkspaceId {
resource.Schema = AddWorkspaceIdField(resource.Schema, r.WorkspaceIdField)
}
if r.Update != nil {
resource.UpdateContext = func(ctx context.Context, d *schema.ResourceData,
m any) diag.Diagnostics {
c, err := getClient(ctx, m, d)
if err != nil {
return diag.FromErr(err)
}
if err := recoverable(r.Update)(ctx, d, c); err != nil {
err = nicerError(ctx, err, "update")
return diag.FromErr(err)
}
if err := recoverable(r.Read)(ctx, d, c); err != nil {
err = nicerError(ctx, err, "read")
return diag.FromErr(err)
}
return nil
}
} else {
// set ForceNew to all attributes with CRD
queue := []*schema.Resource{
{Schema: r.Schema},
}
for {
head := queue[0]
queue = queue[1:]
for _, v := range head.Schema {
if v.Computed {
continue
}
if nested, ok := v.Elem.(*schema.Resource); ok {
queue = append(queue, nested)
}
v.ForceNew = true
}
if len(queue) == 0 {
break
}
}
}
return resource
}

Expand Down
10 changes: 10 additions & 0 deletions common/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ func TestRecoverableFromPanic(t *testing.T) {

func TestCustomizeDiffRobustness(t *testing.T) {
r := Resource{
Create: func(ctx context.Context,
d *schema.ResourceData,
c *DatabricksClient) error {
return d.Set("foo", 1)
},
CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff) error {
return fmt.Errorf("nope")
},
Expand All @@ -170,6 +175,11 @@ func TestCustomizeDiffRobustness(t *testing.T) {
assert.EqualError(t, err, "cannot customize diff for sample: nope")

r = Resource{
Create: func(ctx context.Context,
d *schema.ResourceData,
c *DatabricksClient) error {
return d.Set("foo", 1)
},
CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff) error {
panic("oops")
},
Expand Down

0 comments on commit 4ef15d3

Please sign in to comment.