From 5eb96c4261bacde3708037d82b01c200d86a90ac Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Fri, 14 Oct 2022 02:33:17 +0900 Subject: [PATCH] Provide one-stop values for nlb api --- src/api/rest/docs/docs.go | 101 ++++++++++++++++++++++++-------- src/api/rest/docs/swagger.json | 30 ++++++---- src/api/rest/docs/swagger.yaml | 34 ++++++----- src/api/rest/server/mcis/nlb.go | 9 +-- src/core/mcis/nlb.go | 35 ++++++----- 5 files changed, 136 insertions(+), 73 deletions(-) diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index 8b5b6b8ce..9341af845 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -1,10 +1,17 @@ -// Package docs GENERATED BY SWAG; DO NOT EDIT +// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag package docs -import "github.com/swaggo/swag" +import ( + "bytes" + "encoding/json" + "strings" + "text/template" -const docTemplate = `{ + "github.com/swaggo/swag" +) + +var doc = `{ "schemes": {{ marshal .Schemes }}, "swagger": "2.0", "info": { @@ -2127,6 +2134,7 @@ const docTemplate = `{ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -2239,6 +2247,7 @@ const docTemplate = `{ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -2299,6 +2308,7 @@ const docTemplate = `{ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -2366,6 +2376,7 @@ const docTemplate = `{ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -8291,7 +8302,7 @@ const docTemplate = `{ "port": { "description": "1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|UDP", @@ -8799,8 +8810,7 @@ const docTemplate = `{ }, "systemLabel": { "description": "SystemLabel is for describing the mcis in a keyword (any string can be used) for special System purpose", - "type": "string", - "example": "" + "type": "string" }, "vm": { "type": "array", @@ -8910,8 +8920,7 @@ const docTemplate = `{ }, "systemLabel": { "description": "SystemLabel is for describing the mcis in a keyword (any string can be used) for special System purpose", - "type": "string", - "example": "" + "type": "string" }, "vm": { "type": "array", @@ -9057,12 +9066,12 @@ const docTemplate = `{ "dnsName": { "description": "Optional, Auto Generated and attached", "type": "string", - "example": "" + "example": "ns01-group-cd3.elb.ap-northeast-2.amazonaws.com" }, "ip": { "description": "Auto Generated and attached", "type": "string", - "example": "" + "example": "x.x.x.x" }, "keyValueList": { "type": "array", @@ -9073,7 +9082,7 @@ const docTemplate = `{ "port": { "description": "1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|UDP", @@ -9093,15 +9102,18 @@ const docTemplate = `{ ], "properties": { "cspNLBId": { + "description": "Existing NLB (used only for option=register)", "type": "string" }, "description": { "type": "string" }, "healthChecker": { + "description": "HealthChecker", "$ref": "#/definitions/mcis.TbNLBHealthCheckerReq" }, "listener": { + "description": "Frontend", "$ref": "#/definitions/mcis.NLBListenerReq" }, "scope": { @@ -9114,6 +9126,7 @@ const docTemplate = `{ "example": "REGION" }, "targetGroup": { + "description": "Backend", "$ref": "#/definitions/mcis.TbNLBTargetGroupReq" }, "type": { @@ -9139,7 +9152,7 @@ const docTemplate = `{ "port": { "description": "Listener Port or 1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|HTTP|HTTPS", @@ -9148,7 +9161,7 @@ const docTemplate = `{ }, "vmGroupId": { "type": "string", - "example": "group-1" + "example": "group-0" }, "vms": { "type": "array", @@ -9164,7 +9177,7 @@ const docTemplate = `{ "port": { "description": "Listener Port or 1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|HTTP|HTTPS", @@ -9173,7 +9186,7 @@ const docTemplate = `{ }, "vmGroupId": { "type": "string", - "example": "group-1" + "example": "group-0" } } }, @@ -9685,18 +9698,56 @@ const docTemplate = `{ } }` +type swaggerInfo struct { + Version string + Host string + BasePath string + Schemes []string + Title string + Description string +} + // SwaggerInfo holds exported Swagger Info so clients can modify it -var SwaggerInfo = &swag.Spec{ - Version: "latest", - Host: "", - BasePath: "/tumblebug", - Schemes: []string{}, - Title: "CB-Tumblebug REST API", - Description: "CB-Tumblebug REST API", - InfoInstanceName: "swagger", - SwaggerTemplate: docTemplate, +var SwaggerInfo = swaggerInfo{ + Version: "latest", + Host: "", + BasePath: "/tumblebug", + Schemes: []string{}, + Title: "CB-Tumblebug REST API", + Description: "CB-Tumblebug REST API", +} + +type s struct{} + +func (s *s) ReadDoc() string { + sInfo := SwaggerInfo + sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1) + + t, err := template.New("swagger_info").Funcs(template.FuncMap{ + "marshal": func(v interface{}) string { + a, _ := json.Marshal(v) + return string(a) + }, + "escape": func(v interface{}) string { + // escape tabs + str := strings.Replace(v.(string), "\t", "\\t", -1) + // replace " with \", and if that results in \\", replace that with \\\" + str = strings.Replace(str, "\"", "\\\"", -1) + return strings.Replace(str, "\\\\\"", "\\\\\\\"", -1) + }, + }).Parse(doc) + if err != nil { + return doc + } + + var tpl bytes.Buffer + if err := t.Execute(&tpl, sInfo); err != nil { + return doc + } + + return tpl.String() } func init() { - swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) + swag.Register(swag.Name, &s{}) } diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index 60807ef0c..db8edd6b5 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -2119,6 +2119,7 @@ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -2231,6 +2232,7 @@ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -2291,6 +2293,7 @@ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -2358,6 +2361,7 @@ }, { "type": "string", + "default": "group-0", "description": "NLB ID", "name": "nlbId", "in": "path", @@ -8283,7 +8287,7 @@ "port": { "description": "1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|UDP", @@ -8791,8 +8795,7 @@ }, "systemLabel": { "description": "SystemLabel is for describing the mcis in a keyword (any string can be used) for special System purpose", - "type": "string", - "example": "" + "type": "string" }, "vm": { "type": "array", @@ -8902,8 +8905,7 @@ }, "systemLabel": { "description": "SystemLabel is for describing the mcis in a keyword (any string can be used) for special System purpose", - "type": "string", - "example": "" + "type": "string" }, "vm": { "type": "array", @@ -9049,12 +9051,12 @@ "dnsName": { "description": "Optional, Auto Generated and attached", "type": "string", - "example": "" + "example": "ns01-group-cd3.elb.ap-northeast-2.amazonaws.com" }, "ip": { "description": "Auto Generated and attached", "type": "string", - "example": "" + "example": "x.x.x.x" }, "keyValueList": { "type": "array", @@ -9065,7 +9067,7 @@ "port": { "description": "1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|UDP", @@ -9085,15 +9087,18 @@ ], "properties": { "cspNLBId": { + "description": "Existing NLB (used only for option=register)", "type": "string" }, "description": { "type": "string" }, "healthChecker": { + "description": "HealthChecker", "$ref": "#/definitions/mcis.TbNLBHealthCheckerReq" }, "listener": { + "description": "Frontend", "$ref": "#/definitions/mcis.NLBListenerReq" }, "scope": { @@ -9106,6 +9111,7 @@ "example": "REGION" }, "targetGroup": { + "description": "Backend", "$ref": "#/definitions/mcis.TbNLBTargetGroupReq" }, "type": { @@ -9131,7 +9137,7 @@ "port": { "description": "Listener Port or 1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|HTTP|HTTPS", @@ -9140,7 +9146,7 @@ }, "vmGroupId": { "type": "string", - "example": "group-1" + "example": "group-0" }, "vms": { "type": "array", @@ -9156,7 +9162,7 @@ "port": { "description": "Listener Port or 1-65535", "type": "string", - "example": "22" + "example": "80" }, "protocol": { "description": "TCP|HTTP|HTTPS", @@ -9165,7 +9171,7 @@ }, "vmGroupId": { "type": "string", - "example": "group-1" + "example": "group-0" } } }, diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index cf8b3a844..dcb33f849 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -1237,7 +1237,7 @@ definitions: properties: port: description: 1-65535 - example: "22" + example: "80" type: string protocol: description: TCP|UDP @@ -1589,7 +1589,6 @@ definitions: systemLabel: description: SystemLabel is for describing the mcis in a keyword (any string can be used) for special System purpose - example: "" type: string vm: items: @@ -1677,7 +1676,6 @@ definitions: systemLabel: description: SystemLabel is for describing the mcis in a keyword (any string can be used) for special System purpose - example: "" type: string vm: items: @@ -1786,11 +1784,11 @@ definitions: properties: dnsName: description: Optional, Auto Generated and attached - example: "" + example: ns01-group-cd3.elb.ap-northeast-2.amazonaws.com type: string ip: description: Auto Generated and attached - example: "" + example: x.x.x.x type: string keyValueList: items: @@ -1798,7 +1796,7 @@ definitions: type: array port: description: 1-65535 - example: "22" + example: "80" type: string protocol: description: TCP|UDP @@ -1808,13 +1806,16 @@ definitions: mcis.TbNLBReq: properties: cspNLBId: + description: Existing NLB (used only for option=register) type: string description: type: string healthChecker: $ref: '#/definitions/mcis.TbNLBHealthCheckerReq' + description: HealthChecker listener: $ref: '#/definitions/mcis.NLBListenerReq' + description: Frontend scope: description: REGION(V) | GLOBAL enum: @@ -1824,6 +1825,7 @@ definitions: type: string targetGroup: $ref: '#/definitions/mcis.TbNLBTargetGroupReq' + description: Backend type: description: PUBLIC(V) | INTERNAL enum: @@ -1846,14 +1848,14 @@ definitions: type: array port: description: Listener Port or 1-65535 - example: "22" + example: "80" type: string protocol: description: TCP|HTTP|HTTPS example: TCP type: string vmGroupId: - example: group-1 + example: group-0 type: string vms: items: @@ -1864,14 +1866,14 @@ definitions: properties: port: description: Listener Port or 1-65535 - example: "22" + example: "80" type: string protocol: description: TCP|HTTP|HTTPS example: TCP type: string vmGroupId: - example: group-1 + example: group-0 type: string type: object mcis.TbScaleOutVmGroupReq: @@ -3725,7 +3727,8 @@ paths: name: mcisId required: true type: string - - description: NLB ID + - default: group-0 + description: NLB ID in: path name: nlbId required: true @@ -3766,7 +3769,8 @@ paths: name: mcisId required: true type: string - - description: NLB ID + - default: group-0 + description: NLB ID in: path name: nlbId required: true @@ -3807,7 +3811,8 @@ paths: name: mcisId required: true type: string - - description: NLB ID + - default: group-0 + description: NLB ID in: path name: nlbId required: true @@ -3849,7 +3854,8 @@ paths: name: mcisId required: true type: string - - description: NLB ID + - default: group-0 + description: NLB ID in: path name: nlbId required: true diff --git a/src/api/rest/server/mcis/nlb.go b/src/api/rest/server/mcis/nlb.go index fb266c799..95a2c7070 100644 --- a/src/api/rest/server/mcis/nlb.go +++ b/src/api/rest/server/mcis/nlb.go @@ -72,6 +72,7 @@ func RestPostNLB(c echo.Context) error { // @Produce json // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) +// @Param nlbId path string true "NLB ID" default(group-0) // @Param nlbInfo body mcis.TbNLBInfo true "Details of the NLB object" // @Success 200 {object} mcis.TbNLBInfo // @Failure 404 {object} common.SimpleMsg @@ -93,7 +94,7 @@ func RestPutNLB(c echo.Context) error { // @Produce json // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) -// @Param nlbId path string true "NLB ID" +// @Param nlbId path string true "NLB ID" default(group-0) // @Success 200 {object} mcis.TbNLBInfo // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg @@ -239,7 +240,7 @@ func RestDelAllNLB(c echo.Context) error { // @Produce json // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) -// @Param nlbId path string true "NLB ID" +// @Param nlbId path string true "NLB ID" default(group-0) // @Success 200 {object} mcis.TbNLBInfo // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg @@ -269,7 +270,7 @@ func RestGetNLBHealth(c echo.Context) error { // @Produce json // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) -// @Param nlbId path string true "NLB ID" +// @Param nlbId path string true "NLB ID" default(group-0) // @Param nlbAddRemoveVMReq body mcis.TbNLBAddRemoveVMReq true "VMs to add to NLB" // @Success 200 {object} mcis.TbNLBInfo // @Failure 404 {object} common.SimpleMsg @@ -306,7 +307,7 @@ func RestAddNLBVMs(c echo.Context) error { // @Produce json // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) -// @Param nlbId path string true "NLB ID" +// @Param nlbId path string true "NLB ID" default(group-0) // @Param nlbAddRemoveVMReq body mcis.TbNLBAddRemoveVMReq true "VMs to add to NLB" // @Success 200 {object} common.SimpleMsg // @Failure 404 {object} common.SimpleMsg diff --git a/src/core/mcis/nlb.go b/src/core/mcis/nlb.go index beaf01732..9fe783593 100644 --- a/src/core/mcis/nlb.go +++ b/src/core/mcis/nlb.go @@ -108,14 +108,14 @@ type SpiderNLBInfo struct { // NLBListenerReq is a struct to handle NLB Listener information of the CB-Spider's & CB-Tumblebug's REST API request type NLBListenerReq struct { // for both Spider and Tumblebug Protocol string `json:"protocol" example:"TCP"` // TCP|UDP - Port string `json:"port" example:"22"` // 1-65535 + Port string `json:"port" example:"80"` // 1-65535 } // SpiderNLBListenerInfo is a struct to handle NLB Listener information from the CB-Spider's REST API response type SpiderNLBListenerInfo struct { Protocol string `json:"protocol" example:"TCP"` // TCP|UDP IP string `json:"ip" example:""` // Auto Generated and attached - Port string `json:"port" example:"22"` // 1-65535 + Port string `json:"port" example:"80"` // 1-65535 DNSName string `json:"dnsName" example:""` // Optional, Auto Generated and attached CspID string `json:"cspID"` // Optional, May be Used by Driver. @@ -124,14 +124,15 @@ type SpiderNLBListenerInfo struct { // TbNLBListenerInfo is a struct to handle NLB Listener information from the CB-Tumblebug's REST API response type TbNLBListenerInfo struct { - Protocol string `json:"protocol" example:"TCP"` // TCP|UDP - IP string `json:"ip" example:""` // Auto Generated and attached - Port string `json:"port" example:"22"` // 1-65535 - DNSName string `json:"dnsName" example:""` // Optional, Auto Generated and attached + Protocol string `json:"protocol" example:"TCP"` // TCP|UDP + IP string `json:"ip" example:"x.x.x.x"` // Auto Generated and attached + Port string `json:"port" example:"80"` // 1-65535 + DNSName string `json:"dnsName" example:"ns01-group-cd3.elb.ap-northeast-2.amazonaws.com"` // Optional, Auto Generated and attached KeyValueList []common.KeyValue `json:"keyValueList"` } +// SpiderNLBVMGroupInfo is a struct from NLBVMGroupInfo from Spider type SpiderNLBVMGroupInfo struct { Protocol string // TCP|UDP|HTTP|HTTPS Port string // 1-65535 @@ -174,15 +175,15 @@ type SpiderNLBHealthInfo struct { type TbNLBTargetGroupReq struct { Protocol string `json:"protocol" example:"TCP"` // TCP|HTTP|HTTPS - Port string `json:"port" example:"22"` // Listener Port or 1-65535 - VmGroupId string `json:"vmGroupId" example:"group-1"` + Port string `json:"port" example:"80"` // Listener Port or 1-65535 + VmGroupId string `json:"vmGroupId" example:"group-0"` } type TbNLBTargetGroupInfo struct { Protocol string `json:"protocol" example:"TCP"` // TCP|HTTP|HTTPS - Port string `json:"port" example:"22"` // Listener Port or 1-65535 + Port string `json:"port" example:"80"` // Listener Port or 1-65535 - VmGroupId string `json:"vmGroupId" example:"group-1"` + VmGroupId string `json:"vmGroupId" example:"group-0"` VMs []string `json:"vms"` KeyValueList []common.KeyValue @@ -195,18 +196,17 @@ type TbNLBReq struct { // Tumblebug // VNetId string `json:"vNetId" validate:"required" example:"ns01-systemdefault-aws-ap-northeast-2"` Description string `json:"description"` - CspNLBId string `json:"cspNLBId"` + // Existing NLB (used only for option=register) + CspNLBId string `json:"cspNLBId"` Type string `json:"type" validate:"required" enums:"PUBLIC,INTERNAL" example:"PUBLIC"` // PUBLIC(V) | INTERNAL Scope string `json:"scope" validate:"required" enums:"REGION,GLOBAL" example:"REGION"` // REGION(V) | GLOBAL - //------ Frontend - + // Frontend Listener NLBListenerReq `json:"listener" validate:"required"` - - //------ Backend - - TargetGroup TbNLBTargetGroupReq `json:"targetGroup" validate:"required"` + // Backend + TargetGroup TbNLBTargetGroupReq `json:"targetGroup" validate:"required"` + // HealthChecker HealthChecker TbNLBHealthCheckerReq `json:"healthChecker" validate:"required"` } @@ -384,7 +384,6 @@ func CreateNLB(nsId string, mcisId string, u *TbNLBReq, option string) (TbNLBInf valuesFromYaml.Timeout, _ = strconv.Atoi(cloudSetting.Nlb.Timeout) valuesFromYaml.Threshold, _ = strconv.Atoi(cloudSetting.Nlb.Threshold) - if u.HealthChecker.Interval == "default" || u.HealthChecker.Interval == "" { tempReq.ReqInfo.HealthChecker.Interval = strconv.Itoa(valuesFromYaml.Interval) }