/
NameSpaceController.go
384 lines (326 loc) · 11.4 KB
/
NameSpaceController.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package controller
import (
// "encoding/json"
"fmt"
// "github.com/cloud-barista/cb-webtool/src/model/tumblebug"
tbcommon "github.com/cloud-barista/cb-webtool/src/model/tumblebug/common"
// tbmcir "github.com/cloud-barista/cb-webtool/src/model/tumblebug/mcir"
// tbmcis "github.com/cloud-barista/cb-webtool/src/model/tumblebug/mcis"
service "github.com/cloud-barista/cb-webtool/src/service"
// util "github.com/cloud-barista/cb-webtool/src/util"
"log"
"net/http"
echotemplate "github.com/foolin/echo-template"
echosession "github.com/go-session/echo-session"
"github.com/labstack/echo"
)
// deprecated
// func NsRegController(c echo.Context) error {
// username := c.FormValue("username")
// description := c.FormValue("description")
// fmt.Println("NSRegController : ", username, description)
// return nil
// }
// func NsRegForm(c echo.Context) error {
func NameSpaceRegForm(c echo.Context) error {
loginInfo := service.CallLoginInfo(c)
if loginInfo.UserID == "" {
// Login 정보가 없으므로 login화면으로
return c.Redirect(http.StatusTemporaryRedirect, "/login")
}
// comURL := service.GetCommonURL()
// apiInfo := service.AuthenticationHandler()
// if loginInfo := service.CallLoginInfo(c); loginInfo.Username != "" {
// return c.Render(http.StatusOK, "NSRegister.html", map[string]interface{}{
return echotemplate.Render(c, http.StatusOK, "setting/namespaces/NSRegister.html", map[string]interface{}{
"LoginInfo": loginInfo,
// "comURL": comURL,
// "apiInfo": apiInfo,
})
// }
// return c.Redirect(http.StatusTemporaryRedirect, "/login")
}
// namespace 등록 처리
func NameSpaceRegProc(c echo.Context) error {
loginInfo := service.CallLoginInfo(c)
if loginInfo.UserID == "" {
// Login 정보가 없으므로 login화면으로
return c.Redirect(http.StatusTemporaryRedirect, "/login")
}
// namespace := c.FormValue("name")
// description := c.FormValue("description")
// fmt.Println("namespace : " + namespace + " , description :" + description)
// nameSpaceInfo := new(model.NameSpaceInfo)
// nameSpaceInfo.Name = namespace
// nameSpaceInfo.Description = description
nameSpaceInfo := new(tbcommon.TbNsInfo)
if err := c.Bind(nameSpaceInfo); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// if err = c.Validate(nameSpaceInfo); err != nil {
// return echo.NewHTTPError(http.StatusBadRequest, err.Error())
// }
fmt.Println("nameSpaceInfo : ", nameSpaceInfo)
// Tubblebug 호출하여 namespace 생성
// person := Person{"Alex", 10}
// pbytes, _ := json.Marshal(person)
respBody, respStatus := service.RegNameSpace(nameSpaceInfo)
fmt.Println("=============respBody =============", respBody)
if respStatus.StatusCode != 200 && respStatus.StatusCode != 201 {
return c.JSON(respStatus.StatusCode, map[string]interface{}{
"error": respStatus.Message,
"status": respStatus.StatusCode,
})
}
// 저장 성공하면 namespace 목록 조회
nameSpaceList, nsStatus := service.GetNameSpaceList()
if nsStatus.StatusCode == 500 {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": respStatus.Message,
"status": respStatus.StatusCode,
"NameSpaceList": nil,
})
}
storeNameSpaceErr := service.SetStoreNameSpaceList(c, nameSpaceList)
if storeNameSpaceErr != nil {
log.Println("Store NameSpace Err")
}
// return namespace 목록
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "success",
"status": "200",
"NameSpaceList": nameSpaceList,
})
}
// Namespace 수정
func NameSpaceUpdateProc(c echo.Context) error {
log.Println("NameSpaceUpdateProc : ")
loginInfo := service.CallLoginInfo(c)
if loginInfo.UserID == "" {
// Login 정보가 없으므로 login화면으로
return c.Redirect(http.StatusTemporaryRedirect, "/login")
}
defaultNameSpaceID := loginInfo.DefaultNameSpaceID
nameSpaceInfo := new(tbcommon.TbNsReq)
if err := c.Bind(nameSpaceInfo); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
respBody, respStatus := service.UpdateNameSpace(defaultNameSpaceID, nameSpaceInfo)
fmt.Println("=============respBody =============", respBody)
if respStatus.StatusCode != 200 && respStatus.StatusCode != 201 {
return c.JSON(respStatus.StatusCode, map[string]interface{}{
"error": respStatus.Message,
"status": respStatus.StatusCode,
})
}
// 저장 성공하면 namespace 목록 조회
nameSpaceList, nsStatus := service.GetNameSpaceList()
if nsStatus.StatusCode == 500 {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": nsStatus.Message,
"status": nsStatus.StatusCode,
"NameSpaceList": nil,
})
}
storeNameSpaceErr := service.SetStoreNameSpaceList(c, nameSpaceList)
if storeNameSpaceErr != nil {
log.Println("Store NameSpace Err")
}
// return namespace 목록
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "success",
"status": respStatus.StatusCode,
"NameSpaceList": nameSpaceList,
})
// return c.JSON(http.StatusOK, map[string]interface{}{
// "message": "success",
// "status": "200",
// })
}
// NameSpace 삭제
func NameSpaceDelProc(c echo.Context) error {
log.Println("NameSpaceDelProc : ")
loginInfo := service.CallLoginInfo(c)
if loginInfo.UserID == "" {
// Login 정보가 없으므로 login화면으로
return c.Redirect(http.StatusTemporaryRedirect, "/login")
}
paramNameSpaceID := c.Param("nameSpaceID")
defaultNameSpaceID := loginInfo.DefaultNameSpaceID
log.Println(paramNameSpaceID)
if paramNameSpaceID == defaultNameSpaceID {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"error": "Cannot delete current namespace",
"status": 4001,
})
}
respBody, respStatus := service.DelNameSpace(paramNameSpaceID)
fmt.Println("=============respBody =============", respBody)
if respStatus.StatusCode != 200 && respStatus.StatusCode != 201 {
return c.JSON(respStatus.StatusCode, map[string]interface{}{
"error": respStatus.Message,
"status": respStatus.StatusCode,
})
}
// 저장 성공하면 namespace 목록 조회
nameSpaceList, nsStatus := service.GetNameSpaceList()
if nsStatus.StatusCode == 500 {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": nsStatus.Message,
"status": nsStatus.StatusCode,
"NameSpaceList": nil,
})
}
storeNameSpaceErr := service.SetStoreNameSpaceList(c, nameSpaceList)
if storeNameSpaceErr != nil {
log.Println("Store NameSpace Err")
}
// return namespace 목록
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "success",
"status": respStatus,
"NameSpaceList": nameSpaceList,
})
// return c.JSON(http.StatusOK, map[string]interface{}{
// "message": "success",
// "status": "200",
// })
}
// NsListForm -> NameSpaceMngForm으로 변경
// func NsListForm(c echo.Context) error {
func NameSpaceMngForm(c echo.Context) error {
fmt.Println("=============start NameSpaceMngForm =============")
loginInfo := service.CallLoginInfo(c)
if loginInfo.UserID == "" {
// Login 정보가 없으므로 login화면으로
return c.Redirect(http.StatusTemporaryRedirect, "/login")
}
fmt.Println("=============start GetNSList =============")
nameSpaceList, _ := service.GetNameSpaceList()
fmt.Println("=============start GetNSList =============", nameSpaceList)
storeNameSpaceErr := service.SetStoreNameSpaceList(c, nameSpaceList)
if storeNameSpaceErr != nil {
log.Println("Store NameSpace Err")
}
// status, filepath(vies기준), return params
return echotemplate.Render(c, http.StatusOK,
"setting/namespaces/NameSpaceMng", // 파일명
map[string]interface{}{
"LoginInfo": loginInfo,
"NameSpaceList": nameSpaceList,
})
}
// 사용자의 namespace 목록 조회
func GetNameSpaceList(c echo.Context) error {
fmt.Println("====== GET NAMESPACE LIST ========")
// store := echosession.FromContext(c)
// nameSpaceInfoList, nsStatus := service.GetNameSpaceList()
optionParam := c.QueryParam("option")
if optionParam == "id" {
nameSpaceInfoList, nsStatus := service.GetNameSpaceListByOptionID(optionParam)
if nsStatus.StatusCode == 500 {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": nsStatus.Message,
"status": nsStatus.StatusCode,
})
}
return c.JSON(http.StatusOK, nameSpaceInfoList)
} else {
nameSpaceInfoList, nsStatus := service.GetNameSpaceListByOption(optionParam)
if nsStatus.StatusCode == 500 {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": nsStatus.Message,
"status": nsStatus.StatusCode,
})
}
return c.JSON(http.StatusOK, nameSpaceInfoList)
}
//nameSpaceInfoList, nsStatus := service.GetStoredNameSpaceList(c)
}
// 기본 namespace set. set default Namespace
func SetNameSpace(c echo.Context) error {
fmt.Println("====== SET SELECTED NAME SPACE ========")
loginInfo := service.CallLoginInfo(c)
if loginInfo.UserID == "" {
return c.JSON(http.StatusNotAcceptable, map[string]string{
"message": "Not Login",
"status": "403",
})
}
store := echosession.FromContext(c)
result, ok := store.Get(loginInfo.UserID)
if !ok {
return c.JSON(http.StatusNotAcceptable, map[string]string{
"message": "Not Login",
"status": "403",
})
}
storedUser := result.(map[string]string)
nameSpaceID := c.Param("nameSpaceID")
loginInfo.DefaultNameSpaceID = nameSpaceID
// nsResult, nsOk := store.Get("namespacelist")
// fmt.Println("nsResult : ", nsResult)
// if !nsOk {
// fmt.Println("nsOk : ", nsOk)
// return c.JSON(http.StatusOK, map[string]interface{}{
// "message": "저장된 namespace가 없습니다.",
// "status": "403",
// })
nsList, nsStatus := service.GetStoredNameSpaceList(c)
if nsStatus.StatusCode != 200 && nsStatus.StatusCode != 201 {
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "NameSpace 조회 오류.",
"status": "403",
})
} else {
fmt.Println("______________")
// nsList := nsResult.([]tbcommon.TbNsInfo)
// fmt.Println("nsList ", nsList)
for _, nsInfo := range nsList {
fmt.Println(nsInfo.ID + " : " + nameSpaceID)
if nsInfo.ID == nameSpaceID {
loginInfo.DefaultNameSpaceID = nsInfo.ID
loginInfo.DefaultNameSpaceName = nsInfo.Name
fmt.Println(nsInfo.ID + " : " + nameSpaceID + " found " + nsInfo.Name)
storedUser["defaultnameSpacename"] = nsInfo.ID
storedUser["defaultnamespaceid"] = nsInfo.Name
break
}
}
}
// storedUser["defaultnamespaceid"] = nameSpaceID
fmt.Println("storedUser : ", storedUser)
store.Set(loginInfo.UserID, storedUser)
storeErr := store.Save()
if storeErr != nil {
return c.JSON(http.StatusNotAcceptable, map[string]string{
"message": storeErr.Error(),
"status": "403",
})
}
mcisList, _ := service.GetMcisListByID(loginInfo.DefaultNameSpaceID, "", "")
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "success",
"status": "200",
"LoginInfo": loginInfo,
"McisList": mcisList,
})
}
// 기본 namespace get. get default Namespace
func GetNameSpace(c echo.Context) error {
fmt.Println("====== GET SELECTED NAME SPACE ========")
// store := echosession.FromContext(c)
// getInfo, ok := store.Get("namespace")
// if !ok {
// return c.JSON(http.StatusNotAcceptable, map[string]string{
// "message": "Not Exist",
// })
// }
// nsId := getInfo.(string)
loginInfo := service.CallLoginInfo(c)
res := map[string]string{
"message": "success",
"nsID": loginInfo.DefaultNameSpaceID,
}
return c.JSON(http.StatusOK, res)
}