forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider.go
132 lines (109 loc) · 3.14 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package rancher
import (
"encoding/json"
"io/ioutil"
"net/url"
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
type CLIConfig struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
URL string `json:"url"`
Environment string `json:"environment"`
Path string `json:"path,omitempty"`
}
// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"api_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("RANCHER_URL", ""),
Description: descriptions["api_url"],
},
"access_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("RANCHER_ACCESS_KEY", ""),
Description: descriptions["access_key"],
},
"secret_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("RANCHER_SECRET_KEY", ""),
Description: descriptions["secret_key"],
},
"config": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("RANCHER_CLIENT_CONFIG", ""),
Description: descriptions["config"],
},
},
ResourcesMap: map[string]*schema.Resource{
"rancher_environment": resourceRancherEnvironment(),
"rancher_registration_token": resourceRancherRegistrationToken(),
"rancher_registry": resourceRancherRegistry(),
"rancher_registry_credential": resourceRancherRegistryCredential(),
"rancher_stack": resourceRancherStack(),
},
ConfigureFunc: providerConfigure,
}
}
var descriptions map[string]string
func init() {
descriptions = map[string]string{
"access_key": "API Key used to authenticate with the rancher server",
"secret_key": "API secret used to authenticate with the rancher server",
"api_url": "The URL to the rancher API",
"config": "Path to the Rancher client cli.json config file",
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
apiURL := d.Get("api_url").(string)
accessKey := d.Get("access_key").(string)
secretKey := d.Get("secret_key").(string)
if configFile := d.Get("config").(string); configFile != "" {
config, err := loadConfig(configFile)
if err != nil {
return config, err
}
if apiURL == "" {
u, err := url.Parse(config.URL)
if err != nil {
return config, err
}
apiURL = u.Scheme + "://" + u.Host
}
if accessKey == "" {
accessKey = config.AccessKey
}
if secretKey == "" {
secretKey = config.SecretKey
}
}
config := &Config{
APIURL: apiURL + "/v1",
AccessKey: accessKey,
SecretKey: secretKey,
}
_, err := config.GlobalClient()
return config, err
}
func loadConfig(path string) (CLIConfig, error) {
config := CLIConfig{
Path: path,
}
content, err := ioutil.ReadFile(path)
if os.IsNotExist(err) {
return config, nil
} else if err != nil {
return config, err
}
err = json.Unmarshal(content, &config)
config.Path = path
return config, err
}