Skip to content

Commit

Permalink
Remove result_conn (#48)
Browse files Browse the repository at this point in the history
* Remove result_conn

* Fix resource ID
  • Loading branch information
tenstad authored Feb 6, 2022
1 parent e70b148 commit a3fa6b2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 86 deletions.
17 changes: 0 additions & 17 deletions docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

<a id="nestedblock--conn"></a>
### Nested Schema for `conn`
Expand All @@ -73,19 +72,3 @@ Optional:
- **sudo** (Boolean) Use sudo to gain access to file. Defaults to `false`.


<a id="nestedatt--result_conn"></a>
### 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)


20 changes: 0 additions & 20 deletions docs/resources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

<a id="nestedblock--conn"></a>
### Nested Schema for `conn`

Expand All @@ -80,19 +76,3 @@ Optional:
- **sudo** (Boolean) Use sudo to gain access to file. Defaults to `false`.


<a id="nestedatt--result_conn"></a>
### 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)


16 changes: 8 additions & 8 deletions internal/provider/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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))
Expand All @@ -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 {
Expand All @@ -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
}
7 changes: 0 additions & 7 deletions internal/provider/data_source_remote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 11 additions & 13 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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, "::")
}
Expand Down
35 changes: 14 additions & 21 deletions internal/provider/resource_remote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())
}
Expand All @@ -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 {
Expand All @@ -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())
}
Expand All @@ -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 {
Expand All @@ -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())
}
Expand All @@ -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 {
Expand All @@ -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())
}
Expand Down

0 comments on commit a3fa6b2

Please sign in to comment.