-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.go
108 lines (94 loc) · 2.04 KB
/
json.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
// Copyright 2014 The GoSNMPQuerier Authors. All rights reserved. Use of this
// source code is governed by a MIT-style license that can be found in the
// LICENSE file.
package gosnmpquerier
import (
"encoding/json"
"fmt"
"time"
"github.com/gosnmp/gosnmp"
)
type queryMessage struct {
Command string
Destination string
Community string
Oids []string
Timeout int
Retries int
AdditionalInfo interface{}
}
type outputMessage struct {
Id int
Command string
Community string
Oids []string
Timeout time.Duration
Retries int
Destination string
Response []gosnmp.SnmpPDU
Error string
}
func ToJson(query *Query) (string, error) {
var errString string = ""
if query.Error != nil {
errString = query.Error.Error()
}
d := outputMessage{
Id: query.Id,
Command: convertCommandToCommandString(query.Cmd),
Community: query.Community,
Oids: query.Oids,
Timeout: query.Timeout,
Retries: query.Retries,
Destination: query.Destination,
Response: query.Response,
Error: errString,
}
fmt.Println(d)
b, err := json.Marshal(d)
if err != nil {
return "", err
}
return string(b), nil
}
func FromJson(jsonText string) (*Query, error) {
var m queryMessage
m.Timeout = 2
m.Retries = 1
b := []byte(jsonText)
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
cmd, err := ConvertCommand(m.Command)
if err != nil {
return nil, err
}
q := Query{
Cmd: cmd,
Community: m.Community,
Oids: m.Oids,
Destination: m.Destination,
Timeout: time.Duration(m.Timeout) * time.Second,
Retries: m.Retries,
}
return &q, nil
}
func convertCommandToCommandString(command OpSnmp) string {
switch command {
case WALK:
return "walk"
case GET:
return "get"
}
return ""
}
func ConvertCommand(command string) (OpSnmp, error) {
switch command {
case "walk":
return WALK, nil
case "get":
return GET, nil
default:
return 0, fmt.Errorf("unsupported command %s", command)
}
}