forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
158 lines (135 loc) · 3.58 KB
/
config.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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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
http://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 rest
import (
"net/http"
"strconv"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/cdata"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/julienschmidt/httprouter"
)
func (s *Server) getPluginConfigItem(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
styp := p.ByName("type")
if styp == "" {
cdn := s.mc.GetPluginConfigDataNodeAll()
item := &rbody.PluginConfigItem{ConfigDataNode: cdn}
respond(200, item, w)
return
}
typ, err := getPluginType(styp)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}
name := p.ByName("name")
sver := p.ByName("version")
var iver int
if sver != "" {
if iver, err = strconv.Atoi(sver); err != nil {
respond(400, rbody.FromError(err), w)
return
}
} else {
iver = -2
}
cdn := s.mc.GetPluginConfigDataNode(typ, name, iver)
item := &rbody.PluginConfigItem{ConfigDataNode: cdn}
respond(200, item, w)
}
func (s *Server) deletePluginConfigItem(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
var typ core.PluginType
styp := p.ByName("type")
if styp != "" {
typ, err = getPluginType(styp)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}
}
name := p.ByName("name")
sver := p.ByName("version")
var iver int
if sver != "" {
if iver, err = strconv.Atoi(sver); err != nil {
respond(400, rbody.FromError(err), w)
return
}
} else {
iver = -2
}
src := []string{}
errCode, err := marshalBody(&src, r.Body)
if errCode != 0 && err != nil {
respond(400, rbody.FromError(err), w)
return
}
var res cdata.ConfigDataNode
if styp == "" {
res = s.mc.DeletePluginConfigDataNodeFieldAll(src...)
} else {
res = s.mc.DeletePluginConfigDataNodeField(typ, name, iver, src...)
}
item := &rbody.DeletePluginConfigItem{ConfigDataNode: res}
respond(200, item, w)
}
func (s *Server) setPluginConfigItem(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
var typ core.PluginType
styp := p.ByName("type")
if styp != "" {
typ, err = getPluginType(styp)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}
}
name := p.ByName("name")
sver := p.ByName("version")
var iver int
if sver != "" {
if iver, err = strconv.Atoi(sver); err != nil {
respond(400, rbody.FromError(err), w)
return
}
} else {
iver = -2
}
src := cdata.NewNode()
errCode, err := marshalBody(src, r.Body)
if errCode != 0 && err != nil {
respond(400, rbody.FromError(err), w)
return
}
var res cdata.ConfigDataNode
if styp == "" {
res = s.mc.MergePluginConfigDataNodeAll(src)
} else {
res = s.mc.MergePluginConfigDataNode(typ, name, iver, src)
}
item := &rbody.SetPluginConfigItem{ConfigDataNode: res}
respond(200, item, w)
}
func getPluginType(t string) (core.PluginType, error) {
if ityp, err := strconv.Atoi(t); err == nil {
return core.PluginType(ityp), nil
}
ityp, err := core.ToPluginType(t)
if err != nil {
return core.PluginType(-1), err
}
return ityp, nil
}