From defeb32cd63bd287e11971dea4c5b43f372de8ae Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Thu, 17 Sep 2020 00:29:37 -0400 Subject: [PATCH] When reading a resource, on 404 destroy the resource (#41) This allows configuration to continue after a resource has been destroyed outside of TF by deleting the resource from state instead of erroring. --- internal/provider/resource_account.go | 5 +++++ internal/provider/resource_auth_method.go | 5 +++++ internal/provider/resource_group.go | 5 +++++ internal/provider/resource_host.go | 5 +++++ internal/provider/resource_host_catalog.go | 5 +++++ internal/provider/resource_host_set.go | 5 +++++ internal/provider/resource_role.go | 5 +++++ internal/provider/resource_scope.go | 5 +++++ internal/provider/resource_target.go | 5 +++++ internal/provider/resource_user.go | 5 +++++ 10 files changed, 50 insertions(+) diff --git a/internal/provider/resource_account.go b/internal/provider/resource_account.go index 37efd67a..3a324c6c 100644 --- a/internal/provider/resource_account.go +++ b/internal/provider/resource_account.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/accounts" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -153,6 +154,10 @@ func resourceAccountRead(ctx context.Context, d *schema.ResourceData, meta inter return diag.Errorf("error calling read account: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading account: %s", apiErr.Message) } if arr == nil { diff --git a/internal/provider/resource_auth_method.go b/internal/provider/resource_auth_method.go index d8868f79..16f2e11a 100644 --- a/internal/provider/resource_auth_method.go +++ b/internal/provider/resource_auth_method.go @@ -3,6 +3,7 @@ package provider import ( "context" "encoding/json" + "net/http" "github.com/hashicorp/boundary/api/authmethods" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -163,6 +164,10 @@ func resourceAuthMethodRead(ctx context.Context, d *schema.ResourceData, meta in return diag.Errorf("error calling read auth method: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading auth method: %s", apiErr.Message) } if amrr == nil { diff --git a/internal/provider/resource_group.go b/internal/provider/resource_group.go index 6ac90829..9573a893 100644 --- a/internal/provider/resource_group.go +++ b/internal/provider/resource_group.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/groups" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -127,6 +128,10 @@ func resourceGroupRead(ctx context.Context, d *schema.ResourceData, meta interfa return diag.Errorf("error calling read group: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading group: %s", apiErr.Message) } if g == nil { diff --git a/internal/provider/resource_host.go b/internal/provider/resource_host.go index 263f255b..4042783a 100644 --- a/internal/provider/resource_host.go +++ b/internal/provider/resource_host.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/hosts" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -142,6 +143,10 @@ func resourceHostRead(ctx context.Context, d *schema.ResourceData, meta interfac return diag.Errorf("error calling read host: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading host: %s", apiErr.Message) } if hrr == nil { diff --git a/internal/provider/resource_host_catalog.go b/internal/provider/resource_host_catalog.go index 56c27f18..b9c02169 100644 --- a/internal/provider/resource_host_catalog.go +++ b/internal/provider/resource_host_catalog.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/hostcatalogs" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -116,6 +117,10 @@ func resourceHostCatalogRead(ctx context.Context, d *schema.ResourceData, meta i return diag.Errorf("error calling read host catalog: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading host catalog: %s", apiErr.Message) } if hcrr == nil { diff --git a/internal/provider/resource_host_set.go b/internal/provider/resource_host_set.go index 230b78d2..77710ac9 100644 --- a/internal/provider/resource_host_set.go +++ b/internal/provider/resource_host_set.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/hostsets" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -151,6 +152,10 @@ func resourceHostsetRead(ctx context.Context, d *schema.ResourceData, meta inter return diag.Errorf("error calling read host set: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading host set: %s", apiErr.Message) } if hsrr == nil { diff --git a/internal/provider/resource_role.go b/internal/provider/resource_role.go index c7c9f0f2..d8670cf3 100644 --- a/internal/provider/resource_role.go +++ b/internal/provider/resource_role.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/roles" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -191,6 +192,10 @@ func resourceRoleRead(ctx context.Context, d *schema.ResourceData, meta interfac return diag.Errorf("error calling read role: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading role: %s", apiErr.Message) } if trr == nil { diff --git a/internal/provider/resource_scope.go b/internal/provider/resource_scope.go index e1cf6271..3f6f9e09 100644 --- a/internal/provider/resource_scope.go +++ b/internal/provider/resource_scope.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/scopes" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -132,6 +133,10 @@ func resourceScopeRead(ctx context.Context, d *schema.ResourceData, meta interfa return diag.Errorf("error calling read scope: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading scope: %s", apiErr.Message) } if srr == nil { diff --git a/internal/provider/resource_target.go b/internal/provider/resource_target.go index d8992fd3..15968c7c 100644 --- a/internal/provider/resource_target.go +++ b/internal/provider/resource_target.go @@ -3,6 +3,7 @@ package provider import ( "context" "encoding/json" + "net/http" "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -178,6 +179,10 @@ func resourceTargetRead(ctx context.Context, d *schema.ResourceData, meta interf return diag.Errorf("error calling read target: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading target: %s", apiErr.Message) } if trr == nil { diff --git a/internal/provider/resource_user.go b/internal/provider/resource_user.go index f0dddfb7..9df89782 100644 --- a/internal/provider/resource_user.go +++ b/internal/provider/resource_user.go @@ -2,6 +2,7 @@ package provider import ( "context" + "net/http" "github.com/hashicorp/boundary/api/users" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -93,6 +94,10 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interfac return diag.Errorf("error calling read user: %v", err) } if apiErr != nil { + if apiErr.Status == int32(http.StatusNotFound) { + d.SetId("") + return nil + } return diag.Errorf("error reading user: %s", apiErr.Message) } if urr == nil {