/
workers_tail.go
114 lines (92 loc) · 2.96 KB
/
workers_tail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package khulnasoft
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
var (
ErrMissingScriptName = errors.New("required script name missing")
ErrMissingTailID = errors.New("required tail id missing")
)
type WorkersTail struct {
ID string `json:"id"`
URL string `json:"url"`
ExpiresAt *time.Time `json:"expires_at"`
}
type StartWorkersTailResponse struct {
Response
Result WorkersTail
}
type ListWorkersTailParameters struct {
AccountID string
ScriptName string
}
type ListWorkersTailResponse struct {
Response
Result WorkersTail
}
// StartWorkersTail Starts a tail that receives logs and exception from a Worker.
//
// API reference: https://developers.khulnasoft.com/api/operations/worker-tail-logs-start-tail
func (api *API) StartWorkersTail(ctx context.Context, rc *ResourceContainer, scriptName string) (WorkersTail, error) {
if rc.Identifier == "" {
return WorkersTail{}, ErrMissingAccountID
}
if scriptName == "" {
return WorkersTail{}, ErrMissingScriptName
}
uri := fmt.Sprintf("/accounts/%s/workers/scripts/%s/tails", rc.Identifier, scriptName)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return WorkersTail{}, err
}
var workerstailResponse StartWorkersTailResponse
if err := json.Unmarshal(res, &workerstailResponse); err != nil {
return WorkersTail{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return workerstailResponse.Result, nil
}
// ListWorkersTail Get list of tails currently deployed on a Worker.
//
// API reference: https://developers.khulnasoft.com/api/operations/worker-tail-logs-list-tails
func (api *API) ListWorkersTail(ctx context.Context, rc *ResourceContainer, params ListWorkersTailParameters) (WorkersTail, error) {
if rc.Identifier == "" {
return WorkersTail{}, ErrMissingAccountID
}
if params.ScriptName == "" {
return WorkersTail{}, ErrMissingScriptName
}
uri := fmt.Sprintf("/accounts/%s/workers/scripts/%s/tails", rc.Identifier, params.ScriptName)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return WorkersTail{}, err
}
var workerstailResponse ListWorkersTailResponse
if err := json.Unmarshal(res, &workerstailResponse); err != nil {
return WorkersTail{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return workerstailResponse.Result, nil
}
// DeleteWorkersTail Deletes a tail from a Worker.
//
// API reference: https://developers.khulnasoft.com/api/operations/worker-tail-logs-delete-tail
func (api *API) DeleteWorkersTail(ctx context.Context, rc *ResourceContainer, scriptName, tailID string) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}
if scriptName == "" {
return ErrMissingScriptName
}
if tailID == "" {
return ErrMissingTailID
}
uri := fmt.Sprintf("/accounts/%s/workers/scripts/%s/tails/%s", rc.Identifier, scriptName, tailID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}