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

     1  package ns1
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  
     6  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
     7  	"gopkg.in/ns1/ns1-go.v2/rest/model/monitor"
     8  )
     9  
    10  func notifyListResource() *schema.Resource {
    11  	return &schema.Resource{
    12  		Schema: map[string]*schema.Schema{
    13  			"id": &schema.Schema{
    14  				Type:     schema.TypeString,
    15  				Computed: true,
    16  			},
    17  			"name": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  			},
    21  			"notifications": &schema.Schema{
    22  				Type:     schema.TypeList,
    23  				Optional: true,
    24  				Elem: &schema.Resource{
    25  					Schema: map[string]*schema.Schema{
    26  						"type": &schema.Schema{
    27  							Type:     schema.TypeString,
    28  							Required: true,
    29  						},
    30  						"config": &schema.Schema{
    31  							Type:     schema.TypeMap,
    32  							Required: true,
    33  						},
    34  					},
    35  				},
    36  			},
    37  		},
    38  		Create: NotifyListCreate,
    39  		Read:   NotifyListRead,
    40  		Update: NotifyListUpdate,
    41  		Delete: NotifyListDelete,
    42  	}
    43  }
    44  
    45  func notifyListToResourceData(d *schema.ResourceData, nl *monitor.NotifyList) error {
    46  	d.SetId(nl.ID)
    47  	d.Set("name", nl.Name)
    48  
    49  	if len(nl.Notifications) > 0 {
    50  		notifications := make([]map[string]interface{}, len(nl.Notifications))
    51  		for i, n := range nl.Notifications {
    52  			ni := make(map[string]interface{})
    53  			ni["type"] = n.Type
    54  			if n.Config != nil {
    55  				ni["config"] = n.Config
    56  			}
    57  			notifications[i] = ni
    58  		}
    59  		d.Set("notifications", notifications)
    60  	}
    61  	return nil
    62  }
    63  
    64  func resourceDataToNotifyList(nl *monitor.NotifyList, d *schema.ResourceData) error {
    65  	nl.ID = d.Id()
    66  
    67  	if rawNotifications := d.Get("notifications").([]interface{}); len(rawNotifications) > 0 {
    68  		ns := make([]*monitor.Notification, len(rawNotifications))
    69  		for i, notificationRaw := range rawNotifications {
    70  			ni := notificationRaw.(map[string]interface{})
    71  			config := ni["config"].(map[string]interface{})
    72  
    73  			switch ni["type"].(string) {
    74  			case "webhook":
    75  				ns[i] = monitor.NewWebNotification(config["url"].(string))
    76  			case "email":
    77  				ns[i] = monitor.NewEmailNotification(config["email"].(string))
    78  			case "datafeed":
    79  				ns[i] = monitor.NewFeedNotification(config["sourceid"].(string))
    80  			}
    81  		}
    82  		nl.Notifications = ns
    83  	}
    84  	return nil
    85  }
    86  
    87  // NotifyListCreate creates an ns1 notifylist
    88  func NotifyListCreate(d *schema.ResourceData, meta interface{}) error {
    89  	client := meta.(*ns1.Client)
    90  	nl := monitor.NewNotifyList(d.Get("name").(string))
    91  
    92  	if err := resourceDataToNotifyList(nl, d); err != nil {
    93  		return err
    94  	}
    95  
    96  	if _, err := client.Notifications.Create(nl); err != nil {
    97  		return err
    98  	}
    99  
   100  	return notifyListToResourceData(d, nl)
   101  }
   102  
   103  // NotifyListRead fetches info for the given notifylist from ns1
   104  func NotifyListRead(d *schema.ResourceData, meta interface{}) error {
   105  	client := meta.(*ns1.Client)
   106  
   107  	nl, _, err := client.Notifications.Get(d.Id())
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	return notifyListToResourceData(d, nl)
   113  }
   114  
   115  // NotifyListDelete deletes the given notifylist from ns1
   116  func NotifyListDelete(d *schema.ResourceData, meta interface{}) error {
   117  	client := meta.(*ns1.Client)
   118  
   119  	_, err := client.Notifications.Delete(d.Id())
   120  	d.SetId("")
   121  
   122  	return err
   123  }
   124  
   125  // NotifyListUpdate updates the notifylist with given parameters
   126  func NotifyListUpdate(d *schema.ResourceData, meta interface{}) error {
   127  	client := meta.(*ns1.Client)
   128  
   129  	nl := monitor.NewNotifyList(d.Get("name").(string))
   130  
   131  	if err := resourceDataToNotifyList(nl, d); err != nil {
   132  		return err
   133  	}
   134  
   135  	if _, err := client.Notifications.Update(nl); err != nil {
   136  		return err
   137  	}
   138  
   139  	return notifyListToResourceData(d, nl)
   140  }