forked from DataDog/pup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebooks.go
More file actions
269 lines (222 loc) · 6.72 KB
/
Copy pathnotebooks.go
File metadata and controls
269 lines (222 loc) · 6.72 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.
package cmd
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/DataDog/pup/pkg/formatter"
"github.com/spf13/cobra"
)
var notebooksCmd = &cobra.Command{
Use: "notebooks",
Short: "Manage notebooks",
Long: `Manage Datadog notebooks for investigation and documentation.
Notebooks combine graphs, logs, and narrative text to document
investigations, share findings, and create runbooks.
CAPABILITIES:
• List notebooks
• Get notebook details
• Create new notebooks
• Update notebooks
• Delete notebooks
EXAMPLES:
# List all notebooks
pup notebooks list
# Get notebook details
pup notebooks get notebook-id
# Create a notebook from file
pup notebooks create --body @notebook.json
# Create from stdin
cat notebook.json | pup notebooks create --body -
# Update a notebook
pup notebooks update 12345 --body @updated.json
# Delete a notebook
pup notebooks delete 12345
AUTHENTICATION:
Requires API key authentication (DD_API_KEY + DD_APP_KEY).
OAuth2 is not supported for this endpoint.`,
}
var notebooksListCmd = &cobra.Command{
Use: "list",
Short: "List notebooks",
RunE: runNotebooksList,
}
var notebooksGetCmd = &cobra.Command{
Use: "get [notebook-id]",
Short: "Get notebook details",
Args: cobra.ExactArgs(1),
RunE: runNotebooksGet,
}
var notebooksCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new notebook",
RunE: runNotebooksCreate,
}
var notebooksUpdateCmd = &cobra.Command{
Use: "update [notebook-id]",
Short: "Update a notebook",
Args: cobra.ExactArgs(1),
RunE: runNotebooksUpdate,
}
var notebooksDeleteCmd = &cobra.Command{
Use: "delete [notebook-id]",
Short: "Delete a notebook",
Args: cobra.ExactArgs(1),
RunE: runNotebooksDelete,
}
func init() {
notebooksCreateCmd.Flags().String("body", "", "JSON body (@filepath or - for stdin) (required)")
if err := notebooksCreateCmd.MarkFlagRequired("body"); err != nil {
panic(fmt.Errorf("failed to mark flag as required: %w", err))
}
notebooksUpdateCmd.Flags().String("body", "", "JSON body (@filepath or - for stdin) (required)")
if err := notebooksUpdateCmd.MarkFlagRequired("body"); err != nil {
panic(fmt.Errorf("failed to mark flag as required: %w", err))
}
notebooksCmd.AddCommand(notebooksListCmd, notebooksGetCmd, notebooksCreateCmd, notebooksUpdateCmd, notebooksDeleteCmd)
}
// readBody reads JSON body content from a file (@path) or stdin (-).
func readBody(value string) ([]byte, error) {
var data []byte
var err error
switch {
case value == "-":
data, err = io.ReadAll(inputReader)
if err != nil {
return nil, fmt.Errorf("failed to read body from stdin: %w", err)
}
case strings.HasPrefix(value, "@"):
path := strings.TrimPrefix(value, "@")
data, err = os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read body file: %w", err)
}
default:
return nil, fmt.Errorf("body must be @filepath or - for stdin")
}
if !json.Valid(data) {
return nil, fmt.Errorf("invalid JSON in body")
}
return data, nil
}
func runNotebooksCreate(cmd *cobra.Command, args []string) error {
client, err := getClientForEndpoint("POST", "/api/v1/notebooks")
if err != nil {
return err
}
bodyFlag, _ := cmd.Flags().GetString("body")
data, err := readBody(bodyFlag)
if err != nil {
return err
}
var body datadogV1.NotebookCreateRequest
if err := json.Unmarshal(data, &body); err != nil {
return fmt.Errorf("failed to parse notebook create request: %w", err)
}
api := datadogV1.NewNotebooksApi(client.V1())
resp, r, err := api.CreateNotebook(client.Context(), body)
if err != nil {
return formatAPIError("create notebook", err, r)
}
output, err := formatter.FormatOutput(resp, formatter.OutputFormat(outputFormat))
if err != nil {
return err
}
printOutput("%s\n", output)
return nil
}
func runNotebooksUpdate(cmd *cobra.Command, args []string) error {
client, err := getClientForEndpoint("PUT", "/api/v1/notebooks/")
if err != nil {
return err
}
notebookID := parseInt64(args[0])
bodyFlag, _ := cmd.Flags().GetString("body")
data, err := readBody(bodyFlag)
if err != nil {
return err
}
var body datadogV1.NotebookUpdateRequest
if err := json.Unmarshal(data, &body); err != nil {
return fmt.Errorf("failed to parse notebook update request: %w", err)
}
api := datadogV1.NewNotebooksApi(client.V1())
resp, r, err := api.UpdateNotebook(client.Context(), notebookID, body)
if err != nil {
return formatAPIError("update notebook", err, r)
}
output, err := formatter.FormatOutput(resp, formatter.OutputFormat(outputFormat))
if err != nil {
return err
}
printOutput("%s\n", output)
return nil
}
func runNotebooksList(cmd *cobra.Command, args []string) error {
client, err := getClientForEndpoint("GET", "/api/v1/notebooks")
if err != nil {
return err
}
api := datadogV1.NewNotebooksApi(client.V1())
resp, r, err := api.ListNotebooks(client.Context())
if err != nil {
return formatAPIError("list notebooks", err, r)
}
output, err := formatter.FormatOutput(resp, formatter.OutputFormat(outputFormat))
if err != nil {
return err
}
printOutput("%s\n", output)
return nil
}
func runNotebooksGet(cmd *cobra.Command, args []string) error {
client, err := getClientForEndpoint("GET", "/api/v1/notebooks/")
if err != nil {
return err
}
notebookID := parseInt64(args[0])
api := datadogV1.NewNotebooksApi(client.V1())
resp, r, err := api.GetNotebook(client.Context(), notebookID)
if err != nil {
return formatAPIError("get notebook", err, r)
}
output, err := formatter.FormatOutput(resp, formatter.OutputFormat(outputFormat))
if err != nil {
return err
}
printOutput("%s\n", output)
return nil
}
func runNotebooksDelete(cmd *cobra.Command, args []string) error {
client, err := getClientForEndpoint("DELETE", "/api/v1/notebooks/")
if err != nil {
return err
}
notebookID := parseInt64(args[0])
if !cfg.AutoApprove {
printOutput("⚠️ WARNING: This will permanently delete notebook %d\n", notebookID)
printOutput("Are you sure you want to continue? (y/N): ")
response, err := readConfirmation()
if err != nil {
printOutput("\nOperation cancelled\n")
return nil
}
if response != "y" && response != "Y" {
printOutput("Operation cancelled\n")
return nil
}
}
api := datadogV1.NewNotebooksApi(client.V1())
r, err := api.DeleteNotebook(client.Context(), notebookID)
if err != nil {
return formatAPIError("delete notebook", err, r)
}
printOutput("Successfully deleted notebook %d\n", notebookID)
return nil
}