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