-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathmock_server.go
204 lines (177 loc) · 4.27 KB
/
mock_server.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
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 mock
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"time"
)
// IPType ip type
type IPType string
const (
Host IPType = "host"
Mask IPType = "mask"
Backoff IPType = "backoff"
)
// Request request cmdb data
type Request struct {
RequestID string `json:"request_id"`
PageNo int64 `json:"page_no"`
PageSize int64 `json:"page_size"`
}
// Response response cmdb data
type Response struct {
Total int `json:"total"`
Size int `json:"size"`
Code int `json:"code"`
Info string `json:"info"`
Priority string `json:"priority"`
Data []IPInfo `json:"data"`
}
// IPInfo ip info
type IPInfo struct {
IP string `json:"ip"`
Type IPType `json:"type"`
Region LocationInfo `json:"region"`
Zone LocationInfo `json:"zone"`
Campus LocationInfo `json:"campus"`
}
// LocationInfo
type LocationInfo struct {
Name string `json:"name"`
Metadata map[string]interface{} `json:"metadata"`
}
func RunMockCMDBServer(cnt int) (int, net.Listener) {
initData(cnt)
ln, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", 0))
if err != nil {
log.Fatalf("[ERROR]fail to listen tcp, err is %v", err)
}
port := ln.Addr().(*net.TCPAddr).Port
log.Printf("listen port : %d", port)
mux := http.NewServeMux()
mux.HandleFunc("/", handle)
go func() {
_ = http.Serve(ln, mux)
}()
return port, ln
}
var (
IPInfos []IPInfo
)
func initData(cnt int) {
IPInfos = []IPInfo{}
for i := 0; i < cnt; i++ {
ipv4 := IPv4Int(RandomIpv4Int())
IPInfos = append(IPInfos, IPInfo{
IP: ipv4.ip().String(),
Type: Host,
Region: LocationInfo{
Name: "ap-gz",
},
Zone: LocationInfo{
Name: fmt.Sprintf("ap-gz-%d", i),
Metadata: map[string]interface{}{
"id": i,
},
},
})
}
}
func handle(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
resp := Response{
Code: 500001,
Info: err.Error(),
}
if ret, err := json.Marshal(resp); err != nil {
log.Printf("json marshal %+v", err)
w.WriteHeader(http.StatusInternalServerError)
} else {
_, _ = w.Write(ret)
}
return
}
req := Request{}
if err := json.Unmarshal(data, &req); err != nil {
resp := Response{
Code: 500001,
Info: err.Error(),
}
if ret, err := json.Marshal(resp); err != nil {
log.Printf("json marshal %+v", err)
w.WriteHeader(http.StatusInternalServerError)
} else {
_, _ = w.Write(ret)
}
return
}
pageNo := req.PageNo
pageSize := req.PageSize
offset := (pageNo - 1) * pageSize
end := offset + pageSize
if int(offset) > len(IPInfos) {
resp := Response{
Code: 200000,
Total: len(IPInfos),
Size: 0,
Data: []IPInfo{},
}
if ret, err := json.Marshal(resp); err != nil {
log.Printf("json marshal %+v", err)
w.WriteHeader(http.StatusInternalServerError)
} else {
_, _ = w.Write(ret)
}
return
}
if int(end) > len(IPInfos) {
end = int64(len(IPInfos))
}
values := IPInfos[offset:end]
resp := Response{
Code: 200000,
Total: len(IPInfos),
Size: len(values),
Data: values,
}
if ret, err := json.Marshal(resp); err != nil {
log.Printf("json marshal %+v", err)
w.WriteHeader(http.StatusInternalServerError)
} else {
_, _ = w.Write(ret)
}
return
}
type IPv4Int uint32
func (i IPv4Int) ip() net.IP {
ip := make(net.IP, net.IPv6len)
copy(ip, net.IPv4zero)
binary.BigEndian.PutUint32(ip.To4(), uint32(i))
return ip.To16()
}
func RandomIpv4Int() uint32 {
return rand.New(rand.NewSource(time.Now().UnixNano())).Uint32()
}