Skip to content

Commit

Permalink
Create connection settings and use path as id
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Apr 30, 2021
1 parent 52e3de8 commit 4cb6ce4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 61 deletions.
62 changes: 30 additions & 32 deletions internal/provider/data_source_remotefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,47 @@ package provider

import (
"context"
"fmt"
"hash/fnv"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceRemotefile() *schema.Resource {
return &schema.Resource{
Description: "Sample data source in the Terraform provider scaffolding.",
Description: "File on remote host.",

ReadContext: dataSourceRemotefileRead,

Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_USERNAME", nil),
Description: "The username on the target host. May alternatively be set via the `REMOTEFILE_USERNAME` environment variable.",
},
"private_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PRIVATE_KEY", nil),
Description: "The private key used to login to target host. May alternatively be set via the `REMOTEFILE_PRIVATE_KEY` environment variable.",
},
"host": {
Type: schema.TypeString,
"connection": {
Type: schema.TypeList,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_HOST", nil),
Description: "The target host where files are located. May alternatively be set via the `REMOTEFILE_HOST` environment variable.",
},
"port": {
Type: schema.TypeInt,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PORT", 22),
Description: "The ssh port to the target host. May alternatively be set via the `REMOTEFILE_PORT` environment variable.",
Description: "Connection to host where files are located.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
Description: "The username on the target host.",
},
"private_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Description: "The private key used to login to the target host.",
},
"host": {
Type: schema.TypeString,
Required: true,
Description: "The target host.",
},
"port": {
Type: schema.TypeInt,
Required: true,
Description: "The ssh port to the target host.",
},
},
},
},
"path": {
Description: "Path to file on remote host.",
Expand All @@ -54,11 +56,7 @@ func dataSourceRemotefile() *schema.Resource {
func dataSourceRemotefileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
err := resourceRemotefileRead(ctx, d, meta)

if !err.HasError() {
h := fnv.New32a()
h.Write([]byte(fmt.Sprintf("%s @ %s", d.Get("content"), d.Get("path"))))
d.SetId(strconv.Itoa(int(h.Sum32())))
}
d.SetId(d.Get("path").(string))

return err
}
58 changes: 29 additions & 29 deletions internal/provider/resource_remotefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package provider
import (
"bytes"
"context"
"fmt"
"hash/fnv"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -22,30 +19,35 @@ func resourceRemotefile() *schema.Resource {
DeleteContext: resourceRemotefileDelete,

Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_USERNAME", nil),
Description: "The username on the target host. May alternatively be set via the `REMOTEFILE_USERNAME` environment variable.",
},
"private_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PRIVATE_KEY", nil),
Description: "The private key used to login to target host. May alternatively be set via the `REMOTEFILE_PRIVATE_KEY` environment variable.",
},
"host": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_HOST", nil),
Description: "The target host where files are located. May alternatively be set via the `REMOTEFILE_HOST` environment variable.",
},
"port": {
Type: schema.TypeInt,
"connection": {
Type: schema.TypeList,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PORT", 22),
Description: "The ssh port to the target host. May alternatively be set via the `REMOTEFILE_PORT` environment variable.",
Description: "Connection to host where files are located.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
Description: "The username on the target host.",
},
"private_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Description: "The private key used to login to the target host.",
},
"host": {
Type: schema.TypeString,
Required: true,
Description: "The target host.",
},
"port": {
Type: schema.TypeInt,
Required: true,
Description: "The ssh port to the target host.",
},
},
},
},
"path": {
Description: "Path to file on remote host.",
Expand All @@ -70,9 +72,7 @@ func resourceRemotefile() *schema.Resource {
func resourceRemotefileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*apiClient)

h := fnv.New32a()
h.Write([]byte(fmt.Sprintf("%s @ %s", d.Get("content"), d.Get("path"))))
d.SetId(strconv.Itoa(int(h.Sum32())))
d.SetId(d.Get("path").(string))

resourceClient, err := client.fromResourceData(d)
if err != nil {
Expand Down

0 comments on commit 4cb6ce4

Please sign in to comment.