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

     1  package ns1
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  
    10  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
    11  	"gopkg.in/ns1/ns1-go.v2/rest/model/monitor"
    12  )
    13  
    14  func monitoringJobResource() *schema.Resource {
    15  	return &schema.Resource{
    16  		Schema: map[string]*schema.Schema{
    17  			// Required
    18  			"name": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  			},
    22  			"job_type": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"regions": &schema.Schema{
    28  				Type:     schema.TypeList,
    29  				Required: true,
    30  				Elem: &schema.Schema{
    31  					Type: schema.TypeString,
    32  				},
    33  			},
    34  			"frequency": &schema.Schema{
    35  				Type:     schema.TypeInt,
    36  				Required: true,
    37  			},
    38  			"config": &schema.Schema{
    39  				Type:     schema.TypeMap,
    40  				Required: true,
    41  			},
    42  			// Optional
    43  			"active": &schema.Schema{
    44  				Type:     schema.TypeBool,
    45  				Optional: true,
    46  				Default:  true,
    47  			},
    48  			"rapid_recheck": &schema.Schema{
    49  				Type:     schema.TypeBool,
    50  				Optional: true,
    51  				Default:  false,
    52  			},
    53  			"policy": &schema.Schema{
    54  				Type:     schema.TypeString,
    55  				Optional: true,
    56  				Default:  "quorum",
    57  				ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
    58  					value := v.(string)
    59  					if !regexp.MustCompile(`^(all|one|quorum)$`).MatchString(value) {
    60  						es = append(es, fmt.Errorf(
    61  							"only all, one, quorum allowed in %q", k))
    62  					}
    63  					return
    64  				},
    65  			},
    66  			"notes": &schema.Schema{
    67  				Type:     schema.TypeString,
    68  				Optional: true,
    69  			},
    70  			"notify_delay": &schema.Schema{
    71  				Type:     schema.TypeInt,
    72  				Optional: true,
    73  			},
    74  			"notify_repeat": &schema.Schema{
    75  				Type:     schema.TypeInt,
    76  				Optional: true,
    77  			},
    78  			"notify_failback": &schema.Schema{
    79  				Type:     schema.TypeBool,
    80  				Optional: true,
    81  			},
    82  			"notify_regional": &schema.Schema{
    83  				Type:     schema.TypeBool,
    84  				Optional: true,
    85  			},
    86  			"notify_list": &schema.Schema{
    87  				Type:     schema.TypeString,
    88  				Optional: true,
    89  			},
    90  			"rules": &schema.Schema{
    91  				Type:     schema.TypeList,
    92  				Optional: true,
    93  				Elem: &schema.Resource{
    94  					Schema: map[string]*schema.Schema{
    95  						"value": &schema.Schema{
    96  							Type:     schema.TypeString,
    97  							Required: true,
    98  						},
    99  						"comparison": &schema.Schema{
   100  							Type:     schema.TypeString,
   101  							Required: true,
   102  						},
   103  						"key": &schema.Schema{
   104  							Type:     schema.TypeString,
   105  							Required: true,
   106  						},
   107  					},
   108  				},
   109  			},
   110  			// Computed
   111  			"id": &schema.Schema{
   112  				Type:     schema.TypeString,
   113  				Computed: true,
   114  			},
   115  		},
   116  		Create: MonitoringJobCreate,
   117  		Read:   MonitoringJobRead,
   118  		Update: MonitoringJobUpdate,
   119  		Delete: MonitoringJobDelete,
   120  	}
   121  }
   122  
   123  func monitoringJobToResourceData(d *schema.ResourceData, r *monitor.Job) error {
   124  	d.SetId(r.ID)
   125  	d.Set("name", r.Name)
   126  	d.Set("job_type", r.Type)
   127  	d.Set("active", r.Active)
   128  	d.Set("regions", r.Regions)
   129  	d.Set("frequency", r.Frequency)
   130  	d.Set("rapid_recheck", r.RapidRecheck)
   131  	config := make(map[string]string)
   132  	for k, v := range r.Config {
   133  		if k == "ssl" {
   134  			if v.(bool) {
   135  				config[k] = "1"
   136  			} else {
   137  				config[k] = "0"
   138  			}
   139  		} else {
   140  			switch t := v.(type) {
   141  			case string:
   142  				config[k] = t
   143  			case float64:
   144  				config[k] = strconv.FormatFloat(t, 'f', -1, 64)
   145  			}
   146  		}
   147  	}
   148  	err := d.Set("config", config)
   149  	if err != nil {
   150  		panic(fmt.Errorf("[DEBUG] Error setting Config error: %#v %#v", r.Config, err))
   151  	}
   152  	d.Set("policy", r.Policy)
   153  	d.Set("notes", r.Notes)
   154  	d.Set("frequency", r.Frequency)
   155  	d.Set("notify_delay", r.NotifyDelay)
   156  	d.Set("notify_repeat", r.NotifyRepeat)
   157  	d.Set("notify_regional", r.NotifyRegional)
   158  	d.Set("notify_failback", r.NotifyFailback)
   159  	d.Set("notify_list", r.NotifyListID)
   160  	if len(r.Rules) > 0 {
   161  		rules := make([]map[string]interface{}, len(r.Rules))
   162  		for i, r := range r.Rules {
   163  			m := make(map[string]interface{})
   164  			m["value"] = r.Value
   165  			m["comparison"] = r.Comparison
   166  			m["key"] = r.Key
   167  			rules[i] = m
   168  		}
   169  		d.Set("rules", rules)
   170  	}
   171  	return nil
   172  }
   173  
   174  func resourceDataToMonitoringJob(r *monitor.Job, d *schema.ResourceData) error {
   175  	r.ID = d.Id()
   176  	r.Name = d.Get("name").(string)
   177  	r.Type = d.Get("job_type").(string)
   178  	r.Active = d.Get("active").(bool)
   179  	rawRegions := d.Get("regions").([]interface{})
   180  	r.Regions = make([]string, len(rawRegions))
   181  	for i, v := range rawRegions {
   182  		r.Regions[i] = v.(string)
   183  	}
   184  	r.Frequency = d.Get("frequency").(int)
   185  	r.RapidRecheck = d.Get("rapid_recheck").(bool)
   186  	var rawRules []interface{}
   187  	if rawRules := d.Get("rules"); rawRules != nil {
   188  		r.Rules = make([]*monitor.Rule, len(rawRules.([]interface{})))
   189  		for i, v := range rawRules.([]interface{}) {
   190  			rule := v.(map[string]interface{})
   191  			r.Rules[i] = &monitor.Rule{
   192  				Value:      rule["value"].(string),
   193  				Comparison: rule["comparison"].(string),
   194  				Key:        rule["key"].(string),
   195  			}
   196  		}
   197  	} else {
   198  		r.Rules = make([]*monitor.Rule, 0)
   199  	}
   200  	for i, v := range rawRules {
   201  		rule := v.(map[string]interface{})
   202  		r.Rules[i] = &monitor.Rule{
   203  			Comparison: rule["comparison"].(string),
   204  			Key:        rule["key"].(string),
   205  		}
   206  		value := rule["value"].(string)
   207  		if i, err := strconv.Atoi(value); err == nil {
   208  			r.Rules[i].Value = i
   209  		} else {
   210  			r.Rules[i].Value = value
   211  		}
   212  	}
   213  	config := make(map[string]interface{})
   214  	if rawConfig := d.Get("config"); rawConfig != nil {
   215  		for k, v := range rawConfig.(map[string]interface{}) {
   216  			if k == "ssl" {
   217  				if v.(string) == "1" {
   218  					config[k] = true
   219  				}
   220  			} else {
   221  				if i, err := strconv.Atoi(v.(string)); err == nil {
   222  					config[k] = i
   223  				} else {
   224  					config[k] = v
   225  				}
   226  			}
   227  		}
   228  	}
   229  	r.Config = config
   230  	r.RegionScope = "fixed"
   231  	r.Policy = d.Get("policy").(string)
   232  	if v, ok := d.GetOk("notes"); ok {
   233  		r.Notes = v.(string)
   234  	}
   235  	r.Frequency = d.Get("frequency").(int)
   236  	if v, ok := d.GetOk("notify_delay"); ok {
   237  		r.NotifyDelay = v.(int)
   238  	}
   239  	if v, ok := d.GetOk("notify_repeat"); ok {
   240  		r.NotifyRepeat = v.(int)
   241  	}
   242  	if v, ok := d.GetOk("notify_regional"); ok {
   243  		r.NotifyRegional = v.(bool)
   244  	}
   245  	if v, ok := d.GetOk("notify_failback"); ok {
   246  		r.NotifyFailback = v.(bool)
   247  	}
   248  	if v, ok := d.GetOk("notify_list"); ok {
   249  		r.NotifyListID = v.(string)
   250  	}
   251  	return nil
   252  }
   253  
   254  // MonitoringJobCreate Creates monitoring job in ns1
   255  func MonitoringJobCreate(d *schema.ResourceData, meta interface{}) error {
   256  	client := meta.(*ns1.Client)
   257  	j := monitor.Job{}
   258  	if err := resourceDataToMonitoringJob(&j, d); err != nil {
   259  		return err
   260  	}
   261  	if _, err := client.Jobs.Create(&j); err != nil {
   262  		return err
   263  	}
   264  	return monitoringJobToResourceData(d, &j)
   265  }
   266  
   267  // MonitoringJobRead reads the given monitoring job from ns1
   268  func MonitoringJobRead(d *schema.ResourceData, meta interface{}) error {
   269  	client := meta.(*ns1.Client)
   270  	j, _, err := client.Jobs.Get(d.Id())
   271  	if err != nil {
   272  		return err
   273  	}
   274  	return monitoringJobToResourceData(d, j)
   275  }
   276  
   277  // MonitoringJobDelete deteltes the given monitoring job from ns1
   278  func MonitoringJobDelete(d *schema.ResourceData, meta interface{}) error {
   279  	client := meta.(*ns1.Client)
   280  	_, err := client.Jobs.Delete(d.Id())
   281  	d.SetId("")
   282  	return err
   283  }
   284  
   285  // MonitoringJobUpdate updates the given monitoring job
   286  func MonitoringJobUpdate(d *schema.ResourceData, meta interface{}) error {
   287  	client := meta.(*ns1.Client)
   288  	j := monitor.Job{
   289  		ID: d.Id(),
   290  	}
   291  	if err := resourceDataToMonitoringJob(&j, d); err != nil {
   292  		return err
   293  	}
   294  	if _, err := client.Jobs.Update(&j); err != nil {
   295  		return err
   296  	}
   297  	return monitoringJobToResourceData(d, &j)
   298  }