Skip to content

Commit

Permalink
feat: 丰富运维管理接口相关接口swagger信息 (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
onecer authored Oct 25, 2022
1 parent b052ca8 commit 1b75849
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 11 deletions.
16 changes: 8 additions & 8 deletions apiserver/httpserver/maintain_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func (h *HTTPServer) GetMaintainAccessServer() *restful.WebService {
ws := new(restful.WebService)
ws.Path("/maintain/v1").Consumes(restful.MIME_JSON).Produces(restful.MIME_JSON)

ws.Route(ws.GET("/apiserver/conn").To(h.GetServerConnections))
ws.Route(ws.GET("/apiserver/conn/stats").To(h.GetServerConnStats))
ws.Route(ws.POST("apiserver/conn/close").To(h.CloseConnections))
ws.Route(ws.POST("/memory/free").To(h.FreeOSMemory))
ws.Route(ws.POST("/instance/clean").Consumes(restful.MIME_JSON).To(h.CleanInstance))
ws.Route(ws.GET("/instance/heartbeat").To(h.GetLastHeartbeat))
ws.Route(ws.GET("/log/outputlevel").To(h.GetLogOutputLevel))
ws.Route(ws.PUT("/log/outputlevel").To(h.SetLogOutputLevel))
ws.Route(enrichGetServerConnectionsApiDocs(ws.GET("/apiserver/conn").To(h.GetServerConnections)))
ws.Route(enrichGetServerConnStatsApiDocs(ws.GET("/apiserver/conn/stats").To(h.GetServerConnStats)))
ws.Route(enrichCloseConnectionsApiDocs(ws.POST("apiserver/conn/close").To(h.CloseConnections)))
ws.Route(enrichFreeOSMemoryApiDocs(ws.POST("/memory/free").To(h.FreeOSMemory)))
ws.Route(enrichCleanInstanceApiDocs(ws.POST("/instance/clean").To(h.CleanInstance)))
ws.Route(enrichGetLastHeartbeatApiDocs(ws.GET("/instance/heartbeat").To(h.GetLastHeartbeat)))
ws.Route(enrichGetLogOutputLevelApiDocs(ws.GET("/log/outputlevel").To(h.GetLogOutputLevel)))
ws.Route(enrichSetLogOutputLevelApiDocs(ws.PUT("/log/outputlevel").To(h.SetLogOutputLevel)))
return ws
}

Expand Down
99 changes: 99 additions & 0 deletions apiserver/httpserver/maintain_apidoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 httpserver

import (
"github.com/emicklei/go-restful/v3"
restfulspec "github.com/polarismesh/go-restful-openapi/v2"

api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/maintain"
)

var (
maintainApiTags = []string{"Maintain"}
)

func enrichGetServerConnectionsApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("获取服务端连接数").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Param(restful.QueryParameter("protocol", "查看指定协议").DataType("string").Required(true)).
Param(restful.QueryParameter("host", "查看指定host").DataType("string").Required(false)).
Notes(enrichGetServerConnectionsApiNotes)
}

func enrichGetServerConnStatsApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("获取服务端连接统计信息").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Param(restful.QueryParameter("protocol", "查看指定协议").DataType("string").Required(true)).
Param(restful.QueryParameter("host", "查看指定host").DataType("string").Required(false)).
Param(restful.QueryParameter("amount", "总数").DataType("integer").Required(false)).
Notes(enrichGetServerConnStatsApiNotes)
}

func enrichCloseConnectionsApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("关闭指定client ip的连接").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Reads([]maintain.ConnReq{}).
Notes(enrichCloseConnectionsApiNotes)
}

func enrichFreeOSMemoryApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("释放系统内存").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Notes(enrichFreeOSMemoryApiNotes)
}

func enrichCleanInstanceApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("彻底清理flag=1的实例").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Reads(api.Instance{}).
Notes(enrichCleanInstanceApiNotes)
}

func enrichGetLastHeartbeatApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("获取上一次心跳的时间").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Param(restful.QueryParameter("id", "实例ID 如果存在则其它参数可不填").DataType("string").Required(false)).
Param(restful.QueryParameter("service", "服务名").DataType("string").Required(false)).
Param(restful.QueryParameter("namespace", "命名空间").DataType("string").Required(false)).
Param(restful.QueryParameter("host", "主机名").DataType("string").Required(false)).
Param(restful.QueryParameter("port", "端口").DataType("integer").Required(false)).
Param(restful.QueryParameter("vpv_id", "VPC ID").DataType("string").Required(false)).
Notes(enrichGetLastHeartbeatApiNotes)
}

func enrichGetLogOutputLevelApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("获取日志输出级别").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Notes(enrichGetLogOutputLevelApiNotes)
}

func enrichSetLogOutputLevelApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
return r.
Doc("设置日志输出级别").
Metadata(restfulspec.KeyOpenAPITags, maintainApiTags).
Notes(enrichSetLogOutputLevelApiNotes)
}
165 changes: 165 additions & 0 deletions apiserver/httpserver/maintain_apinotes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 httpserver

