github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/dnsimple/provider.go (about)

     1  package dnsimple
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  // Provider returns a terraform.ResourceProvider.
    11  func Provider() terraform.ResourceProvider {
    12  	return &schema.Provider{
    13  		Schema: map[string]*schema.Schema{
    14  			"email": &schema.Schema{
    15  				Type:        schema.TypeString,
    16  				Optional:    true,
    17  				DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_EMAIL", nil),
    18  				Description: "The DNSimple account email address.",
    19  			},
    20  
    21  			"token": &schema.Schema{
    22  				Type:        schema.TypeString,
    23  				Required:    true,
    24  				DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_TOKEN", nil),
    25  				Description: "The API v2 token for API operations.",
    26  			},
    27  
    28  			"account": &schema.Schema{
    29  				Type:        schema.TypeString,
    30  				Required:    true,
    31  				DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_ACCOUNT", nil),
    32  				Description: "The account for API operations.",
    33  			},
    34  		},
    35  
    36  		ResourcesMap: map[string]*schema.Resource{
    37  			"dnsimple_record": resourceDNSimpleRecord(),
    38  		},
    39  
    40  		ConfigureFunc: providerConfigure,
    41  	}
    42  }
    43  
    44  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    45  	// DNSimple API v1 requires email+token to authenticate.
    46  	// DNSimple API v2 requires only an OAuth token and in this particular case
    47  	// the reference of the account for API operations (to avoid fetching it in real time).
    48  	//
    49  	// v2 is not backward compatible with v1, therefore return an error in case email is set,
    50  	// to inform the user to upgrade to v2. Also, v1 token is not the same of v2.
    51  	if email := d.Get("email").(string); email != "" {
    52  		return nil, errors.New(
    53  			"DNSimple API v2 requires an account identifier and the new OAuth token. " +
    54  				"Please upgrade your configuration.")
    55  	}
    56  
    57  	config := Config{
    58  		Token:   d.Get("token").(string),
    59  		Account: d.Get("account").(string),
    60  	}
    61  
    62  	return config.Client()
    63  }