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

     1  package scaleway
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/hashicorp/terraform/helper/mutexkv"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  var mu = sync.Mutex{}
    12  
    13  // Provider returns a terraform.ResourceProvider.
    14  func Provider() terraform.ResourceProvider {
    15  	return &schema.Provider{
    16  		Schema: map[string]*schema.Schema{
    17  			"access_key": &schema.Schema{
    18  				Type:        schema.TypeString,
    19  				Optional:    true,
    20  				DefaultFunc: schema.EnvDefaultFunc("SCALEWAY_ACCESS_KEY", nil),
    21  				Deprecated:  "Use `token` instead.",
    22  				Description: "The API key for Scaleway API operations.",
    23  			},
    24  			"token": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    28  					"SCALEWAY_TOKEN",
    29  					"SCALEWAY_ACCESS_KEY",
    30  				}, nil),
    31  				Description: "The API key for Scaleway API operations.",
    32  			},
    33  			"organization": &schema.Schema{
    34  				Type:        schema.TypeString,
    35  				Required:    true,
    36  				DefaultFunc: schema.EnvDefaultFunc("SCALEWAY_ORGANIZATION", nil),
    37  				Description: "The Organization ID (a.k.a. 'access key') for Scaleway API operations.",
    38  			},
    39  			"region": &schema.Schema{
    40  				Type:        schema.TypeString,
    41  				Optional:    true,
    42  				DefaultFunc: schema.EnvDefaultFunc("SCALEWAY_REGION", "par1"),
    43  				Description: "The Scaleway API region to use.",
    44  			},
    45  		},
    46  
    47  		ResourcesMap: map[string]*schema.Resource{
    48  			"scaleway_server":              resourceScalewayServer(),
    49  			"scaleway_ip":                  resourceScalewayIP(),
    50  			"scaleway_security_group":      resourceScalewaySecurityGroup(),
    51  			"scaleway_security_group_rule": resourceScalewaySecurityGroupRule(),
    52  			"scaleway_volume":              resourceScalewayVolume(),
    53  			"scaleway_volume_attachment":   resourceScalewayVolumeAttachment(),
    54  		},
    55  
    56  		DataSourcesMap: map[string]*schema.Resource{
    57  			"scaleway_bootscript": dataSourceScalewayBootscript(),
    58  			"scaleway_image":      dataSourceScalewayImage(),
    59  		},
    60  
    61  		ConfigureFunc: providerConfigure,
    62  	}
    63  }
    64  
    65  var scalewayMutexKV = mutexkv.NewMutexKV()
    66  
    67  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    68  	apiKey := ""
    69  	if v, ok := d.Get("token").(string); ok {
    70  		apiKey = v
    71  	} else {
    72  		if v, ok := d.Get("access_key").(string); ok {
    73  			apiKey = v
    74  		}
    75  	}
    76  
    77  	config := Config{
    78  		Organization: d.Get("organization").(string),
    79  		APIKey:       apiKey,
    80  		Region:       d.Get("region").(string),
    81  	}
    82  
    83  	return config.Client()
    84  }