From 60a86959bcec727866a29f255de879d607ad809e Mon Sep 17 00:00:00 2001 From: Amund Tenstad Date: Sun, 12 Jun 2022 11:53:45 +0200 Subject: [PATCH] Remove file from state on file-not-found --- internal/provider/resource_remote_file.go | 62 +++++++++++------------ 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/internal/provider/resource_remote_file.go b/internal/provider/resource_remote_file.go index bf7819e..034c414 100644 --- a/internal/provider/resource_remote_file.go +++ b/internal/provider/resource_remote_file.go @@ -136,36 +136,36 @@ func resourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta in if err != nil { return diag.Errorf("unable to check if remote file exists: %s", err.Error()) } - if !exists { - return diag.Errorf("cannot read remote file, it does not exist.") - } - - content, err := client.ReadFile(path, sudo) - if err != nil { - return diag.Errorf("unable to read remote file: %s", err.Error()) - } - d.Set("content", content) - - permissions, err := client.ReadFilePermissions(path, sudo) - if err != nil { - return diag.Errorf("unable to read remote file permissions: %s", err.Error()) - } - d.Set("permissions", permissions) - - if owner != "" { - owner, err := client.ReadFileOwner(path, sudo) + if exists { + content, err := client.ReadFile(path, sudo) if err != nil { - return diag.Errorf("unable to read remote file owner: %s", err.Error()) + return diag.Errorf("unable to read remote file: %s", err.Error()) } - d.Set("owner", owner) - } + d.Set("content", content) - if group != "" { - group, err := client.ReadFileGroup(path, sudo) + permissions, err := client.ReadFilePermissions(path, sudo) if err != nil { - return diag.Errorf("unable to read remote file group: %s", err.Error()) + return diag.Errorf("unable to read remote file permissions: %s", err.Error()) + } + d.Set("permissions", permissions) + + if owner != "" { + owner, err := client.ReadFileOwner(path, sudo) + if err != nil { + return diag.Errorf("unable to read remote file owner: %s", err.Error()) + } + d.Set("owner", owner) } - d.Set("group", group) + + if group != "" { + group, err := client.ReadFileGroup(path, sudo) + if err != nil { + return diag.Errorf("unable to read remote file group: %s", err.Error()) + } + d.Set("group", group) + } + } else { + d.SetId("") } err = meta.(*apiClient).closeRemoteClient(conn) @@ -199,13 +199,11 @@ func resourceRemoteFileDelete(ctx context.Context, d *schema.ResourceData, meta if err != nil { return diag.Errorf("unable to check if remote file exists: %s", err.Error()) } - if !exists { - return diag.Errorf("cannot delete remote file, it does not exist.") - } - - err = client.DeleteFile(path, sudo) - if err != nil { - return diag.Errorf("unable to delete remote file: %s", err.Error()) + if exists { + err = client.DeleteFile(path, sudo) + if err != nil { + return diag.Errorf("unable to delete remote file: %s", err.Error()) + } } err = meta.(*apiClient).closeRemoteClient(conn)