github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/newrelic/resource_newrelic_alert_policy_channel.go (about) 1 package newrelic 2 3 import ( 4 "log" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 newrelic "github.com/paultyng/go-newrelic/api" 8 ) 9 10 func policyChannelExists(client *newrelic.Client, policyID int, channelID int) (bool, error) { 11 channel, err := client.GetAlertChannel(channelID) 12 if err != nil { 13 if err == newrelic.ErrNotFound { 14 return false, nil 15 } 16 17 return false, err 18 } 19 20 for _, id := range channel.Links.PolicyIDs { 21 if id == policyID { 22 return true, nil 23 } 24 } 25 26 return false, nil 27 } 28 29 func resourceNewRelicAlertPolicyChannel() *schema.Resource { 30 return &schema.Resource{ 31 Create: resourceNewRelicAlertPolicyChannelCreate, 32 Read: resourceNewRelicAlertPolicyChannelRead, 33 // Update: Not currently supported in API 34 Delete: resourceNewRelicAlertPolicyChannelDelete, 35 Schema: map[string]*schema.Schema{ 36 "policy_id": { 37 Type: schema.TypeInt, 38 Required: true, 39 ForceNew: true, 40 }, 41 "channel_id": { 42 Type: schema.TypeInt, 43 Required: true, 44 ForceNew: true, 45 }, 46 }, 47 } 48 } 49 50 func resourceNewRelicAlertPolicyChannelCreate(d *schema.ResourceData, meta interface{}) error { 51 client := meta.(*newrelic.Client) 52 53 policyID := d.Get("policy_id").(int) 54 channelID := d.Get("channel_id").(int) 55 56 serializedID := serializeIDs([]int{policyID, channelID}) 57 58 log.Printf("[INFO] Creating New Relic alert policy channel %s", serializedID) 59 60 exists, err := policyChannelExists(client, policyID, channelID) 61 if err != nil { 62 return err 63 } 64 65 if !exists { 66 err = client.UpdateAlertPolicyChannels(policyID, []int{channelID}) 67 if err != nil { 68 return err 69 } 70 } 71 72 d.SetId(serializedID) 73 74 return nil 75 } 76 77 func resourceNewRelicAlertPolicyChannelRead(d *schema.ResourceData, meta interface{}) error { 78 client := meta.(*newrelic.Client) 79 80 ids, err := parseIDs(d.Id(), 2) 81 if err != nil { 82 return err 83 } 84 85 policyID := ids[0] 86 channelID := ids[1] 87 88 log.Printf("[INFO] Reading New Relic alert policy channel %s", d.Id()) 89 90 exists, err := policyChannelExists(client, policyID, channelID) 91 if err != nil { 92 return err 93 } 94 95 if !exists { 96 d.SetId("") 97 return nil 98 } 99 100 d.Set("policy_id", policyID) 101 d.Set("channel_id", channelID) 102 103 return nil 104 } 105 106 func resourceNewRelicAlertPolicyChannelDelete(d *schema.ResourceData, meta interface{}) error { 107 client := meta.(*newrelic.Client) 108 109 ids, err := parseIDs(d.Id(), 2) 110 if err != nil { 111 return err 112 } 113 114 policyID := ids[0] 115 channelID := ids[1] 116 117 log.Printf("[INFO] Deleting New Relic alert policy channel %s", d.Id()) 118 119 exists, err := policyChannelExists(client, policyID, channelID) 120 if err != nil { 121 return err 122 } 123 124 if exists { 125 if err := client.DeleteAlertPolicyChannel(policyID, channelID); err != nil { 126 switch err { 127 case newrelic.ErrNotFound: 128 return nil 129 } 130 return err 131 } 132 } 133 134 d.SetId("") 135 136 return nil 137 }