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