Skip to content

Commit

Permalink
Handle missing resources on refresh
Browse files Browse the repository at this point in the history
Gracefully handle the situation where a resource has gone missing
outside of Terraform (e.g., because the user manually ran a `DROP`
command).

This logic was applied to table resources as a one-off in #155, but that
code appears to have gotten lost in 9235b4b. This commit re-adds the
logic for all resource types.

No tests, because our current testing infrastructure is not powerful
enough to handle this. Will put up a test in a separate PR that enhances
the testing infrastructure (see #165).

Fix #157.
  • Loading branch information
benesch committed Jun 5, 2023
1 parent 9f314b1 commit 6b656fd
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 17 deletions.
6 changes: 5 additions & 1 deletion pkg/resources/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -33,7 +34,10 @@ func Cluster() *schema.Resource {
func clusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
i := d.Id()
s, err := materialize.ScanCluster(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_cluster_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -68,7 +69,10 @@ func clusterReplicaRead(ctx context.Context, d *schema.ResourceData, meta interf
i := d.Id()

s, err := materialize.ScanClusterReplica(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand All @@ -14,7 +15,10 @@ func connectionRead(ctx context.Context, d *schema.ResourceData, meta interface{
i := d.Id()

s, err := materialize.ScanConnection(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/resources/resource_connection_aws_privatelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func connectionAwsPrivatelinkRead(ctx context.Context, d *schema.ResourceData, m
i := d.Id()

s, err := materialize.ScanConnectionAwsPrivatelink(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_connection_ssh_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -63,7 +64,10 @@ func connectionSshTunnelRead(ctx context.Context, d *schema.ResourceData, meta i
i := d.Id()

s, err := materialize.ScanConnectionSshTunnel(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -34,7 +35,10 @@ func databaseRead(ctx context.Context, d *schema.ResourceData, meta interface{})
i := d.Id()

s, err := materialize.ScanDatabase(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -94,7 +95,10 @@ func Index() *schema.Resource {
func indexRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
i := d.Id()
s, err := materialize.ScanIndex(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_materialized_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -50,7 +51,10 @@ func materializedViewRead(ctx context.Context, d *schema.ResourceData, meta inte
i := d.Id()

s, err := materialize.ScanMaterializedView(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"
"fmt"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"
Expand Down Expand Up @@ -92,7 +93,10 @@ func ownershipRead(ctx context.Context, d *schema.ResourceData, meta interface{}
i := d.Id()

s, err := materialize.ScanOwnership(meta.(*sqlx.DB), i, objectType)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -60,7 +61,10 @@ func Role() *schema.Resource {
func roleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
i := d.Id()
s, err := materialize.ScanRole(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -35,7 +36,10 @@ func Schema() *schema.Resource {
func schemaRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
i := d.Id()
s, err := materialize.ScanSchema(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -44,7 +45,10 @@ func secretRead(ctx context.Context, d *schema.ResourceData, meta interface{}) d
i := d.Id()

s, err := materialize.ScanSecret(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand All @@ -14,7 +15,10 @@ func sinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) dia
i := d.Id()

s, err := materialize.ScanSink(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/resources/resource_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func sourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) d
i := d.Id()

s, err := materialize.ScanSource(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -64,7 +65,10 @@ func tableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) di
i := d.Id()

s, err := materialize.ScanTable(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -85,7 +86,10 @@ func typeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) dia
i := d.Id()

s, err := materialize.ScanType(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/resources/resource_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"database/sql"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -44,7 +45,10 @@ func viewRead(ctx context.Context, d *schema.ResourceData, meta interface{}) dia
i := d.Id()

s, err := materialize.ScanView(meta.(*sqlx.DB), i)
if err != nil {
if err == sql.ErrNoRows {
d.SetId("")
return nil
} else if err != nil {
return diag.FromErr(err)
}

Expand Down

0 comments on commit 6b656fd

Please sign in to comment.