/
services.go
150 lines (133 loc) · 3.07 KB
/
services.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
package handlers
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"github.com/golang/glog"
"github.com/kubernetes/client-go/kubernetes/scheme"
"github.com/yinwoods/anchor/anchor/cmd"
"k8s.io/api/core/v1"
)
func servicesListHandler(c *gin.Context) {
err := parseSessionCookie(c)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
services, err := cmd.ServicesList("")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.HTML(http.StatusOK, "services.tmpl", services)
}
func serviceInfoHandler(c *gin.Context) {
err := parseSessionCookie(c)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
namespace := c.Param("namespace")
name := c.Param("name")
service, err := cmd.ServiceGet(namespace, name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
serviceJSON, err := json.Marshal(&service)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
var data []interface{}
data = append(data, service)
data = append(data, string(serviceJSON))
c.HTML(http.StatusOK, "service_info.tmpl", data)
}
func serviceCreateHandler(c *gin.Context) {
err := parseSessionCookie(c)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
type Input struct {
Body string `json:"body"`
}
var input Input
c.BindJSON(&input)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(input.Body), nil, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
service := obj.(*v1.Service)
_, err = cmd.ServiceCreate(service.Namespace, service)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.Redirect(http.StatusFound, "/services")
}
func serviceDeleteHandler(c *gin.Context) {
type Input struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
}
var input Input
c.BindJSON(&input)
err := cmd.ServiceDelete(input.Namespace, input.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}
func serviceUpdateHandler(c *gin.Context) {
err := parseSessionCookie(c)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
type Input struct {
Body string `json:"body"`
}
var input Input
c.BindJSON(&input)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(input.Body), nil, nil)
if err != nil {
glog.Errorf("URL=%s; Method=%s; Err=%s", c.Request.URL.Path, c.Request.Method, err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
service := obj.(*v1.Service)
cmd.ServiceUpdate(service.Namespace, service)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}