Skip to content

Commit

Permalink
feat: timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Sep 28, 2022
1 parent 8dfc65a commit d67665b
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 12 deletions.
5 changes: 4 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ clean:
# Run acceptance tests
test: hosts
$(CONTAINER_RUNTIME) run --rm --net remote -v ~/go:/go:z -v $(PWD):/provider:z --workdir /provider \
-e "TF_ACC=1" -e "TF_ACC_TERRAFORM_VERSION=1.0.11" -e "TESTARGS=$(TESTARGS)" \
-e "TF_LOG=INFO" -e "TF_ACC=1" -e "TF_ACC_TERRAFORM_VERSION=1.0.11" -e "TESTARGS=$(TESTARGS)" \
golang:1.16 bash tests/test.sh

# Install provider in playground
Expand All @@ -29,3 +29,6 @@ BIN_PATH=$(INSTALL_DIR)/$(PROVIDER_PATH)/terraform-provider-remote_v99.0.0
install:
mkdir -p $(INSTALL_DIR)/$(PROVIDER_PATH)
go build -ldflags="-s -w -X main.version=99.0.0" -o $(BIN_PATH)

doc:
go generate
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ In `playground/`:

- `make install` to compile and install the provider in the playground
- `make hosts` to start containers to use as remote hosts
- Optionally use `export TF_LOG=INFO` and
`tflog.Info(ctx, "message from provider")` to log from the provider.
- Evaluate your changes my modifying `main.tf`
and running `terraform plan` or `terraform apply`
- `docker exec -it remotehost sh` to enter remote host
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ Optional:
- `private_key_env_var` (String) The name of the local environment variable containing the private key used to login to the remote host.
- `private_key_path` (String) The local path to the private key used to login to the remote host.
- `sudo` (Boolean) Use sudo to gain access to file. Defaults to `false`.
- `timeout` (Number) The maximum amount of time, in milliseconds, for the TCP connection to establish. Timeout of zero means no timeout.


1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ Optional:
- `private_key_env_var` (String) The name of the local environment variable containing the private key used to login to the remote host.
- `private_key_path` (String) The local path to the private key used to login to the remote host.
- `sudo` (Boolean) Use sudo to gain access to file. Defaults to `false`.
- `timeout` (Number) The maximum amount of time, in milliseconds, for the TCP connection to establish. Timeout of zero means no timeout.
1 change: 1 addition & 0 deletions docs/resources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ Optional:
- `private_key_env_var` (String) The name of the local environment variable containing the private key used to login to the remote host.
- `private_key_path` (String) The local path to the private key used to login to the remote host.
- `sudo` (Boolean) Use sudo to gain access to file. Defaults to `false`.
- `timeout` (Number) The maximum amount of time, in milliseconds, for the TCP connection to establish. Timeout of zero means no timeout.


1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/bramvdbogaerde/go-scp v0.0.0-20210327204631-70ee53679fc9
github.com/hashicorp/terraform-plugin-docs v0.10.1
github.com/hashicorp/terraform-plugin-log v0.4.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.17.0
github.com/pkg/sftp v1.13.5
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
Expand Down
16 changes: 14 additions & 2 deletions internal/provider/connection.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package provider

import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -65,11 +67,16 @@ var connectionSchemaResource = &schema.Resource{
Default: false,
Description: "Use a local SSH agent to login to the remote host.",
},

"timeout": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: "The maximum amount of time, in milliseconds, for the TCP connection to establish. Timeout of zero means no timeout.",
},
},
}

func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConfig, error) {
func ConnectionFromResourceData(ctx context.Context, d *schema.ResourceData) (string, *ssh.ClientConfig, error) {
_, ok := d.GetOk("conn")
if !ok {
return "", nil, fmt.Errorf("resouce does not have a connection configured")
Expand Down Expand Up @@ -126,6 +133,11 @@ func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConf
clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeysCallback(agent.NewClient(connection).Signers))
}

timeout, ok := d.GetOk("conn.0.timeout")
if ok {
clientConfig.Timeout = time.Duration(timeout.(int)) * time.Millisecond
}

host := fmt.Sprintf("%s:%d", d.Get("conn.0.host").(string), d.Get("conn.0.port").(int))
return host, &clientConfig, nil
}
2 changes: 1 addition & 1 deletion internal/provider/data_source_remote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func dataSourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta

setResourceID(d, conn)

client, err := meta.(*apiClient).getRemoteClient(conn)
client, err := meta.(*apiClient).getRemoteClient(ctx, conn)
if err != nil {
return diag.Errorf("unable to open remote client: %s", err.Error())
}
Expand Down
10 changes: 5 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (c *apiClient) getConnWithDefault(d *schema.ResourceData) (*schema.Resource

c.mux.Lock()
defer c.mux.Unlock()

_, ok = c.resourceData.GetOk("conn")
if ok {
return c.resourceData, nil
Expand All @@ -100,7 +100,7 @@ func (c *apiClient) getConnWithDefault(d *schema.ResourceData) (*schema.Resource
return nil, errors.New("neither the provider nor the resource/data source have a configured connection")
}

func (c *apiClient) getRemoteClient(d *schema.ResourceData) (*RemoteClient, error) {
func (c *apiClient) getRemoteClient(ctx context.Context, d *schema.ResourceData) (*RemoteClient, error) {
connectionID := resourceConnectionHash(d)
defer c.mux.Unlock()
for {
Expand All @@ -117,7 +117,7 @@ func (c *apiClient) getRemoteClient(d *schema.ResourceData) (*RemoteClient, erro
return client, nil
}

client, err := remoteClientFromResourceData(d)
client, err := remoteClientFromResourceData(ctx, d)
if err != nil {
return nil, err
}
Expand All @@ -128,8 +128,8 @@ func (c *apiClient) getRemoteClient(d *schema.ResourceData) (*RemoteClient, erro
}
}

func remoteClientFromResourceData(d *schema.ResourceData) (*RemoteClient, error) {
host, clientConfig, err := ConnectionFromResourceData(d)
func remoteClientFromResourceData(ctx context.Context, d *schema.ResourceData) (*RemoteClient, error) {
host, clientConfig, err := ConnectionFromResourceData(ctx, d)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/resource_remote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta

setResourceID(d, conn)

client, err := meta.(*apiClient).getRemoteClient(conn)
client, err := meta.(*apiClient).getRemoteClient(ctx, conn)
if err != nil {
return diag.Errorf("unable to open remote client: %s", err.Error())
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func resourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta in

setResourceID(d, conn)

client, err := meta.(*apiClient).getRemoteClient(conn)
client, err := meta.(*apiClient).getRemoteClient(ctx, conn)
if err != nil {
return diag.Errorf("unable to open remote client: %s", err.Error())
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func resourceRemoteFileDelete(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf(err.Error())
}

client, err := meta.(*apiClient).getRemoteClient(conn)
client, err := meta.(*apiClient).getRemoteClient(ctx, conn)
if err != nil {
return diag.Errorf("unable to open remote client: %s", err.Error())
}
Expand Down
1 change: 1 addition & 0 deletions internal/provider/resource_remote_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestAccResourceRemoteFile(t *testing.T) {
user = "root"
sudo = true
password = "password"
timeout = 1000
}
path = "/tmp/resource_1.txt"
content = "resource_1"
Expand Down

0 comments on commit d67665b

Please sign in to comment.