-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.go
482 lines (455 loc) · 14.5 KB
/
resources.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package resources defines Golang representations for all of the resources defined by the Jupyter Swagger API.
package resources
import (
"encoding/json"
"fmt"
"net/http"
"github.com/GoogleCloudPlatform/notebook-kernels-mixer/util"
)
// KernelSpecs represents the collection of kernel specs returned by a kernel spec list call.
type KernelSpecs struct {
Default string `json:"default"`
KernelSpecs map[string]*KernelSpec `json:"kernelspecs"`
rawFields map[string]any
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (ks *KernelSpecs) UnmarshalJSON(b []byte) error {
rawFields := make(map[string]any)
if err := json.Unmarshal(b, &rawFields); err != nil {
return err
}
if len(rawFields) == 0 {
// The JSON object was empty; leave the structured object empty too.
return nil
}
if defaultVal, ok := rawFields["default"]; ok {
defaultString, ok := defaultVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'default': %+v: %w", defaultVal, util.HTTPError(http.StatusBadRequest))
}
ks.Default = defaultString
}
specs, ok := rawFields["kernelspecs"]
if !ok {
ks.rawFields = rawFields
return nil
}
ksMap, ok := specs.(map[string]any)
if !ok {
return fmt.Errorf("invalid value for the field 'kernelspecs': %+v: %w", specs, util.HTTPError(http.StatusBadRequest))
}
ks.KernelSpecs = make(map[string]*KernelSpec)
for name, specObj := range ksMap {
specBytes, err := json.Marshal(specObj)
if err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
var spec KernelSpec
if err := json.Unmarshal(specBytes, &spec); err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
ks.KernelSpecs[name] = &spec
}
ks.rawFields = rawFields
return nil
}
// MarshalJSON implements the json.Marshaler interface
func (ks KernelSpecs) MarshalJSON() ([]byte, error) {
rawFields := make(map[string]any)
for k, v := range ks.rawFields {
rawFields[k] = v
}
if len(ks.Default) > 0 {
rawFields["default"] = ks.Default
}
specMap := make(map[string]any)
rawFields["kernelspecs"] = specMap
for name, spec := range ks.KernelSpecs {
specBytes, err := json.Marshal(spec)
if err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
spec := make(map[string]any)
if err := json.Unmarshal(specBytes, &spec); err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
specMap[name] = spec
}
return json.Marshal(rawFields)
}
// Spec defines the `spec` field nested within a KernelSpec
type Spec struct {
Language string `json:"language"`
Argv []string `json:"argv"`
DisplayName string `json:"display_name"`
CodemirrorMode string `json:"codemirror_mode,omitempty"`
Env map[string]string `json:"env"`
HelpLinks map[string]string `json:"help_links,omitempty"`
Metadata map[string]any `json:"metadata"`
InterruptMode string `json:"interrupt_mode,omitempty"`
}
// KernelSpec defines one of the available kernel configurations supported by a Jupyter server.
type KernelSpec struct {
ID string `json:"name"`
Spec *Spec `json:"spec"`
Resources map[string]string `json:"resources"`
rawFields map[string]any
}
// Identify returns the ID of the kernelspec.
func (ks *KernelSpec) Identify() string {
return ks.ID
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (ks *KernelSpec) UnmarshalJSON(b []byte) error {
rawFields := make(map[string]any)
if err := json.Unmarshal(b, &rawFields); err != nil {
return err
}
if len(rawFields) == 0 {
// The JSON object was empty; leave the structured object empty too.
return nil
}
if name, ok := rawFields["name"]; ok {
idString, ok := name.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'name': %+v: %w", name, util.HTTPError(http.StatusBadRequest))
}
ks.ID = idString
}
if resources, ok := rawFields["resources"]; ok {
resourcesMap, ok := resources.(map[string]any)
if !ok {
return fmt.Errorf("invalid value for the field 'resources': %+v: %w", resources, util.HTTPError(http.StatusBadRequest))
}
ks.Resources = make(map[string]string)
for name, val := range resourcesMap {
if resource, ok := val.(string); ok {
ks.Resources[name] = resource
}
}
}
spec, ok := rawFields["spec"]
if !ok {
ks.rawFields = rawFields
return nil
}
specBytes, err := json.Marshal(spec)
if err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
if err := json.Unmarshal(specBytes, &ks.Spec); err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
ks.rawFields = rawFields
return nil
}
// MarshalJSON implements the json.Marshaler interface
func (ks KernelSpec) MarshalJSON() ([]byte, error) {
rawFields := make(map[string]any)
for k, v := range ks.rawFields {
rawFields[k] = v
}
if len(ks.ID) > 0 {
rawFields["name"] = ks.ID
}
if ks.Spec != nil {
specBytes, err := json.Marshal(ks.Spec)
if err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
specMap := make(map[string]any)
if err := json.Unmarshal(specBytes, &specMap); err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
for k, v := range specMap {
if v == nil {
delete(specMap, k)
}
}
rawFields["spec"] = specMap
}
if len(ks.Resources) > 0 {
rawFields["resources"] = ks.Resources
}
return json.Marshal(rawFields)
}
// Kernel defines a running process for executing code inside of a Jupyter server.
type Kernel struct {
ID string `json:"id,omitempty"`
SpecID string `json:"name"`
LastActivity string `json:"last_activity,omitempty"`
Connections int `json:"connections"`
ExecutionState string `json:"execution_state,omitempty"`
// The `env` field is not part of the documented API, but is set by the notebook
// server when calling into gateway servers. See here:
// https://github.com/jupyter/notebook/blob/2cfff07a39fa486a3f05c26b400fa26e1802a053/notebook/gateway/managers.py#L408
Env map[string]any `json:"env,omitempty"`
// This `metadata` field is not part of the Jupyter API. It is added as an extension for reporting
// additional metadata.
Metadata map[string]any `json:"metadata,omitempty"`
rawFields map[string]any
}
// Identify returns the ID of the kernel.
func (k *Kernel) Identify() string {
return k.ID
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (k *Kernel) UnmarshalJSON(b []byte) error {
rawFields := make(map[string]any)
if err := json.Unmarshal(b, &rawFields); err != nil {
return err
}
if len(rawFields) == 0 {
// The JSON object was empty; leave the structured object empty too.
return nil
}
if idVal, ok := rawFields["id"]; ok {
idString, ok := idVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'id': %+v: %w", idVal, util.HTTPError(http.StatusBadRequest))
}
k.ID = idString
}
if specIDVal, ok := rawFields["name"]; ok {
specIDString, ok := specIDVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'name': %+v: %w", specIDVal, util.HTTPError(http.StatusBadRequest))
}
k.SpecID = specIDString
}
if lastActivityVal, ok := rawFields["last_activity"]; ok {
lastActivityString, ok := lastActivityVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'last_activity': %+v: %w", lastActivityVal, util.HTTPError(http.StatusBadRequest))
}
k.LastActivity = lastActivityString
}
if connectionsVal, ok := rawFields["connections"]; ok {
connectionsNumber, ok := connectionsVal.(float64)
if !ok {
return fmt.Errorf("invalid type for the field 'connections': %+v: %w", connectionsVal, util.HTTPError(http.StatusBadRequest))
}
k.Connections = int(connectionsNumber)
}
if executionStateVal, ok := rawFields["execution_state"]; ok {
executionStateString, ok := executionStateVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'execution_state': %+v: %w", executionStateVal, util.HTTPError(http.StatusBadRequest))
}
k.ExecutionState = executionStateString
}
if envVal, ok := rawFields["env"]; ok {
envMap, ok := envVal.(map[string]any)
if !ok {
return fmt.Errorf("invalid value for the field 'env': %+v: %w", envVal, util.HTTPError(http.StatusBadRequest))
}
k.Env = envMap
}
if metadataVal, ok := rawFields["metadata"]; ok {
metadataMap, ok := metadataVal.(map[string]any)
if !ok {
return fmt.Errorf("invalid value for the field 'metadata': %+v: %w", metadataVal, util.HTTPError(http.StatusBadRequest))
}
k.Metadata = metadataMap
}
k.rawFields = rawFields
return nil
}
// MarshalJSON implements the json.Marshaler interface
func (k Kernel) MarshalJSON() ([]byte, error) {
rawFields := make(map[string]any)
for key, v := range k.rawFields {
rawFields[key] = v
}
if len(k.ID) > 0 {
rawFields["id"] = k.ID
}
if len(k.SpecID) > 0 {
rawFields["name"] = k.SpecID
}
if len(k.LastActivity) > 0 {
rawFields["last_activity"] = k.LastActivity
}
// Unconditionally set the "connections" field because Jupyter requires it to be present.
rawFields["connections"] = k.Connections
if len(k.ExecutionState) > 0 {
rawFields["execution_state"] = k.ExecutionState
}
if len(k.Env) > 0 {
rawFields["env"] = k.Env
}
if len(k.Metadata) > 0 {
rawFields["metadata"] = k.Metadata
}
return json.Marshal(rawFields)
}
// Session defines a mapping between a file path and a kernel.
type Session struct {
ID string `json:"id"`
Path string `json:"path"`
Name string `json:"name"`
Type string `json:"type"`
Kernel *Kernel `json:"kernel"`
Notebook map[string]string `json:"notebook,omitempty"`
rawFields map[string]any
}
// Identify returns the ID of the kernel.
func (s *Session) Identify() string {
return s.ID
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (s *Session) UnmarshalJSON(b []byte) error {
rawFields := make(map[string]any)
if err := json.Unmarshal(b, &rawFields); err != nil {
return err
}
if len(rawFields) == 0 {
// The JSON object was empty; leave the structured object empty too.
return nil
}
if idVal, ok := rawFields["id"]; ok {
idString, ok := idVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'id': %+v: %w", idVal, util.HTTPError(http.StatusBadRequest))
}
s.ID = idString
}
if pathVal, ok := rawFields["path"]; ok {
pathString, ok := pathVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'path': %+v: %w", pathVal, util.HTTPError(http.StatusBadRequest))
}
s.Path = pathString
}
if nameVal, ok := rawFields["name"]; ok {
nameString, ok := nameVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'name': %+v: %w", nameVal, util.HTTPError(http.StatusBadRequest))
}
s.Name = nameString
}
if typeVal, ok := rawFields["type"]; ok {
typeString, ok := typeVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'type': %+v: %w", typeVal, util.HTTPError(http.StatusBadRequest))
}
s.Type = typeString
}
if notebookVal, ok := rawFields["notebook"]; ok {
notebookMap, ok := notebookVal.(map[string]any)
if !ok {
return fmt.Errorf("invalid value for the field 'notebook': %+v: %w", notebookVal, util.HTTPError(http.StatusBadRequest))
}
s.Notebook = make(map[string]string)
for name, val := range notebookMap {
if valStr, ok := val.(string); ok {
s.Notebook[name] = valStr
}
}
}
k, ok := rawFields["kernel"]
if !ok {
return nil
}
kernelBytes, err := json.Marshal(k)
if err != nil {
return fmt.Errorf("failure unmarshalling a nested `kernel` field: %w", err)
}
if err := json.Unmarshal(kernelBytes, &s.Kernel); err != nil {
return fmt.Errorf("failure unmarshalling a nested `kernel` field: %w", err)
}
s.rawFields = rawFields
return nil
}
// MarshalJSON implements the json.Marshaler interface
func (s Session) MarshalJSON() ([]byte, error) {
rawFields := make(map[string]any)
for k, v := range s.rawFields {
rawFields[k] = v
}
if len(s.ID) > 0 {
rawFields["id"] = s.ID
}
if len(s.Path) > 0 {
rawFields["path"] = s.Path
}
if len(s.Name) > 0 {
rawFields["name"] = s.Name
}
if len(s.Type) > 0 {
rawFields["type"] = s.Type
}
if len(s.Notebook) > 0 {
rawFields["notebook"] = s.Notebook
}
if s.Kernel == nil {
return json.Marshal(rawFields)
}
kernelBytes, err := json.Marshal(s.Kernel)
if err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `kernel` field: %w", err)
}
kernelMap := make(map[string]any)
if err := json.Unmarshal(kernelBytes, &kernelMap); err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `kernel` field: %w", err)
}
for k, v := range kernelMap {
if v == nil {
delete(kernelMap, k)
}
}
rawFields["kernel"] = kernelMap
return json.Marshal(rawFields)
}
// Terminal defines an interactive terminal running inside of a Jupyter server.
type Terminal struct {
ID string `json:"name"`
rawFields map[string]any
}
// Identify returns the ID of the kernel.
func (t *Terminal) Identify() string {
return t.ID
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *Terminal) UnmarshalJSON(b []byte) error {
rawFields := make(map[string]any)
if err := json.Unmarshal(b, &rawFields); err != nil {
return err
}
if len(rawFields) == 0 {
// The JSON object was empty; leave the structured object empty too.
return nil
}
if idVal, ok := rawFields["name"]; ok {
idString, ok := idVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'name': %+v: %w", idVal, util.HTTPError(http.StatusBadRequest))
}
t.ID = idString
}
t.rawFields = rawFields
return nil
}
// MarshalJSON implements the json.Marshaler interface
func (t Terminal) MarshalJSON() ([]byte, error) {
rawFields := make(map[string]any)
for k, v := range t.rawFields {
rawFields[k] = v
}
if len(t.ID) > 0 {
rawFields["name"] = t.ID
}
return json.Marshal(rawFields)
}