github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/provider.go (about)

     1  package google
     2  
     3  import (
     4  	"os"
     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  			"account_file": &schema.Schema{
    15  				Type:        schema.TypeString,
    16  				Required:    true,
    17  				DefaultFunc: envDefaultFunc("GOOGLE_ACCOUNT_FILE"),
    18  			},
    19  
    20  			"client_secrets_file": &schema.Schema{
    21  				Type:        schema.TypeString,
    22  				Required:    true,
    23  				DefaultFunc: envDefaultFunc("GOOGLE_CLIENT_FILE"),
    24  			},
    25  
    26  			"project": &schema.Schema{
    27  				Type:        schema.TypeString,
    28  				Required:    true,
    29  				DefaultFunc: envDefaultFunc("GOOGLE_PROJECT"),
    30  			},
    31  
    32  			"region": &schema.Schema{
    33  				Type:        schema.TypeString,
    34  				Required:    true,
    35  				DefaultFunc: envDefaultFunc("GOOGLE_REGION"),
    36  			},
    37  		},
    38  
    39  		ResourcesMap: map[string]*schema.Resource{
    40  			"google_compute_address":  resourceComputeAddress(),
    41  			"google_compute_disk":     resourceComputeDisk(),
    42  			"google_compute_firewall": resourceComputeFirewall(),
    43  			"google_compute_instance": resourceComputeInstance(),
    44  			"google_compute_network":  resourceComputeNetwork(),
    45  			"google_compute_route":    resourceComputeRoute(),
    46  		},
    47  
    48  		ConfigureFunc: providerConfigure,
    49  	}
    50  }
    51  
    52  func envDefaultFunc(k string) schema.SchemaDefaultFunc {
    53  	return func() (interface{}, error) {
    54  		if v := os.Getenv(k); v != "" {
    55  			return v, nil
    56  		}
    57  
    58  		return nil, nil
    59  	}
    60  }
    61  
    62  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    63  	config := Config{
    64  		AccountFile:       d.Get("account_file").(string),
    65  		ClientSecretsFile: d.Get("client_secrets_file").(string),
    66  		Project:           d.Get("project").(string),
    67  		Region:            d.Get("region").(string),
    68  	}
    69  
    70  	if err := config.loadAndValidate(); err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	return &config, nil
    75  }