pulsar_job.go raw

   1  package rest
   2  
   3  import (
   4  	"errors"
   5  	"fmt"
   6  	"net/http"
   7  
   8  	"gopkg.in/ns1/ns1-go.v2/rest/model/pulsar"
   9  )
  10  
  11  // PulsarJobsService handles 'pulsar/apps/APPID/jobs/JOBID' endpoint.
  12  type PulsarJobsService service
  13  
  14  // List takes an Application ID and returns all Jobs inside said Application.
  15  //
  16  // NS1 API docs: https://ns1.com/api/#getlist-jobs-within-an-app
  17  func (s *PulsarJobsService) List(appID string) ([]*pulsar.Job, *http.Response, error) {
  18  	path := fmt.Sprintf("pulsar/apps/%s/jobs", appID)
  19  	req, err := s.client.NewRequest("GET", path, nil)
  20  	if err != nil {
  21  		return nil, nil, err
  22  	}
  23  
  24  	jl := []*pulsar.Job{}
  25  	var resp *http.Response
  26  	resp, err = s.client.Do(req, &jl)
  27  	if err != nil {
  28  		switch errorType := err.(type) {
  29  		case *Error:
  30  			if errorType.Resp.StatusCode == 404 {
  31  				return nil, resp, ErrAppMissing
  32  			}
  33  		}
  34  		return nil, resp, err
  35  	}
  36  
  37  	return jl, resp, nil
  38  }
  39  
  40  // Get takes an Application ID and Job Id and returns full configuration for a pulsar Job.
  41  //
  42  // NS1 API docs: https://ns1.com/api/#getview-job-details
  43  func (s *PulsarJobsService) Get(appID string, jobID string) (*pulsar.Job, *http.Response, error) {
  44  	path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", appID, jobID)
  45  
  46  	req, err := s.client.NewRequest("GET", path, nil)
  47  	if err != nil {
  48  		return nil, nil, err
  49  	}
  50  
  51  	var job pulsar.Job
  52  
  53  	resp, err := s.client.Do(req, &job)
  54  	if err != nil {
  55  		switch errorType := err.(type) {
  56  		case *Error:
  57  			jobNotFound := fmt.Sprintf("pulsar job %s not found for appid %s", jobID, appID)
  58  			switch errorType.Message {
  59  			case jobNotFound:
  60  				return nil, resp, ErrJobMissing
  61  
  62  			case "pulsar app not found":
  63  				return nil, resp, ErrAppMissing
  64  			}
  65  		}
  66  		return nil, resp, err
  67  	}
  68  
  69  	return &job, resp, nil
  70  }
  71  
  72  // Create takes a *PulsarJob and an AppId and creates a new Pulsar Job in the specified Application with the specific name, typeid, host and url_path.
  73  //
  74  // NS1 API docs: https://ns1.com/api/#putcreate-a-pulsar-job
  75  func (s *PulsarJobsService) Create(j *pulsar.Job) (*http.Response, error) {
  76  	path := fmt.Sprintf("pulsar/apps/%s/jobs", j.AppID)
  77  
  78  	req, err := s.client.NewRequest("PUT", path, j)
  79  	if err != nil {
  80  		return nil, err
  81  	}
  82  
  83  	// Update job fields with data from api(ensure consistent)
  84  	resp, err := s.client.Do(req, j)
  85  	if err != nil {
  86  		switch errorType := err.(type) {
  87  		case *Error:
  88  			if errorType.Resp.StatusCode == 404 {
  89  				return resp, ErrAppMissing
  90  			}
  91  		}
  92  		return resp, err
  93  	}
  94  
  95  	return resp, nil
  96  }
  97  
  98  // Update takes a *PulsarJob and modifies configuration details for an existing Pulsar job.
  99  //
 100  // Only the fields to be updated are required in the given job.
 101  // NS1 API docs: https://ns1.com/api/#postmodify-a-pulsar-job
 102  func (s *PulsarJobsService) Update(j *pulsar.Job) (*http.Response, error) {
 103  	path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", j.AppID, j.JobID)
 104  
 105  	req, err := s.client.NewRequest("POST", path, j)
 106  	if err != nil {
 107  		return nil, err
 108  	}
 109  
 110  	// Update jobs fields with data from api(ensure consistent)
 111  	resp, err := s.client.Do(req, j)
 112  	if err != nil {
 113  		switch errorType := err.(type) {
 114  		case *Error:
 115  			jobNotFound := fmt.Sprintf("pulsar job %s not found for appid %s", j.JobID, j.AppID)
 116  			switch errorType.Message {
 117  			case "pulsar app not found":
 118  				return resp, ErrAppMissing
 119  			case jobNotFound:
 120  				return resp, ErrJobMissing
 121  			}
 122  		}
 123  		return resp, err
 124  	}
 125  
 126  	return resp, nil
 127  }
 128  
 129  // Delete takes a appId and jobId and removes an existing Pulsar job .
 130  //
 131  // NS1 API docs: https://ns1.com/api/#deletedelete-a-pulsar-job
 132  func (s *PulsarJobsService) Delete(pulsarJob *pulsar.Job) (*http.Response, error) {
 133  	path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID)
 134  
 135  	req, err := s.client.NewRequest("DELETE", path, nil)
 136  	if err != nil {
 137  		return nil, err
 138  	}
 139  
 140  	resp, err := s.client.Do(req, nil)
 141  	if err != nil {
 142  		switch errorType := err.(type) {
 143  		case *Error:
 144  			jobNotFound := fmt.Sprintf("pulsar job %s not found for appid %s", pulsarJob.JobID, pulsarJob.AppID)
 145  			switch errorType.Message {
 146  			case jobNotFound:
 147  				return resp, ErrJobMissing
 148  
 149  			case "pulsar app not found":
 150  				return resp, ErrAppMissing
 151  			}
 152  		}
 153  		return resp, err
 154  	}
 155  
 156  	return resp, nil
 157  }
 158  
 159  var (
 160  	// ErrAppMissing bundles GET/PUT/POST/DELETE
 161  	ErrAppMissing = errors.New("pulsar application does not exist")
 162  	// ErrJobMissing bundles GET/POST/DELETE error.
 163  	ErrJobMissing = errors.New("pulsar job does not exist")
 164  )
 165