diff --git a/docs/data-sources/file.md b/docs/data-sources/file.md index e306179..c954be8 100644 --- a/docs/data-sources/file.md +++ b/docs/data-sources/file.md @@ -52,7 +52,6 @@ data "remote_file" "server2_hosts" { ### Read-Only - **content** (String) Content of file. -- **result_conn** (List of Object) Result of applying provider's `conn` as default to optional `conn` (see [below for nested schema](#nestedatt--result_conn)) ### Nested Schema for `conn` @@ -73,19 +72,3 @@ Optional: - **sudo** (Boolean) Use sudo to gain access to file. Defaults to `false`. - -### Nested Schema for `result_conn` - -Read-Only: - -- **agent** (Boolean) -- **host** (String) -- **password** (String) -- **port** (Number) -- **private_key** (String) -- **private_key_env_var** (String) -- **private_key_path** (String) -- **sudo** (Boolean) -- **user** (String) - - diff --git a/docs/resources/file.md b/docs/resources/file.md index 318dc76..9740056 100644 --- a/docs/resources/file.md +++ b/docs/resources/file.md @@ -57,10 +57,6 @@ resource "remote_file" "server2_bashrc" { - **id** (String) The ID of this resource. - **permissions** (String) Permissions of file. Defaults to `0644`. -### Read-Only - -- **result_conn** (List of Object) Computed conn for handling default functionality. (see [below for nested schema](#nestedatt--result_conn)) - ### Nested Schema for `conn` @@ -80,19 +76,3 @@ Optional: - **sudo** (Boolean) Use sudo to gain access to file. Defaults to `false`. - -### Nested Schema for `result_conn` - -Read-Only: - -- **agent** (Boolean) -- **host** (String) -- **password** (String) -- **port** (Number) -- **private_key** (String) -- **private_key_env_var** (String) -- **private_key_path** (String) -- **sudo** (Boolean) -- **user** (String) - - diff --git a/internal/provider/connection.go b/internal/provider/connection.go index 2d3c243..0858323 100644 --- a/internal/provider/connection.go +++ b/internal/provider/connection.go @@ -70,22 +70,22 @@ var connectionSchemaResource = &schema.Resource{ } func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConfig, error) { - _, ok := d.GetOk("result_conn") + _, ok := d.GetOk("conn") if !ok { return "", nil, fmt.Errorf("resouce does not have a connection configured") } clientConfig := ssh.ClientConfig{ - User: d.Get("result_conn.0.user").(string), + User: d.Get("conn.0.user").(string), HostKeyCallback: ssh.InsecureIgnoreHostKey(), } - password, ok := d.GetOk("result_conn.0.password") + password, ok := d.GetOk("conn.0.password") if ok { clientConfig.Auth = append(clientConfig.Auth, ssh.Password(password.(string))) } - private_key, ok := d.GetOk("result_conn.0.private_key") + private_key, ok := d.GetOk("conn.0.private_key") if ok { signer, err := ssh.ParsePrivateKey([]byte(private_key.(string))) if err != nil { @@ -94,7 +94,7 @@ func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConf clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer)) } - private_key_path, ok := d.GetOk("result_conn.0.private_key_path") + private_key_path, ok := d.GetOk("conn.0.private_key_path") if ok { content, err := ioutil.ReadFile(private_key_path.(string)) if err != nil { @@ -107,7 +107,7 @@ func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConf clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer)) } - private_key_env_var, ok := d.GetOk("result_conn.0.private_key_env_var") + private_key_env_var, ok := d.GetOk("conn.0.private_key_env_var") if ok { private_key := os.Getenv(private_key_env_var.(string)) signer, err := ssh.ParsePrivateKey([]byte(private_key)) @@ -117,7 +117,7 @@ func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConf clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer)) } - enableAgent, ok := d.GetOk("result_conn.0.agent") + enableAgent, ok := d.GetOk("conn.0.agent") if ok && enableAgent.(bool) { connection, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")) if err != nil { @@ -126,6 +126,6 @@ func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConf clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeysCallback(agent.NewClient(connection).Signers)) } - host := fmt.Sprintf("%s:%d", d.Get("result_conn.0.host").(string), d.Get("result_conn.0.port").(int)) + host := fmt.Sprintf("%s:%d", d.Get("conn.0.host").(string), d.Get("conn.0.port").(int)) return host, &clientConfig, nil } diff --git a/internal/provider/data_source_remote_file.go b/internal/provider/data_source_remote_file.go index 4f0978a..9c40d61 100644 --- a/internal/provider/data_source_remote_file.go +++ b/internal/provider/data_source_remote_file.go @@ -22,13 +22,6 @@ func dataSourceRemoteFile() *schema.Resource { Description: "Connection to host where files are located.", Elem: connectionSchemaResource, }, - "result_conn": { - Type: schema.TypeList, - Computed: true, - ForceNew: true, - Description: "Result of applying provider's `conn` as default to optional `conn`", - Elem: connectionSchemaResource, - }, "path": { Description: "Path to file on remote host.", Type: schema.TypeString, diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 305df5e..c592c69 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -83,17 +83,15 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema } } -func (c *apiClient) applyResultConn(d *schema.ResourceData) (*schema.ResourceData, error) { - conn, ok := d.GetOk("conn") +func (c *apiClient) getConnWithDefault(d *schema.ResourceData) (*schema.ResourceData, error) { + _, ok := d.GetOk("conn") if ok { - d.Set("result_conn", conn) return d, nil } - conn, ok = c.resourceData.GetOk("conn") + _, ok = c.resourceData.GetOk("conn") if ok { - d.Set("result_conn", conn) - return d, nil + return c.resourceData, nil } return nil, errors.New("neither the provider nor the resource/data source have a configured connection") @@ -152,13 +150,13 @@ func (c *apiClient) closeRemoteClient(d *schema.ResourceData) error { func resourceConnectionHash(d *schema.ResourceData) string { elements := []string{ - d.Get("result_conn.0.host").(string), - d.Get("result_conn.0.user").(string), - strconv.Itoa(d.Get("result_conn.0.port").(int)), - resourceStringWithDefault(d, "result_conn.0.password", ""), - resourceStringWithDefault(d, "result_conn.0.private_key", ""), - resourceStringWithDefault(d, "result_conn.0.private_key_path", ""), - strconv.FormatBool(d.Get("result_conn.0.agent").(bool)), + d.Get("conn.0.host").(string), + d.Get("conn.0.user").(string), + strconv.Itoa(d.Get("conn.0.port").(int)), + resourceStringWithDefault(d, "conn.0.password", ""), + resourceStringWithDefault(d, "conn.0.private_key", ""), + resourceStringWithDefault(d, "conn.0.private_key_path", ""), + strconv.FormatBool(d.Get("conn.0.agent").(bool)), } return strings.Join(elements, "::") } diff --git a/internal/provider/resource_remote_file.go b/internal/provider/resource_remote_file.go index 778f5bb..fc43577 100644 --- a/internal/provider/resource_remote_file.go +++ b/internal/provider/resource_remote_file.go @@ -26,13 +26,6 @@ func resourceRemoteFile() *schema.Resource { Description: "Connection to host where files are located.", Elem: connectionSchemaResource, }, - "result_conn": { - Type: schema.TypeList, - Computed: true, - ForceNew: true, - Description: "Computed conn for handling default functionality.", - Elem: connectionSchemaResource, - }, "path": { Description: "Path to file on remote host.", Type: schema.TypeString, @@ -57,7 +50,7 @@ func resourceRemoteFile() *schema.Resource { } func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - d, err := meta.(*apiClient).applyResultConn(d) + conn, err := meta.(*apiClient).getConnWithDefault(d) if err != nil { return diag.Errorf(err.Error()) } @@ -66,14 +59,14 @@ func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta path := d.Get("path").(string) permissions := d.Get("permissions").(string) - d.SetId(fmt.Sprintf("%s:%s", d.Get("result_conn.0.host").(string), path)) + d.SetId(fmt.Sprintf("%s:%s", conn.Get("conn.0.host").(string), path)) - client, err := meta.(*apiClient).getRemoteClient(d) + client, err := meta.(*apiClient).getRemoteClient(conn) if err != nil { return diag.Errorf("error while opening remote client: %s", err.Error()) } - sudo, ok := d.GetOk("result_conn.0.sudo") + sudo, ok := conn.GetOk("conn.0.sudo") if ok && sudo.(bool) { err := client.WriteFileSudo(content, path) if err != nil { @@ -90,7 +83,7 @@ func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta } } - err = meta.(*apiClient).closeRemoteClient(d) + err = meta.(*apiClient).closeRemoteClient(conn) if err != nil { return diag.Errorf("error while closing remote client: %s", err.Error()) } @@ -99,21 +92,21 @@ func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta } func resourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - d, err := meta.(*apiClient).applyResultConn(d) + conn, err := meta.(*apiClient).getConnWithDefault(d) if err != nil { return diag.Errorf(err.Error()) } path := d.Get("path").(string) - d.SetId(fmt.Sprintf("%s:%s", d.Get("result_conn.0.host").(string), path)) + d.SetId(fmt.Sprintf("%s:%s", conn.Get("conn.0.host").(string), path)) - client, err := meta.(*apiClient).getRemoteClient(d) + client, err := meta.(*apiClient).getRemoteClient(conn) if err != nil { return diag.Errorf("error while opening remote client: %s", err.Error()) } - sudo, ok := d.GetOk("result_conn.0.sudo") + sudo, ok := conn.GetOk("conn.0.sudo") if ok && sudo.(bool) { exists, err := client.FileExistsSudo(path) if err != nil { @@ -136,7 +129,7 @@ func resourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta in d.Set("content", content) } - err = meta.(*apiClient).closeRemoteClient(d) + err = meta.(*apiClient).closeRemoteClient(conn) if err != nil { return diag.Errorf("error while closing remote client: %s", err.Error()) } @@ -149,19 +142,19 @@ func resourceRemoteFileUpdate(ctx context.Context, d *schema.ResourceData, meta } func resourceRemoteFileDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - d, err := meta.(*apiClient).applyResultConn(d) + conn, err := meta.(*apiClient).getConnWithDefault(d) if err != nil { return diag.Errorf(err.Error()) } - client, err := meta.(*apiClient).getRemoteClient(d) + client, err := meta.(*apiClient).getRemoteClient(conn) if err != nil { return diag.Errorf("error while opening remote client: %s", err.Error()) } path := d.Get("path").(string) - sudo, ok := d.GetOk("result_conn.0.sudo") + sudo, ok := conn.GetOk("conn.0.sudo") if ok && sudo.(bool) { exists, err := client.FileExistsSudo(path) if err != nil { @@ -182,7 +175,7 @@ func resourceRemoteFileDelete(ctx context.Context, d *schema.ResourceData, meta } } - err = meta.(*apiClient).closeRemoteClient(d) + err = meta.(*apiClient).closeRemoteClient(conn) if err != nil { return diag.Errorf("error while closing remote client: %s", err.Error()) }