From 58aedb9a24db8ef5368dde6717a2582020e5bf30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Can=C3=A9vet?= Date: Wed, 20 Dec 2017 11:29:36 +0100 Subject: [PATCH 1/2] Load configuration from ~/.ovh.conf --- ovh/provider.go | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/ovh/provider.go b/ovh/provider.go index ad67653d8..dc6d56128 100644 --- a/ovh/provider.go +++ b/ovh/provider.go @@ -1,6 +1,13 @@ package ovh import ( + "fmt" + "log" + "os" + "os/user" + + ini "gopkg.in/ini.v1" + "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -17,19 +24,19 @@ func Provider() terraform.ResourceProvider { }, "application_key": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, DefaultFunc: schema.EnvDefaultFunc("OVH_APPLICATION_KEY", ""), Description: descriptions["application_key"], }, "application_secret": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, DefaultFunc: schema.EnvDefaultFunc("OVH_APPLICATION_SECRET", ""), Description: descriptions["application_secret"], }, "consumer_key": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, DefaultFunc: schema.EnvDefaultFunc("OVH_CONSUMER_KEY", ""), Description: descriptions["consumer_key"], }, @@ -66,11 +73,36 @@ func init() { } func configureProvider(d *schema.ResourceData) (interface{}, error) { + usr, err := user.Current() + if err != nil { + log.Fatal(err) + } config := Config{ - Endpoint: d.Get("endpoint").(string), - ApplicationKey: d.Get("application_key").(string), - ApplicationSecret: d.Get("application_secret").(string), - ConsumerKey: d.Get("consumer_key").(string), + Endpoint: d.Get("endpoint").(string), + } + configFile := fmt.Sprintf("%s/.ovh.conf", usr.HomeDir) + if _, err := os.Stat(configFile); err == nil { + c, err := ini.Load(configFile) + if err != nil { + return nil, err + } + + section, err := c.GetSection(d.Get("endpoint").(string)) + if err != nil { + return nil, err + } + config.ApplicationKey = section.Key("application_key").String() + config.ApplicationSecret = section.Key("application_secret").String() + config.ConsumerKey = section.Key("consumer_key").String() + } + if v, ok := d.GetOk("application_key"); ok { + config.ApplicationKey = v.(string) + } + if v, ok := d.GetOk("application_secret"); ok { + config.ApplicationSecret = v.(string) + } + if v, ok := d.GetOk("consumer_key"); ok { + config.ConsumerKey = v.(string) } if err := config.loadAndValidate(); err != nil { From 5b5e9b01337c3506cb199d6bbce4eb4790d832b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Can=C3=A9vet?= Date: Wed, 20 Dec 2017 11:38:23 +0100 Subject: [PATCH 2/2] Update documentation --- website/docs/index.html.markdown | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index ac0e1a005..c6c9755ef 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -31,6 +31,10 @@ resource "ovh_publiccloud_user" "user-test" { } ``` +If you don't provider "application_key", "application_secret" or +"consumer_key", the provider will try to fetch them from the ~/.ovh.conf file +generated by ovh-cli. + ## Configuration Reference The following arguments are supported: @@ -39,13 +43,13 @@ The following arguments are supported: It can be set using the OVH_ENDPOINT environment variable. Value can be set to either "ovh-eu" or "ovh-ca". -* `application_key` - (Required) The API Application Key. If omitted, +* `application_key` - (Optional) The API Application Key. If omitted, the `OVH_APPLICATION_KEY` environment variable is used. -* `application_secret` - (Required) The API Application Secret. If omitted, +* `application_secret` - (Optional) The API Application Secret. If omitted, the `OVH_APPLICATION_SECRET` environment variable is used. -* `consumer_key` - (Required) The API Consumer key. If omitted, +* `consumer_key` - (Optional) The API Consumer key. If omitted, the `OVH_CONSUMER_KEY` environment variable is used.