github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/newrelic/resource_newrelic_alert_policy.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"log"
     5  	"strconv"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/hashicorp/terraform/helper/validation"
     9  	newrelic "github.com/paultyng/go-newrelic/api"
    10  )
    11  
    12  func resourceNewRelicAlertPolicy() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceNewRelicAlertPolicyCreate,
    15  		Read:   resourceNewRelicAlertPolicyRead,
    16  		// Update: Not currently supported in API
    17  		Delete: resourceNewRelicAlertPolicyDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  		Schema: map[string]*schema.Schema{
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"incident_preference": {
    28  				Type:         schema.TypeString,
    29  				Optional:     true,
    30  				Default:      "PER_POLICY",
    31  				ForceNew:     true,
    32  				ValidateFunc: validation.StringInSlice([]string{"PER_POLICY", "PER_CONDITION", "PER_CONDITION_AND_TARGET"}, false),
    33  			},
    34  			"created_at": {
    35  				Type:     schema.TypeInt,
    36  				Computed: true,
    37  			},
    38  			"updated_at": {
    39  				Type:     schema.TypeInt,
    40  				Computed: true,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func buildAlertPolicyStruct(d *schema.ResourceData) *newrelic.AlertPolicy {
    47  	policy := newrelic.AlertPolicy{
    48  		Name: d.Get("name").(string),
    49  	}
    50  
    51  	if attr, ok := d.GetOk("incident_preference"); ok {
    52  		policy.IncidentPreference = attr.(string)
    53  	}
    54  
    55  	return &policy
    56  }
    57  
    58  func resourceNewRelicAlertPolicyCreate(d *schema.ResourceData, meta interface{}) error {
    59  	client := meta.(*newrelic.Client)
    60  	policy := buildAlertPolicyStruct(d)
    61  
    62  	log.Printf("[INFO] Creating New Relic alert policy %s", policy.Name)
    63  
    64  	policy, err := client.CreateAlertPolicy(*policy)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	d.SetId(strconv.Itoa(policy.ID))
    70  
    71  	return nil
    72  }
    73  
    74  func resourceNewRelicAlertPolicyRead(d *schema.ResourceData, meta interface{}) error {
    75  	client := meta.(*newrelic.Client)
    76  
    77  	id, err := strconv.ParseInt(d.Id(), 10, 32)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	log.Printf("[INFO] Reading New Relic alert policy %v", id)
    83  
    84  	policy, err := client.GetAlertPolicy(int(id))
    85  	if err != nil {
    86  		if err == newrelic.ErrNotFound {
    87  			d.SetId("")
    88  			return nil
    89  		}
    90  
    91  		return err
    92  	}
    93  
    94  	d.Set("name", policy.Name)
    95  	d.Set("incident_preference", policy.IncidentPreference)
    96  	d.Set("created_at", policy.CreatedAt)
    97  	d.Set("updated_at", policy.UpdatedAt)
    98  
    99  	return nil
   100  }
   101  
   102  func resourceNewRelicAlertPolicyDelete(d *schema.ResourceData, meta interface{}) error {
   103  	client := meta.(*newrelic.Client)
   104  
   105  	id, err := strconv.ParseInt(d.Id(), 10, 32)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	log.Printf("[INFO] Deleting New Relic alert policy %v", id)
   111  
   112  	if err := client.DeleteAlertPolicy(int(id)); err != nil {
   113  		return err
   114  	}
   115  
   116  	d.SetId("")
   117  
   118  	return nil
   119  }