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