...
1 package types
2
3
4
5
6 import (
7 "encoding/json"
8 "fmt"
9
10 "edge-infra.dev/pkg/sds/emergencyaccess/msgdata"
11 )
12
13 type ConnectionPayload struct {
14 Error string `json:"error"`
15 Message msgdata.CommandResponse `json:"message"`
16 }
17
18 func (cp *ConnectionPayload) Validate() error {
19 if cp.Error == "" && cp.Message == nil {
20 return errBothNil
21 }
22 if len(cp.Error) > 0 && cp.Message != nil {
23 return errBothInit
24 }
25 return nil
26 }
27 func (cp *ConnectionPayload) UnmarshalJSON(data []byte) error {
28 m := struct {
29 Error string `json:"error"`
30 Message json.RawMessage `json:"message"`
31 }{}
32
33 err := json.Unmarshal(data, &m)
34 if err != nil {
35 return fmt.Errorf("error unmarshalling ConnectionPayload: %v", err)
36 }
37
38 cp.Error = m.Error
39
40 n := struct {
41 Attributes map[string]string `json:"attributes"`
42 Data json.RawMessage `json:"data"`
43 }{}
44
45 err = json.Unmarshal(m.Message, &n)
46 if err != nil {
47 return fmt.Errorf("error unmarshalling CommandResponse: %v", err)
48 }
49
50 out, err := msgdata.NewCommandResponse(n.Data, n.Attributes)
51 if err != nil {
52 return fmt.Errorf("error creating new command response: %v", err)
53 }
54
55 cp.Message = out
56 return nil
57 }
58
59 type StartSessionPayload struct {
60 Target Target `json:"target"`
61 SessionID string `json:"sessionid"`
62 }
63
64 func (payload *StartSessionPayload) Validate() error {
65 var errs ValidationErr
66
67 if payload.SessionID == "" {
68 errs = append(errs, userError{
69 errNilSessionID,
70 "Payload missing Session ID",
71 })
72 }
73 if err := payload.Target.validate(); err != nil {
74 if t, ok := err.(ValidationErr); ok {
75 errs = append(errs, t...)
76 } else {
77 errs = append(errs, userError{
78 err,
79 "Target is not valid",
80 })
81 }
82 }
83
84 if len(errs) != 0 {
85 return errs
86 }
87 return nil
88 }
89
90 type SendPayload struct {
91 Target Target `json:"target"`
92 AuthDetails AuthDetails `json:"authDetails"`
93 SessionID string `json:"sessionid"`
94 Command string `json:"command"`
95 }
96
97 func (payload *SendPayload) Validate() error {
98 var errs ValidationErr
99
100 if payload.SessionID == "" {
101 errs = append(errs, userError{
102 errNilSessionID,
103 "Payload missing Session ID",
104 })
105 }
106 if payload.Command == "" {
107 errs = append(errs, userError{
108 errNilCommand,
109 "Payload missing Command",
110 })
111 }
112
113 if err := payload.Target.validate(); err != nil {
114 if t, ok := err.(ValidationErr); ok {
115 errs = append(errs, t...)
116 } else {
117 errs = append(errs, userError{
118 err,
119 "Target is not valid",
120 })
121 }
122 }
123
124 if len(errs) != 0 {
125 return errs
126 }
127 return nil
128 }
129
130 type EndSessionPayload struct {
131 SessionID string `json:"sessionid"`
132 }
133
134 func (payload *EndSessionPayload) Validate() error {
135 if payload.SessionID == "" {
136 return ValidationErr{userError{
137 errNilSessionID,
138 "Payload missing Session ID",
139 }}
140 }
141 return nil
142 }
143
View as plain text