Skip to content

Commit 36b8118

Browse files
committed
Determine existence before retrieving the permissions
1 parent 0a48f3e commit 36b8118

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

internal/provider/resource/directory_resource.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package resource
33
import (
44
"context"
55
"fmt"
6-
"github.com/askrella/askrella-ssh-provider/internal/provider/ssh"
76
"os"
87

8+
"github.com/askrella/askrella-ssh-provider/internal/provider/ssh"
9+
910
"github.com/hashicorp/terraform-plugin-framework/resource"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1112
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -150,7 +151,7 @@ func (r *DirectoryResource) Create(ctx context.Context, req resource.CreateReque
150151

151152
permissions := parsePermissions(plan.Permissions.ValueString())
152153

153-
if exists, _ := client.DirectoryExists(ctx, plan.Path.ValueString()); !exists {
154+
if exists, _ := client.Exists(ctx, plan.Path.ValueString()); !exists {
154155
err = client.CreateDirectory(ctx, plan.Path.ValueString(), os.FileMode(permissions))
155156
if err != nil {
156157
resp.Diagnostics.AddError(
@@ -227,6 +228,19 @@ func (r *DirectoryResource) Read(ctx context.Context, req resource.ReadRequest,
227228
}
228229
defer client.Close()
229230

231+
exists, err := client.Exists(ctx, state.Path.ValueString())
232+
if err != nil {
233+
resp.Diagnostics.AddError(
234+
"Error determining directory existence",
235+
fmt.Sprintf("Could not determine directory existence: %s", err),
236+
)
237+
return
238+
}
239+
if !exists {
240+
resp.State.RemoveResource(ctx)
241+
return
242+
}
243+
230244
// Get directory mode
231245
mode, err := client.GetFileMode(ctx, state.Path.ValueString())
232246
if err != nil {
@@ -322,7 +336,7 @@ func (r *DirectoryResource) Update(ctx context.Context, req resource.UpdateReque
322336

323337
permissions := parsePermissions(plan.Permissions.ValueString())
324338

325-
if exists, _ := client.DirectoryExists(ctx, plan.Path.ValueString()); !exists {
339+
if exists, _ := client.Exists(ctx, plan.Path.ValueString()); !exists {
326340
err = client.CreateDirectory(ctx, plan.Path.ValueString(), os.FileMode(permissions))
327341
if err != nil {
328342
resp.Diagnostics.AddError(

internal/provider/resource/file_resource.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ func (r *FileResource) Create(ctx context.Context, req resource.CreateRequest, r
154154
}
155155
defer client.Close()
156156

157+
exists, err := client.Exists(ctx, plan.Path.ValueString())
158+
if err != nil {
159+
resp.Diagnostics.AddError(
160+
"Error checking file existence",
161+
fmt.Sprintf("Could not determine file existence: %s", err),
162+
)
163+
return
164+
}
165+
if exists {
166+
resp.State.RemoveResource(ctx)
167+
return
168+
}
169+
157170
permissions := parsePermissions(plan.Permissions.ValueString())
158171

159172
err = client.CreateFile(ctx, plan.Path.ValueString(), plan.Content.ValueString(), os.FileMode(permissions))

internal/provider/resource/test/directory_resource_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestAccDirectoryResource(t *testing.T) {
4848
resource.TestCheckResourceAttr("ssh_directory.test", "ssh.username", "testuser"),
4949
func(s *terraform.State) error {
5050
// Verify directory exists
51-
exists, err := client.DirectoryExists(context.Background(), testDirPath)
51+
exists, err := client.Exists(context.Background(), testDirPath)
5252
if err != nil {
5353
return fmt.Errorf("failed to check directory: %v", err)
5454
}
@@ -94,7 +94,7 @@ func TestAccDirectoryResource(t *testing.T) {
9494
resource.TestCheckResourceAttr("ssh_directory.test", "ssh.username", "testuser"),
9595
func(s *terraform.State) error {
9696
// Verify directory exists
97-
exists, err := client.DirectoryExists(context.Background(), testDirPath)
97+
exists, err := client.Exists(context.Background(), testDirPath)
9898
if err != nil {
9999
return fmt.Errorf("failed to check directory: %v", err)
100100
}

internal/provider/ssh/ssh_client.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (c *SSHClient) CreateFile(ctx context.Context, path string, content string,
132132

133133
// Ensure parent directory exists
134134
parentDir := filepath.Dir(path)
135-
if exists, _ := c.DirectoryExists(ctx, parentDir); !exists {
135+
if exists, _ := c.Exists(ctx, parentDir); !exists {
136136
if err := c.CreateDirectory(ctx, parentDir, 0755); err != nil {
137137
return fmt.Errorf("failed to create parent directory: %w", err)
138138
}
@@ -197,7 +197,7 @@ func (c *SSHClient) CreateDirectory(ctx context.Context, path string, permission
197197
ctx, span := otel.Tracer("ssh-provider").Start(ctx, "CreateDirectory")
198198
defer span.End()
199199

200-
if exists, _ := c.DirectoryExists(ctx, path); exists {
200+
if exists, _ := c.Exists(ctx, path); exists {
201201
return fmt.Errorf("directory %s already exists", path)
202202
}
203203

@@ -227,21 +227,21 @@ func (c *SSHClient) DeleteDirectory(ctx context.Context, path string) error {
227227
return nil
228228
}
229229

230-
// DirectoryExists checks if a directory exists and is a directory
231-
func (c *SSHClient) DirectoryExists(ctx context.Context, path string) (bool, error) {
232-
ctx, span := otel.Tracer("ssh-provider").Start(ctx, "DirectoryExists")
230+
// Exists checks if a directory or file exists
231+
func (c *SSHClient) Exists(ctx context.Context, path string) (bool, error) {
232+
ctx, span := otel.Tracer("ssh-provider").Start(ctx, "Exists")
233233
defer span.End()
234234

235-
info, err := c.SftpClient.Stat(path)
235+
_, err := c.SftpClient.Stat(path)
236236
if err != nil {
237237
if os.IsNotExist(err) {
238238
return false, nil
239239
}
240-
c.logger.WithContext(ctx).WithError(err).Error("Failed to check directory existence")
241-
return false, fmt.Errorf("failed to check directory existence: %w", err)
240+
c.logger.WithContext(ctx).WithError(err).Error("Failed to check existence")
241+
return false, fmt.Errorf("failed to check existence: %w", err)
242242
}
243243

244-
return info.IsDir(), nil
244+
return true, nil
245245
}
246246

247247
// GetFileMode gets the permissions of a file or directory

internal/provider/ssh/ssh_client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestDirectoryOperations(t *testing.T) {
2525
directoryPath := path.Join(basePath, "dir/simple")
2626

2727
t.Log("Check directory does not exist before we've done anything")
28-
exists, err := client.DirectoryExists(context.Background(), directoryPath)
28+
exists, err := client.Exists(context.Background(), directoryPath)
2929
Expect(err).ToNot(HaveOccurred())
3030
Expect(exists).To(BeFalse())
3131

@@ -34,7 +34,7 @@ func TestDirectoryOperations(t *testing.T) {
3434
Expect(err).ToNot(HaveOccurred())
3535

3636
t.Log("Check directory exists after creation")
37-
exists, err = client.DirectoryExists(context.Background(), directoryPath)
37+
exists, err = client.Exists(context.Background(), directoryPath)
3838
Expect(err).ToNot(HaveOccurred())
3939
Expect(exists).To(BeTrue())
4040

@@ -47,7 +47,7 @@ func TestDirectoryOperations(t *testing.T) {
4747
Expect(err).ToNot(HaveOccurred())
4848

4949
t.Log("Check directory exists after deletion")
50-
exists, err = client.DirectoryExists(context.Background(), directoryPath)
50+
exists, err = client.Exists(context.Background(), directoryPath)
5151
Expect(err).ToNot(HaveOccurred())
5252
Expect(exists).To(BeFalse())
5353
}

0 commit comments

Comments
 (0)