<commit_before>package nsone

import (
	"fmt"
	"github.com/bobtfish/go-nsone-api"
	"github.com/hashicorp/terraform/helper/schema"
	"regexp"
	"strconv"
)

func monitoringJobResource() *schema.Resource {
	return &schema.Resource{
		Schema: map[string]*schema.Schema{
			"id": &schema.Schema{
				Type:     schema.TypeString,
				Computed: true,
			},
			"name": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
			},
			"active": &schema.Schema{
				Type:     schema.TypeBool,
				Required: true,
			},
			"regions": &schema.Schema{
				Type:     schema.TypeList,
				Required: true,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"job_type": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"frequency": &schema.Schema{
				Type:     schema.TypeInt,
				Required: true,
			},
			"rapid_recheck": &schema.Schema{
				Type:     schema.TypeBool,
				Required: true,
			},
			"policy": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
					value := v.(string)
					if !regexp.MustCompile(`^(all|one|quorum)$`).MatchString(value) {
						es = append(es, fmt.Errorf(
							"only all, one, quorum allowed in %q", k))
					}
					return
				},
			},
			"notes": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
			},
			"config": &schema.Schema{
				Type:     schema.TypeMap,
				Required: true,
			},
			"notify_delay": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
			},
			"notify_repeat": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
			},
			"notify_failback": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
			},
			"notify_regional": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
			},
			"notify_list": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
			},
		},
		Create: MonitoringJobCreate,
		Read:   MonitoringJobRead,
		Update: MonitoringJobUpdate,
		Delete: MonitoringJobDelete,
	}
}

func monitoringJobToResourceData(d *schema.ResourceData, r *nsone.MonitoringJob) error {
	d.SetId(r.Id)
	return nil
}

func resourceDataToMonitoringJob(r *nsone.MonitoringJob, d *schema.ResourceData) error {
	r.Id = d.Id()
	r.Name = d.Get("name").(string)
	r.JobType = d.Get("job_type").(string)
	r.Active = d.Get("active").(bool)
	raw_regions := d.Get("regions").([]interface{})
	r.Regions = make([]string, len(raw_regions))
	for i, v := range raw_regions {
		r.Regions[i] = v.(string)
	}
	r.Frequency = d.Get("frequency").(int)
	r.RapidRecheck = d.Get("rapid_recheck").(bool)
	var raw_rules []interface{}
	if r := d.Get("rules"); r != nil {
		raw_rules = r.([]interface{})
	}
	r.Rules = make([]nsone.MonitoringJobRule, len(raw_rules))
	for i, v := range raw_rules {
		rule := v.(map[string]interface{})
		r.Rules[i] = nsone.MonitoringJobRule{
			Value:      rule["value"].(int),
			Comparison: rule["comparison"].(string),
			Key:        rule["key"].(string),
		}
	}
	config := make(map[string]interface{})
	if raw_config := d.Get("config"); raw_config != nil {
		for k, v := range raw_config.(map[string]interface{}) {
			if i, err := strconv.Atoi(v.(string)); err == nil {
				config[k] = i
			} else {
				config[k] = v
			}
		}
	}
	r.Config = config
	r.RegionScope = "fixed"
	r.Policy = d.Get("policy").(string)
	if v, ok = d.GetOk("notes"); ok {
		r.Notes = v.(string)
	}<commit_msg>Add rules to the schema<commit_after>package nsone

import (
	"fmt"
	"github.com/bobtfish/go-nsone-api"
	"github.com/hashicorp/terraform/helper/schema"
	"regexp"
	"strconv"
)

func monitoringJobResource() *schema.Resource {
	return &schema.Resource{
		Schema: map[string]*schema.Schema{
			"id": &schema.Schema{
				Type:     schema.TypeString,
				Computed: true,
			},
			"name": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
			},
			"active": &schema.Schema{
				Type:     schema.TypeBool,
				Required: true,
			},
			"regions": &schema.Schema{
				Type:     schema.TypeList,
				Required: true,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"job_type": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"frequency": &schema.Schema{
				Type:     schema.TypeInt,
				Required: true,
			},
			"rapid_recheck": &schema.Schema{
				Type:     schema.TypeBool,
				Required: true,
			},
			"policy": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
					value := v.(string)
					if !regexp.MustCompile(`^(all|one|quorum)$`).MatchString(value) {
						es = append(es, fmt.Errorf(
							"only all, one, quorum allowed in %q", k))
					}
					return
				},
			},
			"notes": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
			},
			"config": &schema.Schema{
				Type:     schema.TypeMap,
				Required: true,
			},
			"notify_delay": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
			},
			"notify_repeat": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
			},
			"notify_failback": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
			},
			"notify_regional": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
			},
			"notify_list": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
			},
			"rules": &schema.Schema{
				Type:     schema.TypeList,
				Optional: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"value": &schema.Schema{
							Type:     schema.TypeString,
							Required: true,
						},
						"comparison": &schema.Schema{
							Type:     schema.TypeString,
							Required: true,
						},
						"key": &schema.Schema{
							Type:     schema.TypeString,
							Required: true,
						},
					},
				},
			},
		},
		Create: MonitoringJobCreate,
		Read:   MonitoringJobRead,
		Update: MonitoringJobUpdate,
		Delete: MonitoringJobDelete,
	}
}

func monitoringJobToResourceData(d *schema.ResourceData, r *nsone.MonitoringJob) error {
	d.SetId(r.Id)
	return nil
}

func resourceDataToMonitoringJob(r *nsone.MonitoringJob, d *schema.ResourceData) error {
	r.Id = d.Id()
	r.Name = d.Get("name").(string)
	r.JobType = d.Get("job_type").(string)
	r.Active = d.Get("active").(bool)
	raw_regions := d.Get("regions").([]interface{})
	r.Regions = make([]string, len(raw_regions))
	for i, v := range raw_regions {
		r.Regions[i] = v.(string)
	}
	r.Frequency = d.Get("frequency").(int)
	r.RapidRecheck = d.Get("rapid_recheck").(bool)
	var raw_rules []interface{}
	if r := d.Get("rules"); r != nil {
		raw_rules = r.([]interface{})
	}
	r.Rules = make([]nsone.MonitoringJobRule, len(raw_rules))
	for i, v := range raw_rules {
		rule := v.(map[string]interface{})
		r.Rules[i] = nsone.MonitoringJobRule{
			Value:      rule["value"].(int),
			Comparison: rule["comparison"].(string),
			Key:        rule["key"].(string),
		}
	}
	config := make(map[string]interface{})
	if raw_config := d.Get("config"); raw_config != nil {
		for k, v := range raw_config.(map[string]interface{}) {
			if i, err := strconv.Atoi(v.(string)); err == nil {
				config[k] = i
			} else {
				config[k] = v
			}
		}
	}
	r.Config = config
	r.RegionScope = "fixed"
	r.Policy = d.Get("policy").(string)
	if v, ok := d.GetOk("notes"); ok {
		r.Notes = v.(string)
	}
{'fraction_non_alphanumeric': 0.16597454789015406, 'fraction_numerical': 0.0, 'mean_word_length': 2.956544780074192, 'pattern_counts': {'":': 34, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5426523', 'n_tokens_mistral': 3233, 'n_tokens_neox': 2660, 'n_words': 526}