github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/newrelic/resource_newrelic_alert_channel.go (about) 1 package newrelic 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/hashicorp/terraform/helper/validation" 10 newrelic "github.com/paultyng/go-newrelic/api" 11 ) 12 13 var alertChannelTypes = map[string][]string{ 14 "campfire": []string{ 15 "room", 16 "subdomain", 17 "token", 18 }, 19 "email": []string{ 20 "include_json_attachment", 21 "recipients", 22 }, 23 "hipchat": []string{ 24 "auth_token", 25 "base_url", 26 "room_id", 27 }, 28 "opsgenie": []string{ 29 "api_key", 30 "recipients", 31 "tags", 32 "teams", 33 }, 34 "pagerduty": []string{ 35 "service_key", 36 }, 37 "slack": []string{ 38 "channel", 39 "url", 40 }, 41 "user": []string{ 42 "user_id", 43 }, 44 "victorops": []string{ 45 "key", 46 "route_key", 47 }, 48 "webhook": []string{ 49 "auth_password", 50 "auth_type", 51 "auth_username", 52 "base_url", 53 "headers", 54 "payload_type", 55 "payload", 56 }, 57 } 58 59 func resourceNewRelicAlertChannel() *schema.Resource { 60 validAlertChannelTypes := make([]string, 0, len(alertChannelTypes)) 61 for k := range alertChannelTypes { 62 validAlertChannelTypes = append(validAlertChannelTypes, k) 63 } 64 65 return &schema.Resource{ 66 Create: resourceNewRelicAlertChannelCreate, 67 Read: resourceNewRelicAlertChannelRead, 68 // Update: Not currently supported in API 69 Delete: resourceNewRelicAlertChannelDelete, 70 Importer: &schema.ResourceImporter{ 71 State: schema.ImportStatePassthrough, 72 }, 73 Schema: map[string]*schema.Schema{ 74 "name": { 75 Type: schema.TypeString, 76 Required: true, 77 ForceNew: true, 78 }, 79 "type": { 80 Type: schema.TypeString, 81 Required: true, 82 ForceNew: true, 83 ValidateFunc: validation.StringInSlice(validAlertChannelTypes, false), 84 }, 85 "configuration": { 86 Type: schema.TypeMap, 87 Required: true, 88 ForceNew: true, 89 //TODO: ValidateFunc: (use list of keys from map above) 90 Sensitive: true, 91 }, 92 }, 93 } 94 } 95 96 func buildAlertChannelStruct(d *schema.ResourceData) *newrelic.AlertChannel { 97 channel := newrelic.AlertChannel{ 98 Name: d.Get("name").(string), 99 Type: d.Get("type").(string), 100 Configuration: d.Get("configuration").(map[string]interface{}), 101 } 102 103 return &channel 104 } 105 106 func resourceNewRelicAlertChannelCreate(d *schema.ResourceData, meta interface{}) error { 107 client := meta.(*newrelic.Client) 108 channel := buildAlertChannelStruct(d) 109 110 log.Printf("[INFO] Creating New Relic alert channel %s", channel.Name) 111 112 channel, err := client.CreateAlertChannel(*channel) 113 if err != nil { 114 return err 115 } 116 117 d.SetId(strconv.Itoa(channel.ID)) 118 119 return nil 120 } 121 122 func resourceNewRelicAlertChannelRead(d *schema.ResourceData, meta interface{}) error { 123 client := meta.(*newrelic.Client) 124 125 id, err := strconv.ParseInt(d.Id(), 10, 32) 126 if err != nil { 127 return err 128 } 129 130 log.Printf("[INFO] Reading New Relic alert channel %v", id) 131 132 channel, err := client.GetAlertChannel(int(id)) 133 if err != nil { 134 if err == newrelic.ErrNotFound { 135 d.SetId("") 136 return nil 137 } 138 139 return err 140 } 141 142 d.Set("name", channel.Name) 143 d.Set("type", channel.Type) 144 if err := d.Set("configuration", channel.Configuration); err != nil { 145 return fmt.Errorf("[DEBUG] Error setting Alert Channel Configuration: %#v", err) 146 } 147 148 return nil 149 } 150 151 func resourceNewRelicAlertChannelDelete(d *schema.ResourceData, meta interface{}) error { 152 client := meta.(*newrelic.Client) 153 154 id, err := strconv.ParseInt(d.Id(), 10, 32) 155 if err != nil { 156 return err 157 } 158 159 log.Printf("[INFO] Deleting New Relic alert channel %v", id) 160 161 if err := client.DeleteAlertChannel(int(id)); err != nil { 162 return err 163 } 164 165 d.SetId("") 166 167 return nil 168 }