opentofu/builtin/providers/circonus/provider.go
Jake Champlin bd68789006
core: Use environment variables to set VersionPrerelease at compile time
Instead of using a hardcoded version prerelease string, which makes release automation difficult, set the version prerelease string from an environment variable via the go linker tool during compile time.

The environment variable `TF_RELEASE` should only be set via the `make bin` target, and thus leaves the version prerelease string unset. Otherwise, when running a local compile of terraform via the `make dev` makefile target, the version prerelease string is set to `"dev"`, as usual.

This also requires some changes to both the circonus and postgresql providers, as they directly used the `VersionPrerelease` constant. We now simply call the `VersionString()` function, which returns the proper interpolated version string with the prerelease string populated correctly.

`TF_RELEASE` is unset:

```sh
$ make dev
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/05/22 10:38:19 Generated command/internal_plugin_list.go
==> Removing old directory...
==> Building...
Number of parallel builds: 3

-->     linux/amd64: github.com/hashicorp/terraform

==> Results:
total 209M
-rwxr-xr-x 1 jake jake 209M May 22 10:39 terraform

$ terraform version
Terraform v0.9.6-dev (fd472e4a86500606b03c314f70d11f2bc4bc84e5+CHANGES)
```

`TF_RELEASE` is set (mimicking the `make bin` target):

```sh
$ TF_RELEASE=1 make dev
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/05/22 10:40:39 Generated command/internal_plugin_list.go
==> Removing old directory...
==> Building...
Number of parallel builds: 3

-->     linux/amd64: github.com/hashicorp/terraform

==> Results:
total 121M
-rwxr-xr-x 1 jake jake 121M May 22 10:42 terraform

$ terraform version
Terraform v0.9.6
```
2017-05-22 10:49:15 -04:00

127 lines
3.8 KiB
Go

package circonus
import (
"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 {
return fmt.Sprintf("Terraform v%s", terraform.VersionString())
}