monitor_job.go raw

   1  package rest
   2  
   3  import (
   4  	"fmt"
   5  	"net/http"
   6  	"net/url"
   7  
   8  	"gopkg.in/ns1/ns1-go.v2/rest/model/monitor"
   9  )
  10  
  11  // JobsService handles 'monitoring/jobs' endpoint.
  12  type JobsService service
  13  
  14  // List returns all monitoring jobs for the account.
  15  //
  16  // NS1 API docs: https://ns1.com/api/#jobs-get
  17  func (s *JobsService) List() ([]*monitor.Job, *http.Response, error) {
  18  	req, err := s.client.NewRequest("GET", "monitoring/jobs", nil)
  19  	if err != nil {
  20  		return nil, nil, err
  21  	}
  22  
  23  	mjl := []*monitor.Job{}
  24  	resp, err := s.client.Do(req, &mjl)
  25  	if err != nil {
  26  		return nil, resp, err
  27  	}
  28  
  29  	return mjl, resp, nil
  30  }
  31  
  32  // Get takes an ID and returns details for a specific monitoring job.
  33  //
  34  // NS1 API docs: https://ns1.com/api/#jobs-jobid-get
  35  func (s *JobsService) Get(id string) (*monitor.Job, *http.Response, error) {
  36  	path := fmt.Sprintf("%s/%s", "monitoring/jobs", id)
  37  
  38  	req, err := s.client.NewRequest("GET", path, nil)
  39  	if err != nil {
  40  		return nil, nil, err
  41  	}
  42  
  43  	var mj monitor.Job
  44  	resp, err := s.client.Do(req, &mj)
  45  	if err != nil {
  46  		return nil, resp, err
  47  	}
  48  
  49  	return &mj, resp, nil
  50  }
  51  
  52  // Create takes a *MonitoringJob and creates a new monitoring job.
  53  //
  54  // NS1 API docs: https://ns1.com/api/#jobs-put
  55  func (s *JobsService) Create(mj *monitor.Job) (*http.Response, error) {
  56  	path := fmt.Sprintf("%s", "monitoring/jobs")
  57  
  58  	req, err := s.client.NewRequest("PUT", path, &mj)
  59  	if err != nil {
  60  		return nil, err
  61  	}
  62  
  63  	// Update mon jobs' fields with data from api(ensure consistent)
  64  	resp, err := s.client.Do(req, &mj)
  65  	if err != nil {
  66  		return resp, err
  67  	}
  68  
  69  	return resp, nil
  70  }
  71  
  72  // Update takes a *MonitoringJob and change the configuration details of an existing monitoring job.
  73  //
  74  // NS1 API docs: https://ns1.com/api/#jobs-jobid-post
  75  func (s *JobsService) Update(mj *monitor.Job) (*http.Response, error) {
  76  	path := fmt.Sprintf("%s/%s", "monitoring/jobs", mj.ID)
  77  
  78  	req, err := s.client.NewRequest("POST", path, &mj)
  79  	if err != nil {
  80  		return nil, err
  81  	}
  82  
  83  	// Update mon jobs' fields with data from api(ensure consistent)
  84  	resp, err := s.client.Do(req, &mj)
  85  	if err != nil {
  86  		return resp, err
  87  	}
  88  
  89  	return resp, nil
  90  }
  91  
  92  // Delete takes an ID and immediately terminates and deletes and existing monitoring job.
  93  //
  94  // NS1 API docs: https://ns1.com/api/#jobs-jobid-delete
  95  func (s *JobsService) Delete(id string) (*http.Response, error) {
  96  	path := fmt.Sprintf("%s/%s", "monitoring/jobs", id)
  97  
  98  	req, err := s.client.NewRequest("DELETE", path, nil)
  99  	if err != nil {
 100  		return nil, err
 101  	}
 102  
 103  	resp, err := s.client.Do(req, nil)
 104  	if err != nil {
 105  		return resp, err
 106  	}
 107  
 108  	return resp, nil
 109  }
 110  
 111  // History takes an ID and returns status log history for a specific monitoring job.
 112  //
 113  // NS1 API docs: https://ns1.com/api/#history-get
 114  func (s *JobsService) History(id string, opts ...func(*url.Values)) ([]*monitor.StatusLog, *http.Response, error) {
 115  	v := url.Values{}
 116  	for _, opt := range opts {
 117  		opt(&v)
 118  	}
 119  
 120  	path := fmt.Sprintf("%s/%s?%s", "monitoring/history", id, v.Encode())
 121  
 122  	req, err := s.client.NewRequest("GET", path, nil)
 123  	if err != nil {
 124  		return nil, nil, err
 125  	}
 126  
 127  	var slgs []*monitor.StatusLog
 128  	resp, err := s.client.Do(req, &slgs)
 129  	if err != nil {
 130  		return nil, resp, err
 131  	}
 132  
 133  	return slgs, resp, nil
 134  }
 135