github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/provider.go (about)

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