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
ApiKey string
SecretKey string
Timeout int64
}
// Client() returns a new CloudStack client.
func (c *Config) NewClient() (*cloudstack.CloudStackClient, error) {
cs := cloudstack.NewAsyncClient(c.ApiURL, c.ApiKey, c.SecretKey, false)
cs.AsyncTimeout(180)
cs.AsyncTimeout(c.Timeout)
return cs, nil
}

View File

@ -14,19 +14,25 @@ func Provider() terraform.ResourceProvider {
"api_url": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_URL"),
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_URL", nil),
},
"api_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_KEY"),
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_KEY", nil),
},
"secret_key": &schema.Schema{
Type: schema.TypeString,
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),
ApiKey: d.Get("api_key").(string),
SecretKey: d.Get("secret_key").(string),
Timeout: d.Get("timeout").(int64),
}
return config.NewClient()
}
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
func envDefaultFunc(k string, dv interface{}) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
return dv, nil
}
}