github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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_pubsub_topic":			 resourcePubsubTopic(),
    74  			"google_pubsub_subscription":		 resourcePubsubSubscription(),
    75  			"google_storage_bucket":                 resourceStorageBucket(),
    76  			"google_storage_bucket_acl":             resourceStorageBucketAcl(),
    77  			"google_storage_bucket_object":          resourceStorageBucketObject(),
    78  			"google_storage_object_acl":             resourceStorageObjectAcl(),
    79  		},
    80  
    81  		ConfigureFunc: providerConfigure,
    82  	}
    83  }
    84  
    85  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    86  	credentials := d.Get("credentials").(string)
    87  	if credentials == "" {
    88  		credentials = d.Get("account_file").(string)
    89  	}
    90  	config := Config{
    91  		Credentials: credentials,
    92  		Project:     d.Get("project").(string),
    93  		Region:      d.Get("region").(string),
    94  	}
    95  
    96  	if err := config.loadAndValidate(); err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	return &config, nil
   101  }
   102  
   103  func validateAccountFile(v interface{}, k string) (warnings []string, errors []error) {
   104  	if v == nil {
   105  		return
   106  	}
   107  
   108  	value := v.(string)
   109  
   110  	if value == "" {
   111  		return
   112  	}
   113  
   114  	contents, wasPath, err := pathorcontents.Read(value)
   115  	if err != nil {
   116  		errors = append(errors, fmt.Errorf("Error loading Account File: %s", err))
   117  	}
   118  	if wasPath {
   119  		warnings = append(warnings, `account_file was provided as a path instead of 
   120  as file contents. This support will be removed in the future. Please update
   121  your configuration to use ${file("filename.json")} instead.`)
   122  	}
   123  
   124  	var account accountFile
   125  	if err := json.Unmarshal([]byte(contents), &account); err != nil {
   126  		errors = append(errors,
   127  			fmt.Errorf("account_file not valid JSON '%s': %s", contents, err))
   128  	}
   129  
   130  	return
   131  }
   132  
   133  func validateCredentials(v interface{}, k string) (warnings []string, errors []error) {
   134  	if v == nil || v.(string) == "" {
   135  		return
   136  	}
   137  	creds := v.(string)
   138  	var account accountFile
   139  	if err := json.Unmarshal([]byte(creds), &account); err != nil {
   140  		errors = append(errors,
   141  			fmt.Errorf("credentials are not valid JSON '%s': %s", creds, err))
   142  	}
   143  
   144  	return
   145  }