github.com/blacked/terraform@v0.6.2-0.20150806163846-669c4ad71586/builtin/providers/google/provider.go (about) 1 package google 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 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 Required: true, 19 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil), 20 ValidateFunc: validateAccountFile, 21 }, 22 23 "project": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", nil), 27 }, 28 29 "region": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_REGION", nil), 33 }, 34 }, 35 36 ResourcesMap: map[string]*schema.Resource{ 37 "google_compute_autoscaler": resourceComputeAutoscaler(), 38 "google_compute_address": resourceComputeAddress(), 39 "google_compute_disk": resourceComputeDisk(), 40 "google_compute_firewall": resourceComputeFirewall(), 41 "google_compute_forwarding_rule": resourceComputeForwardingRule(), 42 "google_compute_http_health_check": resourceComputeHttpHealthCheck(), 43 "google_compute_instance": resourceComputeInstance(), 44 "google_compute_instance_template": resourceComputeInstanceTemplate(), 45 "google_compute_network": resourceComputeNetwork(), 46 "google_compute_route": resourceComputeRoute(), 47 "google_compute_target_pool": resourceComputeTargetPool(), 48 "google_container_cluster": resourceContainerCluster(), 49 "google_dns_managed_zone": resourceDnsManagedZone(), 50 "google_dns_record_set": resourceDnsRecordSet(), 51 "google_compute_instance_group_manager": resourceComputeInstanceGroupManager(), 52 "google_storage_bucket": resourceStorageBucket(), 53 }, 54 55 ConfigureFunc: providerConfigure, 56 } 57 } 58 59 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 60 config := Config{ 61 AccountFile: d.Get("account_file").(string), 62 Project: d.Get("project").(string), 63 Region: d.Get("region").(string), 64 } 65 66 if err := config.loadAndValidate(); err != nil { 67 return nil, err 68 } 69 70 return &config, nil 71 } 72 73 func validateAccountFile(v interface{}, k string) (warnings []string, errors []error) { 74 value := v.(string) 75 76 if value == "" { 77 return 78 } 79 80 var account accountFile 81 if err := json.Unmarshal([]byte(value), &account); err != nil { 82 warnings = append(warnings, ` 83 account_file is not valid JSON, so we are assuming it is a file path. This 84 support will be removed in the future. Please update your configuration to use 85 ${file("filename.json")} instead.`) 86 } else { 87 return 88 } 89 90 if _, err := os.Stat(value); err != nil { 91 errors = append(errors, 92 fmt.Errorf( 93 "account_file path could not be read from '%s': %s", 94 value, 95 err)) 96 } 97 98 return 99 }