github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/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  // CLIConfig used to store data from file.
    15  type CLIConfig struct {
    16  	AccessKey   string `json:"accessKey"`
    17  	SecretKey   string `json:"secretKey"`
    18  	URL         string `json:"url"`
    19  	Environment string `json:"environment"`
    20  	Path        string `json:"path,omitempty"`
    21  }
    22  
    23  // Provider returns a terraform.ResourceProvider.
    24  func Provider() terraform.ResourceProvider {
    25  	return &schema.Provider{
    26  		Schema: map[string]*schema.Schema{
    27  			"api_url": &schema.Schema{
    28  				Type:        schema.TypeString,
    29  				Optional:    true,
    30  				DefaultFunc: schema.EnvDefaultFunc("RANCHER_URL", ""),
    31  				Description: descriptions["api_url"],
    32  			},
    33  			"access_key": &schema.Schema{
    34  				Type:        schema.TypeString,
    35  				Optional:    true,
    36  				DefaultFunc: schema.EnvDefaultFunc("RANCHER_ACCESS_KEY", ""),
    37  				Description: descriptions["access_key"],
    38  			},
    39  			"secret_key": &schema.Schema{
    40  				Type:        schema.TypeString,
    41  				Optional:    true,
    42  				DefaultFunc: schema.EnvDefaultFunc("RANCHER_SECRET_KEY", ""),
    43  				Description: descriptions["secret_key"],
    44  			},
    45  			"config": &schema.Schema{
    46  				Type:        schema.TypeString,
    47  				Optional:    true,
    48  				DefaultFunc: schema.EnvDefaultFunc("RANCHER_CLIENT_CONFIG", ""),
    49  				Description: descriptions["config"],
    50  			},
    51  		},
    52  
    53  		ResourcesMap: map[string]*schema.Resource{
    54  			"rancher_certificate":         resourceRancherCertificate(),
    55  			"rancher_environment":         resourceRancherEnvironment(),
    56  			"rancher_host":                resourceRancherHost(),
    57  			"rancher_registration_token":  resourceRancherRegistrationToken(),
    58  			"rancher_registry":            resourceRancherRegistry(),
    59  			"rancher_registry_credential": resourceRancherRegistryCredential(),
    60  			"rancher_stack":               resourceRancherStack(),
    61  		},
    62  
    63  		ConfigureFunc: providerConfigure,
    64  	}
    65  }
    66  
    67  var descriptions map[string]string
    68  
    69  func init() {
    70  	descriptions = map[string]string{
    71  		"access_key": "API Key used to authenticate with the rancher server",
    72  
    73  		"secret_key": "API secret used to authenticate with the rancher server",
    74  
    75  		"api_url": "The URL to the rancher API, must include version uri (ie. v1 or v2-beta)",
    76  
    77  		"config": "Path to the Rancher client cli.json config file",
    78  	}
    79  }
    80  
    81  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    82  	apiURL := d.Get("api_url").(string)
    83  	accessKey := d.Get("access_key").(string)
    84  	secretKey := d.Get("secret_key").(string)
    85  
    86  	if configFile := d.Get("config").(string); configFile != "" {
    87  		config, err := loadConfig(configFile)
    88  		if err != nil {
    89  			return config, err
    90  		}
    91  
    92  		if apiURL == "" && config.URL != "" {
    93  			u, err := url.Parse(config.URL)
    94  			if err != nil {
    95  				return config, err
    96  			}
    97  			apiURL = u.Scheme + "://" + u.Host
    98  		}
    99  
   100  		if accessKey == "" {
   101  			accessKey = config.AccessKey
   102  		}
   103  
   104  		if secretKey == "" {
   105  			secretKey = config.SecretKey
   106  		}
   107  	}
   108  
   109  	if apiURL == "" {
   110  		return &Config{}, fmt.Errorf("No api_url provided")
   111  	}
   112  
   113  	config := &Config{
   114  		APIURL:    apiURL,
   115  		AccessKey: accessKey,
   116  		SecretKey: secretKey,
   117  	}
   118  
   119  	client, err := config.GlobalClient()
   120  	if err != nil {
   121  		return &Config{}, err
   122  	}
   123  	// Let Rancher Client normalizes the URL making it reliable as a base.
   124  	config.APIURL = client.GetOpts().Url
   125  
   126  	return config, nil
   127  }
   128  
   129  func loadConfig(path string) (CLIConfig, error) {
   130  	config := CLIConfig{
   131  		Path: path,
   132  	}
   133  
   134  	content, err := ioutil.ReadFile(path)
   135  	if os.IsNotExist(err) {
   136  		return config, nil
   137  	} else if err != nil {
   138  		return config, err
   139  	}
   140  
   141  	err = json.Unmarshal(content, &config)
   142  	config.Path = path
   143  
   144  	return config, err
   145  }