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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package client
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// MockScaleIOAPIServer represents VxFlex OS Gateway.
type MockScaleIOAPIServer struct {
User string
Password string
Token string
Version string
Instances Instances
Statistics SelectedStatistics
}
func (s MockScaleIOAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
default:
w.WriteHeader(http.StatusNotFound)
msg := fmt.Sprintf("unknown URL path: %s", r.URL.Path)
writeAPIError(w, msg)
case "/api/login":
s.handleLogin(w, r)
case "/api/logout":
s.handleLogout(w, r)
case "/api/version":
s.handleVersion(w, r)
case "/api/instances":
s.handleInstances(w, r)
case "/api/instances/querySelectedStatistics":
s.handleQuerySelectedStatistics(w, r)
}
}
func (s MockScaleIOAPIServer) handleLogin(w http.ResponseWriter, r *http.Request) {
if user, pass, ok := r.BasicAuth(); !ok || user != s.User || pass != s.Password {
w.WriteHeader(http.StatusUnauthorized)
msg := fmt.Sprintf("user got/expected: %s/%s, pass got/expected: %s/%s", user, s.User, pass, s.Password)
writeAPIError(w, msg)
return
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("wrong method: '%s', expected '%s'", r.Method, http.MethodGet)
writeAPIError(w, msg)
return
}
_, _ = w.Write([]byte(s.Token))
}
func (s MockScaleIOAPIServer) handleLogout(w http.ResponseWriter, r *http.Request) {
if _, pass, ok := r.BasicAuth(); !ok || pass != s.Token {
w.WriteHeader(http.StatusUnauthorized)
msg := fmt.Sprintf("token got/expected: %s/%s", pass, s.Token)
writeAPIError(w, msg)
return
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("wrong method: '%s', expected '%s'", r.Method, http.MethodGet)
writeAPIError(w, msg)
return
}
}
func (s MockScaleIOAPIServer) handleVersion(w http.ResponseWriter, r *http.Request) {
if _, pass, ok := r.BasicAuth(); !ok || pass != s.Token {
w.WriteHeader(http.StatusUnauthorized)
msg := fmt.Sprintf("token got/expected: %s/%s", pass, s.Token)
writeAPIError(w, msg)
return
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("wrong method: '%s', expected '%s'", r.Method, http.MethodGet)
writeAPIError(w, msg)
return
}
_, _ = w.Write([]byte(s.Version))
}
func (s MockScaleIOAPIServer) handleInstances(w http.ResponseWriter, r *http.Request) {
if _, pass, ok := r.BasicAuth(); !ok || pass != s.Token {
w.WriteHeader(http.StatusUnauthorized)
msg := fmt.Sprintf("token got/expected: %s/%s", pass, s.Token)
writeAPIError(w, msg)
return
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("wrong method: '%s', expected '%s'", r.Method, http.MethodGet)
writeAPIError(w, msg)
return
}
b, err := json.Marshal(s.Instances)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
msg := fmt.Sprintf("marshal Instances: %v", err)
writeAPIError(w, msg)
return
}
_, _ = w.Write(b)
}
func (s MockScaleIOAPIServer) handleQuerySelectedStatistics(w http.ResponseWriter, r *http.Request) {
if _, pass, ok := r.BasicAuth(); !ok || pass != s.Token {
w.WriteHeader(http.StatusUnauthorized)
msg := fmt.Sprintf("token got/expected: %s/%s", pass, s.Token)
writeAPIError(w, msg)
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("wrong method: '%s', expected '%s'", r.Method, http.MethodPost)
writeAPIError(w, msg)
return
}
if r.Header.Get("Content-Type") != "application/json" {
w.WriteHeader(http.StatusBadRequest)
writeAPIError(w, "no \"Content-Type: application/json\" in the header")
return
}
if err := json.NewDecoder(r.Body).Decode(&SelectedStatisticsQuery{}); err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("body decode error: %v", err)
writeAPIError(w, msg)
return
}
b, err := json.Marshal(s.Statistics)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
msg := fmt.Sprintf("marshal SelectedStatistics: %v", err)
writeAPIError(w, msg)
return
}
_, _ = w.Write(b)
}
func writeAPIError(w io.Writer, msg string) {
err := apiError{Message: msg}
b, _ := json.Marshal(err)
_, _ = w.Write(b)
}
|