Skip to content

Commit 5ae37c3

Browse files
authored
Merge pull request #12 from mcanevet/endpoint
Load configuration from ~/.ovh.conf (fixes #10)
2 parents db40a08 + 5b5e9b0 commit 5ae37c3

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

ovh/provider.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package ovh
22

33
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"os/user"
8+
9+
ini "gopkg.in/ini.v1"
10+
411
"github.com/hashicorp/terraform/helper/schema"
512
"github.com/hashicorp/terraform/terraform"
613
)
@@ -17,19 +24,19 @@ func Provider() terraform.ResourceProvider {
1724
},
1825
"application_key": &schema.Schema{
1926
Type: schema.TypeString,
20-
Required: true,
27+
Optional: true,
2128
DefaultFunc: schema.EnvDefaultFunc("OVH_APPLICATION_KEY", ""),
2229
Description: descriptions["application_key"],
2330
},
2431
"application_secret": &schema.Schema{
2532
Type: schema.TypeString,
26-
Required: true,
33+
Optional: true,
2734
DefaultFunc: schema.EnvDefaultFunc("OVH_APPLICATION_SECRET", ""),
2835
Description: descriptions["application_secret"],
2936
},
3037
"consumer_key": &schema.Schema{
3138
Type: schema.TypeString,
32-
Required: true,
39+
Optional: true,
3340
DefaultFunc: schema.EnvDefaultFunc("OVH_CONSUMER_KEY", ""),
3441
Description: descriptions["consumer_key"],
3542
},
@@ -66,11 +73,36 @@ func init() {
6673
}
6774

6875
func configureProvider(d *schema.ResourceData) (interface{}, error) {
76+
usr, err := user.Current()
77+
if err != nil {
78+
log.Fatal(err)
79+
}
6980
config := Config{
70-
Endpoint: d.Get("endpoint").(string),
71-
ApplicationKey: d.Get("application_key").(string),
72-
ApplicationSecret: d.Get("application_secret").(string),
73-
ConsumerKey: d.Get("consumer_key").(string),
81+
Endpoint: d.Get("endpoint").(string),
82+
}
83+
configFile := fmt.Sprintf("%s/.ovh.conf", usr.HomeDir)
84+
if _, err := os.Stat(configFile); err == nil {
85+
c, err := ini.Load(configFile)
86+
if err != nil {
87+
return nil, err
88+
}
89+
90+
section, err := c.GetSection(d.Get("endpoint").(string))
91+
if err != nil {
92+
return nil, err
93+
}
94+
config.ApplicationKey = section.Key("application_key").String()
95+
config.ApplicationSecret = section.Key("application_secret").String()
96+
config.ConsumerKey = section.Key("consumer_key").String()
97+
}
98+
if v, ok := d.GetOk("application_key"); ok {
99+
config.ApplicationKey = v.(string)
100+
}
101+
if v, ok := d.GetOk("application_secret"); ok {
102+
config.ApplicationSecret = v.(string)
103+
}
104+
if v, ok := d.GetOk("consumer_key"); ok {
105+
config.ConsumerKey = v.(string)
74106
}
75107

76108
if err := config.loadAndValidate(); err != nil {

website/docs/index.html.markdown

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ resource "ovh_publiccloud_user" "user-test" {
3131
}
3232
```
3333

34+
If you don't provider "application_key", "application_secret" or
35+
"consumer_key", the provider will try to fetch them from the ~/.ovh.conf file
36+
generated by ovh-cli.
37+
3438
## Configuration Reference
3539

3640
The following arguments are supported:
@@ -39,13 +43,13 @@ The following arguments are supported:
3943
It can be set using the OVH_ENDPOINT environment
4044
variable. Value can be set to either "ovh-eu" or "ovh-ca".
4145

42-
* `application_key` - (Required) The API Application Key. If omitted,
46+
* `application_key` - (Optional) The API Application Key. If omitted,
4347
the `OVH_APPLICATION_KEY` environment variable is used.
4448

45-
* `application_secret` - (Required) The API Application Secret. If omitted,
49+
* `application_secret` - (Optional) The API Application Secret. If omitted,
4650
the `OVH_APPLICATION_SECRET` environment variable is used.
4751

48-
* `consumer_key` - (Required) The API Consumer key. If omitted,
52+
* `consumer_key` - (Optional) The API Consumer key. If omitted,
4953
the `OVH_CONSUMER_KEY` environment variable is used.
5054

5155

0 commit comments

Comments
 (0)