Skip to content

Commit

Permalink
Make port part of resource id (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad authored Feb 6, 2022
1 parent a3fa6b2 commit bd46bbf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions internal/provider/data_source_remote_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestAccDataSourceRemoteFile(t *testing.T) {
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.remote_file.data_1", "id", regexp.MustCompile("remotehost:22:/tmp/data_1.txt")),
resource.TestMatchResourceAttr(
"data.remote_file.data_1", "content", regexp.MustCompile("data_1")),
),
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ func (c *apiClient) closeRemoteClient(d *schema.ResourceData) error {
return nil
}

func setResourceID(d *schema.ResourceData, conn *schema.ResourceData) {
id := fmt.Sprintf("%s:%d:%s",
conn.Get("conn.0.host").(string),
conn.Get("conn.0.port").(int),
d.Get("path").(string))
d.SetId(id)
}

func resourceConnectionHash(d *schema.ResourceData) string {
elements := []string{
d.Get("conn.0.host").(string),
Expand Down
7 changes: 2 additions & 5 deletions internal/provider/resource_remote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -55,12 +54,11 @@ func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf(err.Error())
}

setResourceID(d, conn)
content := d.Get("content").(string)
path := d.Get("path").(string)
permissions := d.Get("permissions").(string)

d.SetId(fmt.Sprintf("%s:%s", conn.Get("conn.0.host").(string), path))

client, err := meta.(*apiClient).getRemoteClient(conn)
if err != nil {
return diag.Errorf("error while opening remote client: %s", err.Error())
Expand Down Expand Up @@ -97,10 +95,9 @@ func resourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta in
return diag.Errorf(err.Error())
}

setResourceID(d, conn)
path := d.Get("path").(string)

d.SetId(fmt.Sprintf("%s:%s", conn.Get("conn.0.host").(string), path))

client, err := meta.(*apiClient).getRemoteClient(conn)
if err != nil {
return diag.Errorf("error while opening remote client: %s", err.Error())
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_remote_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func TestAccResourceRemoteFileWithDefaultConnection(t *testing.T) {
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"remote_file.resource_2", "id", regexp.MustCompile("remotehost:22:/tmp/resource_2.txt")),
resource.TestMatchResourceAttr(
"remote_file.resource_2", "content", regexp.MustCompile("resource_2")),
),
Expand Down

0 comments on commit bd46bbf

Please sign in to comment.