Update provider docs and add validation (#14863)

This commit is contained in:
Sander van Harmelen 2017-05-26 17:58:10 +02:00 committed by GitHub
parent ddf2cf920f
commit 12c2e3222d
2 changed files with 45 additions and 22 deletions

View File

@ -1,7 +1,7 @@
package cloudstack package cloudstack
import ( import (
"fmt" "errors"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
@ -90,17 +90,33 @@ func Provider() terraform.ResourceProvider {
} }
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
apiURL := d.Get("api_url").(string) apiURL, apiURLOK := d.GetOk("api_url")
apiKey := d.Get("api_key").(string) apiKey, apiKeyOK := d.GetOk("api_key")
secretKey := d.Get("secret_key").(string) secretKey, secretKeyOK := d.GetOk("secret_key")
config, configOK := d.GetOk("config")
profile, profileOK := d.GetOk("profile")
if configFile, ok := d.GetOk("config"); ok { switch {
config, err := ini.Load(configFile.(string)) case apiURLOK, apiKeyOK, secretKeyOK:
if !(apiURLOK && apiKeyOK && secretKeyOK) {
return nil, errors.New("'api_url', 'api_key' and 'secret_key' should all have values")
}
case configOK, profileOK:
if !(configOK && profileOK) {
return nil, errors.New("'config' and 'profile' should both have a value")
}
default:
return nil, errors.New(
"either 'api_url', 'api_key' and 'secret_key' or 'config' and 'profile' should have values")
}
if configOK && profileOK {
cfg, err := ini.Load(config.(string))
if err != nil { if err != nil {
return nil, err return nil, err
} }
section, err := config.GetSection(d.Get("profile").(string)) section, err := cfg.GetSection(profile.(string))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -110,17 +126,13 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
secretKey = section.Key("secretkey").String() secretKey = section.Key("secretkey").String()
} }
if apiURL == "" || apiKey == "" || secretKey == "" { cfg := Config{
return nil, fmt.Errorf("No api_url or api_key or secretKey provided") APIURL: apiURL.(string),
} APIKey: apiKey.(string),
SecretKey: secretKey.(string),
config := Config{
APIURL: apiURL,
APIKey: apiKey,
SecretKey: secretKey,
HTTPGETOnly: d.Get("http_get_only").(bool), HTTPGETOnly: d.Get("http_get_only").(bool),
Timeout: int64(d.Get("timeout").(int)), Timeout: int64(d.Get("timeout").(int)),
} }
return config.NewClient() return cfg.NewClient()
} }

View File

@ -13,6 +13,11 @@ supported by CloudStack. The provider needs to be configured with a
URL pointing to a running CloudStack API and the proper credentials URL pointing to a running CloudStack API and the proper credentials
before it can be used. before it can be used.
In order to provide the required configuration options you can either
supply values for the `api_url`, `api_key` and `secret_key` fields, or
for the `config` and `profile` fields. A combination of both is not
allowed and will not work.
Use the navigation to the left to read about the available resources. Use the navigation to the left to read about the available resources.
## Example Usage ## Example Usage
@ -35,14 +40,20 @@ resource "cloudstack_instance" "web" {
The following arguments are supported: The following arguments are supported:
* `api_url` - (Required) This is the CloudStack API URL. It must be provided, but * `api_url` - (Optional) This is the CloudStack API URL. It can also be sourced
it can also be sourced from the `CLOUDSTACK_API_URL` environment variable. from the `CLOUDSTACK_API_URL` environment variable.
* `api_key` - (Required) This is the CloudStack API key. It must be provided, but * `api_key` - (Optional) This is the CloudStack API key. It can also be sourced
it can also be sourced from the `CLOUDSTACK_API_KEY` environment variable. from the `CLOUDSTACK_API_KEY` environment variable.
* `secret_key` - (Required) This is the CloudStack secret key. It must be provided, * `secret_key` - (Optional) This is the CloudStack secret key. It can also be
but it can also be sourced from the `CLOUDSTACK_SECRET_KEY` environment variable. sourced from the `CLOUDSTACK_SECRET_KEY` environment variable.
* `config` - (Optional) The path to a `CloudMonkey` config file. If set the API
URL, key and secret will be retrieved from this file.
* `profile` - (Optional) Used together with the `config` option. Specifies which
`CloudMonkey` profile in the config file to use.
* `http_get_only` - (Optional) Some cloud providers only allow HTTP GET calls to * `http_get_only` - (Optional) Some cloud providers only allow HTTP GET calls to
their CloudStack API. If using such a provider, you need to set this to `true` their CloudStack API. If using such a provider, you need to set this to `true`