-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from numtide/add-text-file-datasource
added linuxbox_text_file datasource
- Loading branch information
Showing
7 changed files
with
106 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ terraform-provider-linuxbox | |
terraform.tfstate.backup | ||
terraform.tfstate | ||
|
||
|
||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package textfile | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/alessio/shellescape" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/numtide/terraform-provider-linuxbox/sshsession" | ||
) | ||
|
||
func Resource() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: resourceRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"ssh_key": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Sensitive: true, | ||
}, | ||
|
||
"ssh_user": { | ||
Type: schema.TypeString, | ||
Required: false, | ||
Default: "root", | ||
Optional: true, | ||
}, | ||
|
||
"host_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"content": { | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceRead(d *schema.ResourceData, m interface{}) error { | ||
path := d.Get("path").(string) | ||
|
||
cmd := fmt.Sprintf("cat %s", shellescape.Quote(path)) | ||
stdout, _, err := sshsession.Run(d, cmd) | ||
if err != nil { | ||
return fmt.Errorf("while getting content of %s: %w", path, err) | ||
} | ||
|
||
stdoutString := string(stdout) | ||
|
||
d.Set("content", stdoutString) | ||
|
||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# `linuxbox_text_file` Resource | ||
|
||
Reads a file from the target host. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
datasource "linuxbox_text_file" "authorized_keys" { | ||
host_address = digitalocean_droplet.test.ipv4_address | ||
ssh_key = tls_private_key.ssh_key.private_key_pem | ||
path = "/root/.ssh/authorized_keys" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `host_address` - (Required) Machine hostname to connect to. | ||
* `ssh_key` - (Required) Machine SSH key to connect with. | ||
* `ssh_user` - (Optional) Machine SSH user to connect with (default: "root"). | ||
* `path` - (Required) Path of the file to create. | ||
|
||
## Attribute Reference | ||
|
||
- `content` - Content of the file | ||
|
||
None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters