github.com/jmbataller/terraform@v0.6.8-0.20151125192640-b7a12e3a580c/builtin/providers/google/provider.go (about)

     1  package google
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/terraform/helper/pathorcontents"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  // Provider returns a terraform.ResourceProvider.
    13  func Provider() terraform.ResourceProvider {
    14  	return &schema.Provider{
    15  		Schema: map[string]*schema.Schema{
    16  			"account_file": &schema.Schema{
    17  				Type:         schema.TypeString,
    18  				Optional:     true,
    19  				DefaultFunc:  schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil),
    20  				ValidateFunc: validateAccountFile,
    21  				Deprecated:   "Use the credentials field instead",
    22  			},
    23  
    24  			"credentials": &schema.Schema{
    25  				Type:         schema.TypeString,
    26  				Optional:     true,
    27  				DefaultFunc:  schema.EnvDefaultFunc("GOOGLE_CREDENTIALS", nil),
    28  				ValidateFunc: validateCredentials,
    29  			},
    30  
    31  			"project": &schema.Schema{
    32  				Type:        schema.TypeString,
    33  				Required:    true,
    34  				DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", nil),
    35  			},
    36  
    37  			"region": &schema.Schema{
    38  				Type:        schema.TypeString,
    39  				Required:    true,
    40  				DefaultFunc: schema.EnvDefaultFunc("GOOGLE_REGION", nil),
    41  			},
    42  		},
    43  
    44  		ResourcesMap: map[string]*schema.Resource{
    45  			"google_compute_autoscaler":             resourceComputeAutoscaler(),
    46  			"google_compute_address":                resourceComputeAddress(),
    47  			"google_compute_backend_service":        resourceComputeBackendService(),
    48  			"google_compute_disk":                   resourceComputeDisk(),
    49  			"google_compute_firewall":               resourceComputeFirewall(),
    50  			"google_compute_forwarding_rule":        resourceComputeForwardingRule(),
    51  			"google_compute_global_address":         resourceComputeGlobalAddress(),
    52  			"google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(),
    53  			"google_compute_http_health_check":      resourceComputeHttpHealthCheck(),
    54  			"google_compute_https_health_check":     resourceComputeHttpsHealthCheck(),
    55  			"google_compute_instance":               resourceComputeInstance(),
    56  			"google_compute_instance_group_manager": resourceComputeInstanceGroupManager(),
    57  			"google_compute_instance_template":      resourceComputeInstanceTemplate(),
    58  			"google_compute_network":                resourceComputeNetwork(),
    59  			"google_compute_project_metadata":       resourceComputeProjectMetadata(),
    60  			"google_compute_route":                  resourceComputeRoute(),
    61  			"google_compute_ssl_certificate":        resourceComputeSslCertificate(),
    62  			"google_compute_target_http_proxy":      resourceComputeTargetHttpProxy(),
    63  			"google_compute_target_https_proxy":     resourceComputeTargetHttpsProxy(),
    64  			"google_compute_target_pool":            resourceComputeTargetPool(),
    65  			"google_compute_url_map":                resourceComputeUrlMap(),
    66  			"google_compute_vpn_gateway":            resourceComputeVpnGateway(),
    67  			"google_compute_vpn_tunnel":             resourceComputeVpnTunnel(),
    68  			"google_container_cluster":              resourceContainerCluster(),
    69  			"google_dns_managed_zone":               resourceDnsManagedZone(),
    70  			"google_dns_record_set":                 resourceDnsRecordSet(),
    71  			"google_sql_database":                   resourceSqlDatabase(),
    72  			"google_sql_database_instance":          resourceSqlDatabaseInstance(),
    73  			"google_storage_bucket":                 resourceStorageBucket(),
    74  			"google_storage_bucket_acl":             resourceStorageBucketAcl(),
    75  			"google_storage_bucket_object":          resourceStorageBucketObject(),
    76  			"google_storage_object_acl":             resourceStorageObjectAcl(),
    77  		},
    78  
    79  		ConfigureFunc: providerConfigure,
    80  	}
    81  }
    82  
    83  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    84  	credentials := d.Get("credentials").(string)
    85  	if credentials == "" {
    86  		credentials = d.Get("account_file").(string)
    87  	}
    88  	config := Config{
    89  		Credentials: credentials,
    90  		Project:     d.Get("project").(string),
    91  		Region:      d.Get("region").(string),
    92  	}
    93  
    94  	if err := config.loadAndValidate(); err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return &config, nil
    99  }
   100  
   101  func validateAccountFile(v interface{}, k string) (warnings []string, errors []error) {
   102  	if v == nil {
   103  		return
   104  	}
   105  
   106  	value := v.(string)
   107  
   108  	if value == "" {
   109  		return
   110  	}
   111  
   112  	contents, wasPath, err := pathorcontents.Read(value)
   113  	if err != nil {
   114  		errors = append(errors, fmt.Errorf("Error loading Account File: %s", err))
   115  	}
   116  	if wasPath {
   117  		warnings = append(warnings, `account_file was provided as a path instead of 
   118  as file contents. This support will be removed in the future. Please update
   119  your configuration to use ${file("filename.json")} instead.`)
   120  	}
   121  
   122  	var account accountFile
   123  	if err := json.Unmarshal([]byte(contents), &account); err != nil {
   124  		errors = append(errors,
   125  			fmt.Errorf("account_file not valid JSON '%s': %s", contents, err))
   126  	}
   127  
   128  	return
   129  }
   130  
   131  func validateCredentials(v interface{}, k string) (warnings []string, errors []error) {
   132  	if v == nil || v.(string) == "" {
   133  		return
   134  	}
   135  	creds := v.(string)
   136  	var account accountFile
   137  	if err := json.Unmarshal([]byte(creds), &account); err != nil {
   138  		errors = append(errors,
   139  			fmt.Errorf("credentials are not valid JSON '%s': %s", creds, err))
   140  	}
   141  
   142  	return
   143  }