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