|
| 1 | +package worker |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/netboxlabs/orb-agent/agent/backend" |
| 13 | +) |
| 14 | + |
| 15 | +func (d *workerBackend) getProcRunningStatus() (backend.RunningStatus, string, error) { |
| 16 | + if d.proc == nil { |
| 17 | + return backend.Unknown, "backend not started yet", nil |
| 18 | + } |
| 19 | + status := d.proc.Status() |
| 20 | + |
| 21 | + if status.Error != nil { |
| 22 | + errMsg := fmt.Sprintf("worker process error: %v", status.Error) |
| 23 | + return backend.BackendError, errMsg, status.Error |
| 24 | + } |
| 25 | + |
| 26 | + if status.Complete { |
| 27 | + err := d.proc.Stop() |
| 28 | + return backend.Offline, "worker process ended", err |
| 29 | + } |
| 30 | + |
| 31 | + if status.StopTs > 0 { |
| 32 | + return backend.Offline, "worker process ended", nil |
| 33 | + } |
| 34 | + return backend.Running, "", nil |
| 35 | +} |
| 36 | + |
| 37 | +// note this needs to be stateless because it is called for multiple go routines |
| 38 | +func (d *workerBackend) request(url string, payload interface{}, method string, body io.Reader, contentType string, timeout int32) error { |
| 39 | + client := http.Client{ |
| 40 | + Timeout: time.Second * time.Duration(timeout), |
| 41 | + } |
| 42 | + |
| 43 | + status, _, err := d.getProcRunningStatus() |
| 44 | + if status != backend.Running { |
| 45 | + d.logger.Warn("skipping device discovery REST API request because process is not running or is unresponsive", zap.String("url", url), zap.String("method", method), zap.Error(err)) |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + URL := fmt.Sprintf("%s://%s:%s/api/v1/%s", d.apiProtocol, d.apiHost, d.apiPort, url) |
| 50 | + |
| 51 | + req, err := http.NewRequest(method, URL, body) |
| 52 | + if err != nil { |
| 53 | + d.logger.Error("received error from payload", zap.Error(err)) |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + req.Header.Add("Content-Type", contentType) |
| 58 | + res, getErr := client.Do(req) |
| 59 | + |
| 60 | + if getErr != nil { |
| 61 | + d.logger.Error("received error from payload", zap.Error(getErr)) |
| 62 | + return getErr |
| 63 | + } |
| 64 | + |
| 65 | + defer func() { |
| 66 | + if err := res.Body.Close(); err != nil { |
| 67 | + d.logger.Error("failed to close response body", zap.Error(err)) |
| 68 | + } |
| 69 | + }() |
| 70 | + |
| 71 | + if (res.StatusCode < 200) || (res.StatusCode > 299) { |
| 72 | + body, err := io.ReadAll(res.Body) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("non 2xx HTTP error code from worker, no or invalid body: %d", res.StatusCode) |
| 75 | + } |
| 76 | + if len(body) == 0 { |
| 77 | + return fmt.Errorf("%d empty body", res.StatusCode) |
| 78 | + } else if body[0] == '{' { |
| 79 | + var jsonBody map[string]interface{} |
| 80 | + err := json.Unmarshal(body, &jsonBody) |
| 81 | + if err == nil { |
| 82 | + if errMsg, ok := jsonBody["detail"]; ok { |
| 83 | + return fmt.Errorf("%d %s", res.StatusCode, errMsg) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if res.Body != nil { |
| 90 | + err = json.NewDecoder(res.Body).Decode(&payload) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
0 commit comments