-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
226 lines (207 loc) · 5.49 KB
/
util.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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"reflect"
"runtime/debug"
"strings"
"github.com/1and1/soma/lib/auth"
"github.com/1and1/soma/lib/proto"
)
func PanicCatcher(w http.ResponseWriter) {
if r := recover(); r != nil {
log.Printf("%s\n", debug.Stack())
msg := fmt.Sprintf("PANIC! %s", r)
http.Error(w, msg, http.StatusInternalServerError)
return
}
}
func DecodeJsonBody(r *http.Request, s interface{}) error {
decoder := json.NewDecoder(r.Body)
var err error
switch s.(type) {
case *proto.Request:
c := s.(*proto.Request)
err = decoder.Decode(c)
case *auth.Kex:
c := s.(*auth.Kex)
err = decoder.Decode(c)
default:
rt := reflect.TypeOf(s)
//return fmt.Errorf("DecodeJsonBody: Unhandled request type: %s", rt)
// XXX Dev Setting
errMsg := fmt.Sprintf("DecodeJsonBody: Unhandled request type: %s", rt)
log.Fatal(errMsg)
}
if err != nil {
return err
}
return nil
}
func ResultLength(r *somaResult, t ErrorMarker) int {
switch t.(type) {
case *proto.Result:
switch {
case r.Datacenters != nil:
return len(r.Datacenters)
case r.Levels != nil:
return len(r.Levels)
case r.Predicates != nil:
return len(r.Predicates)
case r.Status != nil:
return len(r.Status)
case r.Oncall != nil:
return len(r.Oncall)
case r.Teams != nil:
return len(r.Teams)
case r.Nodes != nil:
return len(r.Nodes)
case r.Views != nil:
return len(r.Views)
case r.Servers != nil:
return len(r.Servers)
case r.Units != nil:
return len(r.Units)
case r.Providers != nil:
return len(r.Providers)
case r.Metrics != nil:
return len(r.Metrics)
case r.Modes != nil:
return len(r.Modes)
case r.Users != nil:
return len(r.Users)
case r.Systems != nil:
return len(r.Systems)
case r.Capabilities != nil:
return len(r.Capabilities)
case r.Properties != nil:
return len(r.Properties)
case r.Attributes != nil:
return len(r.Attributes)
case r.Repositories != nil:
return len(r.Repositories)
case r.Buckets != nil:
return len(r.Buckets)
case r.Groups != nil:
return len(r.Groups)
case r.Clusters != nil:
return len(r.Clusters)
case r.CheckConfigs != nil:
return len(r.CheckConfigs)
case r.Validity != nil:
return len(r.Validity)
case r.HostDeployments != nil:
if len(r.Deployments) > len(r.HostDeployments) {
return len(r.Deployments)
}
return len(r.HostDeployments)
case r.Deployments != nil:
return len(r.Deployments)
}
default:
return 0
}
return 0
}
func DispatchBadRequest(w *http.ResponseWriter, err error) {
if err != nil {
http.Error(*w, err.Error(), http.StatusBadRequest)
return
}
http.Error(*w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
func DispatchUnauthorized(w *http.ResponseWriter, err error) {
if err != nil {
http.Error(*w, err.Error(), http.StatusUnauthorized)
return
}
http.Error(*w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
func DispatchForbidden(w *http.ResponseWriter, err error) {
if err != nil {
http.Error(*w, err.Error(), http.StatusForbidden)
return
}
http.Error(*w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
}
func DispatchNotFound(w *http.ResponseWriter, err error) {
if err != nil {
http.Error(*w, err.Error(), http.StatusNotFound)
return
}
http.Error(*w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
func DispatchConflict(w *http.ResponseWriter, err error) {
if err != nil {
http.Error(*w, err.Error(), http.StatusConflict)
return
}
http.Error(*w, http.StatusText(http.StatusConflict), http.StatusConflict)
}
func DispatchInternalError(w *http.ResponseWriter, err error) {
if err != nil {
http.Error(*w, err.Error(), http.StatusInternalServerError)
return
}
http.Error(*w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
func DispatchNotImplemented(w *http.ResponseWriter, err error) {
if err != nil {
http.Error(*w, err.Error(), http.StatusNotImplemented)
return
}
http.Error(*w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
}
func DispatchJsonReply(w *http.ResponseWriter, b *[]byte) {
(*w).Header().Set("Content-Type", "application/json")
(*w).WriteHeader(http.StatusOK)
(*w).Write(*b)
}
func DispatchOctetReply(w *http.ResponseWriter, b *[]byte) {
(*w).Header().Set("Content-Type", `application/octet-stream`)
(*w).WriteHeader(http.StatusOK)
(*w).Write(*b)
}
func GetPropertyTypeFromUrl(u *url.URL) (string, error) {
// strip surrounding / and skip first path element `property|filter`
el := strings.Split(strings.Trim(u.Path, "/"), "/")[1:]
if el[0] == "property" {
// looks like the path was /filter/property/...
el = el[1:]
}
switch el[0] {
case "service":
switch el[1] {
case "team":
return "service", nil
case "global":
return "template", nil
default:
return "", errors.New("Unknown service property type")
}
default:
return el[0], nil
}
}
// extractAddress extracts the IP address part of the IP:port string
// set as net/http.Request.RemoteAddr. It handles IPv4 cases like
// 192.0.2.1:48467 and IPv6 cases like [2001:db8::1%lo0]:48467
func extractAddress(str string) string {
var addr string
switch {
case strings.Contains(str, `]`):
// IPv6 address [2001:db8::1%lo0]:48467
addr = strings.Split(str, `]`)[0]
addr = strings.Split(addr, `%`)[0]
addr = strings.TrimLeft(addr, `[`)
default:
// IPv4 address 192.0.2.1:48467
addr = strings.Split(str, `:`)[0]
}
return addr
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix