Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed May 3, 2021
1 parent 69c3b85 commit 148a81c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
2 changes: 2 additions & 0 deletions internal/provider/data_source_remotefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func dataSourceRemotefile() *schema.Resource {
"host": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The target host.",
},
"port": {
Expand Down Expand Up @@ -65,6 +66,7 @@ func dataSourceRemotefile() *schema.Resource {
"path": {
Description: "Path to file on remote host.",
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
},
Expand Down
38 changes: 35 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (c *apiClient) writeFileSudo(d *schema.ResourceData) error {
stdin.Close()
}()

cmd := fmt.Sprint("cat /dev/stdin | sudo tee ", d.Get("path").(string))
cmd := fmt.Sprintf("cat /dev/stdin | sudo tee %s", d.Get("path").(string))
return session.Run(cmd)
}

Expand Down Expand Up @@ -177,6 +177,38 @@ func (c *apiClient) readFile(d *schema.ResourceData) error {
return nil
}

func (c *apiClient) fileExistsSudo(d *schema.ResourceData) (bool, error) {
sshClient, err := c.getSSHClient()
if err != nil {
return false, err
}

session, err := sshClient.NewSession()
if err != nil {
return false, err
}
defer session.Close()

path := d.Get("path").(string)
cmd := fmt.Sprintf("test -f %s", path)
err = session.Run(cmd)

if err != nil {
session2, err := sshClient.NewSession()
if err != nil {
return false, err
}
defer session2.Close()

cmd := fmt.Sprintf("test ! -f %s", path)
err = session2.Run(cmd)

return err == nil, err
}

return true, nil
}

func (c *apiClient) readFileSudo(d *schema.ResourceData) error {
sshClient, err := c.getSSHClient()
if err != nil {
Expand All @@ -189,7 +221,7 @@ func (c *apiClient) readFileSudo(d *schema.ResourceData) error {
}
defer session.Close()

cmd := fmt.Sprint("sudo cat ", d.Get("path").(string))
cmd := fmt.Sprintf("sudo cat %s", d.Get("path").(string))
content, err := session.Output(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -221,7 +253,7 @@ func (c *apiClient) deleteFileSudo(d *schema.ResourceData) error {
}
defer session.Close()

cmd := fmt.Sprint("sudo cat ", d.Get("path").(string))
cmd := fmt.Sprintf("sudo cat %s", d.Get("path").(string))
return session.Run(cmd)
}

Expand Down
30 changes: 25 additions & 5 deletions internal/provider/resource_remotefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func resourceRemotefile() *schema.Resource {
"host": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The target host.",
},
"port": {
Expand Down Expand Up @@ -69,16 +70,19 @@ func resourceRemotefile() *schema.Resource {
"path": {
Description: "Path to file on remote host.",
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"content": {
Description: "Content of file.",
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"permissions": {
Description: "Permissions of file.",
Type: schema.TypeString,
ForceNew: true,
Default: "0644",
Optional: true,
},
Expand Down Expand Up @@ -124,14 +128,22 @@ func resourceRemotefileRead(ctx context.Context, d *schema.ResourceData, meta in

sudo, ok := d.GetOk("conn.0.sudo")
if ok && sudo.(bool) {
err := client.readFileSudo(d)
exists, err := client.fileExistsSudo(d)
if err != nil {
return diag.Errorf("error while removing remote file with sudo: %s", err.Error())
return diag.Errorf("error while checking if remote file exists with sudo: %s", err.Error())
}
if exists {
err := client.readFileSudo(d)
if err != nil {
return diag.Errorf("error while reading remote file with sudo: %s", err.Error())
}
} else {
return diag.Errorf("cannot read file, it does not exist.")
}
} else {
err := client.readFile(d)
if err != nil {
return diag.Errorf("error while removing remote file: %s", err.Error())
return diag.Errorf("error while reading remote file: %s", err.Error())
}
}

Expand All @@ -150,9 +162,17 @@ func resourceRemotefileDelete(ctx context.Context, d *schema.ResourceData, meta

sudo, ok := d.GetOk("conn.0.sudo")
if ok && sudo.(bool) {
err := client.deleteFileSudo(d)
exists, err := client.fileExistsSudo(d)
if err != nil {
return diag.Errorf("error while removing remote file with sudo: %s", err.Error())
return diag.Errorf("error while checking if remote file exists with sudo: %s", err.Error())
}
if exists {
err := client.deleteFileSudo(d)
if err != nil {
return diag.Errorf("error while removing remote file with sudo: %s", err.Error())
}
} else {
return diag.Errorf("cannot delete file, it does not exist.")
}
} else {
err := client.deleteFile(d)
Expand Down

0 comments on commit 148a81c

Please sign in to comment.