Skip to content

Commit

Permalink
Merge pull request #86 from numtide/add-text-file-datasource
Browse files Browse the repository at this point in the history
added linuxbox_text_file datasource
  • Loading branch information
draganm authored Oct 21, 2024
2 parents 7034e91 + c124681 commit 9ed65bd
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 13 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/dev_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ jobs:
go-version: 1.17
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v3.0.0
uses: crazy-max/ghaction-import-gpg@v6.0.0
with:
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.GPG_PASSPHRASE }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --rm-dist --snapshot
args: release --clean --snapshot
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: darwin-amd64
path: dist/*_darwin_amd64.zip
- uses: actions/upload-artifact@v2
name: darwin-amd64
path: dist/*_darwin_amd64.zip
- uses: actions/upload-artifact@v4
with:
name: linux-amd64
path: dist/*_linux_amd64.zip
name: linux-amd64
path: dist/*_linux_amd64.zip
# - uses: actions/upload-artifact@v2
# with:
# name: windows-amd64
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ name: release
on:
push:
tags:
- 'v*'
- "v*"
jobs:
goreleaser:
runs-on: ubuntu-latest
Expand All @@ -36,7 +36,7 @@ jobs:
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
args: release --clean
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ terraform-provider-linuxbox
terraform.tfstate.backup
terraform.tfstate


dist/
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Visit https://goreleaser.com for documentation on how to customize this
# behavior.
version: 2
before:
hooks:
- go mod tidy
Expand Down Expand Up @@ -45,4 +46,4 @@ release:
# Visit your project's GitHub Releases page to publish this release.
draft: true
changelog:
skip: true
disable: true
61 changes: 61 additions & 0 deletions datasource/textfile/text_file_resource.go
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

}
27 changes: 27 additions & 0 deletions docs/data-sources/text_file.md
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
2 changes: 2 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/numtide/terraform-provider-linuxbox/datasource/sourcehash"
datasource_textfile "github.com/numtide/terraform-provider-linuxbox/datasource/textfile"
"github.com/numtide/terraform-provider-linuxbox/resource/binaryfile"
"github.com/numtide/terraform-provider-linuxbox/resource/directory"
"github.com/numtide/terraform-provider-linuxbox/resource/docker"
Expand Down Expand Up @@ -31,6 +32,7 @@ func Provider() *schema.Provider {

DataSourcesMap: map[string]*schema.Resource{
"linuxbox_source_hash": sourcehash.Resource(),
"linuxbox_text_file": datasource_textfile.Resource(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down

0 comments on commit 9ed65bd

Please sign in to comment.