-
Notifications
You must be signed in to change notification settings - Fork 46
/
utility.go
637 lines (568 loc) · 22.1 KB
/
utility.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
Copyright 2019 The Cloud-Barista Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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 common is to handle REST API for common funcitonalities
package common
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/cloud-barista/cb-tumblebug/src/core/common"
"github.com/cloud-barista/cb-tumblebug/src/core/mcis"
"github.com/rs/zerolog/log"
)
var validate *validator.Validate
func init() {
validate = validator.New()
}
type TbConnectionName struct {
ConnectionName string `json:"connectionName"`
}
type Existence struct {
Exists bool `json:"exists"`
}
func SendExistence(c echo.Context, httpCode int, existence bool) error {
return c.JSON(httpCode, Existence{Exists: existence})
}
type Status struct {
Message string `json:"message"`
}
func SendMessage(c echo.Context, httpCode int, msg string) error {
return c.JSON(httpCode, Status{Message: msg})
}
func Send(c echo.Context, httpCode int, json interface{}) error {
return c.JSON(httpCode, json)
}
func Validate(c echo.Context, params []string) error {
var err error
for _, name := range params {
err = validate.Var(c.Param(name), "required")
if err != nil {
return err
}
}
return nil
}
// RestGetReadyz func is for checking CB-Tumblebug server is ready.
// @ID GetReadyz
// RestGetReadyz godoc
// @Summary Check Tumblebug is ready
// @Description Check Tumblebug is ready
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Success 200 {object} common.SimpleMsg
// @Failure 503 {object} common.SimpleMsg
// @Router /readyz [get]
func RestGetReadyz(c echo.Context) error {
message := common.SimpleMsg{}
message.Message = "CB-Tumblebug is ready"
if !common.SystemReady {
message.Message = "CB-Tumblebug is NOT ready"
return c.JSON(http.StatusServiceUnavailable, &message)
}
return c.JSON(http.StatusOK, &message)
}
// RestCheckHTTPVersion godoc
// @ID CheckHTTPVersion
// @Summary Check HTTP version of incoming request
// @Description Checks and logs the HTTP version of the incoming request to the server console.
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /httpVersion [get]
func RestCheckHTTPVersion(c echo.Context) error {
// Access the *http.Request object from the echo.Context
req := c.Request()
// Determine the HTTP protocol version of the request
okMessage := common.SimpleMsg{}
okMessage.Message = req.Proto
return c.JSON(http.StatusOK, &okMessage)
}
/*
// RestGetSwagger func is to get API document web.
// RestGetSwagger godoc
// @Summary Get API document web
// @Description Get API document web
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /swaggerActive [get]
func RestGetSwagger(c echo.Context) error {
docFile := os.Getenv("API_DOC_PATH")
f, err := os.Open(docFile)
if err != nil {
return err
}
defer f.Close()
dec := json.NewDecoder(f)
data := make(map[string]interface{}, 0)
if err := dec.Decode(&data); err != nil {
return err
}
data["host"] = os.Getenv("SELF_ENDPOINT")
return c.JSON(http.StatusOK, data)
}
*/
// RestRegisterCredential func is a rest api wrapper for RegisterCredential.
// RestRegisterCredential godoc
// @ID RegisterCredential
// @Summary Post register Credential info
// @Description Post register Credential info
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Param CredentialReq body common.CredentialReq true "Credential request info"
// @Success 200 {object} common.CredentialInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /credential [post]
func RestRegisterCredential(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
u := &common.CredentialReq{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
}
content, err := common.RegisterCredential(*u)
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestGetConnConfig func is a rest api wrapper for GetConnConfig.
// RestGetConnConfig godoc
// @ID GetConnConfig
// @Summary Get registered ConnConfig info
// @Description Get registered ConnConfig info
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Param connConfigName path string true "Name of connection config (cloud config)"
// @Success 200 {object} common.ConnConfig
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /connConfig/{connConfigName} [get]
func RestGetConnConfig(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
connConfigName := c.Param("connConfigName")
content, err := common.GetConnConfig(connConfigName)
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestGetConnConfigList func is a rest api wrapper for GetConnConfigList.
// RestGetConnConfigList godoc
// @ID GetConnConfigList
// @Summary List all registered ConnConfig
// @Description List all registered ConnConfig
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Param filterCredentialHolder query string false "filter objects by Credential Holder" default()
// @Param filterVerified query boolean false "filter verified connections only" Enums(true, false) default(true)
// @Param filterRegionRepresentative query boolean false "filter connections with the representative region only" Enums(true, false) default(false)
// @Success 200 {object} common.ConnConfigList
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /connConfig [get]
func RestGetConnConfigList(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
filterCredentialHolder := c.QueryParam("filterCredentialHolder")
filterVerified := c.QueryParam("filterVerified")
filterRegionRepresentative := c.QueryParam("filterRegionRepresentative")
filterVerifiedBool, err := strconv.ParseBool(filterVerified)
if err != nil {
filterVerifiedBool = true
}
filterRegionRepresentativeBool, err := strconv.ParseBool(filterRegionRepresentative)
if err != nil {
filterRegionRepresentativeBool = false
}
content, err := common.GetConnConfigList(filterCredentialHolder, filterVerifiedBool, filterRegionRepresentativeBool)
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestGetProviderList func is a rest api wrapper for GetProviderList.
// RestGetProviderList godoc
// @ID GetProviderList
// @Summary List all registered Providers
// @Description List all registered Providers
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Success 200 {object} common.IdList
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /provider [get]
func RestGetProviderList(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
content, err := common.GetProviderList()
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestGetRegion func is a rest api wrapper for GetRegion.
// RestGetRegion godoc
// @ID GetRegion
// @Summary Get registered region info
// @Description Get registered region info
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Param providerName path string true "Name of the CSP to retrieve"
// @Param regionName path string true "Name of region to retrieve"
// @Success 200 {object} common.RegionDetail
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /provider/{providerName}/region/{regionName} [get]
func RestGetRegion(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
providerName := c.Param("providerName")
regionName := c.Param("regionName")
content, err := common.GetRegion(providerName, regionName)
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestGetRegionList func is a rest api wrapper for GetRegionList.
// RestGetRegionList godoc
// @ID GetRegionList
// @Summary List all registered regions
// @Description List all registered regions
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Success 200 {object} common.RegionList
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /region [get]
func RestGetRegionList(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
content, err := common.GetRegionList()
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestGetCloudInfo func is a rest api wrapper for CloudInfo.
// RestGetCloudInfo godoc
// @ID GetCloudInfo
// @Summary Get cloud information
// @Description Get cloud information
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Success 200 {object} common.CloudInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /cloudInfo [get]
func RestGetCloudInfo(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
content, err := common.GetCloudInfo()
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestGetK8sClusterInfo func is a rest api wrapper for K8sClsuterInfo.
// RestGetK8sClusterInfo godoc
// @ID GetK8sClusterInfo
// @Summary Get kubernetes cluster information
// @Description Get kubernetes cluster information
// @Tags [Admin] Multi-Cloud environment configuration
// @Accept json
// @Produce json
// @Success 200 {object} common.K8sClusterInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /k8sClusterInfo [get]
func RestGetK8sClusterInfo(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
content, err := common.GetK8sClusterInfo()
return common.EndRequestWithLog(c, reqID, err, content)
}
// ObjectList struct consists of object IDs
type ObjectList struct {
Object []string `json:"object"`
}
// func RestGetObjects is a rest api wrapper for GetObjectList.
// RestGetObjects godoc
// @ID GetObjects
// @Summary List all objects for a given key
// @Description List all objects for a given key
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Param key query string true "retrieve objects by key"
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /objects [get]
func RestGetObjects(c echo.Context) error {
parentKey := c.QueryParam("key")
content := common.GetObjectList(parentKey)
objectList := ObjectList{}
for _, v := range content {
objectList.Object = append(objectList.Object, v)
}
return c.JSON(http.StatusOK, &objectList)
}
// func RestGetObject is a rest api wrapper for GetObject.
// RestGetObject godoc
// @ID GetObject
// @Summary Get value of an object
// @Description Get value of an object
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Param key query string true "get object value by key"
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /object [get]
func RestGetObject(c echo.Context) error {
parentKey := c.QueryParam("key")
log.Debug().Msgf("[Get Tumblebug Object Value] with Key: %s ", parentKey)
content, err := common.GetObjectValue(parentKey)
if err != nil || content == "" {
return SendMessage(c, http.StatusOK, "Cannot find ["+parentKey+"] object")
}
var contentJSON map[string]interface{}
json.Unmarshal([]byte(content), &contentJSON)
return c.JSON(http.StatusOK, &contentJSON)
}
// func RestDeleteObject is a rest api wrapper for DeleteObject.
// RestDeleteObject godoc
// @ID DeleteObject
// @Summary Delete an object
// @Description Delete an object
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Param key query string true "delete object value by key"
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /object [delete]
func RestDeleteObject(c echo.Context) error {
parentKey := c.QueryParam("key")
log.Debug().Msgf("[Delete Tumblebug Object Value] with Key: %s", parentKey)
content, err := common.GetObjectValue(parentKey)
if err != nil || content == "" {
return SendMessage(c, http.StatusOK, "Cannot find ["+parentKey+"] object")
}
err = common.DeleteObject(parentKey)
if err != nil {
return SendMessage(c, http.StatusOK, "Cannot delete ["+parentKey+"] object")
}
return SendMessage(c, http.StatusOK, "The object has been deleted")
}
// func RestDeleteObjects is a rest api wrapper for DeleteObjects.
// RestDeleteObjects godoc
// @ID DeleteObjects
// @Summary Delete child objects along with the given object
// @Description Delete child objects along with the given object
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Param key query string true "Delete child objects based on the given key string"
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /objects [delete]
func RestDeleteObjects(c echo.Context) error {
parentKey := c.QueryParam("key")
log.Debug().Msgf("[Delete Tumblebug child Object Value] with Key: %s", parentKey)
err := common.DeleteObjects(parentKey)
if err != nil {
return SendMessage(c, http.StatusOK, "Cannot delete objects")
}
return SendMessage(c, http.StatusOK, "Objects have been deleted")
}
// Request struct for RestInspectResources
type RestInspectResourcesRequest struct {
ConnectionName string `json:"connectionName" example:"aws-ap-southeast-1"`
ResourceType string `json:"resourceType" example:"vNet" enums:"vNet,securityGroup,sshKey,vm"`
}
// RestInspectResources godoc
// @ID InspectResources
// @Summary Inspect Resources (vNet, securityGroup, sshKey, vm) registered in CB-Tumblebug, CB-Spider, CSP
// @Description Inspect Resources (vNet, securityGroup, sshKey, vm) registered in CB-Tumblebug, CB-Spider, CSP
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Param connectionName body RestInspectResourcesRequest true "Specify connectionName and resource type"
// @Success 200 {object} mcis.InspectResource
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /inspectResources [post]
func RestInspectResources(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
u := &RestInspectResourcesRequest{}
if err := c.Bind(u); err != nil {
return err
}
log.Debug().Msgf("[List Resource Status: %s]", u.ResourceType)
var content interface{}
var err error
// if u.Type == common.StrVNet || u.Type == common.StrSecurityGroup || u.Type == common.StrSSHKey {
// content, err = mcis.InspectResources(u.ConnectionName, u.Type)
// } else if u.Type == "vm" {
// content, err = mcis.InspectVMs(u.ConnectionName)
// }
content, err = mcis.InspectResources(u.ConnectionName, u.ResourceType)
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestInspectResourcesOverview godoc
// @ID InspectResourcesOverview
// @Summary Inspect Resources Overview (vNet, securityGroup, sshKey, vm) registered in CB-Tumblebug and CSP for all connections
// @Description Inspect Resources Overview (vNet, securityGroup, sshKey, vm) registered in CB-Tumblebug and CSP for all connections
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Success 200 {object} mcis.InspectResourceAllResult
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /inspectResourcesOverview [get]
func RestInspectResourcesOverview(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
content, err := mcis.InspectResourcesOverview()
return common.EndRequestWithLog(c, reqID, err, content)
}
// Request struct for RestRegisterCspNativeResources
type RestRegisterCspNativeResourcesRequest struct {
ConnectionName string `json:"connectionName" example:"aws-ap-southeast-1"`
NsId string `json:"nsId" example:"ns01"`
McisName string `json:"mcisName" example:"csp"`
}
// RestRegisterCspNativeResources godoc
// @ID RegisterCspNativeResources
// @Summary Register CSP Native Resources (vNet, securityGroup, sshKey, vm) to CB-Tumblebug
// @Description Register CSP Native Resources (vNet, securityGroup, sshKey, vm) to CB-Tumblebug
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Param Request body RestRegisterCspNativeResourcesRequest true "Specify connectionName, NS Id, and MCIS Name""
// @Param option query string false "Option to specify resourceType" Enums(onlyVm, exceptVm)
// @Param mcisFlag query string false "Flag to show VMs in a collective MCIS form (y,n)" Enums(y, n) default(y)
// @Success 200 {object} mcis.RegisterResourceResult
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /registerCspResources [post]
func RestRegisterCspNativeResources(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
u := &RestRegisterCspNativeResourcesRequest{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
}
option := c.QueryParam("option")
mcisFlag := c.QueryParam("mcisFlag")
content, err := mcis.RegisterCspNativeResources(u.NsId, u.ConnectionName, u.McisName, option, mcisFlag)
return common.EndRequestWithLog(c, reqID, err, content)
}
// Request struct for RestRegisterCspNativeResources
type RestRegisterCspNativeResourcesRequestAll struct {
NsId string `json:"nsId" example:"ns01"`
McisName string `json:"mcisName" example:"csp"`
}
// RestRegisterCspNativeResourcesAll godoc
// @ID RegisterCspNativeResourcesAll
// @Summary Register CSP Native Resources (vNet, securityGroup, sshKey, vm) from all Clouds to CB-Tumblebug
// @Description Register CSP Native Resources (vNet, securityGroup, sshKey, vm) from all Clouds to CB-Tumblebug
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Param Request body RestRegisterCspNativeResourcesRequestAll true "Specify NS Id and MCIS Name"
// @Param option query string false "Option to specify resourceType" Enums(onlyVm, exceptVm)
// @Param mcisFlag query string false "Flag to show VMs in a collective MCIS form (y,n)" Enums(y, n) default(y)
// @Success 200 {object} mcis.RegisterResourceAllResult
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /registerCspResourcesAll [post]
func RestRegisterCspNativeResourcesAll(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
u := &RestRegisterCspNativeResourcesRequest{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
}
option := c.QueryParam("option")
mcisFlag := c.QueryParam("mcisFlag")
content, err := mcis.RegisterCspNativeResourcesAll(u.NsId, u.McisName, option, mcisFlag)
return common.EndRequestWithLog(c, reqID, err, content)
}
// RestForwardAnyReqToAny godoc
// @ID ForwardAnyReqToAny
// @Summary Forward any (GET) request to CB-Spider
// @Description Forward any (GET) request to CB-Spider
// @Tags [Admin] System utility
// @Accept json
// @Produce json
// @Param path path string true "Internal call path to CB-Spider (path without /spider/ prefix) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details"" default(vmspec)
// @Param Request body interface{} false "Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details"
// @Success 200 {object} map[string]interface{}
// @Router /forward/{path} [post]
func RestForwardAnyReqToAny(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
reqPath := c.Param("*")
reqPath, err := url.PathUnescape(reqPath)
if err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
}
log.Info().Msgf("reqPath: %s", reqPath)
method := "GET"
var requestBody interface{}
if c.Request().Body != nil {
bodyBytes, err := io.ReadAll(c.Request().Body)
if err != nil {
return common.EndRequestWithLog(c, reqID, fmt.Errorf("Failed to read request body: %v", err), nil)
}
requestBody = bodyBytes
} else {
requestBody = common.NoBody
}
content, err := common.ForwardRequestToAny(reqPath, method, requestBody)
return common.EndRequestWithLog(c, reqID, err, content)
}