2016-12-05 16:29:41 +01:00
|
|
|
package rancher
|
|
|
|
|
|
|
|
import (
|
2017-02-15 10:51:56 +01:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
2016-12-05 16:29:41 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2017-02-15 10:51:56 +01:00
|
|
|
type CLIConfig struct {
|
|
|
|
AccessKey string `json:"accessKey"`
|
|
|
|
SecretKey string `json:"secretKey"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Environment string `json:"environment"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:29:41 +01:00
|
|
|
// Provider returns a terraform.ResourceProvider.
|
|
|
|
func Provider() terraform.ResourceProvider {
|
|
|
|
return &schema.Provider{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"api_url": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2017-02-15 10:51:56 +01:00
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("RANCHER_URL", ""),
|
2016-12-05 16:29:41 +01:00
|
|
|
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"],
|
|
|
|
},
|
2017-02-15 10:51:56 +01:00
|
|
|
"config": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("RANCHER_CLIENT_CONFIG", ""),
|
|
|
|
Description: descriptions["config"],
|
|
|
|
},
|
2016-12-05 16:29:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2017-03-15 15:22:34 +01:00
|
|
|
"rancher_certificate": resourceRancherCertificate(),
|
2016-12-05 16:29:41 +01:00
|
|
|
"rancher_environment": resourceRancherEnvironment(),
|
2017-03-14 18:17:09 +01:00
|
|
|
"rancher_host": resourceRancherHost(),
|
2016-12-05 16:29:41 +01:00
|
|
|
"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",
|
2017-02-15 10:51:56 +01:00
|
|
|
|
|
|
|
"config": "Path to the Rancher client cli.json config file",
|
2016-12-05 16:29:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
2017-02-15 10:51:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:29:41 +01:00
|
|
|
config := &Config{
|
2017-02-15 10:51:56 +01:00
|
|
|
APIURL: apiURL + "/v1",
|
|
|
|
AccessKey: accessKey,
|
|
|
|
SecretKey: secretKey,
|
2016-12-05 16:29:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 11:35:48 +01:00
|
|
|
_, err := config.GlobalClient()
|
2016-12-05 16:29:41 +01:00
|
|
|
|
|
|
|
return config, err
|
|
|
|
}
|
2017-02-15 10:51:56 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|