Skip to content

Commit

Permalink
Merge pull request #12 from mcanevet/endpoint
Browse files Browse the repository at this point in the history
Load configuration from ~/.ovh.conf (fixes #10)
  • Loading branch information
yanndegat authored Dec 20, 2017
2 parents db40a08 + 5b5e9b0 commit 5ae37c3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
46 changes: 39 additions & 7 deletions ovh/provider.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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"],
},
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.


Expand Down

0 comments on commit 5ae37c3

Please sign in to comment.