provider/cloudstack: make timeout configurable

Seems some platforms need more time, so make it configurable with a
sane default…
This commit is contained in:
Sander van Harmelen 2015-01-12 15:57:24 +01:00
parent e9b3e5733b
commit 7b16c44cc2
2 changed files with 14 additions and 6 deletions

View File

@ -8,11 +8,12 @@ type Config struct {
ApiURL string ApiURL string
ApiKey string ApiKey string
SecretKey string SecretKey string
Timeout int64
} }
// Client() returns a new CloudStack client. // Client() returns a new CloudStack client.
func (c *Config) NewClient() (*cloudstack.CloudStackClient, error) { func (c *Config) NewClient() (*cloudstack.CloudStackClient, error) {
cs := cloudstack.NewAsyncClient(c.ApiURL, c.ApiKey, c.SecretKey, false) cs := cloudstack.NewAsyncClient(c.ApiURL, c.ApiKey, c.SecretKey, false)
cs.AsyncTimeout(180) cs.AsyncTimeout(c.Timeout)
return cs, nil return cs, nil
} }

View File

@ -14,19 +14,25 @@ func Provider() terraform.ResourceProvider {
"api_url": &schema.Schema{ "api_url": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_URL"), DefaultFunc: envDefaultFunc("CLOUDSTACK_API_URL", nil),
}, },
"api_key": &schema.Schema{ "api_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_KEY"), DefaultFunc: envDefaultFunc("CLOUDSTACK_API_KEY", nil),
}, },
"secret_key": &schema.Schema{ "secret_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_SECRET_KEY"), DefaultFunc: envDefaultFunc("CLOUDSTACK_SECRET_KEY", nil),
},
"timeout": &schema.Schema{
Type: schema.TypeInt,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_TIMEOUT", 180),
}, },
}, },
@ -52,17 +58,18 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
ApiURL: d.Get("api_url").(string), ApiURL: d.Get("api_url").(string),
ApiKey: d.Get("api_key").(string), ApiKey: d.Get("api_key").(string),
SecretKey: d.Get("secret_key").(string), SecretKey: d.Get("secret_key").(string),
Timeout: d.Get("timeout").(int64),
} }
return config.NewClient() return config.NewClient()
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc { func envDefaultFunc(k string, dv interface{}) schema.SchemaDefaultFunc {
return func() (interface{}, error) { return func() (interface{}, error) {
if v := os.Getenv(k); v != "" { if v := os.Getenv(k); v != "" {
return v, nil return v, nil
} }
return nil, nil return dv, nil
} }
} }