github.com/sixgill/terraform@v0.9.0-beta2.0.20170316214032-033f6226ae50/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_certificate":         resourceRancherCertificate(),
    53  			"rancher_environment":         resourceRancherEnvironment(),
    54  			"rancher_host":                resourceRancherHost(),
    55  			"rancher_registration_token":  resourceRancherRegistrationToken(),
    56  			"rancher_registry":            resourceRancherRegistry(),
    57  			"rancher_registry_credential": resourceRancherRegistryCredential(),
    58  			"rancher_stack":               resourceRancherStack(),
    59  		},
    60  
    61  		ConfigureFunc: providerConfigure,
    62  	}
    63  }
    64  
    65  var descriptions map[string]string
    66  
    67  func init() {
    68  	descriptions = map[string]string{
    69  		"access_key": "API Key used to authenticate with the rancher server",
    70  
    71  		"secret_key": "API secret used to authenticate with the rancher server",
    72  
    73  		"api_url": "The URL to the rancher API",
    74  
    75  		"config": "Path to the Rancher client cli.json config file",
    76  	}
    77  }
    78  
    79  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    80  	apiURL := d.Get("api_url").(string)
    81  	accessKey := d.Get("access_key").(string)
    82  	secretKey := d.Get("secret_key").(string)
    83  
    84  	if configFile := d.Get("config").(string); configFile != "" {
    85  		config, err := loadConfig(configFile)
    86  		if err != nil {
    87  			return config, err
    88  		}
    89  
    90  		if apiURL == "" {
    91  			u, err := url.Parse(config.URL)
    92  			if err != nil {
    93  				return config, err
    94  			}
    95  			apiURL = u.Scheme + "://" + u.Host
    96  		}
    97  
    98  		if accessKey == "" {
    99  			accessKey = config.AccessKey
   100  		}
   101  
   102  		if secretKey == "" {
   103  			secretKey = config.SecretKey
   104  		}
   105  	}
   106  
   107  	config := &Config{
   108  		APIURL:    apiURL + "/v1",
   109  		AccessKey: accessKey,
   110  		SecretKey: secretKey,
   111  	}
   112  
   113  	_, err := config.GlobalClient()
   114  
   115  	return config, err
   116  }
   117  
   118  func loadConfig(path string) (CLIConfig, error) {
   119  	config := CLIConfig{
   120  		Path: path,
   121  	}
   122  
   123  	content, err := ioutil.ReadFile(path)
   124  	if os.IsNotExist(err) {
   125  		return config, nil
   126  	} else if err != nil {
   127  		return config, err
   128  	}
   129  
   130  	err = json.Unmarshal(content, &config)
   131  	config.Path = path
   132  
   133  	return config, err
   134  }