github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/consul/resource_provider.go (about)

     1  package consul
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"github.com/mitchellh/mapstructure"
     9  )
    10  
    11  // Provider returns a terraform.ResourceProvider.
    12  func Provider() terraform.ResourceProvider {
    13  	return &schema.Provider{
    14  		Schema: map[string]*schema.Schema{
    15  			"datacenter": &schema.Schema{
    16  				Type:     schema.TypeString,
    17  				Optional: true,
    18  			},
    19  
    20  			"address": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    24  					"CONSUL_ADDRESS",
    25  					"CONSUL_HTTP_ADDR",
    26  				}, "localhost:8500"),
    27  			},
    28  
    29  			"scheme": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Optional: true,
    32  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    33  					"CONSUL_SCHEME",
    34  					"CONSUL_HTTP_SCHEME",
    35  				}, "http"),
    36  			},
    37  
    38  			"http_auth": &schema.Schema{
    39  				Type:        schema.TypeString,
    40  				Optional:    true,
    41  				DefaultFunc: schema.EnvDefaultFunc("CONSUL_HTTP_AUTH", ""),
    42  			},
    43  
    44  			"ca_file": &schema.Schema{
    45  				Type:        schema.TypeString,
    46  				Optional:    true,
    47  				DefaultFunc: schema.EnvDefaultFunc("CONSUL_CA_FILE", ""),
    48  			},
    49  
    50  			"cert_file": &schema.Schema{
    51  				Type:        schema.TypeString,
    52  				Optional:    true,
    53  				DefaultFunc: schema.EnvDefaultFunc("CONSUL_CERT_FILE", ""),
    54  			},
    55  
    56  			"key_file": &schema.Schema{
    57  				Type:        schema.TypeString,
    58  				Optional:    true,
    59  				DefaultFunc: schema.EnvDefaultFunc("CONSUL_KEY_FILE", ""),
    60  			},
    61  
    62  			"token": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Optional: true,
    65  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    66  					"CONSUL_TOKEN",
    67  					"CONSUL_HTTP_TOKEN",
    68  				}, ""),
    69  			},
    70  		},
    71  
    72  		DataSourcesMap: map[string]*schema.Resource{
    73  			"consul_agent_self":       dataSourceConsulAgentSelf(),
    74  			"consul_catalog_nodes":    dataSourceConsulCatalogNodes(),
    75  			"consul_catalog_service":  dataSourceConsulCatalogService(),
    76  			"consul_catalog_services": dataSourceConsulCatalogServices(),
    77  			"consul_keys":             dataSourceConsulKeys(),
    78  		},
    79  
    80  		ResourcesMap: map[string]*schema.Resource{
    81  			"consul_agent_service":  resourceConsulAgentService(),
    82  			"consul_catalog_entry":  resourceConsulCatalogEntry(),
    83  			"consul_keys":           resourceConsulKeys(),
    84  			"consul_key_prefix":     resourceConsulKeyPrefix(),
    85  			"consul_node":           resourceConsulNode(),
    86  			"consul_prepared_query": resourceConsulPreparedQuery(),
    87  			"consul_service":        resourceConsulService(),
    88  		},
    89  
    90  		ConfigureFunc: providerConfigure,
    91  	}
    92  }
    93  
    94  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    95  	var config Config
    96  	configRaw := d.Get("").(map[string]interface{})
    97  	if err := mapstructure.Decode(configRaw, &config); err != nil {
    98  		return nil, err
    99  	}
   100  	log.Printf("[INFO] Initializing Consul client")
   101  	return config.Client()
   102  }