diff --git a/internal/provider/data_source_remote_file_test.go b/internal/provider/data_source_remote_file_test.go index 3ed9852..26ad491 100644 --- a/internal/provider/data_source_remote_file_test.go +++ b/internal/provider/data_source_remote_file_test.go @@ -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")), ), diff --git a/internal/provider/provider.go b/internal/provider/provider.go index c592c69..162916f 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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), diff --git a/internal/provider/resource_remote_file.go b/internal/provider/resource_remote_file.go index fc43577..fe558b6 100644 --- a/internal/provider/resource_remote_file.go +++ b/internal/provider/resource_remote_file.go @@ -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" @@ -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()) @@ -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()) diff --git a/internal/provider/resource_remote_file_test.go b/internal/provider/resource_remote_file_test.go index 0240c42..2cfa9a3 100644 --- a/internal/provider/resource_remote_file_test.go +++ b/internal/provider/resource_remote_file_test.go @@ -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")), ),