From 12c2e3222d37d8c6fa1a020e9609897e5d23ca91 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Fri, 26 May 2017 17:58:10 +0200 Subject: [PATCH] Update provider docs and add validation (#14863) --- builtin/providers/cloudstack/provider.go | 44 ++++++++++++------- .../providers/cloudstack/index.html.markdown | 23 +++++++--- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/builtin/providers/cloudstack/provider.go b/builtin/providers/cloudstack/provider.go index 9ce5752d0..ebf6670f1 100644 --- a/builtin/providers/cloudstack/provider.go +++ b/builtin/providers/cloudstack/provider.go @@ -1,7 +1,7 @@ package cloudstack import ( - "fmt" + "errors" "github.com/go-ini/ini" "github.com/hashicorp/terraform/helper/schema" @@ -90,17 +90,33 @@ func Provider() terraform.ResourceProvider { } 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) + apiURL, apiURLOK := d.GetOk("api_url") + apiKey, apiKeyOK := d.GetOk("api_key") + secretKey, secretKeyOK := d.GetOk("secret_key") + config, configOK := d.GetOk("config") + profile, profileOK := d.GetOk("profile") - if configFile, ok := d.GetOk("config"); ok { - config, err := ini.Load(configFile.(string)) + switch { + 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 { return nil, err } - section, err := config.GetSection(d.Get("profile").(string)) + section, err := cfg.GetSection(profile.(string)) if err != nil { return nil, err } @@ -110,17 +126,13 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { secretKey = section.Key("secretkey").String() } - if apiURL == "" || apiKey == "" || secretKey == "" { - return nil, fmt.Errorf("No api_url or api_key or secretKey provided") - } - - config := Config{ - APIURL: apiURL, - APIKey: apiKey, - SecretKey: secretKey, + cfg := Config{ + APIURL: apiURL.(string), + APIKey: apiKey.(string), + SecretKey: secretKey.(string), HTTPGETOnly: d.Get("http_get_only").(bool), Timeout: int64(d.Get("timeout").(int)), } - return config.NewClient() + return cfg.NewClient() } diff --git a/website/source/docs/providers/cloudstack/index.html.markdown b/website/source/docs/providers/cloudstack/index.html.markdown index 54ad7ab2c..8db82a0fa 100644 --- a/website/source/docs/providers/cloudstack/index.html.markdown +++ b/website/source/docs/providers/cloudstack/index.html.markdown @@ -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 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. ## Example Usage @@ -35,14 +40,20 @@ resource "cloudstack_instance" "web" { The following arguments are supported: -* `api_url` - (Required) This is the CloudStack API URL. It must be provided, but - it can also be sourced from the `CLOUDSTACK_API_URL` environment variable. +* `api_url` - (Optional) This is the CloudStack API URL. It can also be sourced + from the `CLOUDSTACK_API_URL` environment variable. -* `api_key` - (Required) This is the CloudStack API key. It must be provided, but - it can also be sourced from the `CLOUDSTACK_API_KEY` environment variable. +* `api_key` - (Optional) This is the CloudStack API key. It can also be sourced + from the `CLOUDSTACK_API_KEY` environment variable. -* `secret_key` - (Required) This is the CloudStack secret key. It must be provided, - but it can also be sourced from the `CLOUDSTACK_SECRET_KEY` environment variable. +* `secret_key` - (Optional) This is the CloudStack secret key. It can also be + 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 their CloudStack API. If using such a provider, you need to set this to `true`