Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added timeout param #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ErrInvalidDecodeResponseParameter = errors.New("nil interface provided to de
type Config struct {
Address string
Auth *Auth
Timeout time.Duration
}

// NewConfig new config
Expand Down Expand Up @@ -53,7 +54,7 @@ func NewClient(config *Config) (*Client, error) {

func newTLSHTTPClient(config *Config) (*http.Client, error) {
if config.Auth == nil {
return http.DefaultClient, nil
return &http.Client{Timeout: config.Timeout}, nil
}

var cert tls.Certificate
Expand All @@ -75,7 +76,7 @@ func newTLSHTTPClient(config *Config) (*http.Client, error) {
}
cert, err = tls.LoadX509KeyPair(certPath, keyPath)
} else {
return http.DefaultClient, nil
return &http.Client{Timeout: config.Timeout}, nil
}
if err != nil {
return nil, err
Expand All @@ -87,7 +88,7 @@ func newTLSHTTPClient(config *Config) (*http.Client, error) {
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
return &http.Client{Transport: transport}, nil
return &http.Client{Transport: transport, Timeout: config.Timeout}, nil
}

func decodeBase64KeyPair(cert64, key64 string) (tls.Certificate, error) {
Expand Down
7 changes: 7 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func Provider() terraform.ResourceProvider {
Default: true,
Description: "If http client should skip ssl validation",
},

"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
Description: "Time in seconds to wait for operations of api client, default is 300s",
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
3 changes: 3 additions & 0 deletions provider/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"github.com/jgramoll/terraform-provider-spinnaker/client"
"time"
)

// config for provider
Expand All @@ -14,6 +15,7 @@ type providerConfig struct {
KeyContent string `mapstructure:"key_content"`
UserEmail string `mapstructure:"user_email"`
Insecure bool `mapstructure:"insecure"`
Timeout int `mapstructure:"timeout"`
}

func newProviderConfig() *providerConfig {
Expand All @@ -31,6 +33,7 @@ func (c *providerConfig) toClientConfig() *client.Config {
clientConfig.Auth.KeyContent = c.KeyContent
clientConfig.Auth.UserEmail = c.UserEmail
clientConfig.Auth.Insecure = c.Insecure
clientConfig.Timeout = time.Duration(c.Timeout) * time.Second

return clientConfig
}
7 changes: 7 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"os"
"testing"
"time"

"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -34,6 +35,7 @@ func TestProviderConfigure(t *testing.T) {
"cert_path": os.Getenv("SPINNAKER_CERT"),
"key_path": os.Getenv("SPINNAKER_KEY"),
},
"timeout": 300,
}
rawConfig, configErr := config.NewRawConfig(raw)
if configErr != nil {
Expand Down Expand Up @@ -62,6 +64,11 @@ func TestProviderConfigure(t *testing.T) {
if config.Auth.KeyPath != auth["key_path"] {
t.Fatalf("keyPath should be %#v, not %#v", auth["key_path"], config.Auth.KeyPath)
}

configTimeout := int(config.Timeout / time.Second)
if configTimeout != raw["timeout"] {
t.Fatalf("timeout should be %#v, not %#v", raw["timeout"], configTimeout)
}
}

func testAccPreCheck(t *testing.T) {
Expand Down