1
1
package ovh
2
2
3
3
import (
4
+ "fmt"
5
+ "log"
6
+ "os"
7
+ "os/user"
8
+
9
+ ini "gopkg.in/ini.v1"
10
+
4
11
"github.com/hashicorp/terraform/helper/schema"
5
12
"github.com/hashicorp/terraform/terraform"
6
13
)
@@ -17,19 +24,19 @@ func Provider() terraform.ResourceProvider {
17
24
},
18
25
"application_key" : & schema.Schema {
19
26
Type : schema .TypeString ,
20
- Required : true ,
27
+ Optional : true ,
21
28
DefaultFunc : schema .EnvDefaultFunc ("OVH_APPLICATION_KEY" , "" ),
22
29
Description : descriptions ["application_key" ],
23
30
},
24
31
"application_secret" : & schema.Schema {
25
32
Type : schema .TypeString ,
26
- Required : true ,
33
+ Optional : true ,
27
34
DefaultFunc : schema .EnvDefaultFunc ("OVH_APPLICATION_SECRET" , "" ),
28
35
Description : descriptions ["application_secret" ],
29
36
},
30
37
"consumer_key" : & schema.Schema {
31
38
Type : schema .TypeString ,
32
- Required : true ,
39
+ Optional : true ,
33
40
DefaultFunc : schema .EnvDefaultFunc ("OVH_CONSUMER_KEY" , "" ),
34
41
Description : descriptions ["consumer_key" ],
35
42
},
@@ -66,11 +73,36 @@ func init() {
66
73
}
67
74
68
75
func configureProvider (d * schema.ResourceData ) (interface {}, error ) {
76
+ usr , err := user .Current ()
77
+ if err != nil {
78
+ log .Fatal (err )
79
+ }
69
80
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 )
74
106
}
75
107
76
108
if err := config .loadAndValidate (); err != nil {
0 commit comments