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