const (
enrichGetServerConnectionsApiNotes = `
请求示例:
~~~
GET /maintain/v1/apiserver/conn?protocol=xxx&host=xxx
Header X-Polaris-Token: {访问凭据}
~~~
| 参数名 | 类型 | 描述 | 是否必填 |
|----------|--------|---------------------|----------|
| protocol | string | 查看指定协议 server | 是 |
| host | string | 查看指定host | 否 |
应答示例:
~~~json
~~~
`
enrichGetServerConnStatsApiNotes = `
请求示例:
~~~
GET /maintain/v1/apiserver/conn/stats?protocol=xxx&host=xxx
Header X-Polaris-Token: {访问凭据}
~~~
| 参数名 | 类型 | 描述 | 是否必填 |
|---------- |-------- |--------------------- |---------- |
| protocol | string | 查看指定协议 server | 是 |
| host | string | 查看指定host | 否 |
| amount | integer | 总量 | 否 |
`
enrichCloseConnectionsApiNotes = `
请求示例:
~~~
POST /maintain/v1/apiserver/conn?protocol=xxx&host=xxx
Header X-Polaris-Token: {访问凭据}
Header Content-Type: application/json
[
{
"protocol": "someProtocol",
"host": "someHost",
"amount": "someAmount",
"port": "port",
}
]
~~~
`
enrichFreeOSMemoryApiNotes = `
请求示例:
~~~
POST /maintain/v1/memory/free
Header X-Polaris-Token: {访问凭据}
Header Content-Type: application/json
~~~
`
enrichCleanInstanceApiNotes = `
请求示例:
~~~
POST /maintain/v1/instance/clean
Header X-Polaris-Token: {访问凭据}
Header Content-Type: application/json
{
"service": "tdsql-ops-server",
"namespace": "default",
"host": "127.0.0.1",
"port": 8080,
"location": {
"region": "ap-guangzhou",
"zone": "ap-guangzhou-3",
"campus": ""
},
"enable_health_check": true,
"health_check": {
"type": 1,
"heartbeat": {
"ttl": 10
}
},
"metadata": {
"env": "pre"
}
}
~~~
`
enrichGetLastHeartbeatApiNotes = `
请求示例:
~~~
GET /maintain/v1//instance/heartbeat?id=xxx
Header X-Polaris-Token: {访问凭据}
~~~
请求参数:
| 参数名 | 类型 | 描述 | 是否必填 |
| ------------------- | ------------------ | ----------------------------------------------------------------- | -------- |
| id | string | 实例id 如果存在id,后面参数可以不填名 | 否 |
| service | string | 服务名 | 否 |
| namespace | string | 命名空间 | 否 |
| host | string | 实例的IP | 否 |
| port | string | 实例的端口 | 否 |
| vpc_id | string | VPC ID | 否 |
`
enrichGetLogOutputLevelApiNotes = `
请求示例:
~~~
GET /maintain/v1/log/outputlevel
Header X-Polaris-Token: {访问凭据}
~~~
返回示例:
~~~
{
"apiserver": "info",
"auth": "info",
"cache": "info",
"config": "info",
"default": "info",
"healthcheck": "info",
"naming": "info",
"store": "info",
"xdsv3": "info"
}
~~~
`
enrichSetLogOutputLevelApiNotes = `
请求示例:
~~~
POST /maintain/v1/log/outputlevel
Header X-Polaris-Token: {访问凭据}
{
"scope": "apiserver",
"level": "info"
}
`
)
3 changes: 3 additions & 0 deletions apiserver/httpserver/swagger_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func enrichSwaggerObject(swo *spec.Swagger) {
{TagProps: spec.TagProps{
Name: "Instances",
Description: "实例管理"}},
{TagProps: spec.TagProps{
Name: "Maintain",
Description: "运维接口"}},
{TagProps: spec.TagProps{
Name: "Namespaces",
Description: "命名空间管理"}},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/nicksnyder/go-i18n/v2 v2.2.0
github.com/pkg/errors v0.9.1
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220915080537-fbc8c2ec9c38
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219
github.com/prometheus/client_golang v1.12.2
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/cobra v1.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220915080537-fbc8c2ec9c38 h1:yTaogvIiOs9aNPW6RAO2J7nf/34rK2kQlKiY6dJAE1s=
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220915080537-fbc8c2ec9c38/go.mod h1:4WhwBysTom9Eoy0hQ4W69I0FmO+T0EpjEW9/5sgHoUk=
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219 h1:XnFyNUWnciM6zgXaz6tm+Egs35rhoD0KGMmKh4gCdi0=
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219/go.mod h1:4WhwBysTom9Eoy0hQ4W69I0FmO+T0EpjEW9/5sgHoUk=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down

0 comments on commit 1b75849

Please sign in to comment.