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

     1  package circonus
     2  
     3  // The `circonus_metric` type is a synthetic, top-level resource that doesn't
     4  // actually exist within Circonus.  The `circonus_check` resource uses
     5  // `circonus_metric` as input to its `metric` attribute.  The `circonus_check`
     6  // resource can, if configured, override various parameters in the
     7  // `circonus_metric` resource if no value was set (e.g. the `icmp_ping` will
     8  // implicitly set the `unit` metric to `seconds`).
     9  
    10  import (
    11  	"github.com/hashicorp/errwrap"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  const (
    16  	// circonus_metric.* resource attribute names
    17  	metricActiveAttr = "active"
    18  	metricIDAttr     = "id"
    19  	metricNameAttr   = "name"
    20  	metricTypeAttr   = "type"
    21  	metricTagsAttr   = "tags"
    22  	metricUnitAttr   = "unit"
    23  
    24  	// CheckBundle.Metric.Status can be one of these values
    25  	metricStatusActive    = "active"
    26  	metricStatusAvailable = "available"
    27  )
    28  
    29  var metricDescriptions = attrDescrs{
    30  	metricActiveAttr: "Enables or disables the metric",
    31  	metricNameAttr:   "Name of the metric",
    32  	metricTypeAttr:   "Type of metric (e.g. numeric, histogram, text)",
    33  	metricTagsAttr:   "Tags assigned to the metric",
    34  	metricUnitAttr:   "The unit of measurement for a metric",
    35  }
    36  
    37  func resourceMetric() *schema.Resource {
    38  	return &schema.Resource{
    39  		Create: metricCreate,
    40  		Read:   metricRead,
    41  		Update: metricUpdate,
    42  		Delete: metricDelete,
    43  		Exists: metricExists,
    44  		Importer: &schema.ResourceImporter{
    45  			State: schema.ImportStatePassthrough,
    46  		},
    47  
    48  		Schema: convertToHelperSchema(metricDescriptions, map[schemaAttr]*schema.Schema{
    49  			metricActiveAttr: &schema.Schema{
    50  				Type:     schema.TypeBool,
    51  				Optional: true,
    52  				Default:  true,
    53  			},
    54  			metricNameAttr: &schema.Schema{
    55  				Type:         schema.TypeString,
    56  				Required:     true,
    57  				ValidateFunc: validateRegexp(metricNameAttr, `[\S]+`),
    58  			},
    59  			metricTypeAttr: &schema.Schema{
    60  				Type:         schema.TypeString,
    61  				Required:     true,
    62  				ValidateFunc: validateStringIn(metricTypeAttr, validMetricTypes),
    63  			},
    64  			metricTagsAttr: tagMakeConfigSchema(metricTagsAttr),
    65  			metricUnitAttr: &schema.Schema{
    66  				Type:         schema.TypeString,
    67  				Optional:     true,
    68  				Default:      metricUnit,
    69  				ValidateFunc: validateRegexp(metricUnitAttr, metricUnitRegexp),
    70  			},
    71  		}),
    72  	}
    73  }
    74  
    75  func metricCreate(d *schema.ResourceData, meta interface{}) error {
    76  	m := newMetric()
    77  
    78  	id := d.Id()
    79  	if id == "" {
    80  		var err error
    81  		id, err = newMetricID()
    82  		if err != nil {
    83  			return errwrap.Wrapf("metric ID creation failed: {{err}}", err)
    84  		}
    85  	}
    86  
    87  	if err := m.ParseConfig(id, d); err != nil {
    88  		return errwrap.Wrapf("error parsing metric schema during create: {{err}}", err)
    89  	}
    90  
    91  	if err := m.Create(d); err != nil {
    92  		return errwrap.Wrapf("error creating metric: {{err}}", err)
    93  	}
    94  
    95  	return metricRead(d, meta)
    96  }
    97  
    98  func metricRead(d *schema.ResourceData, meta interface{}) error {
    99  	m := newMetric()
   100  
   101  	if err := m.ParseConfig(d.Id(), d); err != nil {
   102  		return errwrap.Wrapf("error parsing metric schema during read: {{err}}", err)
   103  	}
   104  
   105  	if err := m.SaveState(d); err != nil {
   106  		return errwrap.Wrapf("error saving metric during read: {{err}}", err)
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func metricUpdate(d *schema.ResourceData, meta interface{}) error {
   113  	m := newMetric()
   114  
   115  	if err := m.ParseConfig(d.Id(), d); err != nil {
   116  		return errwrap.Wrapf("error parsing metric schema during update: {{err}}", err)
   117  	}
   118  
   119  	if err := m.Update(d); err != nil {
   120  		return errwrap.Wrapf("error updating metric: {{err}}", err)
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  func metricDelete(d *schema.ResourceData, meta interface{}) error {
   127  	d.SetId("")
   128  
   129  	return nil
   130  }
   131  
   132  func metricExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   133  	if id := d.Id(); id != "" {
   134  		return true, nil
   135  	}
   136  
   137  	return false, nil
   138  }