-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
161 lines (132 loc) · 3.54 KB
/
main.go
File metadata and controls
161 lines (132 loc) · 3.54 KB
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
package main
import (
"encoding/base64"
"encoding/json"
"github.com/beopencloud/network-watcher-api-mock/env"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
// "github.com/beopencloud/network-watcher-api-mock/event"
//"k8s.io/client-go/dynamic"
//"k8s.io/client-go/kubernetes"
)
func main() {
// public views
http.HandleFunc("/", HandleIndex)
// private views
http.HandleFunc("/service/post", PostOnly(BasicAuth(HandlePost)))
log.Fatal(http.ListenAndServe(":80", nil))
}
type handler func(w http.ResponseWriter, r *http.Request)
func BasicAuth(pass handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
http.Error(w, "authorization failed", http.StatusUnauthorized)
return
}
payload, _ := base64.StdEncoding.DecodeString(auth[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || !validate(pair[0], pair[1]) {
http.Error(w, "authorization failed", http.StatusUnauthorized)
return
}
pass(w, r)
}
}
func validate(username, password string) bool {
if username == env.USERNAME && password == env.PASSWORD {
return true
}
return false
}
func HandleIndex(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello, world\n")
}
func HandlePost(w http.ResponseWriter, r *http.Request) {
log.Println("======= =====")
log.Println("CREATE EVENT")
res, _ := ioutil.ReadAll(r.Body)
ParsingData(res)
io.WriteString(w, "post\n")
return
}
func exitOnErr(err error) {
if err != nil {
log.Fatal(err)
}
}
type Result struct {
FirstName string `json:"first"`
LastName string `json:"last"`
}
func HandleJSON(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
result, _ := json.Marshal(Result{"tee", "dub"})
io.WriteString(w, string(result))
}
func GetOnly(h handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
h(w, r)
return
}
http.Error(w, "get only", http.StatusMethodNotAllowed)
}
}
func PostOnly(h handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
h(w, r)
return
}
http.Error(w, "post only", http.StatusMethodNotAllowed)
}
}
func PutOnly(h handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "PUT" {
h(w, r)
return
}
http.Error(w, "put only", http.StatusMethodNotAllowed)
}
}
func DeleteOnly(h handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" {
h(w, r)
return
}
http.Error(w, "delete only", http.StatusMethodNotAllowed)
}
}
// This function allow to retrieve data sended by the operator
func ParsingData(data []byte) {
var birdJson = string(data)
var result map[string]interface{}
json.Unmarshal([]byte(birdJson), &result)
metadata := result["metadata"].(map[string]interface{})
spec := result["spec"].(map[string]interface{})
log.Println("Name: ", metadata["name"].(string))
log.Println("Namespace: ", metadata["namespace"].(string))
log.Println("Type: ", spec["type"].(string))
var pspec map[string]interface{}
_ = json.Unmarshal([]byte(birdJson), &pspec)
for _, v := range pspec {
for key, value := range v.(map[string]interface{}) {
if key == "loadBalancer" {
for kk, vv := range value.(map[string]interface{}) {
if kk == "ingress" {
for _, vip := range vv.([]interface{}) {
vs := vip.(map[string]interface{})
log.Println("IP: ", vs["ip"])
}
}
}
}
}
}
}