github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/circonus/provider.go (about) 1 package circonus 2 3 import ( 4 "fmt" 5 6 "github.com/circonus-labs/circonus-gometrics/api" 7 "github.com/hashicorp/errwrap" 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 const ( 13 defaultCirconus404ErrorString = "API response code 404:" 14 defaultCirconusAggregationWindow = "300s" 15 defaultCirconusAlertMinEscalateAfter = "300s" 16 defaultCirconusCheckPeriodMax = "300s" 17 defaultCirconusCheckPeriodMin = "30s" 18 defaultCirconusHTTPFormat = "json" 19 defaultCirconusHTTPMethod = "POST" 20 defaultCirconusSlackUsername = "Circonus" 21 defaultCirconusTimeoutMax = "300s" 22 defaultCirconusTimeoutMin = "0s" 23 maxSeverity = 5 24 minSeverity = 1 25 ) 26 27 var providerDescription = map[string]string{ 28 providerAPIURLAttr: "URL of the Circonus API", 29 providerAutoTagAttr: "Signals that the provider should automatically add a tag to all API calls denoting that the resource was created by Terraform", 30 providerKeyAttr: "API token used to authenticate with the Circonus API", 31 } 32 33 // Constants that want to be a constant but can't in Go 34 var ( 35 validContactHTTPFormats = validStringValues{"json", "params"} 36 validContactHTTPMethods = validStringValues{"GET", "POST"} 37 ) 38 39 type contactMethods string 40 41 // globalAutoTag controls whether or not the provider should automatically add a 42 // tag to each resource. 43 // 44 // NOTE(sean): This is done as a global variable because the diff suppress 45 // functions does not have access to the providerContext, only the key, old, and 46 // new values. 47 var globalAutoTag bool 48 49 type providerContext struct { 50 // Circonus API client 51 client *api.API 52 53 // autoTag, when true, automatically appends defaultCirconusTag 54 autoTag bool 55 56 // defaultTag make up the tag to be used when autoTag tags a tag. 57 defaultTag circonusTag 58 } 59 60 // Provider returns a terraform.ResourceProvider. 61 func Provider() terraform.ResourceProvider { 62 return &schema.Provider{ 63 Schema: map[string]*schema.Schema{ 64 providerAPIURLAttr: { 65 Type: schema.TypeString, 66 Optional: true, 67 Default: "https://api.circonus.com/v2", 68 Description: providerDescription[providerAPIURLAttr], 69 }, 70 providerAutoTagAttr: { 71 Type: schema.TypeBool, 72 Optional: true, 73 Default: defaultAutoTag, 74 Description: providerDescription[providerAutoTagAttr], 75 }, 76 providerKeyAttr: { 77 Type: schema.TypeString, 78 Required: true, 79 Sensitive: true, 80 DefaultFunc: schema.EnvDefaultFunc("CIRCONUS_API_TOKEN", nil), 81 Description: providerDescription[providerKeyAttr], 82 }, 83 }, 84 85 DataSourcesMap: map[string]*schema.Resource{ 86 "circonus_account": dataSourceCirconusAccount(), 87 "circonus_collector": dataSourceCirconusCollector(), 88 }, 89 90 ResourcesMap: map[string]*schema.Resource{ 91 "circonus_check": resourceCheck(), 92 "circonus_contact_group": resourceContactGroup(), 93 "circonus_graph": resourceGraph(), 94 "circonus_metric": resourceMetric(), 95 "circonus_metric_cluster": resourceMetricCluster(), 96 "circonus_rule_set": resourceRuleSet(), 97 }, 98 99 ConfigureFunc: providerConfigure, 100 } 101 } 102 103 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 104 globalAutoTag = d.Get(providerAutoTagAttr).(bool) 105 106 config := &api.Config{ 107 URL: d.Get(providerAPIURLAttr).(string), 108 TokenKey: d.Get(providerKeyAttr).(string), 109 TokenApp: tfAppName(), 110 } 111 112 client, err := api.NewAPI(config) 113 if err != nil { 114 return nil, errwrap.Wrapf("Error initializing Circonus: %s", err) 115 } 116 117 return &providerContext{ 118 client: client, 119 autoTag: d.Get(providerAutoTagAttr).(bool), 120 defaultTag: defaultCirconusTag, 121 }, nil 122 } 123 124 func tfAppName() string { 125 return fmt.Sprintf("Terraform v%s", terraform.VersionString()) 126 }