Skip to content

Commit

Permalink
Add Swagger pilot codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoon-seo committed Jun 17, 2021
1 parent c0a902f commit 8137308
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ cli-dist: cli
cli:
@echo -e '\t[CB-Spider] build ./interface/spctl...'
@go build -o ./interface/spctl ./interface/cli/spider/spider.go
swag swagger:
@echo -e '\t[CB-Spider] build Swagger docs'
@~/go/bin/swag i -g api-runtime/rest-runtime/CBSpiderRuntime.go -o api-runtime/rest-runtime/docs

15 changes: 14 additions & 1 deletion api-runtime/rest-runtime/CBSpiderRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
// REST API (echo)
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"

// echo-swagger middleware
_ "github.com/cloud-barista/cb-spider/api-runtime/rest-runtime/docs"
echoSwagger "github.com/swaggo/echo-swagger"
)

var cblog *logrus.Logger
Expand All @@ -40,7 +44,7 @@ func init() {
cr.ServicePort = ":1024"
}

// REST API Return struct for boolena type
// REST API Return struct for boolean type
type BooleanInfo struct {
Result string // true or false
}
Expand Down Expand Up @@ -87,6 +91,9 @@ func RunServer() {
{"GET", "", aw.SpiderInfo},
{"GET", "/", aw.SpiderInfo},

//----------Swagger
{"GET", "/swagger/*", echoSwagger.WrapHandler},

//----------EndpointInfo
{"GET", "/endpointinfo", endpointInfo},

Expand Down Expand Up @@ -291,6 +298,8 @@ func endpointInfo(c echo.Context) error {
endpointInfo += fmt.Sprintf(" - AdminWeb: %s\n", adminWebURL)
restEndPoint := "http://" + cr.HostIPorName + cr.ServicePort + "/spider"
endpointInfo += fmt.Sprintf(" - REST API: %s\n", restEndPoint)
swaggerURL := "http://" + cr.HostIPorName + cr.ServicePort + "/spider/swagger/index.html"
endpointInfo += fmt.Sprintf(" - Swagger : %s\n", swaggerURL)
gRPCServer := "grpc://" + cr.HostIPorName + cr.GoServicePort
endpointInfo += fmt.Sprintf(" - Go API: %s\n", gRPCServer)

Expand All @@ -307,4 +316,8 @@ func spiderBanner() {
// REST API EndPoint
restEndPoint := "http://" + cr.HostIPorName + cr.ServicePort + "/spider"
fmt.Printf(" - REST API: %s\n", restEndPoint)

// Swagger
swaggerURL := "http://" + cr.HostIPorName + cr.ServicePort + "/spider/swagger/index.html"
fmt.Printf(" - Swagger : %s\n", swaggerURL)
}
31 changes: 24 additions & 7 deletions api-runtime/rest-runtime/CCMRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,16 +649,33 @@ func deleteCSPSecurity(c echo.Context) error {
return c.JSON(http.StatusOK, &resultInfo)
}

//================ KeyPair Handler
type keyPairCreateReq struct {
ConnectionName string
ReqInfo struct {
Name string
}
}

// JSON Simple message struct
type SimpleMsg struct {
Message string `json:"message" example:"Any message"`
}

// createKey godoc
// @Summary Create SSH Key
// @Description Create SSH Key
// @Tags [CCM] Access key management
// @Accept json
// @Produce json
// @Param keyPairCreateReq body keyPairCreateReq true "Request body to create key"
// @Success 200 {object} resources.KeyPairInfo
// @Failure 404 {object} SimpleMsg
// @Failure 500 {object} SimpleMsg
// @Router /keypair [post]
func createKey(c echo.Context) error {
cblog.Info("call createKey()")

var req struct {
ConnectionName string
ReqInfo struct {
Name string
}
}
req := keyPairCreateReq{}

if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
Expand Down
199 changes: 199 additions & 0 deletions api-runtime/rest-runtime/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag

package docs

import (
"bytes"
"encoding/json"
"strings"

"github.com/alecthomas/template"
"github.com/swaggo/swag"
)

var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/keypair": {
"post": {
"description": "Create SSH Key",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[CCM] Access key management"
],
"summary": "Create SSH Key",
"parameters": [
{
"description": "Request body to create key",
"name": "keyPairCreateReq",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/restruntime.keyPairCreateReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/resources.KeyPairInfo"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/restruntime.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/restruntime.SimpleMsg"
}
}
}
}
}
},
"definitions": {
"resources.IID": {
"type": "object",
"properties": {
"nameId": {
"description": "NameID by user",
"type": "string"
},
"systemId": {
"description": "SystemID by CloudOS",
"type": "string"
}
}
},
"resources.KeyPairInfo": {
"type": "object",
"properties": {
"fingerprint": {
"type": "string"
},
"iid": {
"description": "{NameId, SystemId}",
"$ref": "#/definitions/resources.IID"
},
"keyValueList": {
"type": "array",
"items": {
"$ref": "#/definitions/resources.KeyValue"
}
},
"privateKey": {
"type": "string"
},
"publicKey": {
"type": "string"
},
"vmuserID": {
"type": "string"
}
}
},
"resources.KeyValue": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
}
},
"restruntime.SimpleMsg": {
"type": "object",
"properties": {
"message": {
"type": "string",
"example": "Any message"
}
}
},
"restruntime.keyPairCreateReq": {
"type": "object",
"properties": {
"connectionName": {
"type": "string"
},
"reqInfo": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
}`

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 = swaggerInfo{
Version: "",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "",
Description: "",
}

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)
},
}).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(swag.Name, &s{})
}
Loading

0 comments on commit 8137308

Please sign in to comment.