forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
73 lines (63 loc) · 1.79 KB
/
request.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
package logic
import (
"encoding/json"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/Juniper/asf/pkg/format"
)
// Neutron API request operations
const (
OperationCreate = "CREATE"
OperationRead = "READ"
OperationReadAll = "READALL"
OperationUpdate = "UPDATE"
OperationDelete = "DELETE"
OperationReadCount = "READCOUNT"
OperationAddInterface = "ADDINTERFACE"
OperationDelInterface = "DELINTERFACE"
)
// Request defines an API request.
type Request struct {
Data Data `json:"data" yaml:"data"`
Context RequestContext `json:"context" yaml:"context"`
}
// Data defines API request data.
type Data struct {
Filters Filters `json:"filters" yaml:"filters"`
ID string `json:"id" yaml:"id"`
Fields Fields `json:"fields" yaml:"fields"`
Resource Resource `json:"resource" yaml:"resource"`
FieldMask types.FieldMask
}
// GetType returns resource type of the Request.
func (r *Request) GetType() string {
return r.Context.Type
}
// UnmarshalJSON performs custom unmarshalling of Request.
func (r *Request) UnmarshalJSON(data []byte) error {
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
return err
}
return r.ApplyMap(m)
}
// ApplyMap applies map into request.
func (r *Request) ApplyMap(m map[string]interface{}) error {
cm, ok := m["context"].(map[string]interface{})
if !ok {
return errors.Errorf("got invalid context: %v", m["context"])
}
if err := format.ApplyMap(cm, &r.Context); err != nil {
return err
}
resource, err := MakeResource(r.Context.Type)
if err != nil {
return err
}
r.Data.Resource = resource
dm, ok := m["data"].(map[string]interface{})
if !ok {
return errors.Errorf("got invalid data: %v", m["data"])
}
return format.ApplyMap(dm, &r.Data)
}