Skip to content

Commit e4bd7e2

Browse files
mcanevetSander van Harmelen
authored andcommitted
Load Cloudstack configuration from file (hashicorp#13926)
* Load Cloudstack configuration from file * Add ConflictsWith
1 parent 17bbdef commit e4bd7e2

1 file changed

Lines changed: 54 additions & 12 deletions

File tree

builtin/providers/cloudstack/provider.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cloudstack
22

33
import (
4+
"fmt"
5+
6+
"github.com/go-ini/ini"
47
"github.com/hashicorp/terraform/helper/schema"
58
"github.com/hashicorp/terraform/terraform"
69
)
@@ -10,21 +13,36 @@ func Provider() terraform.ResourceProvider {
1013
return &schema.Provider{
1114
Schema: map[string]*schema.Schema{
1215
"api_url": &schema.Schema{
13-
Type: schema.TypeString,
14-
Required: true,
15-
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_URL", nil),
16+
Type: schema.TypeString,
17+
Optional: true,
18+
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_URL", nil),
19+
ConflictsWith: []string{"config", "profile"},
1620
},
1721

1822
"api_key": &schema.Schema{
19-
Type: schema.TypeString,
20-
Required: true,
21-
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_KEY", nil),
23+
Type: schema.TypeString,
24+
Optional: true,
25+
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_KEY", nil),
26+
ConflictsWith: []string{"config", "profile"},
2227
},
2328

2429
"secret_key": &schema.Schema{
25-
Type: schema.TypeString,
26-
Required: true,
27-
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_SECRET_KEY", nil),
30+
Type: schema.TypeString,
31+
Optional: true,
32+
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_SECRET_KEY", nil),
33+
ConflictsWith: []string{"config", "profile"},
34+
},
35+
36+
"config": &schema.Schema{
37+
Type: schema.TypeString,
38+
Optional: true,
39+
ConflictsWith: []string{"api_url", "api_key", "secret_key"},
40+
},
41+
42+
"profile": &schema.Schema{
43+
Type: schema.TypeString,
44+
Optional: true,
45+
ConflictsWith: []string{"api_url", "api_key", "secret_key"},
2846
},
2947

3048
"http_get_only": &schema.Schema{
@@ -72,10 +90,34 @@ func Provider() terraform.ResourceProvider {
7290
}
7391

7492
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
93+
apiURL := d.Get("api_url").(string)
94+
apiKey := d.Get("api_key").(string)
95+
secretKey := d.Get("secret_key").(string)
96+
97+
if configFile, ok := d.GetOk("config"); ok {
98+
config, err := ini.Load(configFile.(string))
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
section, err := config.GetSection(d.Get("profile").(string))
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
apiURL = section.Key("url").String()
109+
apiKey = section.Key("apikey").String()
110+
secretKey = section.Key("secretkey").String()
111+
}
112+
113+
if apiURL == "" || apiKey == "" || secretKey == "" {
114+
return nil, fmt.Errorf("No api_url or api_key or secretKey provided")
115+
}
116+
75117
config := Config{
76-
APIURL: d.Get("api_url").(string),
77-
APIKey: d.Get("api_key").(string),
78-
SecretKey: d.Get("secret_key").(string),
118+
APIURL: apiURL,
119+
APIKey: apiKey,
120+
SecretKey: secretKey,
79121
HTTPGETOnly: d.Get("http_get_only").(bool),
80122
Timeout: int64(d.Get("timeout").(int)),
81123
}

0 commit comments

Comments
 (0)